From 7b8367fa4a384c8621eec89fe6d6fe7c90b85316 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Mon, 21 May 2018 17:48:43 +0200 Subject: [PATCH] reworked rtos tasking & minor core sanitizations --- platformio.ini | 4 +- src/globals.h | 8 +--- src/lorawan.cpp | 9 ++--- src/main.cpp | 100 ++++++++++++++++++++++-------------------------- 4 files changed, 53 insertions(+), 68 deletions(-) diff --git a/platformio.ini b/platformio.ini index 5f08fca6..fc426838 100644 --- a/platformio.ini +++ b/platformio.ini @@ -33,8 +33,8 @@ build_flags = ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; otherwise device may crash in dense environments due to serial buffer overflow ; - -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE -; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO +; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE + -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG ; diff --git a/src/globals.h b/src/globals.h index 2578596d..275f1c3b 100644 --- a/src/globals.h +++ b/src/globals.h @@ -44,16 +44,10 @@ typedef struct { } configData_t; extern configData_t cfg; -extern uint8_t mydata[]; extern uint64_t uptimecounter; extern osjob_t sendjob; extern char display_lora[], display_lmic[]; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters -extern bool joinstate; extern std::set macs; -extern hw_timer_t * channelSwitch; // hardware timer used for wifi channel switching - -#ifdef HAS_DISPLAY - extern HAS_DISPLAY u8x8; -#endif +extern hw_timer_t * channelSwitch; // hardware timer used for wifi channel switching \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 38777bfb..4cde6d3f 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -116,9 +116,9 @@ void do_send(osjob_t* j){ goto end; } - uint8_t mydata[4]; - // prepare payload with sum of unique WIFI MACs seen + static uint8_t mydata[4]; + mydata[0] = (macs_wifi & 0xff00) >> 8; mydata[1] = macs_wifi & 0xff; @@ -169,13 +169,12 @@ void onEvent (ev_t ev) { case EV_JOINED: - joinstate=true; strcpy_P(buff, PSTR("JOINED")); sprintf(display_lora, ""); // clear previous lmic status message from display // Disable link check validation (automatically enabled - // during join, but not supported by TTN at this time). - // LMIC_setLinkCheckMode(0); -> do we need this? + // during join, but not supported by TTN at this time). -> do we need this? + LMIC_setLinkCheckMode(0); // set data rate adaptation LMIC_setAdrMode(cfg.adrmode); diff --git a/src/main.cpp b/src/main.cpp index 282a955c..75cfa8d5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -39,7 +39,7 @@ Refer to LICENSE.txt file in repository for more details. // Initialize global variables configData_t cfg; // struct holds current device configuration -osjob_t sendjob, initjob; // LMIC jobs +osjob_t sendjob; // LMIC job handler uint64_t uptimecounter = 0; // timer global for uptime counter uint8_t DisplayState = 0; // globals for state machine uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display @@ -50,8 +50,6 @@ led_states previousLEDState = LED_ON; // This will force LED to be off at boo unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started) uint16_t LEDBlinkDuration = 0; // How long the blink need to be uint16_t LEDColor = COLOR_NONE; // state machine variable to set RGB LED color -bool joinstate = false; // LoRa network joined? global flag -bool blinkdone = true; // flag for state machine for blinking LED once hw_timer_t * displaytimer = NULL; // configure hardware timer used for cyclic display refresh hw_timer_t * channelSwitch = NULL; // configure hardware timer used for wifi channel switching @@ -128,34 +126,20 @@ const lmic_pinmap lmic_pins = { .dio = {DIO0, DIO1, DIO2} }; -// LoRaWAN Initjob -static void lora_init (osjob_t* j) { - // reset MAC state - LMIC_reset(); - // This tells LMIC to make the receive windows bigger, in case your clock is 1% faster or slower. - LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100); - // start joining -> will happen automatically after first do_send job - // LMIC_startJoining(); - // lmic init done - onEvent() callback will be invoked when events occur -} // LMIC FreeRTos Task void lorawan_loop(void * pvParameters) { configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check - static uint16_t lorawait = 0; + //static uint16_t lorawait = 0; while(1) { // execute LMIC jobs os_runloop_once(); - // indicate LMIC state on LEDs if present - #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) - led_loop(); - #endif -/* + /* // check if payload is sent while(LMIC.opmode & OP_TXRXPEND) { if(!lorawait) @@ -169,13 +153,14 @@ void lorawan_loop(void * pvParameters) { }; vTaskDelay(1000/portTICK_PERIOD_MS); } -*/ + */ + vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog } } -/* end LMIC specific parts --------------------------------------------------------------- */ +/* end LMIC specific parts --------------------------------------------------------------- */ /* beginn hardware specific parts -------------------------------------------------------- */ @@ -384,6 +369,7 @@ uint64_t uptime() { #endif #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { LEDColor = set_color; // set color for RGB LED LEDBlinkDuration = set_blinkduration; // duration @@ -457,7 +443,6 @@ uint64_t uptime() { #endif - /* begin Aruino SETUP ------------------------------------------------------------ */ void setup() { @@ -575,10 +560,16 @@ ESP_LOGI(TAG, "Features %s", features); printKeys(); #endif -os_init(); // initialize lmic run-time environment -os_setCallback(&initjob, lora_init); // setup initial job & join LoRaWAN network +// initialize LoRaWAN LMIC run-time environment +os_init(); +// reset LMIC MAC state +LMIC_reset(); +// This tells LMIC to make the receive windows bigger, in case your clock is 1% faster or slower. +LMIC_setClockError(MAX_CLOCK_ERROR * 1 / 100); + +// 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/ -// start lmic runloop in rtos task on core 1 (arduino main loop runs on core 1) ESP_LOGI(TAG, "Starting Lora task on core 1"); xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1); @@ -597,46 +588,47 @@ xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 2048, ( void * ) 1, 1, NULL } #endif -// kickoff first sendjob -> joins network and transmits initial payload "0000" -uint8_t mydata[] = "0000"; +// kickoff sendjob -> joins network and rescedules sendjob for cyclic transmitting payload do_send(&sendjob); + } -/* end Aruino SETUP ------------------------------------------------------------ */ +/* end Arduino SETUP ------------------------------------------------------------ */ +/* begin Arduino main loop ------------------------------------------------------ */ -/* begin Aruino LOOP ------------------------------------------------------------ */ - -// Arduino main moop, runs on core 1 -// https://techtutorialsx.com/2017/05/09/esp32-get-task-execution-core/ void loop() { - // simple state machine for controlling display, LED, button, etc. - uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) + while (1) { - #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) - led_loop(); - #endif + // simple state machine for controlling uptime, display, LED, button, memory. + + uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) + + #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) + led_loop(); + #endif - #ifdef HAS_BUTTON - readButton(); - #endif + #ifdef HAS_BUTTON + readButton(); + #endif - #ifdef HAS_DISPLAY - updateDisplay(); - #endif + #ifdef HAS_DISPLAY + updateDisplay(); + #endif - // check free memory - if (esp_get_minimum_free_heap_size() <= MEM_LOW) { - ESP_LOGI(TAG, "Memory full, counter cleared (heap low water mark = %d Bytes / free heap = %d bytes)", \ - esp_get_minimum_free_heap_size(), ESP.getFreeHeap()); - do_send(&sendjob); // send count - reset_counters(); // clear macs container and reset all counters - reset_salt(); // get new salt for salting hashes - } + // check free memory + if (esp_get_minimum_free_heap_size() <= MEM_LOW) { + ESP_LOGI(TAG, "Memory full, counter cleared (heap low water mark = %d Bytes / free heap = %d bytes)", \ + esp_get_minimum_free_heap_size(), ESP.getFreeHeap()); + do_send(&sendjob); // send count + reset_counters(); // clear macs container and reset all counters + reset_salt(); // get new salt for salting hashes + } - vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog + vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog - } + } // end of infinite main loop +} -/* end Aruino LOOP ------------------------------------------------------------ */ +/* end Arduino main loop ------------------------------------------------------------ */