DCF77 code sanitization

This commit is contained in:
Klaus K Wilting 2019-02-23 20:28:11 +01:00
parent 590e4d91f8
commit f08a649086
3 changed files with 43 additions and 47 deletions

View File

@ -12,13 +12,12 @@ enum dcf_pinstate { dcf_high, dcf_low };
enum dcf_pinstate { dcf_low, dcf_high }; enum dcf_pinstate { dcf_low, dcf_high };
#endif #endif
enum dcf_pulses { dcf_off, dcf_zero, dcf_one }; enum DCF77_Pulses { dcf_Z, dcf_0, dcf_1 };
extern uint8_t DCFpulse[];
void DCF_Pulse(time_t t); void DCF77_Pulse(time_t t, uint8_t const *DCFpulse);
void IRAM_ATTR DCF77_Frame(time_t t); uint8_t *IRAM_ATTR DCF77_Frame(time_t const t);
uint8_t IRAM_ATTR dec2bcd(uint8_t dec, uint8_t startpos, uint8_t endpos, uint8_t IRAM_ATTR dec2bcd(uint8_t const dec, uint8_t const startpos, uint8_t const endpos,
uint8_t pArray[]); uint8_t *DCFpulse);
uint8_t IRAM_ATTR setParityBit(uint8_t p); uint8_t IRAM_ATTR setParityBit(uint8_t const p);
#endif #endif

View File

@ -16,35 +16,24 @@ https://github.com/udoklein/dcf77
// Local logging tag // Local logging tag
static const char TAG[] = "main"; static const char TAG[] = "main";
// array of dcf pulses for one minute, secs 0..16 and 20 are never touched, so // triggered by second timepulse to ticker out DCF signal
// we initialize them statically to avoid dumb recalculation every minute void DCF77_Pulse(time_t t, uint8_t const *DCFpulse) {
uint8_t DCFpulse[DCF77_FRAME_SIZE + 1] = {
dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero,
dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero,
dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_zero, dcf_one};
// triggered by 1 second timepulse to ticker out DCF signal
void DCF_Pulse(time_t t) {
TickType_t startTime = xTaskGetTickCount(); TickType_t startTime = xTaskGetTickCount();
uint8_t sec = second(t); uint8_t sec = second(t);
ESP_LOGD(TAG, "DCF77 sec %d", sec);
// induce 10 pulses // induce 10 pulses
for (uint8_t pulse = 0; pulse <= 9; pulse++) { for (uint8_t pulse = 0; pulse <= 9; pulse++) {
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 (DCFpulse[sec] != dcf_off) if (DCFpulse[sec] != dcf_Z)
digitalWrite(HAS_DCF77, dcf_low); digitalWrite(HAS_DCF77, dcf_low);
else // 59th second reached, nothing more to do
return;
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 (DCFpulse[sec] == dcf_zero) if (DCFpulse[sec] == dcf_0)
digitalWrite(HAS_DCF77, dcf_high); digitalWrite(HAS_DCF77, dcf_high);
break; break;
@ -53,27 +42,35 @@ void DCF_Pulse(time_t t) {
break; break;
case 9: // 900ms after start -> last pulse case 9: // 900ms after start -> last pulse
return; break;
} // switch } // switch
// impulse period pause // pulse pause
vTaskDelayUntil(&startTime, pdMS_TO_TICKS(DCF77_PULSE_LENGTH)); vTaskDelayUntil(&startTime, pdMS_TO_TICKS(DCF77_PULSE_LENGTH));
} // for } // for
} // DCF_Pulse() } // DCF77_Pulse()
void IRAM_ATTR DCF77_Frame(time_t tt) { uint8_t *IRAM_ATTR DCF77_Frame(time_t const tt) {
// array of dcf pulses for one minute, secs 0..16 and 20 are never touched, so
// we keep them statically to avoid same recalculation every minute
static uint8_t DCFpulse[DCF77_FRAME_SIZE + 1] = {
dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0,
dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0,
dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_0, dcf_1};
uint8_t Parity; uint8_t Parity;
time_t t = myTZ.toLocal(tt); // convert to local time time_t t = myTZ.toLocal(tt); // convert to local time
// ENCODE DST CHANGE ANNOUNCEMENT (Sec 16) // ENCODE DST CHANGE ANNOUNCEMENT (Sec 16)
DCFpulse[16] = dcf_zero; // not yet implemented DCFpulse[16] = dcf_0; // not yet implemented
// ENCODE DAYLIGHTSAVING (secs 17..18) // ENCODE DAYLIGHTSAVING (secs 17..18)
DCFpulse[17] = myTZ.locIsDST(t) ? dcf_one : dcf_zero; DCFpulse[17] = myTZ.locIsDST(t) ? dcf_1 : dcf_0;
DCFpulse[18] = myTZ.locIsDST(t) ? dcf_zero : dcf_one; DCFpulse[18] = myTZ.locIsDST(t) ? dcf_0 : dcf_1;
// ENCODE MINUTE (secs 21..28) // ENCODE MINUTE (secs 21..28)
Parity = dec2bcd(minute(t), 21, 27, DCFpulse); Parity = dec2bcd(minute(t), 21, 27, DCFpulse);
@ -87,32 +84,28 @@ void IRAM_ATTR DCF77_Frame(time_t tt) {
Parity = dec2bcd(day(t), 36, 41, DCFpulse); Parity = dec2bcd(day(t), 36, 41, DCFpulse);
Parity += dec2bcd((weekday(t) - 1) ? (weekday(t) - 1) : 7, 42, 44, DCFpulse); Parity += dec2bcd((weekday(t) - 1) ? (weekday(t) - 1) : 7, 42, 44, DCFpulse);
Parity += dec2bcd(month(t), 45, 49, DCFpulse); Parity += dec2bcd(month(t), 45, 49, DCFpulse);
Parity += dec2bcd(year(t) - 2000, 50, 57, Parity += dec2bcd(year(t) - 2000, 50, 57, DCFpulse);
DCFpulse); // yes, we have a millenium 3000 bug here ;-)
DCFpulse[58] = setParityBit(Parity); DCFpulse[58] = setParityBit(Parity);
// ENCODE MARK (sec 59) // ENCODE MARK (sec 59)
DCFpulse[59] = dcf_off; // !! missing code here for leap second !! DCFpulse[59] = dcf_Z; // !! missing code here for leap second !!
// timestamp this frame with it's minute // timestamp this frame with it's minute
DCFpulse[60] = minute(t); DCFpulse[60] = minute(t);
return DCFpulse;
} // DCF77_Frame() } // DCF77_Frame()
// helper function to encode parity
uint8_t IRAM_ATTR setParityBit(uint8_t p) {
return ((p & 1) ? dcf_one : dcf_zero);
}
// helper function to convert decimal to bcd digit // helper function to convert decimal to bcd digit
uint8_t IRAM_ATTR dec2bcd(uint8_t dec, uint8_t startpos, uint8_t endpos, uint8_t IRAM_ATTR dec2bcd(uint8_t const dec, uint8_t const startpos, uint8_t const endpos,
uint8_t pArray[]) { uint8_t *DCFpulse) {
uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10); uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
uint8_t parity = 0; uint8_t parity = 0;
for (uint8_t n = startpos; n <= endpos; n++) { for (uint8_t i = startpos; i <= endpos; i++) {
pArray[n] = (data & 1) ? dcf_one : dcf_zero; DCFpulse[i] = (data & 1) ? dcf_1 : dcf_0;
parity += (data & 1); parity += (data & 1);
data >>= 1; data >>= 1;
} }
@ -120,4 +113,7 @@ uint8_t IRAM_ATTR dec2bcd(uint8_t dec, uint8_t startpos, uint8_t endpos,
return parity; return parity;
} }
// helper function to encode parity
uint8_t IRAM_ATTR setParityBit(uint8_t const p) { return ((p & 1) ? dcf_1 : dcf_0); }
#endif // HAS_DCF77 #endif // HAS_DCF77

View File

@ -74,7 +74,7 @@ int timepulse_init() {
// use time pulse from GPS as time base with fixed 1Hz frequency // use time pulse from GPS as time base with fixed 1Hz frequency
#ifdef GPS_INT #ifdef GPS_INT
// setup external interupt for active low RTC INT pin // setup external interupt pin for GPS INT output
pinMode(GPS_INT, INPUT_PULLDOWN); pinMode(GPS_INT, INPUT_PULLDOWN);
// setup external rtc 1Hz clock as pulse per second clock // setup external rtc 1Hz clock as pulse per second clock
ESP_LOGI(TAG, "Timepulse: external (GPS)"); ESP_LOGI(TAG, "Timepulse: external (GPS)");
@ -83,7 +83,7 @@ int timepulse_init() {
// use pulse from on board RTC chip as time base with fixed frequency // use pulse from on board RTC chip as time base with fixed frequency
#elif defined RTC_INT #elif defined RTC_INT
// setup external interupt for active low RTC INT pin // setup external interupt pin for active low RTC INT output
pinMode(RTC_INT, INPUT_PULLUP); pinMode(RTC_INT, INPUT_PULLUP);
// setup external rtc 1Hz clock as pulse per second clock // setup external rtc 1Hz clock as pulse per second clock
@ -196,7 +196,8 @@ void clock_loop(void *pvParameters) { // ClockTask
// preload first DCF frame before start // preload first DCF frame before start
#ifdef HAS_DCF77 #ifdef HAS_DCF77
DCF77_Frame(t1(now())); uint8_t *DCFpulse;
DCFpulse = DCF77_Frame(t1(now()));
#endif #endif
// output time telegram for second following sec beginning with timepulse // output time telegram for second following sec beginning with timepulse
@ -216,11 +217,11 @@ void clock_loop(void *pvParameters) { // ClockTask
#elif defined HAS_DCF77 #elif defined HAS_DCF77
if (second(t) == DCF77_FRAME_SIZE - 1) // is it time to load new frame? if (second(t) == DCF77_FRAME_SIZE - 1) // is it time to load new frame?
DCF77_Frame(t1(t)); // generate next frame DCFpulse = DCF77_Frame(t1(t)); // generate next frame
if (DCFpulse[DCF77_FRAME_SIZE] == if (DCFpulse[DCF77_FRAME_SIZE] ==
minute(t1(t))) // have recent frame? (pulses could be missed!) minute(t1(t))) // have recent frame? (pulses could be missed!)
DCF_Pulse(t2(t)); // then output next second of this frame DCF77_Pulse(t2(t), DCFpulse); // then output next second of this frame
#endif #endif