From 4f2139a715c4174adb46804469feb7946ed0e6e0 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 9 Jun 2018 13:18:59 +0200 Subject: [PATCH] testing GPS --- src/globals.h | 4 ++-- src/gpsread.cpp | 15 ++++++++++++--- src/hal/ttgobeam.h | 20 ++++++++++---------- src/lorawan.cpp | 8 ++------ src/main.cpp | 24 +++++++++++++++--------- src/main.h | 1 + src/rcommand.cpp | 8 +++----- 7 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/globals.h b/src/globals.h index 420481fe..e92e44a6 100644 --- a/src/globals.h +++ b/src/globals.h @@ -54,11 +54,11 @@ typedef struct { uint32_t latitude; uint32_t longitude; uint8_t hdop; - uint8_t satellites; + uint32_t satellites; uint16_t altitude; } gpsStatus_t; extern gpsStatus_t gps_status; // struct for storing gps data - extern TinyGPSPlus my_gps; // Make TinyGPS++ instance globally availabe + extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe #endif extern configData_t cfg; diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 38fd3627..f396d8b7 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -5,7 +5,17 @@ // Local logging tag static const char TAG[] = "main"; -// GPS Read FreeRTos Task +// GPS read data to global struct + +void gps_read(){ + gps_status.latitude = gps.location.lat(); + gps_status.longitude = gps.location.lng(); + gps_status.satellites = (uint8_t) gps.satellites.value(); + gps_status.hdop = (uint8_t) (gps.hdop.value() / 100); + gps_status.altitude = (uint16_t) gps.altitude.meters(); +} + +// GPS serial feed FreeRTos Task void gps_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check @@ -17,8 +27,7 @@ void gps_loop(void * pvParameters) { while(1) { while (GPS_Serial.available()) { - my_gps.encode(GPS_Serial.read()); - ESP_LOGI(TAG, "GSP checksum passed / failed: %d / %d", my_gps.passedChecksum(), my_gps.failedChecksum()); + gps.encode(GPS_Serial.read()); } vTaskDelay(1/portTICK_PERIOD_MS); // reset watchdog diff --git a/src/hal/ttgobeam.h b/src/hal/ttgobeam.h index ec6ba7e1..9a05de26 100644 --- a/src/hal/ttgobeam.h +++ b/src/hal/ttgobeam.h @@ -2,20 +2,20 @@ #define CFG_sx1276_radio 1 // HPD13A LoRa SoC -#define HAS_LED 21 // on board green LED_G1 -#define HAS_BUTTON GPIO_NUM_0 // on board button "BOOT" (next to reset button) +#define HAS_LED GPIO_NUM_21 // on board green LED_G1 +//#define HAS_BUTTON GPIO_NUM_39 // on board button "BOOT" (next to reset button) !! seems not to work!! #define HAS_BATTERY_PROBE ADC1_GPIO35_CHANNEL // battery probe GPIO pin -> ADC1_CHANNEL_7 #define BATT_FACTOR 2 // voltage divider 100k/100k on board -#define HAS_GPS 9600, SERIAL_8N1, 12, 15 //17-TX 18-RX +#define HAS_GPS 9600, SERIAL_8N1, 12, 15 // re-define pin definitions of pins_arduino.h -#define PIN_SPI_SS 18 // ESP32 GPIO18 (Pin18) -- HPD13A NSS/SEL (Pin4) SPI Chip Select Input -#define PIN_SPI_MOSI 27 // ESP32 GPIO27 (Pin27) -- HPD13A MOSI/DSI (Pin6) SPI Data Input -#define PIN_SPI_MISO 19 // ESP32 GPIO19 (Pin19) -- HPD13A MISO/DSO (Pin7) SPI Data Output -#define PIN_SPI_SCK 5 // ESP32 GPIO5 (Pin5) -- HPD13A SCK (Pin5) SPI Clock Input +#define PIN_SPI_SS GPIO_NUM_18 // ESP32 GPIO18 (Pin18) -- HPD13A NSS/SEL (Pin4) SPI Chip Select Input +#define PIN_SPI_MOSI GPIO_NUM_27 // ESP32 GPIO27 (Pin27) -- HPD13A MOSI/DSI (Pin6) SPI Data Input +#define PIN_SPI_MISO GPIO_NUM_19 // ESP32 GPIO19 (Pin19) -- HPD13A MISO/DSO (Pin7) SPI Data Output +#define PIN_SPI_SCK GPIO_NUM_5 // ESP32 GPIO5 (Pin5) -- HPD13A SCK (Pin5) SPI Clock Input // non arduino pin definitions #define RST LMIC_UNUSED_PIN // connected to ESP32 RST/EN -#define DIO0 26 // ESP32 GPIO26 <-> HPD13A IO0 -#define DIO1 33 // Lora1 <-> HPD13A IO1 // !! NEEDS EXTERNAL WIRING !! -#define DIO2 32 // Lora2 <-> HPD13A IO2 // needs external wiring, but not necessary for LoRa, only FSK +#define DIO0 GPIO_NUM_26 // ESP32 GPIO26 <-> HPD13A IO0 +#define DIO1 GPIO_NUM_33 // Lora1 <-> HPD13A IO1 // !! NEEDS EXTERNAL WIRING !! +#define DIO2 LMIC_UNUSED_PIN // Lora2 <-> HPD13A IO2 // needs external wiring, but not necessary for LoRa, only FSK diff --git a/src/lorawan.cpp b/src/lorawan.cpp index dca47d95..b1079c0f 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -137,12 +137,8 @@ void do_send(osjob_t* j){ sprintf(display_lmic, "PACKET QUEUED"); #ifdef HAS_GPS - if (cfg.gpsmode && my_gps.location.isValid()) { - gps_status.latitude = my_gps.location.lat(); - gps_status.longitude = my_gps.location.lng(); - gps_status.satellites = my_gps.satellites.value(); - gps_status.hdop = my_gps.hdop.value(); - gps_status.altitude = my_gps.altitude.meters(); + if (cfg.gpsmode && gps.location.isValid()) { + gps_read(); LMIC_setTxData2(GPSPORT, (byte*)&gps_status, sizeof(gps_status), (cfg.countermode & 0x02)); ESP_LOGI(TAG, "HDOP=%d, SATS=%d, LAT=%d, LON=%d", gps_status.hdop, gps_status.satellites, gps_status.latitude, gps_status.longitude ); } diff --git a/src/main.cpp b/src/main.cpp index e76efc26..61fd74e9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -57,15 +57,15 @@ u1_t rcmd_data_size; // buffer for rcommand results size #ifdef HAS_GPS gpsStatus_t gps_status; // struct for storing gps data - TinyGPSPlus my_gps; // create TinyGPS++ instance + TinyGPSPlus gps; // create TinyGPS++ instance #endif -portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying shared variable DisplayIRQ +portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED; // sync main loop and ISR when modifying IRQ handler shared variables std::set macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE) // this variables will be changed in the ISR, and read in main loop -static volatile int ButtonPressed = 0, DisplayTimerIRQ = 0, ChannelTimerIRQ = 0; +static volatile int ButtonPressedIRQ = 0, DisplayTimerIRQ = 0, ChannelTimerIRQ = 0; // local Tag for logging static const char TAG[] = "main"; @@ -167,14 +167,14 @@ void lorawan_loop(void * pvParameters) { bool btstop = btStop(); #endif +// Button IRQ Handler Routine, IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855 #ifdef HAS_BUTTON - // Button IRQ - // IRAM_ATTR necessary here, see https://github.com/espressif/arduino-esp32/issues/855 void IRAM_ATTR ButtonIRQ() { - ButtonPressed++; + ButtonPressedIRQ++; } #endif +// Wifi Channel Rotation Timer IRQ Handler Routine void IRAM_ATTR ChannelSwitchIRQ() { portENTER_CRITICAL(&timerMux); ChannelTimerIRQ++; @@ -344,11 +344,14 @@ uint64_t uptime() { #ifdef HAS_BUTTON void readButton() { - if (ButtonPressed) { - ButtonPressed--; + if (ButtonPressedIRQ) { + portENTER_CRITICAL(&timerMux); + ButtonPressedIRQ--; + portEXIT_CRITICAL(&timerMux); + ESP_LOGI(TAG, "Button pressed"); ESP_LOGI(TAG, "Button pressed, resetting device to factory defaults"); eraseConfig(); - esp_restart(); + esp_restart(); } } #endif @@ -623,6 +626,9 @@ void loop() { reset_salt(); // get new salt for salting hashes } + if ( (uptime() % 10000) == 0 ) + ESP_LOGI(TAG, "GPS NMEA data passed %d / failed: %d / with fix: %d || Sats: %d / HDOP: %d", gps.passedChecksum(), gps.failedChecksum(), gps.sentencesWithFix(), gps.satellites.value(), gps.hdop.value()); + vTaskDelay(1/portTICK_PERIOD_MS); // reset watchdog } // end of infinite main loop diff --git a/src/main.h b/src/main.h index f0be62a3..18b9bfb8 100644 --- a/src/main.h +++ b/src/main.h @@ -48,5 +48,6 @@ void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); //defined in gpsread.cpp #ifdef HAS_GPS + void gps_read(void); void gps_loop(void * pvParameters); #endif \ No newline at end of file diff --git a/src/rcommand.cpp b/src/rcommand.cpp index ff7a245c..e01860fb 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -260,13 +260,11 @@ void get_voltage (uint8_t val) { void get_gps (uint8_t val) { ESP_LOGI(TAG, "Remote command: get gps status"); #ifdef HAS_GPS - gps_status.latitude = my_gps.location.lat(); - gps_status.longitude = my_gps.location.lng(); - gps_status.satellites = my_gps.satellites.value(); - gps_status.hdop = my_gps.hdop.value(); - gps_status.altitude = my_gps.altitude.meters(); + if (gps.location.isValid()) { + gps_read(); transmit((byte*)&gps_status, sizeof(gps_status)); ESP_LOGI(TAG, "HDOP=%d, SATS=%d, LAT=%d, LON=%d", gps_status.hdop, gps_status.satellites, gps_status.latitude, gps_status.longitude ); + } #endif };