From 5a43d4d48ce359f8a8cd2f0b5c1e91799a37c0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Thu, 5 May 2016 18:03:07 +0200 Subject: [PATCH] add basic DCF77 pulse detection --- lib/rtc_dcf77.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/rtc_dcf77.h | 53 +++++++++++++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 lib/rtc_dcf77.c create mode 100644 lib/rtc_dcf77.h diff --git a/lib/rtc_dcf77.c b/lib/rtc_dcf77.c new file mode 100644 index 0000000..ad4559c --- /dev/null +++ b/lib/rtc_dcf77.c @@ -0,0 +1,110 @@ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +/** @brief library to get time from a DCF77 module + * @file rtc_dcf77.c + * @author King Kévin + * @date 2016 + * @note peripherals used: GPIO @ref rtc_dcf77_gpio, timer @ref rtc_dcf77_timer + */ + +/* standard libraries */ +#include // standard integer types +#include // general utilities + +#include + +/* STM32 (including CM3) libraries */ +#include // real-time control clock library +#include // general purpose input output library +#include // SPI library +#include // timer library +#include // interrupt handler +#include // Cortex M3 utilities + +#include "rtc_dcf77.h" // RTC DCF77 library API +#include "global.h" // common methods + +volatile bool rtc_dcf77_time_flag = false; + +void rtc_dcf77_setup(void) +{ + // setup enable output + rcc_periph_clock_enable(RTC_DCF77_ENABLE_RCC); // enable clock GPIO peripherial + 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 + exti_enable_request(RTC_DCF77_SIGNAL_EXTI); // enable external interrupt + nvic_enable_irq(RTC_DCF77_SIGNAL_IRQ); // 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 +} + +void rtc_dcf77_on(void) +{ + gpio_clear(RTC_DCF77_ENABLE_PORT, 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 +} + +/** @brief interrupt service routine called when signal edge is detected */ +void RTC_DCF77_SIGNAL_ISR(void) +{ + exti_reset_request(RTC_DCF77_SIGNAL_EXTI); // 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_rising = 0, pulse_falling = 0; // time on when the last pulse edge has been detected + static uint16_t pulse_rising = 0; + uint16_t time = timer_get_counter(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 + if (old_state!=new_state) { // pulse edge detected + if (new_state) { // rising edge + time = (uint32_t)(time-pulse_rising)*RTC_DCF77_TIMER_MAX_TIME/(1<<16); // get time since last rising edge (integer underflow possible) + if (time < 900) { // ignore glitches + goto end; + } else if (time < 1100) { // a normal pulse + pulse = (pulse+1)%59; // go to next pulse (there should be no more than 59) + } else if (time < 1900) { // ignore glitch + goto end; + } else if (time < 2100) { // first pulse of a frame + pulse = 0; + } else { // something is wrong, restart + pulse = 0; + } + led_toggle(); + pulse_rising = 0; // save new edge + timer_set_counter(RTC_DCF77_TIMER, 0); // reset timer to count + printf("%u\n", pulse); + } + } +end: + old_state = new_state; // save new state +} + + diff --git a/lib/rtc_dcf77.h b/lib/rtc_dcf77.h new file mode 100644 index 0000000..40dfc92 --- /dev/null +++ b/lib/rtc_dcf77.h @@ -0,0 +1,53 @@ +/* This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +/** @brief library to get time from a DCF77 module + * @file rtc_dcf77.h + * @author King Kévin + * @date 2016 + * @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 */ +/** @} */ + +/** @brief set when time information has been received */ +extern volatile bool rtc_dcf77_time_flag; + +/** @brief setup DCF77 time receiver module */ +void rtc_dcf77_setup(void); +/** @brief switch on DCF77 time receiver module */ +void rtc_dcf77_on(void); +/** @brief switch off DCF77 time receiver module */ +void rtc_dcf77_off(void);