From ee46833dfa555fe616db7d9e844cfa10bb723d43 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Tue, 18 Jan 2022 01:00:40 +0100 Subject: [PATCH] rework dcf77 (part 2) --- include/dcf77.h | 11 ++--- src/dcf77.cpp | 117 ++++++++++++++++++--------------------------- src/timekeeper.cpp | 30 +++++++----- 3 files changed, 69 insertions(+), 89 deletions(-) diff --git a/include/dcf77.h b/include/dcf77.h index c870fe70..df9400e9 100644 --- a/include/dcf77.h +++ b/include/dcf77.h @@ -4,18 +4,15 @@ #include "globals.h" #include "timekeeper.h" +#define set_dcfbit(b) (1ULL << (b)) + #ifdef DCF77_ACTIVE_LOW enum dcf_pinstate { dcf_high, dcf_low }; #else enum dcf_pinstate { dcf_low, dcf_high }; #endif -enum DCF77_Pulses { dcf_Z, dcf_0, dcf_1 }; - -void DCF77_Pulse(uint8_t const bit); -void DCF77_Frame(const struct tm t, uint8_t *frame); -uint8_t dec2bcd(uint8_t const dec, uint8_t const startpos, uint8_t const endpos, - uint8_t *frame); -uint8_t setParityBit(uint8_t const p); +void DCF77_Pulse(uint8_t bit); +uint64_t DCF77_Frame(const struct tm t); #endif \ No newline at end of file diff --git a/src/dcf77.cpp b/src/dcf77.cpp index ff6c54aa..f8ca280c 100644 --- a/src/dcf77.cpp +++ b/src/dcf77.cpp @@ -17,7 +17,7 @@ https://github.com/udoklein/dcf77 static const char TAG[] = __FILE__; // triggered by second timepulse to ticker out DCF signal -void DCF77_Pulse(uint8_t const bit) { +void DCF77_Pulse(uint8_t bit) { TickType_t startTime = xTaskGetTickCount(); @@ -27,12 +27,11 @@ void DCF77_Pulse(uint8_t const bit) { switch (pulse) { case 0: // start of second -> start of timeframe for logic signal - if (bit != dcf_Z) - digitalWrite(HAS_DCF77, dcf_low); + digitalWrite(HAS_DCF77, dcf_low); break; case 1: // 100ms after start of second -> end of timeframe for logic 0 - if (bit == dcf_0) + if (bit == 0) digitalWrite(HAS_DCF77, dcf_high); break; @@ -48,86 +47,64 @@ void DCF77_Pulse(uint8_t const bit) { } // for } // DCF77_Pulse() -void DCF77_Frame(const struct tm t, uint8_t *frame) { +// helper function to convert decimal to bcd digit +uint64_t dec2bcd(uint8_t const dec, uint8_t const startpos, + uint8_t const endpos, uint8_t *odd_parity) { - // writes a 1 minute dcf pulse scheme for calendar time t to frame + uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10); + uint64_t bcd = 0; - uint8_t Parity; + *odd_parity = 0; + for (uint8_t i = startpos; i <= endpos; i++) { + bcd += (data & 1) ? set_dcfbit(i) : 0; + *odd_parity += (data & 1); + data >>= 1; + } + *odd_parity %= 2; - // START OF NEW MINUTE - frame[0] = dcf_0; + return bcd; +} - // PAYLOAD -> not used here - frame[1] = dcf_0; - frame[2] = dcf_0; - frame[3] = dcf_0; - frame[4] = dcf_0; - frame[5] = dcf_0; - frame[6] = dcf_0; - frame[7] = dcf_0; - frame[8] = dcf_0; - frame[9] = dcf_0; - frame[10] = dcf_0; - frame[11] = dcf_0; - frame[12] = dcf_0; - frame[13] = dcf_0; - frame[14] = dcf_0; - frame[15] = dcf_0; +// generates a 1 minute dcf pulse frame for calendar time t +uint64_t DCF77_Frame(const struct tm t) { - // DST CHANGE ANNOUNCEMENT - frame[16] = dcf_0; // not yet implemented + uint8_t parity = 0, parity_sum = 0; + uint64_t frame = 0; // start with all bits 0 - // DAYLIGHTSAVING + // DST CHANGE ANNOUNCEMENT (16) + // -- not implemented -- + + // DAYLIGHTSAVING (17, 18) // "01" = MEZ / "10" = MESZ - frame[17] = (t.tm_isdst > 0) ? dcf_1 : dcf_0; - frame[18] = (t.tm_isdst > 0) ? dcf_0 : dcf_1; + frame += t.tm_isdst > 0 ? set_dcfbit(17) : set_dcfbit(18); - // LEAP SECOND - frame[19] = dcf_0; // not implemented + // LEAP SECOND (19) + // -- not implemented -- - // BEGIN OF TIME INFORMATION - frame[20] = dcf_1; + // BEGIN OF TIME INFORMATION (20) + frame += set_dcfbit(20); - // MINUTE (bits 21..28) - Parity = dec2bcd(t.tm_min, 21, 27, frame); - frame[28] = setParityBit(Parity); + // MINUTE (21..28) + frame += dec2bcd(t.tm_min, 21, 27, &parity); + frame += parity ? set_dcfbit(28) : 0; - // HOUR (bits 29..35) - Parity = dec2bcd(t.tm_hour, 29, 34, frame); - frame[35] = setParityBit(Parity); + // HOUR (29..35) + frame += dec2bcd(t.tm_hour, 29, 34, &parity); + frame += parity ? set_dcfbit(35) : 0; - // DATE (bits 36..58) - Parity = dec2bcd(t.tm_mday, 36, 41, frame); - Parity += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, frame); - Parity += dec2bcd(t.tm_mon + 1, 45, 49, frame); - Parity += dec2bcd(t.tm_year + 1900 - 2000, 50, 57, frame); - frame[58] = setParityBit(Parity); + // DATE (36..58) + frame += dec2bcd(t.tm_mday, 36, 41, &parity); + parity_sum += parity; + frame += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, &parity); + parity_sum += parity; + frame += dec2bcd(t.tm_mon + 1, 45, 49, &parity); + parity_sum += parity; + frame += dec2bcd(t.tm_year + 1900 - 2000, 50, 57, &parity); + parity_sum += parity; + frame += parity_sum % 2 ? set_dcfbit(58) : 0; - // MARK (bit 59) - frame[59] = dcf_Z; // !! missing code here for leap second !! - - // internal timestamp for the frame - frame[60] = t.tm_min; + return frame; } // DCF77_Frame() -// helper function to convert decimal to bcd digit -uint8_t dec2bcd(uint8_t const dec, uint8_t const startpos, uint8_t const endpos, - uint8_t *array) { - - uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10); - uint8_t parity = 0; - - for (uint8_t i = startpos; i <= endpos; i++) { - array[i] = (data & 1) ? dcf_1 : dcf_0; - parity += (data & 1); - data >>= 1; - } - - return parity; -} - -// helper function to encode parity -uint8_t setParityBit(uint8_t const p) { return ((p & 1) ? dcf_1 : dcf_0); } - #endif // HAS_DCF77 \ No newline at end of file diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index cf58e189..2b08e4e0 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -258,8 +258,9 @@ void clock_init(void) { void clock_loop(void *taskparameter) { // ClockTask - uint8_t ClockPulse[61] = {0}; + uint64_t ClockPulse = 0; uint32_t current_time = 0, previous_time = 0; + uint8_t ClockMinute = 0; time_t tt; struct tm t = {0}; #ifdef HAS_TWO_LED @@ -283,7 +284,7 @@ void clock_loop(void *taskparameter) { // ClockTask if (!(timeIsValid(current_time)) || (current_time == previous_time)) continue; - // initialize calendar time for next second of clock output + // set calendar time for next second of clock output tt = (time_t)(current_time + 1); localtime_r(&tt, &t); mktime(&t); @@ -306,19 +307,24 @@ void clock_loop(void *taskparameter) { // ClockTask #elif defined HAS_DCF77 - if (t.tm_min == // do we still have a recent frame? - ClockPulse[60]) { // (timepulses could be missed!) - DCF77_Pulse(ClockPulse[t.tm_sec]); // then output next second's pulse - ESP_LOGD(TAG, "[%0.3f] DCF77: %02d:%02d:%02d", _seconds(), t.tm_hour, - t.tm_min, t.tm_sec); - } + // load new frame if second 59 is reached + if (t.tm_sec == 0) { + ClockMinute = t.tm_min; + t.tm_min++; // follow-up minute + mktime(&t); // normalize calendar time + ClockPulse = DCF77_Frame(t); // generate pulse frame + + /* to do here: leap second handling in second 59 */ - if (t.tm_sec == 59) { // is it time to load new frame? - t.tm_min++; - mktime(&t); // normalize calendar time - DCF77_Frame(t, ClockPulse); // generate frame for next minute ESP_LOGD(TAG, "[%0.3f] DCF77: new frame for min %d", _seconds(), t.tm_min); + } else { + + // generate impulse + if (t.tm_min == ClockMinute) { // ensure frame is recent + DCF77_Pulse(ClockPulse & 1); // output next second + ClockPulse >>= 1; + } } #endif