diff --git a/src/globals.h b/src/globals.h index fd057b5d..2d8d692f 100644 --- a/src/globals.h +++ b/src/globals.h @@ -55,6 +55,7 @@ extern std::array::iterator it; extern std::array beacons; #ifdef HAS_GPS +extern TaskHandle_t GpsTask; #include "gps.h" #endif diff --git a/src/gps.cpp b/src/gps.cpp index 435cd9a5..15dd9638 100644 --- a/src/gps.cpp +++ b/src/gps.cpp @@ -25,19 +25,23 @@ void gps_loop(void *pvParameters) { // initialize and, if needed, configure, GPS #if defined GPS_SERIAL HardwareSerial GPS_Serial(1); - GPS_Serial.begin(GPS_SERIAL); // serial connect to GPS device + GPS_Serial.begin(GPS_SERIAL); #elif defined GPS_QUECTEL_L76 + uint8_t ret; Wire.begin(GPS_QUECTEL_L76, 400000); // I2C connect to GPS device with 400 KHz - uint8_t i2c_ret; Wire.beginTransmission(GPS_ADDR); - Wire.write(0x00); // dummy write to start read - i2c_ret = Wire.endTransmission(); // check if chip is seen on i2c bus + Wire.write(0x00); // dummy write + ret = Wire.endTransmission(); // check if chip is seen on i2c bus - if (i2c_ret) { - ESP_LOGE(TAG, "Quectel L76 GPS chip not found on i2c bus, bus error %d", - i2c_ret); - return; + if (ret) { + ESP_LOGE(TAG, + "Quectel L76 GPS chip not found on i2c bus, bus error %d. " + "Stopping GPS-Task.", + ret); + vTaskDelete(GpsTask); + } else { + ESP_LOGI(TAG, "Quectel L76 GPS chip found."); } #endif @@ -46,31 +50,18 @@ void gps_loop(void *pvParameters) { if (cfg.gpsmode) { #if defined GPS_SERIAL - - while (cfg.gpsmode) { - // feed GPS decoder with serial NMEA data from GPS device - while (GPS_Serial.available()) { - gps.encode(GPS_Serial.read()); - } - vTaskDelay(2 / portTICK_PERIOD_MS); // reset watchdog + // feed GPS decoder with serial NMEA data from GPS device + while (GPS_Serial.available()) { + gps.encode(GPS_Serial.read()); } - // after GPS function was disabled, close connect to GPS device - GPS_Serial.end(); - #elif defined GPS_QUECTEL_L76 - - while (cfg.gpsmode) { - Wire.requestFrom(GPS_ADDR, - 128); // 128 is Wire.h buffersize arduino-ESP32 - while (Wire.available()) { - gps.encode(Wire.read()); - vTaskDelay(2 / portTICK_PERIOD_MS); // delay see L76 datasheet - } - vTaskDelay(2 / portTICK_PERIOD_MS); // reset watchdog + Wire.requestFrom(GPS_ADDR, 32); // caution: this is a blocking call + while (Wire.available()) { + gps.encode(Wire.read()); + vTaskDelay(2 / portTICK_PERIOD_MS); // 2ms delay according L76 datasheet } - -#endif // GPS Type - } +#endif + } // if (cfg.gpsmode) vTaskDelay(2 / portTICK_PERIOD_MS); // reset watchdog diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 5cb93af7..a9939893 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -103,7 +103,7 @@ void get_hard_deveui(uint8_t *pdeveui) { i2c_ret = Wire.endTransmission(); // check if device was seen on i2c bus - if (ic2_ret == 0) { + if (i2c_ret == 0) { char deveui[32] = ""; uint8_t data; diff --git a/src/main.cpp b/src/main.cpp index d84deb18..aeb9ffd4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,6 +52,10 @@ TaskHandle_t LoraTask = NULL; QueueHandle_t SPISendQueue; #endif +#ifdef HAS_GPS +TaskHandle_t GpsTask = NULL; +#endif + portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying IRQ // handler shared variables @@ -280,7 +284,7 @@ void setup() { // (note: arduino main loop runs on core 1, too) // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ - ESP_LOGI(TAG, "Starting Lora task on core 1"); + ESP_LOGI(TAG, "Starting Lora..."); xTaskCreatePinnedToCore(lorawan_loop, "loraloop", 2048, (void *)1, (5 | portPRIVILEGE_BIT), &LoraTask, 1); #endif @@ -289,22 +293,20 @@ void setup() { // higher priority than wifi channel rotation task since we process serial // streaming NMEA data #ifdef HAS_GPS - if (cfg.gpsmode) { - ESP_LOGI(TAG, "Starting GPS task on core 0"); - xTaskCreatePinnedToCore(gps_loop, "gpsloop", 2048, (void *)1, 2, NULL, 0); - } + ESP_LOGI(TAG, "Starting GPS..."); + xTaskCreatePinnedToCore(gps_loop, "gpsloop", 2048, (void *)1, 2, &GpsTask, 0); #endif // start BLE scan callback if BLE function is enabled in NVRAM configuration #ifdef BLECOUNTER if (cfg.blescan) { - ESP_LOGI(TAG, "Starting BLE task on core 1"); + ESP_LOGI(TAG, "Starting Bluetooth..."); start_BLEscan(); } #endif // start wifi in monitor mode and start channel rotation task on core 0 - ESP_LOGI(TAG, "Starting Wifi task on core 0"); + ESP_LOGI(TAG, "Starting Wifi..."); wifi_sniffer_init(); // initialize salt value using esp_random() called by random() in // arduino-esp32 core. Note: do this *after* wifi has started, since