From fde157dd0d2c8321c1c8d620053265c563709cc0 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 9 Feb 2019 13:02:38 +0100 Subject: [PATCH] preparations for changing gps library --- include/gpsread.h | 1 + platformio.ini | 6 ++++-- src/dcf77.cpp | 6 ++++++ src/gpsread.cpp | 24 ++++++++++++++++++++++-- src/hal/ttgobeam.h | 12 +++++++++++- src/hal/ttgofox.h | 11 ++++++++--- src/if482.cpp | 10 ++++++++-- src/rtctime.cpp | 20 +++++++++++++------- 8 files changed, 73 insertions(+), 17 deletions(-) diff --git a/include/gpsread.h b/include/gpsread.h index 3cd4a931..9fccefb4 100644 --- a/include/gpsread.h +++ b/include/gpsread.h @@ -16,5 +16,6 @@ int gps_init(void); void gps_read(void); void gps_loop(void *pvParameters); time_t get_gpstime(void); +int gps_config(); #endif \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index dc5b8873..e28b6047 100644 --- a/platformio.ini +++ b/platformio.ini @@ -15,8 +15,8 @@ ;env_default = ttgov2 ;env_default = ttgov21old ;env_default = ttgov21new -env_default = ttgobeam -;env_default = ttgofox +;env_default = ttgobeam +env_default = ttgofox ;env_default = lopy ;env_default = lopy4 ;env_default = fipy @@ -50,6 +50,7 @@ lib_deps_rgbled = SmartLeds@>=1.1.5 lib_deps_gps = TinyGPSPlus@>=1.0.2 +; NeoGPS^4.2.9 lib_deps_rtc = RTC@^2.3.0 lib_deps_basic = @@ -222,6 +223,7 @@ build_flags = upload_protocol = ${common.upload_protocol} extra_scripts = ${common.extra_scripts} monitor_speed = ${common.monitor_speed} +upload_port = COM61 [env:ttgobeam] platform = ${common.platform_espressif32} diff --git a/src/dcf77.cpp b/src/dcf77.cpp index 0648d854..a4b1c62a 100644 --- a/src/dcf77.cpp +++ b/src/dcf77.cpp @@ -20,8 +20,11 @@ static const char TAG[] = "main"; #define DCF77_FRAME_SIZE (60) #define DCF77_PULSE_DURATION (100) +// select internal / external clock #if defined RTC_INT && defined RTC_CLK #define PPS RTC_CLK +#elif defined GPS_INT && defined GPS_CLK +#define PPS GPS_CLK #else #define PPS DCF77_PULSE_DURATION #endif @@ -127,13 +130,16 @@ void dcf77_loop(void *pvParameters) { &wakeTime, // receives moment of call from isr portMAX_DELAY); // wait forever (missing error handling here...) +// select clock scale #if (PPS == DCF77_PULSE_DURATION) // we don't need clock rescaling DCF_Out(0); + #elif (PPS > DCF77_PULSE_DURATION) // we need upclocking for (uint8_t i = 1; i <= PPS / DCF77_PULSE_DURATION; i++) { DCF_Out(0); vTaskDelayUntil(&wakeTime, pdMS_TO_TICKS(DCF77_PULSE_DURATION)); } + #elif (PPS < DCF77_PULSE_DURATION) // we need downclocking vTaskDelayUntil(&wakeTime, pdMS_TO_TICKS(DCF77_PULSE_DURATION - PPS)); DCF_Out(0); diff --git a/src/gpsread.cpp b/src/gpsread.cpp index a446118e..6e094faf 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -18,6 +18,11 @@ int gps_init(void) { int ret = 1; + if (!gps_config()) { + ESP_LOGE(TAG, "GPS chip initializiation error"); + return 0; + } + #if defined GPS_SERIAL GPS_Serial.begin(GPS_SERIAL); ESP_LOGI(TAG, "Using serial GPS"); @@ -41,6 +46,21 @@ int gps_init(void) { return ret; } // gps_init() +// detect gps chipset type and configure it with device specific settings +int gps_config() { + int rslt = 1; // success +#if defined GPS_SERIAL + + /* to come */ + +#elif defined GPS_I2C + + /* to come */ + +#endif + return rslt; +} + // read GPS data and cast to global struct void gps_read() { gps_status.latitude = (int32_t)(gps.location.lat() * 1e6); @@ -73,8 +93,8 @@ time_t get_gpstime(void) { if ((gps.time.age() < 1500) && (gps.time.isValid())) { t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(), gps.time.hour(), gps.time.minute(), gps.time.second()); - ESP_LOGD(TAG, "GPS time: %4d/%02d/%02d %02d:%02d:%02d", year(t), month(t), day(t), - hour(t), minute(t), second(t)); + ESP_LOGD(TAG, "GPS time: %4d/%02d/%02d %02d:%02d:%02d", year(t), month(t), + day(t), hour(t), minute(t), second(t)); } else { ESP_LOGW(TAG, "GPS has no confident time"); } diff --git a/src/hal/ttgobeam.h b/src/hal/ttgobeam.h index f49eb27e..c32994a8 100644 --- a/src/hal/ttgobeam.h +++ b/src/hal/ttgobeam.h @@ -19,8 +19,14 @@ #define HAS_BUTTON GPIO_NUM_39 // on board button (next to reset) #define HAS_BATTERY_PROBE ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC1_CHANNEL_7 #define BATT_FACTOR 2 // voltage divider 100k/100k on board + +// GPS settings #define HAS_GPS 1 // use on board GPS #define GPS_SERIAL 9600, SERIAL_8N1, GPIO_NUM_12, GPIO_NUM_15 // UBlox NEO 6M or 7M with default configuration +// to use +#define GPS_INT GPIO_NUM_34 // 30ns accurary timepulse, to be external wired on pcb: R34 -> GPIO34 +// 1 pulse per second, synchronized at rising edge, pulse length 100ms, accuracy +/- 3 *e-8 [nanoseconds] = 0,95sec / year +#define GPS_CLK (1000) // enable only if device has these sensors, otherwise comment these lines // BME680 sensor on I2C bus @@ -35,7 +41,11 @@ //#define DISPLAY_FLIP 1 // use if display is rotated // Settings for DCF77 interface -#define HAS_DCF77 GPIO_NUM_13 +//#define HAS_DCF77 GPIO_NUM_13 + +// Settings for IF482 interface +#define HAS_IF482 9600, SERIAL_7E1, GPIO_NUM_12, GPIO_NUM_14 // IF482 serial port parameters + // user defined sensors (if connected) //#define HAS_SENSORS 1 // comment out if device has user defined sensors diff --git a/src/hal/ttgofox.h b/src/hal/ttgofox.h index f7cd3033..7be3544c 100644 --- a/src/hal/ttgofox.h +++ b/src/hal/ttgofox.h @@ -20,12 +20,17 @@ #define MY_OLED_SCL (22) #define MY_OLED_RST U8X8_PIN_NONE -// Pins for on board DS3231 RTC chip +// Settings 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 +//#define RTC_INT GPIO_NUM_34 // timepulse with accuracy +/- 2*e-6 [microseconds] = 0,1728sec / day +//#define RTC_CLK (1000) // frequency of RTC clock signal in ms // Settings for IF482 interface -#define HAS_IF482 9600, SERIAL_7E1, GPIO_NUM_12, GPIO_NUM_14 // IF482 serial port parameters +//#define HAS_IF482 9600, SERIAL_7E1, GPIO_NUM_12, GPIO_NUM_14 // IF482 serial port parameters + +// Settings for DCF77 interface +#define HAS_DCF77 GPIO_NUM_14 +#define DCF77_ACTIVE_LOW 1 // Settings for external GPS chip #define HAS_GPS 1 // use on board GPS diff --git a/src/if482.cpp b/src/if482.cpp index 71df7a56..600e3fd5 100644 --- a/src/if482.cpp +++ b/src/if482.cpp @@ -91,12 +91,16 @@ static const char TAG[] = "main"; #define IF482_FRAME_SIZE (17) #define IF482_PULSE_DURATION (1000) -#ifdef RTC_CLK +// select internal / external clock +#if defined RTC_INT && defined RTC_CLK #define PPS RTC_CLK +#elif defined GPS_INT && defined GPS_CLK +#define PPS GPS_CLK #else #define PPS IF482_PULSE_DURATION #endif + HardwareSerial IF482(2); // use UART #2 (note: #1 may be in use for serial GPS) // initialize and configure IF482 Generator @@ -175,20 +179,22 @@ void if482_loop(void *pvParameters) { &wakeTime, // receives moment of call from isr portMAX_DELAY); // wait forever (missing error handling here...) +// select clock scale #if (PPS == 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)); + #elif (PPS > IF482_PULSE_DURATION) // we need upclocking for (uint8_t i = 1; i <= PPS / IF482_PULSE_DURATION; i++) { vTaskDelayUntil(&wakeTime, shotTime); // sets waketime to moment of shot IF482.print(IF482_Out(now() + 1)); } + #elif (PPS < IF482_PULSE_DURATION) // we need downclocking IF482.print(IF482_Out(now() + 1)); vTaskDelayUntil(&wakeTime, shotTime - PPS); // sets waketime to moment of shot - #endif } } // if482_loop() diff --git a/src/rtctime.cpp b/src/rtctime.cpp index de909866..aee26515 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -102,8 +102,10 @@ float get_rtctemp(void) { #endif // HAS_RTC -int pps_init(uint32_t clk_freq_ms) { -// use fixed pulse clock as time base +// helper function to setup a pulse for time synchronisation +int pps_init(uint32_t pulse_period_ms) { + +// use pulse from on board RTC chip as time base with fixed frequency #if defined RTC_INT && defined RTC_CLK // setup external interupt for active low RTC INT pin @@ -121,13 +123,16 @@ int pps_init(uint32_t clk_freq_ms) { } return 1; // success + #elif defined RTC_INT && defined HAS_GPS + #else - // use clock with adjustable frequency - if (clk_freq_ms) { + // use ESP32 hardware timer as time base with adjustable frequency + if (pulse_period_ms) { ESP_LOGI(TAG, "Time base ESP32 clock"); - clockCycle = timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec + clockCycle = + timerBegin(1, 8000, true); // set 80 MHz prescaler to 1/10000 sec timerAttachInterrupt(clockCycle, &CLOCKIRQ, true); - timerAlarmWrite(clockCycle, 10 * clk_freq_ms, true); //ms + timerAlarmWrite(clockCycle, 10 * pulse_period_ms, true); // ms } else { ESP_LOGE(TAG, "Invalid pulse clock frequency"); return 0; // failure @@ -138,7 +143,8 @@ int pps_init(uint32_t clk_freq_ms) { void pps_start() { #ifdef RTC_INT // start external clock - attachInterrupt(digitalPinToInterrupt(RTC_INT), CLOCKIRQ, FALLING); + //attachInterrupt(digitalPinToInterrupt(RTC_INT), CLOCKIRQ, FALLING); + attachInterrupt(digitalPinToInterrupt(RTC_INT), CLOCKIRQ, RISING); #else // start internal clock timerAlarmEnable(clockCycle); #endif