From 5fcfdd91e5f70644fa22740f5d7fb8d1c2524342 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 8 Feb 2019 21:48:56 +0100 Subject: [PATCH] Revert "DCF77 + IF482 code sanitizations" This reverts commit 160b282c3a411fa37b7ce687b5778f202c93b02b. --- include/rtctime.h | 1 + src/dcf77.cpp | 22 +++++++++------------- src/if482.cpp | 31 ++++++++++++++----------------- src/main.cpp | 6 ++++-- src/rtctime.cpp | 33 +++++++++++++++++---------------- 5 files changed, 45 insertions(+), 48 deletions(-) diff --git a/include/rtctime.h b/include/rtctime.h index 5b8672e9..479ac557 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -22,6 +22,7 @@ time_t get_rtctime(void); float get_rtctemp(void); void IRAM_ATTR CLOCKIRQ(); int pps_init(uint32_t pps_freq); +int pps_init(); void pps_start(); uint8_t sync_clock(time_t t); diff --git a/src/dcf77.cpp b/src/dcf77.cpp index 8cc2313c..29182a5f 100644 --- a/src/dcf77.cpp +++ b/src/dcf77.cpp @@ -6,11 +6,7 @@ https://www-user.tu-chemnitz.de/~heha/viewzip.cgi/hs/Funkuhr.zip/ // */ -#ifdef HAS_DCF77 - -#ifdef IF_482 -#error "You must define at most one of IF482 or DCF_77" -#endif +#if defined HAS_DCF77 #include "dcf77.h" @@ -19,11 +15,6 @@ static const char TAG[] = "main"; #define DCF77_FRAME_SIZE (60) #define DCF77_PULSE_DURATION (100) -#ifdef RTC_CLK -#define PPS (RTC_CLK / DCF77_PULSE_DURATION) -#else -#define PPS DCF77_PULSE_DURATION -#endif // array of dcf pulses for three minutes uint8_t DCFtimeframe[DCF77_FRAME_SIZE]; @@ -46,7 +37,12 @@ int dcf77_init(void) { assert(ClockTask); // has clock task started? - pps_init(PPS); // setup pulse +#if defined RTC_INT && (RTC_CLK == DCF77_PULSE_DURATION) + pps_init(); // use pps clock +#else + pps_init(DCF77_PULSE_DURATION); // use esp32 clock +#endif + DCF_Out(sync_clock(now())); // sync DCF time on next second pps_start(); // start pulse @@ -122,10 +118,10 @@ void dcf77_loop(void *pvParameters) { &wakeTime, // receives moment of call from isr portMAX_DELAY); // wait forever (missing error handling here...) -#if (PPS == DCF77_PULSE_DURATION) // we don't need clock rescaling +#if !defined RTC_CLK || (RTC_CLK == DCF77_PULSE_DURATION) // we don't need clock rescaling DCF_Out(0); #else // we need clock rescaling by software timer - for (uint8_t i = 1; i <= PPS; i++) { + for (uint8_t i = 1; i <= RTC_CLK / DCF77_PULSE_DURATION; i++) { DCF_Out(0); vTaskDelayUntil(&wakeTime, pdMS_TO_TICKS(DCF77_PULSE_DURATION)); } diff --git a/src/if482.cpp b/src/if482.cpp index 41ac2834..da582716 100644 --- a/src/if482.cpp +++ b/src/if482.cpp @@ -1,3 +1,5 @@ +#if defined HAS_IF482 + /* NOTE: The IF482 Generator needs an high precise 1 Hz clock signal which cannot be acquired in suitable precision on the ESP32 SoC itself. Additional clocking @@ -77,12 +79,6 @@ not evaluated by model BU-190 */ /////////////////////////////////////////////////////////////////////////////// -#ifdef HAS_IF482 - -#ifdef HAS_DCF77 -#error "You must define at most one of IF482 or DCF_77" -#endif - #include "if482.h" // Local logging tag @@ -90,11 +86,6 @@ static const char TAG[] = "main"; #define IF482_FRAME_SIZE (17) #define IF482_PULSE_DURATION (1000) -#ifdef RTC_CLK -#define PPS (RTC_CLK / IF482_PULSE_DURATION) -#else -#define PPS IF482_PULSE_DURATION -#endif HardwareSerial IF482(2); // use UART #2 (note: #1 may be in use for serial GPS) @@ -115,8 +106,13 @@ int if482_init(void) { assert(ClockTask); // has clock task started? - pps_init(PPS); // setup pulse - pps_start(); // start pulse +#if defined RTC_INT && (RTC_CLK == IF482_PULSE_DURATION) + pps_init(); // use pps clock +#else + pps_init(IF482_PULSE_DURATION); // use esp32 clock +#endif + + pps_start(); // start pulse return 1; // success } // if482_init @@ -174,14 +170,15 @@ void if482_loop(void *pvParameters) { &wakeTime, // receives moment of call from isr portMAX_DELAY); // wait forever (missing error handling here...) -#if (PPS == DCF77_PULSE_DURATION) // we don't need clock rescaling +#if !defined RTC_CLK || (RTC_CLK == IF482_PULSE_DURATION) // we don't need clock rescaling // wait until it's time to start transmit telegram for next second vTaskDelayUntil(&wakeTime, shotTime); // sets waketime to moment of shot IF482.print(IF482_Out(now() + 1)); + #else // we need clock rescaling by software timer - /* - not yet implemented for IF482 - */ + /* + not yet implemented for IF482 + */ #endif } } // if482_loop() diff --git a/src/main.cpp b/src/main.cpp index b2ce3336..288c5e61 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,7 +65,7 @@ char display_line6[16], display_line7[16]; // display buffers uint8_t volatile channel = 0; // channel rotation counter uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0, batt_voltage = 0; // globals for display -bool volatile BitsPending = false; // DCF77 or IF482 ticker indicator +bool volatile BitsPending = false; // DCF77 or IF482 ticker indicator hw_timer_t *sendCycle = NULL, *homeCycle = NULL; #ifdef HAS_DISPLAY @@ -416,7 +416,9 @@ void setup() { setSyncInterval(TIME_SYNC_INTERVAL_GPS * 60); #endif -#ifdef HAS_IF482 +#if (defined HAS_IF482) && (defined DCF_77) +#error "You may define at most one of HAS_IF482 or DCF_77" +#elif defined HAS_IF482 ESP_LOGI(TAG, "Starting IF482 Generator..."); assert(if482_init()); #elif defined HAS_DCF77 diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 5c08af77..bd2ae503 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -1,15 +1,15 @@ +#ifdef HAS_RTC + #include "rtctime.h" // Local logging tag static const char TAG[] = "main"; +RtcDS3231 Rtc(Wire); // RTC hardware i2c interface + TaskHandle_t ClockTask; hw_timer_t *clockCycle = NULL; -#ifdef HAS_RTC // we have hardware RTC - -RtcDS3231 Rtc(Wire); // RTC hardware i2c interface - // initialize RTC int rtc_init(void) { @@ -100,11 +100,9 @@ float get_rtctemp(void) { return 0; } // get_rtctemp() -#endif // HAS_RTC - -int pps_init(uint32_t clk_freq_Hz) { -// use fixed pulse clock as time base -#if defined RTC_INT && (RTC_CLK == clk_freq_Hz) +int pps_init() { +// we have hardware pps signal as time base +#if defined RTC_INT && defined RTC_CLK // setup external interupt for active low RTC INT pin pinMode(RTC_INT, INPUT_PULLUP); @@ -120,20 +118,21 @@ int pps_init(uint32_t clk_freq_Hz) { return 0; // failure } return 1; // success +#endif +} -#else - // use clock with adjustable frequency - if (clk_freq_Hz) { +int pps_init(uint32_t pps_freq) { + // if we don't have hardware pps we use ESP32 hardware timer + if (pps_freq) { ESP_LOGI(TAG, "Time base ESP32 clock"); clockCycle = timerBegin(1, 8000, true); // set 80 MHz prescaler timerAttachInterrupt(clockCycle, &CLOCKIRQ, true); - timerAlarmWrite(clockCycle, 100 * clk_freq_Hz, true); + timerAlarmWrite(clockCycle, 10 * pps_freq, true); } else { - ESP_LOGE(TAG, "Invalid pulse clock frequency"); + ESP_LOGE(TAG, "Invalid pps clock frequency"); return 0; // failure } return 1; // success -#endif } void pps_start() { @@ -160,4 +159,6 @@ uint8_t sync_clock(time_t t) { void IRAM_ATTR CLOCKIRQ() { xTaskNotifyFromISR(ClockTask, xTaskGetTickCountFromISR(), eSetBits, NULL); portYIELD_FROM_ISR(); -} \ No newline at end of file +} + +#endif // HAS_RTC \ No newline at end of file