gpio_set_mode(RTC_DCF77_ENABLE_PORT,GPIO_MODE_OUTPUT_2_MHZ,GPIO_CNF_OUTPUT_PUSHPULL,RTC_DCF77_ENABLE_PIN);// set pin to output push-pull to be able to enable the module
rtc_dcf77_off();// disable module at start
// setup signal input
rcc_periph_clock_enable(RTC_DCF77_SIGNAL_RCC);// enable clock for signal input peripheral
gpio_set_mode(RTC_DCF77_SIGNAL_PORT,GPIO_MODE_INPUT,GPIO_CNF_INPUT_FLOAT,RTC_DCF77_SIGNAL_PIN);// set signal pin to input
rcc_periph_clock_enable(RCC_AFIO);// enable alternate function clock for external interrupt
exti_select_source(RTC_DCF77_SIGNAL_EXTI,RTC_DCF77_SIGNAL_PORT);// mask external interrupt of this pin only for this port
exti_set_trigger(RTC_DCF77_SIGNAL_EXTI,EXTI_TRIGGER_BOTH);// trigger on both edges
rcc_periph_clock_enable(RTC_DCF77_TIMER_RCC);// enable clock for timer peripheral
timer_reset(RTC_DCF77_TIMER);// reset timer state
timer_set_mode(RTC_DCF77_TIMER,TIM_CR1_CKD_CK_INT,TIM_CR1_CMS_EDGE,TIM_CR1_DIR_UP);// set timer mode, use undivided timer clock, edge alignment (simple count), and count up
timer_set_prescaler(RTC_DCF77_TIMER,RTC_DCF77_TIMER_MAX_TIME*(rcc_ahb_frequency/1000)/(1<<16));// set prescaler to count up to the maximum time
timer_enable_counter(RTC_DCF77_TIMER);// start timer to measure time
}
voidrtc_dcf77_on(void)
{
gpio_clear(RTC_DCF77_ENABLE_PORT,RTC_DCF77_ENABLE_PIN);// enable module by pull pin low
}
voidrtc_dcf77_off(void)
{
gpio_set(RTC_DCF77_ENABLE_PORT,RTC_DCF77_ENABLE_PIN);// disable module by pull pin high
}
uint8_t*rtc_dcf77_time(void)
{
staticuint8_tto_return[6]={0};// arrays with date values to return
uint8_tparity=0;// to check parity
if(rtc_dcf77_frame==0){// no time received yet
returnNULL;
}
if(!(rtc_dcf77_frame&((uint64_t)1<<20))){// start of encode time should always be 1
to_return[2]=1*(((rtc_dcf77_frame>>30)>>6)&(0x1))+2*(((rtc_dcf77_frame>>30)>>7)&(0x1))+4*(((rtc_dcf77_frame>>30)>>8)&(0x1))+8*(((rtc_dcf77_frame>>30)>>9)&(0x1))+10*(((rtc_dcf77_frame>>30)>>10)&(0x1))+20*(((rtc_dcf77_frame>>30)>>11)&(0x1));// read day of the month (01-31)
to_return[3]=1*(((rtc_dcf77_frame>>30)>>12)&(0x1))+2*(((rtc_dcf77_frame>>30)>>13)&(0x1))+4*(((rtc_dcf77_frame>>30)>>14)&(0x1));// read day of the week (1=Monday - 7=Sunday)
to_return[4]=1*(((rtc_dcf77_frame>>30)>>15)&(0x1))+2*(((rtc_dcf77_frame>>30)>>16)&(0x1))+4*(((rtc_dcf77_frame>>30)>>17)&(0x1))+8*(((rtc_dcf77_frame>>30)>>18)&(0x1))+10*(((rtc_dcf77_frame>>30)>>19)&(0x1));// read month of the year (01-12)
to_return[5]=1*(((rtc_dcf77_frame>>30)>>20)&(0x1))+2*(((rtc_dcf77_frame>>30)>>21)&(0x1))+4*(((rtc_dcf77_frame>>30)>>22)&(0x1))+8*(((rtc_dcf77_frame>>30)>>23)&(0x1))+10*(((rtc_dcf77_frame>>30)>>24)&(0x1))+20*(((rtc_dcf77_frame>>30)>>25)&(0x1))+40*(((rtc_dcf77_frame>>30)>>26)&(0x1))+80*(((rtc_dcf77_frame>>30)>>27)&(0x1));// read year of the century (00-99)