diff --git a/lib/rtc_dcf77.c b/lib/rtc_dcf77.c index 6c548c3..ec6676c 100644 --- a/lib/rtc_dcf77.c +++ b/lib/rtc_dcf77.c @@ -15,7 +15,7 @@ /** library to get time from a DCF77 module (code) * @file rtc_dcf77.c * @author King Kévin - * @date 2016 + * @date 2016-2017 * @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer */ @@ -35,41 +35,57 @@ #include "rtc_dcf77.h" // RTC DCF77 library API #include "global.h" // common methods +/** @defgroup rtc_dcf77_gpio output to enable DCF module and input to capture DCF signal + * @{ + */ +#define RTC_DCF77_ENABLE_PORT A /**< GPIO port to enable the module */ +#define RTC_DCF77_ENABLE_PIN 2 /**< GPIO pinto enable the module */ +#define RTC_DCF77_SIGNAL_PORT A /**< GPIO port to capture the DCF signal */ +#define RTC_DCF77_SIGNAL_PIN 3 /**< GPIO pin to capture the DCF signal */ +/** @} */ + +/** @defgroup rtc_dcf77_timer timer to measure signal puls + * @{ + */ +#define RTC_DCF77_TIMER 4 /**< timer peripheral */ +#define RTC_DCF77_TIMER_MAX_TIME 2200 /**< the maximum time in ms the timer can count. DCF77 have pulses < 2s */ +/** @} */ + volatile bool rtc_dcf77_time_flag = false; static volatile uint64_t rtc_dcf77_frame = 0; /**< the received DCF77 frame bits */ void rtc_dcf77_setup(void) { // setup enable output - rcc_periph_clock_enable(RTC_DCF77_ENABLE_RCC); // enable clock GPIO peripheral - 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 + rcc_periph_clock_enable(RCC_GPIO(RTC_DCF77_ENABLE_PORT)); // enable clock GPIO peripheral + gpio_set_mode(GPIO(RTC_DCF77_ENABLE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(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_GPIO(RTC_DCF77_SIGNAL_PORT)); // enable clock for signal input peripheral + gpio_set_mode(GPIO(RTC_DCF77_SIGNAL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(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 - exti_enable_request(RTC_DCF77_SIGNAL_EXTI); // enable external interrupt - nvic_enable_irq(RTC_DCF77_SIGNAL_IRQ); // enable interrupt + exti_select_source(EXTI(RTC_DCF77_SIGNAL_PIN), GPIO(RTC_DCF77_SIGNAL_PORT)); // mask external interrupt of this pin only for this port + exti_set_trigger(EXTI(RTC_DCF77_SIGNAL_PIN), EXTI_TRIGGER_BOTH); // trigger on both edges + exti_enable_request(EXTI(RTC_DCF77_SIGNAL_PIN)); // enable external interrupt + nvic_enable_irq(NVIC_EXTI_IRQ(RTC_DCF77_SIGNAL_PIN)); // enable interrupt // setup timer to measure pulses - 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 + rcc_periph_clock_enable(RCC_TIM(RTC_DCF77_TIMER)); // enable clock for timer peripheral + timer_reset(TIM(RTC_DCF77_TIMER)); // reset timer state + timer_set_mode(TIM(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(TIM(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(TIM(RTC_DCF77_TIMER)); // start timer to measure time } void rtc_dcf77_on(void) { - gpio_clear(RTC_DCF77_ENABLE_PORT, RTC_DCF77_ENABLE_PIN); // enable module by pull pin low + gpio_clear(GPIO(RTC_DCF77_ENABLE_PORT), GPIO(RTC_DCF77_ENABLE_PIN)); // enable module by pull pin low } void rtc_dcf77_off(void) { - gpio_set(RTC_DCF77_ENABLE_PORT, RTC_DCF77_ENABLE_PIN); // disable module by pull pin high + gpio_set(GPIO(RTC_DCF77_ENABLE_PORT), GPIO(RTC_DCF77_ENABLE_PIN)); // disable module by pull pin high } uint8_t* rtc_dcf77_time(void) @@ -140,16 +156,16 @@ uint8_t* rtc_dcf77_time(void) } /** interrupt service routine called when signal edge is detected, decoding the received DCF77 frame (composed by high pulses) */ -void RTC_DCF77_SIGNAL_ISR(void) +void EXTI_ISR(RTC_DCF77_SIGNAL_PIN)(void) { - exti_reset_request(RTC_DCF77_SIGNAL_EXTI); // reset interrupt + exti_reset_request(EXTI(RTC_DCF77_SIGNAL_PIN)); // reset interrupt static uint16_t old_state = 0; // save last port state to detect difference static uint8_t pulse = 0; // next pulse number in the DCF77 frame static uint16_t pulse_edge = 0; // time on when the last pulse (rising edge) has been detected static uint64_t rtc_dcf77_frame_tmp = 0; // the DCF77 frame bits as they get filled - uint16_t time = timer_get_counter(RTC_DCF77_TIMER); // get timer value + uint16_t time = timer_get_counter(TIM(RTC_DCF77_TIMER)); // get timer value - uint16_t new_state = gpio_get(RTC_DCF77_SIGNAL_PORT, RTC_DCF77_SIGNAL_PIN); // save last port state to detect difference + uint16_t new_state = gpio_get(GPIO(RTC_DCF77_SIGNAL_PORT), GPIO(RTC_DCF77_SIGNAL_PIN)); // save last port state to detect difference if (old_state!=new_state) { // pulse edge detected time = (uint32_t)(time-pulse_edge)*RTC_DCF77_TIMER_MAX_TIME/(1<<16); // get time since last rising edge (integer underflow possible) if (new_state) { // rising edge @@ -172,7 +188,7 @@ void RTC_DCF77_SIGNAL_ISR(void) pulse = 0; } pulse_edge = 0; // save new edge - timer_set_counter(RTC_DCF77_TIMER, 0); // reset timer to count + timer_set_counter(TIM(RTC_DCF77_TIMER), 0); // reset timer to count } else { // falling edge if (time < 90) { // glitch goto end; // ignore glitch @@ -188,5 +204,3 @@ void RTC_DCF77_SIGNAL_ISR(void) end: old_state = new_state; // save new state } - - diff --git a/lib/rtc_dcf77.h b/lib/rtc_dcf77.h index c12edec..f90bc9c 100644 --- a/lib/rtc_dcf77.h +++ b/lib/rtc_dcf77.h @@ -15,33 +15,11 @@ /** library to get time from a DCF77 module (API) * @file rtc_dcf77.h * @author King Kévin - * @date 2016 + * @date 2016-2017 * @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer */ #pragma once -/** @defgroup rtc_dcf77_gpio output to enable DCF module and input to capture DCF signal - * @{ - */ -#define RTC_DCF77_ENABLE_RCC RCC_GPIOA /**< GPIO peripheral clock to enable the module */ -#define RTC_DCF77_ENABLE_PORT GPIOA /**< GPIO port to enable the module */ -#define RTC_DCF77_ENABLE_PIN GPIO2 /**< GPIO pinto enable the module */ -#define RTC_DCF77_SIGNAL_RCC RCC_GPIOA /**< GPIO peripheral clock to capture the DCF signal */ -#define RTC_DCF77_SIGNAL_PORT GPIOA /**< GPIO port to capture the DCF signal */ -#define RTC_DCF77_SIGNAL_PIN GPIO3 /**< GPIO pin to capture the DCF signal */ -#define RTC_DCF77_SIGNAL_EXTI EXTI3 /**< GPIO external interrupt to capture the DCF signal */ -#define RTC_DCF77_SIGNAL_IRQ NVIC_EXTI3_IRQ /**< GPIO line interrupt */ -#define RTC_DCF77_SIGNAL_ISR exti3_isr /**< GPIO line interrupt service routine */ -/** @} */ - -/** @defgroup rtc_dcf77_timer timer to measure signal puls - * @{ - */ -#define RTC_DCF77_TIMER TIM4 /**< timer peripheral */ -#define RTC_DCF77_TIMER_RCC RCC_TIM4 /**< timer peripheral clock */ -#define RTC_DCF77_TIMER_MAX_TIME 2200 /**< the maximum time in ms the timer can count. DCF77 have pulses < 2s */ -/** @} */ - /** set when time information has been received */ extern volatile bool rtc_dcf77_time_flag;