diff --git a/src/main.cpp b/src/main.cpp index f7bbcbf1..8f4bd99c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,25 @@ NOTICE: Parts of the source files in this repository are made available under different licenses. Refer to LICENSE.txt file in repository for more details. +//////////////////////// ESP32-Paxcounter \\\\\\\\\\\\\\\\\\\\\\\\\\ + +Uused tasks and timers: + +Task Core Prio Purpose +==================================================================== +IDLE 0 0 ESP32 arduino scheduler +gpsloop 0 2 read data from GPS over serial or i2c +IDLE 1 0 Arduino loop() -> used for LED switching +loraloop 1 1 runs the LMIC stack +statemachine 1 3 switches application process logic + +ESP32 hardware timers +========================== + 0 Display-Refresh + 1 Wifi Channel Switch + 2 Send Cycle + 3 Housekeeping + */ // Basic Config @@ -90,39 +109,66 @@ void setup() { esp_log_set_vprintf(redirect_log); #endif - ESP_LOGI(TAG, "Starting %s v%s", PRODUCTNAME, PROGVERSION); - - // initialize system event handler for wifi task, needed for - // wifi_sniffer_init() - // esp_event_loop_init(NULL, NULL); - // ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); - - // print chip information on startup if in verbose mode -#ifdef VERBOSE - esp_chip_info_t chip_info; - esp_chip_info(&chip_info); - ESP_LOGI(TAG, - "This is ESP32 chip with %d CPU cores, WiFi%s%s, silicon revision " - "%d, %dMB %s Flash", - chip_info.cores, (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", - (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "", - chip_info.revision, spi_flash_get_chip_size() / (1024 * 1024), - (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" - : "external"); - ESP_LOGI(TAG, "ESP32 SDK: %s", ESP.getSdkVersion()); - ESP_LOGI(TAG, "Free RAM: %d bytes", ESP.getFreeHeap()); - -#ifdef HAS_GPS - ESP_LOGI(TAG, "TinyGPS+ v%s", TinyGPSPlus::libraryVersion()); -#endif - -#endif // verbose - - // read settings from NVRAM + // read (and initialize on first run) runtime settings from NVRAM loadConfig(); // includes initialize if necessary -#ifdef VENDORFILTER - strcat_P(features, " OUIFLT"); + // initialize leds +#if (HAS_LED != NOT_A_PIN) + pinMode(HAS_LED, OUTPUT); + strcat_P(features, " LED"); +#endif +#ifdef HAS_RGB_LED + rgb_set_color(COLOR_PINK); + strcat_P(features, " RGB"); +#endif + + // initialize wifi antenna +#ifdef HAS_ANTENNA_SWITCH + strcat_P(features, " ANT"); + antenna_init(); + antenna_select(cfg.wifiant); +#endif + +// switch off bluetooth, if not compiled +#ifdef BLECOUNTER + strcat_P(features, " BLE"); +#else + bool btstop = btStop(); +#endif + +// initialize battery status +#ifdef HAS_BATTERY_PROBE + strcat_P(features, " BATT"); + calibrate_voltage(); + batt_voltage = read_voltage(); +#endif + + // reboot to firmware update mode if ota trigger switch is set + if (cfg.runmode == 1) { + cfg.runmode = 0; + saveConfig(); + start_ota_update(); + } + + // initialize button +#ifdef HAS_BUTTON + strcat_P(features, " BTN_"); +#ifdef BUTTON_PULLUP + strcat_P(features, "PU"); + // install button interrupt (pullup mode) + pinMode(HAS_BUTTON, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, RISING); +#else + strcat_P(features, "PD"); + // install button interrupt (pulldown mode) + pinMode(HAS_BUTTON, INPUT_PULLDOWN); + attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, FALLING); +#endif // BUTTON_PULLUP +#endif // HAS_BUTTON + +// initialize gps +#ifdef HAS_GPS + strcat_P(features, " GPS"); #endif // initialize LoRa @@ -149,58 +195,32 @@ void setup() { SEND_QUEUE_SIZE * PAYLOAD_BUFFER_SIZE); #endif - // initialize led -#if (HAS_LED != NOT_A_PIN) - pinMode(HAS_LED, OUTPUT); - strcat_P(features, " LED"); +#ifdef VENDORFILTER + strcat_P(features, " OUIFLT"); #endif -#ifdef HAS_RGB_LED - rgb_set_color(COLOR_PINK); - strcat_P(features, " RGB"); -#endif + ESP_LOGI(TAG, "Starting %s v%s", PRODUCTNAME, PROGVERSION); - // initialize button -#ifdef HAS_BUTTON - strcat_P(features, " BTN_"); -#ifdef BUTTON_PULLUP - strcat_P(features, "PU"); - // install button interrupt (pullup mode) - pinMode(HAS_BUTTON, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, RISING); -#else - strcat_P(features, "PD"); - // install button interrupt (pulldown mode) - pinMode(HAS_BUTTON, INPUT_PULLDOWN); - attachInterrupt(digitalPinToInterrupt(HAS_BUTTON), ButtonIRQ, FALLING); -#endif // BUTTON_PULLUP -#endif // HAS_BUTTON + // print chip information on startup if in verbose mode +#ifdef VERBOSE + esp_chip_info_t chip_info; + esp_chip_info(&chip_info); + ESP_LOGI(TAG, + "This is ESP32 chip with %d CPU cores, WiFi%s%s, silicon revision " + "%d, %dMB %s Flash", + chip_info.cores, (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "", + (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "", + chip_info.revision, spi_flash_get_chip_size() / (1024 * 1024), + (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" + : "external"); + ESP_LOGI(TAG, "ESP32 SDK: %s", ESP.getSdkVersion()); + ESP_LOGI(TAG, "Free RAM: %d bytes", ESP.getFreeHeap()); - // initialize wifi antenna -#ifdef HAS_ANTENNA_SWITCH - strcat_P(features, " ANT"); - antenna_init(); - antenna_select(cfg.wifiant); -#endif - -// switch off bluetooth on esp32 module, if not compiled -#ifdef BLECOUNTER - strcat_P(features, " BLE"); -#else - bool btstop = btStop(); -#endif - -// initialize gps #ifdef HAS_GPS - strcat_P(features, " GPS"); + ESP_LOGI(TAG, "TinyGPS+ v%s", TinyGPSPlus::libraryVersion()); #endif -// initialize battery status -#ifdef HAS_BATTERY_PROBE - strcat_P(features, " BATT"); - calibrate_voltage(); - batt_voltage = read_voltage(); -#endif +#endif // verbose // initialize display #ifdef HAS_DISPLAY @@ -208,16 +228,6 @@ void setup() { DisplayState = cfg.screenon; init_display(PRODUCTNAME, PROGVERSION); - /* - Usage of ESP32 hardware timers - ============================== - - 0 Display-Refresh - 1 Wifi Channel Switch - 2 Send Cycle - 3 Housekeeping - */ - // setup display refresh trigger IRQ using esp32 hardware timer // https://techtutorialsx.com/2017/10/07/esp32-arduino-timer-interrupts/ @@ -232,13 +242,6 @@ void setup() { timerAlarmEnable(displaytimer); #endif - // reboot to firmware update mode if ota trigger switch is set - if (cfg.runmode == 1) { - cfg.runmode = 0; - saveConfig(); - start_ota_update(); - } - // setup channel rotation trigger IRQ using esp32 hardware timer 1 channelSwitch = timerBegin(1, 800, true); timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true); @@ -293,18 +296,6 @@ void setup() { // join network LMIC_startJoining(); - /* - - Task Core Prio Purpose - ==================================================================== - IDLE 0 0 ESP32 arduino scheduler - gpsloop 0 2 read data from GPS over serial or i2c - IDLE 1 0 Arduino loop() -> used for LED switching - loraloop 1 1 runs the LMIC stack - statemachine 1 3 switches application process logic - - */ - // start lmic runloop in rtos task on core 1 // (note: arduino main loop runs on core 1, too) // https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ @@ -332,6 +323,8 @@ void setup() { // start wifi in monitor mode and start channel rotation task on core 0 ESP_LOGI(TAG, "Starting Wifi..."); + // esp_event_loop_init(NULL, NULL); + // ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); wifi_sniffer_init(); // initialize salt value using esp_random() called by random() in // arduino-esp32 core. Note: do this *after* wifi has started, since @@ -345,40 +338,9 @@ void setup() { } // setup() -void stateMachine(void *pvParameters) { - - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check - - while (1) { - -#ifdef HAS_BUTTON - readButton(); -#endif - -#ifdef HAS_DISPLAY - updateDisplay(); -#endif - - // check wifi scan cycle and if due rotate channel - if (ChannelTimerIRQ) - switchWifiChannel(channel); - // check housekeeping cycle and if due do the work - if (HomeCycleIRQ) - doHousekeeping(); - // check send queue and process it - enqueuePayload(); - // check send cycle and if due enqueue payload to send - if (SendCycleTimerIRQ) - sendPayload(); - - // give yield to CPU - vTaskDelay(2 / portTICK_PERIOD_MS); - } -} - void loop() { -// switch LED states if device has a LED +// switch LED state if device has LED(s) #if (HAS_LED != NOT_A_PIN) || defined(HAS_RGB_LED) led_loop(); #endif diff --git a/src/main.h b/src/main.h index e1d8b775..40e9c208 100644 --- a/src/main.h +++ b/src/main.h @@ -1,20 +1,17 @@ #ifndef _MAIN_H #define _MAIN_H -#include "globals.h" -#include "led.h" -#include "macsniff.h" -#include "wifiscan.h" -#include "configmanager.h" -#include "senddata.h" -#include "cyclic.h" -#include "beacon_array.h" -#include "OTA.h" - #include // needed for reading ESP32 chip attributes #include // needed for Wifi event handler #include // needed for timers -void stateMachine(void *pvParameters); +#include "globals.h" +#include "led.h" +#include "wifiscan.h" +#include "configmanager.h" +#include "cyclic.h" +#include "beacon_array.h" +#include "ota.h" +#include "statemachine.h" #endif \ No newline at end of file diff --git a/src/senddata.cpp b/src/senddata.cpp index ad647233..f738ee6b 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -73,8 +73,8 @@ void IRAM_ATTR SendCycleIRQ() { portEXIT_CRITICAL(&timerMux); } -// interrupt triggered function to eat data from RTos send queues and transmit it -void enqueuePayload() { +// interrupt triggered function to eat data from send queues and transmit it +void checkSendQueues() { MessageBuffer_t SendBuffer; #ifdef HAS_LORA @@ -98,7 +98,7 @@ void enqueuePayload() { } #endif -} // enqueuePayload +} // checkSendQueues void flushQueues() { #ifdef HAS_LORA diff --git a/src/senddata.h b/src/senddata.h index eba9d8bd..df7b53f5 100644 --- a/src/senddata.h +++ b/src/senddata.h @@ -4,7 +4,7 @@ void SendData(uint8_t port); void sendPayload(void); void SendCycleIRQ(void); -void enqueuePayload(void); +void checkSendQueues(void); void flushQueues(); #endif // _SENDDATA_H_ \ No newline at end of file diff --git a/src/statemachine.cpp b/src/statemachine.cpp new file mode 100644 index 00000000..9dfac811 --- /dev/null +++ b/src/statemachine.cpp @@ -0,0 +1,35 @@ +#include "statemachine.h" + +// Local logging tag +static const char TAG[] = "main"; + +void stateMachine(void *pvParameters) { + + configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + + while (1) { + +#ifdef HAS_BUTTON + readButton(); +#endif + +#ifdef HAS_DISPLAY + updateDisplay(); +#endif + + // check wifi scan cycle and if due rotate channel + if (ChannelTimerIRQ) + switchWifiChannel(channel); + // check housekeeping cycle and if due do the work + if (HomeCycleIRQ) + doHousekeeping(); + // check send cycle and if due enqueue payload to send + if (SendCycleTimerIRQ) + sendPayload(); + // check send queues and process due payload to send + checkSendQueues(); + + // give yield to CPU + vTaskDelay(2 / portTICK_PERIOD_MS); + } +} \ No newline at end of file diff --git a/src/statemachine.h b/src/statemachine.h new file mode 100644 index 00000000..7390e5c9 --- /dev/null +++ b/src/statemachine.h @@ -0,0 +1,12 @@ +#ifndef _STATEMACHINE_H +#define _STATEMACHINE_H + +#include "globals.h" +#include "led.h" +#include "wifiscan.h" +#include "senddata.h" +#include "cyclic.h" + +void stateMachine(void *pvParameters); + +#endif