2019-02-04 20:02:30 +01:00
|
|
|
/*
|
2019-02-17 19:21:08 +01:00
|
|
|
// Emulate a DCF77 radio receiver to control an external clock
|
2019-02-03 21:19:08 +01:00
|
|
|
//
|
2019-02-05 23:50:05 +01:00
|
|
|
// a nice & free logic test program for DCF77 can be found here:
|
|
|
|
https://www-user.tu-chemnitz.de/~heha/viewzip.cgi/hs/Funkuhr.zip/
|
|
|
|
//
|
2019-02-17 22:12:14 +01:00
|
|
|
// a DCF77 digital scope for Arduino boards can be found here:
|
|
|
|
https://github.com/udoklein/dcf77
|
|
|
|
//
|
2019-02-03 21:19:08 +01:00
|
|
|
*/
|
|
|
|
|
2019-02-08 22:19:44 +01:00
|
|
|
#ifdef HAS_DCF77
|
|
|
|
|
2019-02-03 21:19:08 +01:00
|
|
|
#include "dcf77.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:52:27 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-23 20:28:11 +01:00
|
|
|
// triggered by second timepulse to ticker out DCF signal
|
|
|
|
void DCF77_Pulse(time_t t, uint8_t const *DCFpulse) {
|
2019-02-04 20:02:30 +01:00
|
|
|
|
2019-02-23 18:12:43 +01:00
|
|
|
TickType_t startTime = xTaskGetTickCount();
|
2022-01-13 23:30:18 +01:00
|
|
|
uint8_t sec = t % 60;
|
2019-02-16 15:02:07 +01:00
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
ESP_LOGD(TAG, "[%0.3f] DCF second: %d", _seconds(), sec);
|
2019-02-27 22:40:58 +01:00
|
|
|
|
2019-04-08 21:20:42 +02:00
|
|
|
// induce a DCF Pulse
|
|
|
|
for (uint8_t pulse = 0; pulse <= 2; pulse++) {
|
2019-02-04 20:02:30 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
switch (pulse) {
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-04 20:02:30 +01:00
|
|
|
case 0: // start of second -> start of timeframe for logic signal
|
2019-02-23 20:28:11 +01:00
|
|
|
if (DCFpulse[sec] != dcf_Z)
|
2019-02-23 18:12:43 +01:00
|
|
|
digitalWrite(HAS_DCF77, dcf_low);
|
2019-02-04 23:42:17 +01:00
|
|
|
break;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-04 20:02:30 +01:00
|
|
|
case 1: // 100ms after start of second -> end of timeframe for logic 0
|
2019-02-23 20:28:11 +01:00
|
|
|
if (DCFpulse[sec] == dcf_0)
|
2019-02-23 18:12:43 +01:00
|
|
|
digitalWrite(HAS_DCF77, dcf_high);
|
2019-02-04 23:42:17 +01:00
|
|
|
break;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-04 20:02:30 +01:00
|
|
|
case 2: // 200ms after start of second -> end of timeframe for logic 1
|
2019-02-23 18:12:43 +01:00
|
|
|
digitalWrite(HAS_DCF77, dcf_high);
|
2019-02-04 23:42:17 +01:00
|
|
|
break;
|
2019-02-03 21:19:08 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
} // switch
|
2019-02-05 23:50:05 +01:00
|
|
|
|
2019-02-23 20:28:11 +01:00
|
|
|
// pulse pause
|
2022-01-13 23:30:18 +01:00
|
|
|
vTaskDelayUntil(&startTime, pdMS_TO_TICKS(100));
|
2019-02-05 23:50:05 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
} // for
|
2019-02-23 20:28:11 +01:00
|
|
|
} // DCF77_Pulse()
|
2019-02-05 23:50:05 +01:00
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
uint8_t *IRAM_ATTR DCF77_Frame(time_t const tt) {
|
2019-02-23 20:28:11 +01:00
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
struct tm t = {0};
|
|
|
|
localtime_r(&tt, &t); // convert to local time
|
2019-02-23 20:28:11 +01:00
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
// array of dcf pulses for one minute
|
|
|
|
// secs 0..15 and 20 are never changing, thus we keep them statically to avoid
|
|
|
|
// same recalculation every minute
|
|
|
|
|
|
|
|
static uint8_t DCFpulse[61] = {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};
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-16 15:02:07 +01:00
|
|
|
uint8_t Parity;
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
// ENCODE DST CHANGE ANNOUNCEMENT (sec 16)
|
2019-02-23 20:28:11 +01:00
|
|
|
DCFpulse[16] = dcf_0; // not yet implemented
|
2019-02-23 18:12:43 +01:00
|
|
|
|
|
|
|
// ENCODE DAYLIGHTSAVING (secs 17..18)
|
2022-01-13 23:30:18 +01:00
|
|
|
// "01" = MEZ / "10" = MESZ
|
|
|
|
DCFpulse[17] = (t.tm_isdst > 0) ? dcf_1 : dcf_0;
|
|
|
|
DCFpulse[18] = (t.tm_isdst > 0) ? dcf_0 : dcf_1;
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// ENCODE MINUTE (secs 21..28)
|
2022-01-13 23:30:18 +01:00
|
|
|
Parity = dec2bcd(t.tm_min, 21, 27, DCFpulse);
|
2019-02-23 18:12:43 +01:00
|
|
|
DCFpulse[28] = setParityBit(Parity);
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// ENCODE HOUR (secs 29..35)
|
2022-01-13 23:30:18 +01:00
|
|
|
Parity = dec2bcd(t.tm_hour, 29, 34, DCFpulse);
|
2019-02-23 18:12:43 +01:00
|
|
|
DCFpulse[35] = setParityBit(Parity);
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// ENCODE DATE (secs 36..58)
|
2022-01-13 23:30:18 +01:00
|
|
|
Parity = dec2bcd(t.tm_mday, 36, 41, DCFpulse);
|
|
|
|
Parity += dec2bcd((t.tm_wday == 0) ? 7 : t.tm_wday, 42, 44, DCFpulse);
|
|
|
|
Parity += dec2bcd(t.tm_mon + 1, 45, 49, DCFpulse);
|
|
|
|
Parity += dec2bcd(t.tm_year + 1900 - 2000, 50, 57, DCFpulse);
|
2019-02-23 18:12:43 +01:00
|
|
|
DCFpulse[58] = setParityBit(Parity);
|
|
|
|
|
|
|
|
// ENCODE MARK (sec 59)
|
2019-02-23 20:28:11 +01:00
|
|
|
DCFpulse[59] = dcf_Z; // !! missing code here for leap second !!
|
2019-02-23 18:12:43 +01:00
|
|
|
|
2019-02-23 18:31:47 +01:00
|
|
|
// timestamp this frame with it's minute
|
2022-01-13 23:30:18 +01:00
|
|
|
DCFpulse[60] = t.tm_min;
|
2019-02-23 18:12:43 +01:00
|
|
|
|
2019-02-23 20:28:11 +01:00
|
|
|
return DCFpulse;
|
2019-02-23 18:12:43 +01:00
|
|
|
|
2019-02-23 20:28:11 +01:00
|
|
|
} // DCF77_Frame()
|
2019-02-07 20:39:32 +01:00
|
|
|
|
2019-02-17 19:21:08 +01:00
|
|
|
// helper function to convert decimal to bcd digit
|
2019-03-16 21:01:43 +01:00
|
|
|
uint8_t IRAM_ATTR dec2bcd(uint8_t const dec, uint8_t const startpos,
|
|
|
|
uint8_t const endpos, uint8_t *DCFpulse) {
|
2019-02-17 19:21:08 +01:00
|
|
|
|
|
|
|
uint8_t data = (dec < 10) ? dec : ((dec / 10) << 4) + (dec % 10);
|
|
|
|
uint8_t parity = 0;
|
|
|
|
|
2019-02-23 20:28:11 +01:00
|
|
|
for (uint8_t i = startpos; i <= endpos; i++) {
|
|
|
|
DCFpulse[i] = (data & 1) ? dcf_1 : dcf_0;
|
2019-02-17 19:21:08 +01:00
|
|
|
parity += (data & 1);
|
|
|
|
data >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parity;
|
|
|
|
}
|
|
|
|
|
2019-02-23 20:28:11 +01:00
|
|
|
// helper function to encode parity
|
2019-03-16 21:01:43 +01:00
|
|
|
uint8_t IRAM_ATTR setParityBit(uint8_t const p) {
|
|
|
|
return ((p & 1) ? dcf_1 : dcf_0);
|
|
|
|
}
|
2019-02-23 20:28:11 +01:00
|
|
|
|
2019-02-03 21:19:08 +01:00
|
|
|
#endif // HAS_DCF77
|