rework dcf77 (part 2)

This commit is contained in:
cyberman54 2022-01-18 01:00:40 +01:00
parent a7f957dedd
commit ee46833dfa
3 changed files with 69 additions and 89 deletions

View File

@ -4,18 +4,15 @@
#include "globals.h" #include "globals.h"
#include "timekeeper.h" #include "timekeeper.h"
#define set_dcfbit(b) (1ULL << (b))
#ifdef DCF77_ACTIVE_LOW #ifdef DCF77_ACTIVE_LOW
enum dcf_pinstate { dcf_high, dcf_low }; enum dcf_pinstate { dcf_high, dcf_low };
#else #else
enum dcf_pinstate { dcf_low, dcf_high }; enum dcf_pinstate { dcf_low, dcf_high };
#endif #endif
enum DCF77_Pulses { dcf_Z, dcf_0, dcf_1 }; void DCF77_Pulse(uint8_t bit);
uint64_t DCF77_Frame(const struct tm t);
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);
#endif #endif

View File

@ -17,7 +17,7 @@ https://github.com/udoklein/dcf77
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
// triggered by second timepulse to ticker out DCF signal // 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(); TickType_t startTime = xTaskGetTickCount();
@ -27,12 +27,11 @@ void DCF77_Pulse(uint8_t const bit) {
switch (pulse) { switch (pulse) {
case 0: // start of second -> start of timeframe for logic signal 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; break;
case 1: // 100ms after start of second -> end of timeframe for logic 0 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); digitalWrite(HAS_DCF77, dcf_high);
break; break;
@ -48,86 +47,64 @@ void DCF77_Pulse(uint8_t const bit) {
} // for } // for
} // DCF77_Pulse() } // 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 return bcd;
frame[0] = dcf_0; }
// PAYLOAD -> not used here // generates a 1 minute dcf pulse frame for calendar time t
frame[1] = dcf_0; uint64_t DCF77_Frame(const struct tm t) {
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;
// DST CHANGE ANNOUNCEMENT uint8_t parity = 0, parity_sum = 0;
frame[16] = dcf_0; // not yet implemented uint64_t frame = 0; // start with all bits 0
// DAYLIGHTSAVING // DST CHANGE ANNOUNCEMENT (16)
// -- not implemented --
// DAYLIGHTSAVING (17, 18)
// "01" = MEZ / "10" = MESZ // "01" = MEZ / "10" = MESZ
frame[17] = (t.tm_isdst > 0) ? dcf_1 : dcf_0; frame += t.tm_isdst > 0 ? set_dcfbit(17) : set_dcfbit(18);
frame[18] = (t.tm_isdst > 0) ? dcf_0 : dcf_1;
// LEAP SECOND // LEAP SECOND (19)
frame[19] = dcf_0; // not implemented // -- not implemented --
// BEGIN OF TIME INFORMATION // BEGIN OF TIME INFORMATION (20)
frame[20] = dcf_1; frame += set_dcfbit(20);
// MINUTE (bits 21..28) // MINUTE (21..28)
Parity = dec2bcd(t.tm_min, 21, 27, frame); frame += dec2bcd(t.tm_min, 21, 27, &parity);
frame[28] = setParityBit(Parity); frame += parity ? set_dcfbit(28) : 0;
// HOUR (bits 29..35) // HOUR (29..35)
Parity = dec2bcd(t.tm_hour, 29, 34, frame); frame += dec2bcd(t.tm_hour, 29, 34, &parity);
frame[35] = setParityBit(Parity); frame += parity ? set_dcfbit(35) : 0;
// DATE (bits 36..58) // DATE (36..58)
Parity = dec2bcd(t.tm_mday, 36, 41, frame); frame += dec2bcd(t.tm_mday, 36, 41, &parity);
Parity += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, frame); parity_sum += parity;
Parity += dec2bcd(t.tm_mon + 1, 45, 49, frame); frame += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, &parity);
Parity += dec2bcd(t.tm_year + 1900 - 2000, 50, 57, frame); parity_sum += parity;
frame[58] = setParityBit(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) return frame;
frame[59] = dcf_Z; // !! missing code here for leap second !!
// internal timestamp for the frame
frame[60] = t.tm_min;
} // DCF77_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 #endif // HAS_DCF77

View File

@ -258,8 +258,9 @@ void clock_init(void) {
void clock_loop(void *taskparameter) { // ClockTask void clock_loop(void *taskparameter) { // ClockTask
uint8_t ClockPulse[61] = {0}; uint64_t ClockPulse = 0;
uint32_t current_time = 0, previous_time = 0; uint32_t current_time = 0, previous_time = 0;
uint8_t ClockMinute = 0;
time_t tt; time_t tt;
struct tm t = {0}; struct tm t = {0};
#ifdef HAS_TWO_LED #ifdef HAS_TWO_LED
@ -283,7 +284,7 @@ void clock_loop(void *taskparameter) { // ClockTask
if (!(timeIsValid(current_time)) || (current_time == previous_time)) if (!(timeIsValid(current_time)) || (current_time == previous_time))
continue; 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); tt = (time_t)(current_time + 1);
localtime_r(&tt, &t); localtime_r(&tt, &t);
mktime(&t); mktime(&t);
@ -306,19 +307,24 @@ void clock_loop(void *taskparameter) { // ClockTask
#elif defined HAS_DCF77 #elif defined HAS_DCF77
if (t.tm_min == // do we still have a recent frame? // load new frame if second 59 is reached
ClockPulse[60]) { // (timepulses could be missed!) if (t.tm_sec == 0) {
DCF77_Pulse(ClockPulse[t.tm_sec]); // then output next second's pulse ClockMinute = t.tm_min;
ESP_LOGD(TAG, "[%0.3f] DCF77: %02d:%02d:%02d", _seconds(), t.tm_hour, t.tm_min++; // follow-up minute
t.tm_min, t.tm_sec); 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(), ESP_LOGD(TAG, "[%0.3f] DCF77: new frame for min %d", _seconds(),
t.tm_min); 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 #endif