From 44b821f2a5d4c02059258ab5caddf7a706887d57 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 25 Jan 2019 22:49:26 +0100 Subject: [PATCH] added IF482 time telegram generator (experimental) --- include/if482.h | 4 +++ include/irqhandler.h | 7 +++++ include/rtctime.h | 2 ++ src/hal/ttgofox.h | 6 +++- src/if482.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++-- src/irqhandler.cpp | 15 +++++++++- src/rtctime.cpp | 1 + 7 files changed, 96 insertions(+), 5 deletions(-) diff --git a/include/if482.h b/include/if482.h index 82cc4764..85871867 100644 --- a/include/if482.h +++ b/include/if482.h @@ -2,5 +2,9 @@ #define _IF482_H #include "globals.h" +#include "irqhandler.h" + +void if482_init(void); +void sendIF482(time_t t); #endif \ No newline at end of file diff --git a/include/irqhandler.h b/include/irqhandler.h index d068df32..9adebe42 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -5,6 +5,7 @@ #define BUTTON_IRQ 0x02 #define SENDCOUNTER_IRQ 0x04 #define CYCLIC_IRQ 0x08 +#define IF482_IRQ 0x10 #include "globals.h" #include "cyclic.h" @@ -25,4 +26,10 @@ void IRAM_ATTR DisplayIRQ(); void IRAM_ATTR ButtonIRQ(); #endif +#ifdef HAS_IF482 +#include "if482.h" +void IRAM_ATTR IF482IRQ(); +#endif + + #endif diff --git a/include/rtctime.h b/include/rtctime.h index ae9925b4..b8ecd9cd 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -18,6 +18,8 @@ typedef enum { synced_GPS = 4 // best possible quality, clock is driven by GPS } clock_state_t; +extern RtcDS3231 Rtc; // make RTC instance globally available + int rtc_init(void); void sync_rtctime(void); int set_rtctime(uint32_t UTCTime); diff --git a/src/hal/ttgofox.h b/src/hal/ttgofox.h index 2ecb493e..4d60f877 100644 --- a/src/hal/ttgofox.h +++ b/src/hal/ttgofox.h @@ -10,7 +10,6 @@ #define HAS_DISPLAY U8X8_SSD1306_128X64_NONAME_HW_I2C #define HAS_LED NOT_A_PIN // green on board LED is useless, is GPIO25, which switches power for Lora+Display -//#define LED_ACTIVE_LOW 1 // Onboard LED is active when pin is LOW #define HAS_LOWPOWER_SWITCH GPIO_NUM_25 // switches power for LoRa chip + display (0 = off / 1 = on) #define HAS_BATTERY_PROBE ADC1_GPIO35_CHANNEL #define BATT_FACTOR 2 // voltage divider 100k/100k on board @@ -23,6 +22,11 @@ // Pins for on board DS3231 RTC chip #define HAS_RTC MY_OLED_SDA, MY_OLED_SCL // SDA, SCL +#define RTC_INT GPIO_NUM_34 // interrupt input from rtc + +// Settings for IF482 interface +#define HAS_IF482 9600, SERIAL_7E1, GPIO_NUM_12, GPIO_NUM_15 // IF482 serial port parameters +#define IF482_PORT 1 // serial port 1 // Pins for LORA chip SPI interface, reset line and interrupt lines #define LORA_SCK (5) diff --git a/src/if482.cpp b/src/if482.cpp index a43c1d42..e38f2216 100644 --- a/src/if482.cpp +++ b/src/if482.cpp @@ -1,6 +1,7 @@ -#ifdef HAS_IF482 +#if defined HAS_IF482 && defined HAS_RTC /* + IF482 Generator to control clocks with IF482 timeframe input (e.g. BÜRK BU190) Example IF482 timeframe: "OAL160806F170400" @@ -35,7 +36,7 @@ Byte Bedeutung Zeichen HEX Code *) Überwachung: 'A': Korrekter Zeitempfang des Sendegerätes -'M': Sendegerät hat mehr als 12 Stunden kein Zeitsignal empfangen +'M': Sendegerät hat mehr als 12 Stunden kein Zeitsignal empfangen Zeit wird bei 'A' und 'M' vom Uhrwerk übernommen. **) Lokale Zeit: @@ -43,7 +44,7 @@ Zeit wird bei 'A' und 'M' vom Uhrwerk übernommen. 'S': Sommerzeit 'L': Lokalzeit Wird überprüft, jedoch nicht vom Uhrwerk ausgewertet. - + */ #include "if482.h" @@ -51,5 +52,64 @@ Wird überprüft, jedoch nicht vom Uhrwerk ausgewertet. // Local logging tag static const char TAG[] = "main"; +HardwareSerial IF482(IF482_PORT); // serial port 1 + +// initialize and configure GPS +void if482_init(void) { + + IF482.begin(HAS_IF482); + ESP_LOGI(TAG, "IF482 serial port opened"); + + // use rtc 1Hz clock for triggering IF482 telegram send + Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz); + Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock); + + // setup external interupt for active low RTC INT pin + attachInterrupt(RTC_INT, IF482IRQ, FALLING); + +} // if482_init + +char * if482Telegram(time_t t) { + + static char out[17] = {0}; // 16 bytes IF482 telegram plus null termination char + char buf[14] = {0}; + + strcat_P(out, "O"); // + + switch (timeStatus()) { // indicates if time has been set and recently synced + + case timeSet: // time is set and is synced + strcat_P(out, "A"); + break; + + case timeNeedsSync: // time had been set but sync attempt did not succeed + strcat_P(out, "M"); + break; + + default: // time not set, no valid time + strcat_P(out, "?"); + break; + + } // switch + + strcat_P(out, "L"); // local time + + if (!timeNotSet) { // do we have valid time? + sprintf(buf, "%02u%02u%02uF%02u%02u%02u", year(t), month(t), day(t), hour(t), + minute(t), second(t)); + strcat(out, buf); + } else { + strcat_P(out, "000000F000000"); + } + + strcat_P(out, "\r"); // + + return out; +} + +// interrupt triggered routine +void sendIF482(time_t t) { + IF482.write(if482Telegram(t)); +} #endif // HAS_IF482 \ No newline at end of file diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index a9192a2b..ad01a592 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -18,6 +18,12 @@ void irqHandler(void *pvParameters) { &InterruptStatus, // Receives the notification value portMAX_DELAY); // wait forever (missing error handling here...) +// IF482 interrupt? +#ifdef HAS_IF482 + if (InterruptStatus & IF482_IRQ) + sendIF482(now()); +#endif + // button pressed? #ifdef HAS_BUTTON if (InterruptStatus & BUTTON_IRQ) @@ -61,7 +67,7 @@ void IRAM_ATTR SendCycleIRQ() { #ifdef HAS_DISPLAY void IRAM_ATTR DisplayIRQ() { - xTaskNotifyFromISR(irqHandlerTask, DISPLAY_IRQ, eSetBits, NULL); + xTaskNotifyFromISR(irqHandlerTask, DISPLAY_IRQ, eSetBits, NULL); portYIELD_FROM_ISR(); } #endif @@ -72,3 +78,10 @@ void IRAM_ATTR ButtonIRQ() { portYIELD_FROM_ISR(); } #endif + +#ifdef HAS_IF482 +void IRAM_ATTR IF482IRQ() { + xTaskNotifyFromISR(irqHandlerTask, IF482_IRQ, eSetBits, NULL); + portYIELD_FROM_ISR(); +} +#endif diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 1819c959..624e30e4 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -42,6 +42,7 @@ int rtc_init(void) { // configure RTC chip Rtc.Enable32kHzPin(false); Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); + } else { ESP_LOGE(TAG, "I2c bus busy - RTC initialization error"); goto error;