reworked rtos tasking & minor core sanitizations

This commit is contained in:
Klaus K Wilting 2018-05-21 17:48:43 +02:00
parent 61bd56860f
commit 7b8367fa4a
4 changed files with 53 additions and 68 deletions

View File

@ -33,8 +33,8 @@ build_flags =
; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <--- ; ---> NOTE: For production run set DEBUG_LEVEL level to NONE! <---
; otherwise device may crash in dense environments due to serial buffer overflow ; 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_NONE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG ; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_DEBUG
; ;

View File

@ -44,16 +44,10 @@ typedef struct {
} configData_t; } configData_t;
extern configData_t cfg; extern configData_t cfg;
extern uint8_t mydata[];
extern uint64_t uptimecounter; extern uint64_t uptimecounter;
extern osjob_t sendjob; extern osjob_t sendjob;
extern char display_lora[], display_lmic[]; extern char display_lora[], display_lmic[];
extern int countermode, screensaver, adrmode, lorasf, txpower, rlim; extern int countermode, screensaver, adrmode, lorasf, txpower, rlim;
extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters extern uint16_t macs_total, macs_wifi, macs_ble; // MAC counters
extern bool joinstate;
extern std::set<uint16_t> macs; extern std::set<uint16_t> macs;
extern hw_timer_t * channelSwitch; // hardware timer used for wifi channel switching extern hw_timer_t * channelSwitch; // hardware timer used for wifi channel switching
#ifdef HAS_DISPLAY
extern HAS_DISPLAY u8x8;
#endif

View File

@ -116,9 +116,9 @@ void do_send(osjob_t* j){
goto end; goto end;
} }
uint8_t mydata[4];
// prepare payload with sum of unique WIFI MACs seen // prepare payload with sum of unique WIFI MACs seen
static uint8_t mydata[4];
mydata[0] = (macs_wifi & 0xff00) >> 8; mydata[0] = (macs_wifi & 0xff00) >> 8;
mydata[1] = macs_wifi & 0xff; mydata[1] = macs_wifi & 0xff;
@ -169,13 +169,12 @@ void onEvent (ev_t ev) {
case EV_JOINED: case EV_JOINED:
joinstate=true;
strcpy_P(buff, PSTR("JOINED")); strcpy_P(buff, PSTR("JOINED"));
sprintf(display_lora, ""); // clear previous lmic status message from display sprintf(display_lora, ""); // clear previous lmic status message from display
// Disable link check validation (automatically enabled // Disable link check validation (automatically enabled
// during join, but not supported by TTN at this time). // during join, but not supported by TTN at this time). -> do we need this?
// LMIC_setLinkCheckMode(0); -> do we need this? LMIC_setLinkCheckMode(0);
// set data rate adaptation // set data rate adaptation
LMIC_setAdrMode(cfg.adrmode); LMIC_setAdrMode(cfg.adrmode);

View File

@ -39,7 +39,7 @@ Refer to LICENSE.txt file in repository for more details.
// Initialize global variables // Initialize global variables
configData_t cfg; // struct holds current device configuration 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 uint64_t uptimecounter = 0; // timer global for uptime counter
uint8_t DisplayState = 0; // globals for state machine uint8_t DisplayState = 0; // globals for state machine
uint16_t macs_total = 0, macs_wifi = 0, macs_ble = 0; // MAC counters globals for display 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) unsigned long LEDBlinkStarted = 0; // When (in millis() led blink started)
uint16_t LEDBlinkDuration = 0; // How long the blink need to be uint16_t LEDBlinkDuration = 0; // How long the blink need to be
uint16_t LEDColor = COLOR_NONE; // state machine variable to set RGB LED color 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 * displaytimer = NULL; // configure hardware timer used for cyclic display refresh
hw_timer_t * channelSwitch = NULL; // configure hardware timer used for wifi channel switching 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} .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 // LMIC FreeRTos Task
void lorawan_loop(void * pvParameters) { void lorawan_loop(void * pvParameters) {
configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check
static uint16_t lorawait = 0; //static uint16_t lorawait = 0;
while(1) { while(1) {
// execute LMIC jobs // execute LMIC jobs
os_runloop_once(); 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 // check if payload is sent
while(LMIC.opmode & OP_TXRXPEND) { while(LMIC.opmode & OP_TXRXPEND) {
if(!lorawait) if(!lorawait)
@ -169,13 +153,14 @@ void lorawan_loop(void * pvParameters) {
}; };
vTaskDelay(1000/portTICK_PERIOD_MS); vTaskDelay(1000/portTICK_PERIOD_MS);
} }
*/ */
vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog
} }
} }
/* end LMIC specific parts --------------------------------------------------------------- */
/* end LMIC specific parts --------------------------------------------------------------- */
/* beginn hardware specific parts -------------------------------------------------------- */ /* beginn hardware specific parts -------------------------------------------------------- */
@ -384,6 +369,7 @@ uint64_t uptime() {
#endif #endif
#if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED)
void blink_LED(uint16_t set_color, uint16_t set_blinkduration) { void blink_LED(uint16_t set_color, uint16_t set_blinkduration) {
LEDColor = set_color; // set color for RGB LED LEDColor = set_color; // set color for RGB LED
LEDBlinkDuration = set_blinkduration; // duration LEDBlinkDuration = set_blinkduration; // duration
@ -457,7 +443,6 @@ uint64_t uptime() {
#endif #endif
/* begin Aruino SETUP ------------------------------------------------------------ */ /* begin Aruino SETUP ------------------------------------------------------------ */
void setup() { void setup() {
@ -575,10 +560,16 @@ ESP_LOGI(TAG, "Features %s", features);
printKeys(); printKeys();
#endif #endif
os_init(); // initialize lmic run-time environment // initialize LoRaWAN LMIC run-time environment
os_setCallback(&initjob, lora_init); // setup initial job & join LoRaWAN network 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"); ESP_LOGI(TAG, "Starting Lora task on core 1");
xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1); xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1);
@ -597,21 +588,21 @@ xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 2048, ( void * ) 1, 1, NULL
} }
#endif #endif
// kickoff first sendjob -> joins network and transmits initial payload "0000" // kickoff sendjob -> joins network and rescedules sendjob for cyclic transmitting payload
uint8_t mydata[] = "0000";
do_send(&sendjob); 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() { void loop() {
// simple state machine for controlling display, LED, button, etc. while (1) {
// simple state machine for controlling uptime, display, LED, button, memory.
uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit) uptimecounter = uptime() / 1000; // counts uptime in seconds (64bit)
#if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED) #if (HAS_LED != NOT_A_PIN) || defined (HAS_RGB_LED)
@ -637,6 +628,7 @@ void loop() {
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 ------------------------------------------------------------ */