LED clock
 All Files Functions Variables Macros Groups
rtc_dcf77.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 #include <stdlib.h> // general utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/stm32/rcc.h> // real-time control clock library
28 #include <libopencm3/stm32/gpio.h> // general purpose input output library
29 #include <libopencm3/stm32/spi.h> // SPI library
30 #include <libopencm3/stm32/timer.h> // timer library
31 #include <libopencm3/cm3/nvic.h> // interrupt handler
32 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
33 
34 #include "rtc_dcf77.h" // RTC DCF77 library API
35 #include "global.h" // common methods
36 
37 volatile bool rtc_dcf77_time_flag = false;
38 volatile uint64_t rtc_dcf77_frame = 0;
40 void rtc_dcf77_setup(void)
41 {
42  // setup enable output
43  rcc_periph_clock_enable(RTC_DCF77_ENABLE_RCC); // enable clock GPIO peripheral
44  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
45  rtc_dcf77_off(); // disable module at start
46 
47  // setup signal input
48  rcc_periph_clock_enable(RTC_DCF77_SIGNAL_RCC); // enable clock for signal input peripheral
49  gpio_set_mode(RTC_DCF77_SIGNAL_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, RTC_DCF77_SIGNAL_PIN); // set signal pin to input
50  rcc_periph_clock_enable(RCC_AFIO); // enable alternate function clock for external interrupt
51  exti_select_source(RTC_DCF77_SIGNAL_EXTI, RTC_DCF77_SIGNAL_PORT); // mask external interrupt of this pin only for this port
52  exti_set_trigger(RTC_DCF77_SIGNAL_EXTI, EXTI_TRIGGER_BOTH); // trigger on both edges
53  exti_enable_request(RTC_DCF77_SIGNAL_EXTI); // enable external interrupt
54  nvic_enable_irq(RTC_DCF77_SIGNAL_IRQ); // enable interrupt
55 
56  // setup timer to measure pulses
57  rcc_periph_clock_enable(RTC_DCF77_TIMER_RCC); // enable clock for timer peripheral
58  timer_reset(RTC_DCF77_TIMER); // reset timer state
59  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
60  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
61  timer_enable_counter(RTC_DCF77_TIMER); // start timer to measure time
62 }
63 
64 void rtc_dcf77_on(void)
65 {
66  gpio_clear(RTC_DCF77_ENABLE_PORT, RTC_DCF77_ENABLE_PIN); // enable module by pull pin low
67 }
68 
69 void rtc_dcf77_off(void)
70 {
71  gpio_set(RTC_DCF77_ENABLE_PORT, RTC_DCF77_ENABLE_PIN); // disable module by pull pin high
72 }
73 
74 uint8_t* rtc_dcf77_time(void)
75 {
76  static uint8_t to_return[6] = {0}; // arrays with date values to return
77  uint8_t parity = 0; // to check parity
78  if (rtc_dcf77_frame==0) { // no time received yet
79  return NULL;
80  }
81  if (!(rtc_dcf77_frame&((uint64_t)1<<20))) { // start of encode time should always be 1
82  return NULL;
83  }
84  // check minute parity
85  parity = 0;
86  for (uint8_t bit=21; bit<=28; bit++) {
87  if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
88  parity++; // count the set bits
89  }
90  }
91  if (parity%2) { // parity should be even
92  return NULL;
93  }
94  to_return[0] = 1*((rtc_dcf77_frame>>21)&(0x1))+2*((rtc_dcf77_frame>>22)&(0x1))+4*((rtc_dcf77_frame>>23)&(0x1))+8*((rtc_dcf77_frame>>24)&(0x1))+10*((rtc_dcf77_frame>>25)&(0x1))+20*((rtc_dcf77_frame>>26)&(0x1))+40*((rtc_dcf77_frame>>27)&(0x1)); // read minute (00-59)
95  if (to_return[0]>59) { // minutes should not be more than 59
96  return NULL;
97  }
98  // check hour parity
99  parity = 0;
100  for (uint8_t bit=29; bit<=35; bit++) {
101  if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
102  parity++; // count the set bits
103  }
104  }
105  if (parity%2) { // parity should be even
106  return NULL;
107  }
108  to_return[1] = 1*((rtc_dcf77_frame>>29)&(0x1))+2*((rtc_dcf77_frame>>30)&(0x1))+4*((rtc_dcf77_frame>>31)&(0x1))+8*((rtc_dcf77_frame>>32)&(0x1))+10*((rtc_dcf77_frame>>33)&(0x1))+20*((rtc_dcf77_frame>>34)&(0x1)); // read hour (00-23)
109  if (to_return[1]>23) { // hours should not be more than 23
110  return NULL;
111  }
112  // check date parity
113  parity = 0;
114  for (uint8_t bit=36; bit<=58; bit++) {
115  if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
116  parity++; // count the set bits
117  }
118  }
119  if (parity%2) { // parity should be even
120  return NULL;
121  }
122  to_return[2] = 1*((rtc_dcf77_frame>>36)&(0x1))+2*((rtc_dcf77_frame>>37)&(0x1))+4*((rtc_dcf77_frame>>38)&(0x1))+8*((rtc_dcf77_frame>>39)&(0x1))+10*((rtc_dcf77_frame>>40)&(0x1))+20*((rtc_dcf77_frame>>34)&(0x41)); // read day of the month (01-31)
123  if (to_return[2]==0 || to_return[2]>31) { // day of the month should be 1-31
124  return NULL;
125  }
126  to_return[3] = 1*((rtc_dcf77_frame>>42)&(0x1))+2*((rtc_dcf77_frame>>43)&(0x1))+4*((rtc_dcf77_frame>>44)&(0x1)); // read day of the week (1=Monday - 7=Sunday)
127  if (to_return[3]==0 || to_return[3]>7) { // day of the week should be 1-7
128  return NULL;
129  }
130  to_return[4] = 1*((rtc_dcf77_frame>>45)&(0x1))+2*((rtc_dcf77_frame>>46)&(0x1))+4*((rtc_dcf77_frame>>47)&(0x1))+8*((rtc_dcf77_frame>>48)&(0x1))+10*((rtc_dcf77_frame>>49)&(0x1)); // read month of the year (01-12)
131  if (to_return[4]==0 || to_return[4]>12) { // month of the year should be 1-12
132  return NULL;
133  }
134  to_return[5] = 1*((rtc_dcf77_frame>>50)&(0x1))+2*((rtc_dcf77_frame>>51)&(0x1))+4*((rtc_dcf77_frame>>52)&(0x1))+8*((rtc_dcf77_frame>>53)&(0x1))+10*((rtc_dcf77_frame>>54)&(0x1))+20*((rtc_dcf77_frame>>55)&(0x1))+40*((rtc_dcf77_frame>>56)&(0x1))+80*((rtc_dcf77_frame>>57)&(0x1)); // read year of the century (00-99)
135  if (to_return[5]>99) { // year should be <100
136  return NULL;
137  }
138  return to_return;
139 }
140 
143 {
144  exti_reset_request(RTC_DCF77_SIGNAL_EXTI); // reset interrupt
145  static uint16_t old_state = 0; // save last port state to detect difference
146  static uint8_t pulse = 0; // next pulse number in the DCF77 frame
147  static uint16_t pulse_edge = 0; // time on when the last pulse (rising edge) has been detected
148  static uint64_t rtc_dcf77_frame_tmp = 0; // the DCF77 frame bits as they get filled
149  uint16_t time = timer_get_counter(RTC_DCF77_TIMER); // get timer value
150 
151  uint16_t new_state = gpio_get(RTC_DCF77_SIGNAL_PORT, RTC_DCF77_SIGNAL_PIN); // save last port state to detect difference
152  if (old_state!=new_state) { // pulse edge detected
153  time = (uint32_t)(time-pulse_edge)*RTC_DCF77_TIMER_MAX_TIME/(1<<16); // get time since last rising edge (integer underflow possible)
154  if (new_state) { // rising edge
155  if (time < 980) { // glitch
156  goto end; // ignore glitch
157  } else if (time < 1030) { // a normal pulse
158  pulse++; // go to next pulse
159  if (pulse>58) { // something wrong happened
160  pulse = 0; // restart
161  }
162  } else if (time < 1980) { // glitch
163  goto end; // ignore glitch
164  } else if (time < 2130) { // first pulse of a frame
165  if (pulse==58) { // full frame received
166  rtc_dcf77_frame = rtc_dcf77_frame_tmp; // save received complete frame
167  rtc_dcf77_time_flag = true; // notify user
168  }
169  pulse = 0;
170  } else { // something is wrong, restart
171  pulse = 0;
172  }
173  pulse_edge = 0; // save new edge
174  timer_set_counter(RTC_DCF77_TIMER, 0); // reset timer to count
175  } else { // falling edge
176  if (time < 90) { // glitch
177  goto end; // ignore glitch
178  } else if (time < 120) { // 0 received
179  rtc_dcf77_frame_tmp &= ~((uint64_t)1<<pulse); // save 0 bit
180  } else if (time < 190) { // glitch
181  goto end; // ignore glitch
182  } else if (time < 220) { // 1 received
183  rtc_dcf77_frame_tmp |= ((uint64_t)1<<pulse); // save 1 bit
184  }
185  }
186  }
187 end:
188  old_state = new_state; // save new state
189 }
190 
191 
#define RTC_DCF77_TIMER_MAX_TIME
the maximum time in ms the timer can count.
Definition: rtc_dcf77.h:42
#define RTC_DCF77_ENABLE_PIN
GPIO pinto enable the module.
Definition: rtc_dcf77.h:28
#define RTC_DCF77_TIMER_RCC
timer peripheral clock
Definition: rtc_dcf77.h:41
volatile uint64_t rtc_dcf77_frame
the received DCF77 frame bits
Definition: rtc_dcf77.c:38
void rtc_dcf77_setup(void)
setup DCF77 time receiver module
Definition: rtc_dcf77.c:40
void RTC_DCF77_SIGNAL_ISR(void)
interrupt service routine called when signal edge is detected, decoding the received DCF77 frame (com...
Definition: rtc_dcf77.c:142
#define RTC_DCF77_SIGNAL_PORT
GPIO port to capture the DCF signal.
Definition: rtc_dcf77.h:30
global definitions and methods
void rtc_dcf77_off(void)
switch off DCF77 time receiver module
Definition: rtc_dcf77.c:69
#define RTC_DCF77_SIGNAL_IRQ
GPIO line interrupt.
Definition: rtc_dcf77.h:33
#define RTC_DCF77_ENABLE_PORT
GPIO port to enable the module.
Definition: rtc_dcf77.h:27
volatile bool rtc_dcf77_time_flag
set when time information has been received
Definition: rtc_dcf77.c:37
#define RTC_DCF77_SIGNAL_PIN
GPIO pin to capture the DCF signal.
Definition: rtc_dcf77.h:31
#define RTC_DCF77_SIGNAL_RCC
GPIO peripheral clock to capture the DCF signal.
Definition: rtc_dcf77.h:29
uint8_t * rtc_dcf77_time(void)
get last received DCF77 time
Definition: rtc_dcf77.c:74
#define RTC_DCF77_TIMER
timer peripheral
Definition: rtc_dcf77.h:40
library to get time from a DCF77 module (API)
#define RTC_DCF77_SIGNAL_EXTI
GPIO external interrupt to capture the DCF signal.
Definition: rtc_dcf77.h:32
void rtc_dcf77_on(void)
switch on DCF77 time receiver module
Definition: rtc_dcf77.c:64
#define RTC_DCF77_ENABLE_RCC
GPIO peripheral clock to enable the module.
Definition: rtc_dcf77.h:26