27 #include <libopencmsis/core_cm3.h> 28 #include <libopencm3/cm3/nvic.h> 29 #include <libopencm3/stm32/rcc.h> 30 #include <libopencm3/stm32/gpio.h> 31 #include <libopencm3/stm32/spi.h> 32 #include <libopencm3/stm32/timer.h> 40 #define RTC_DCF77_ENABLE_PORT A 41 #define RTC_DCF77_ENABLE_PIN 2 42 #define RTC_DCF77_SIGNAL_PORT A 43 #define RTC_DCF77_SIGNAL_PIN 3 49 #define RTC_DCF77_TIMER 4 52 volatile bool rtc_dcf77_time_flag = false; 56 static volatile uint64_t rtc_dcf77_frame = 0;
68 #define RTC_DCF77_INVALID_MAX 5 84 timer_set_mode(
TIM(
RTC_DCF77_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
120 if (rtc_dcf77_frame==0) {
123 if (!(rtc_dcf77_frame&((uint64_t)1<<20))) {
129 for (uint8_t bit=21; bit<=28; bit++) {
130 if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
137 rtc_dcf77_time.
minutes = 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));
144 for (uint8_t bit=29; bit<=35; bit++) {
145 if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
152 rtc_dcf77_time.
hours = 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));
159 for (uint8_t bit=36; bit<=58; bit++) {
160 if (rtc_dcf77_frame&((uint64_t)1<<bit)) {
167 rtc_dcf77_time.
day = 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>>41)&(0x1));
171 rtc_dcf77_time.
weekday = 1*((rtc_dcf77_frame>>42)&(0x1))+2*((rtc_dcf77_frame>>43)&(0x1))+4*((rtc_dcf77_frame>>44)&(0x1));
175 rtc_dcf77_time.
month = 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));
179 rtc_dcf77_time.
year = 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));
193 uint8_t integral_i = 0;
194 int16_t integral_max = 0;
197 int16_t integral = 0;
199 int8_t dfc77_signal = -1;
208 if (integral>integral_max) {
209 integral_max = integral;
223 static uint8_t bin_state = 0;
224 static uint8_t bin_i = 0;
225 static uint8_t bit_i = 0;
240 if (0==bin_i && 0==bin_state) {
244 uint16_t modulation = 0;
245 for (uint8_t bin=0; bin<10; bin++) {
256 for (uint8_t bin=10; bin<20; bin++) {
262 rtc_dcf77_frame |= (1ULL<<bit_i);
#define RTC_DCF77_ENABLE_PIN
GPIO pinto enable the module.
static void rtc_dcf77_phase_detector(void)
find phase of 1 seconds DCF77 signal in the bins searches the complete second for the highest correla...
#define TIM_ISR(x)
get interrupt service routine for timer base on TIM identifier
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
static bool rtc_dcf77_phase_locked
if the current phase has been verified
#define NVIC_TIM_IRQ(x)
get NVIC IRQ for timer base on TIM identifier
uint8_t minutes
minutes (00-49)
void rtc_dcf77_setup(void)
setup DCF77 time receiver module
#define RTC_DCF77_SIGNAL_PORT
GPIO port to capture the DCF signal.
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
void rtc_dcf77_off(void)
switch off DCF77 time receiver module
static int16_t rtc_dcf77_phase_max
the maximum phase value
#define RCC_TIM(x)
get RCC for timer based on TIM identifier
uint8_t month
month (01-12)
decoded DCF77 time received
#define RTC_DCF77_ENABLE_PORT
GPIO port to enable the module.
static uint8_t rtc_dcf77_phase
the bin shift for the bit phase
#define RTC_DCF77_INVALID_MAX
maximum number of invalid decoding before switching off
volatile bool rtc_dcf77_time_flag
set when time information has been received
struct rtc_dcf77_time_t rtc_dcf77_time
the received DCF77 frame bits
#define RTC_DCF77_SIGNAL_PIN
GPIO pin to capture the DCF signal.
uint8_t hours
hours (00-23)
uint8_t weekday
day of the week (1-7=Monday-Sunday)
bool valid
if the time is valid
uint8_t year
year within century (00-99)
#define LENGTH(x)
get the length of an array
#define RTC_DCF77_TIMER
timer peripheral
uint8_t day
day of the month (01-31)
#define TIM(x)
get TIM based on TIM identifier
library to get time from a DCF77 module (API)
static void rtc_dcf77_decode(void)
decode rtc_dcf77_frame DCF77 frame into rtc_dcf77_time DCF77 time
static uint8_t rtc_dcf77_bins[100]
values of the DCF77 signal over 10 ms for 1 s (how many times it is high)
uint8_t milliseconds
milliseconds (00-99)
static uint8_t rtc_dcf77_invalid
number of invalid decoding
void rtc_dcf77_on(void)
switch on DCF77 time receiver module