diff --git a/include/globals.h b/include/globals.h index 6aaad2c7..1e8f43d0 100644 --- a/include/globals.h +++ b/include/globals.h @@ -105,8 +105,9 @@ extern std::set, Mallocator> macs; extern std::array::iterator it; extern std::array beacons; -extern TaskHandle_t irqHandlerTask, wifiSwitchTask; -extern Timezone myTZ; // make Timezone myTZ globally available +extern TaskHandle_t irqHandlerTask; +extern TimerHandle_t WifiChanTimer; +extern Timezone myTZ; // application includes #include "led.h" diff --git a/include/irqhandler.h b/include/irqhandler.h index 45d58fee..cc8d15ac 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -11,7 +11,6 @@ #include "senddata.h" void irqHandler(void *pvParameters); -void IRAM_ATTR ChannelSwitchIRQ(); void IRAM_ATTR homeCycleIRQ(); void IRAM_ATTR SendCycleIRQ(); diff --git a/include/wifiscan.h b/include/wifiscan.h index 0e6962a2..0a6e7b6c 100644 --- a/include/wifiscan.h +++ b/include/wifiscan.h @@ -9,6 +9,6 @@ void wifi_sniffer_init(void); void IRAM_ATTR wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); -void switchWifiChannel(void * parameter); +void switchWifiChannel(TimerHandle_t xTimer); #endif \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index a26bf6f8..93c200d7 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -41,7 +41,7 @@ void doHousekeeping() { if ((millis() >= nextRTCTimeSync) && (timeStatus() == timeSet)) { nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * 60000; // set up next time sync period - if (!set_rtctime(now())) // epoch time + if (!set_rtctime(now())) // epoch time ESP_LOGE(TAG, "RTC set time failure"); else ESP_LOGI(TAG, "RTC time updated"); @@ -49,9 +49,6 @@ void doHousekeeping() { #endif // task storage debugging // - ESP_LOGD(TAG, "Wifiloop %d bytes left | Taskstate = %d", - uxTaskGetStackHighWaterMark(wifiSwitchTask), - eTaskGetState(wifiSwitchTask)); ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d", uxTaskGetStackHighWaterMark(irqHandlerTask), eTaskGetState(irqHandlerTask)); diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index f79e035e..fbbf06fa 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -44,11 +44,6 @@ void irqHandler(void *pvParameters) { // esp32 hardware timer triggered interrupt service routines // they notify the irq handler task -void IRAM_ATTR ChannelSwitchIRQ() { - xTaskNotifyGive(wifiSwitchTask); - portYIELD_FROM_ISR(); -} - void IRAM_ATTR homeCycleIRQ() { xTaskNotifyFromISR(irqHandlerTask, CYCLIC_IRQ, eSetBits, NULL); portYIELD_FROM_ISR(); diff --git a/src/main.cpp b/src/main.cpp index 5734f97f..2a029a1f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,6 @@ Uused tasks and timers: Task Core Prio Purpose ==================================================================================== -wifiloop 0 4 rotates wifi channels ledloop 0 3 blinks LEDs if482loop 1 3 serial feed of IF482 time telegrams spiloop 0 2 reads/writes data on spi interface @@ -37,7 +36,7 @@ looptask 1 1 arduino core -> runs the LMIC LoRa stack irqhandler 1 1 executes tasks triggered by irq gpsloop 1 2 reads data from GPS via serial or i2c bmeloop 1 1 reads data from BME sensor via i2c -IDLE 1 0 ESP32 arduino scheduler +IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator Low priority numbers denote low priority tasks. @@ -47,7 +46,7 @@ Tasks using i2c bus all must have same priority, because using mutex semaphore ESP32 hardware timers ================================ 0 triggers display refresh - 1 triggers Wifi channel switch + 1 unused (reserved for DCF77) 2 triggers send payload cycle 3 triggers housekeeping cycle @@ -67,7 +66,7 @@ uint16_t volatile macs_total = 0, macs_wifi = 0, macs_ble = 0, batt_voltage = 0; // globals for display hw_timer_t *channelSwitch = NULL, *sendCycle = NULL, *homeCycle = NULL, *displaytimer = NULL; // irq tasks -TaskHandle_t irqHandlerTask, wifiSwitchTask; +TaskHandle_t irqHandlerTask; SemaphoreHandle_t I2Caccess; // container holding unique MAC address hashes with Memory Alloctor using PSRAM, @@ -305,11 +304,6 @@ void setup() { timerAttachInterrupt(homeCycle, &homeCycleIRQ, true); timerAlarmWrite(homeCycle, HOMECYCLE * 10000, true); - // setup channel rotation trigger IRQ using esp32 hardware timer 1 - channelSwitch = timerBegin(1, 800, true); - timerAttachInterrupt(channelSwitch, &ChannelSwitchIRQ, true); - timerAlarmWrite(channelSwitch, cfg.wifichancycle * 1000, true); - // show payload encoder #if PAYLOAD_ENCODER == 1 strcat_P(features, " PLAIN"); @@ -343,7 +337,7 @@ void setup() { #endif #endif - // start wifi in monitor mode and start channel rotation task on core 0 + // start wifi in monitor mode and start channel rotation timer ESP_LOGI(TAG, "Starting Wifi..."); wifi_sniffer_init(); // initialize salt value using esp_random() called by random() in @@ -361,16 +355,6 @@ void setup() { &irqHandlerTask, // task handle 1); // CPU core - // start wifi channel rotation task - ESP_LOGI(TAG, "Starting Wifi Channel rotation..."); - xTaskCreatePinnedToCore(switchWifiChannel, // task function - "wifiloop", // name of task - 2048, // stack size of task - NULL, // parameter of the task - 4, // priority of the task - &wifiSwitchTask, // task handle - 0); // CPU core - // initialize bme #ifdef HAS_BME strcat_P(features, " BME"); @@ -394,7 +378,6 @@ void setup() { #endif timerAlarmEnable(sendCycle); timerAlarmEnable(homeCycle); - timerAlarmEnable(channelSwitch); // start button interrupt #ifdef HAS_BUTTON diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 3b6bccc6..90a2e20f 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -67,8 +67,8 @@ void set_sendcycle(uint8_t val[]) { void set_wifichancycle(uint8_t val[]) { cfg.wifichancycle = val[0]; - // update channel rotation interrupt - timerAlarmWrite(channelSwitch, cfg.wifichancycle * 10000, true); + // update Wifi channel rotation timer period + xTimerChangePeriod(WifiChanTimer, pdMS_TO_TICKS(cfg.wifichancycle * 10), 100 ); ESP_LOGI(TAG, "Remote command: set Wifi channel switch interval to %.1f seconds", diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index dcd8ebc9..a6130ad9 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -7,6 +7,8 @@ // Local logging tag static const char TAG[] = "wifi"; +TimerHandle_t WifiChanTimer; + static wifi_country_t wifi_country = {WIFI_MY_COUNTRY, WIFI_CHANNEL_MIN, WIFI_CHANNEL_MAX, 100, WIFI_COUNTRY_POLICY_MANUAL}; @@ -43,10 +45,17 @@ IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, mac_add((uint8_t *)hdr->addr2, ppkt->rx_ctrl.rssi, MAC_SNIFF_WIFI); } +// Software-timer driven Wifi channel rotation callback function +void switchWifiChannel(TimerHandle_t xTimer) { + channel = + (channel % WIFI_CHANNEL_MAX) + 1; // rotate channel 1..WIFI_CHANNEL_MAX + esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); + } + void wifi_sniffer_init(void) { - wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); - cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM - cfg.wifi_task_core_id = 0; // we want wifi task running on core 0 + wifi_init_config_t wificfg = WIFI_INIT_CONFIG_DEFAULT(); + wificfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM + wificfg.wifi_task_core_id = 0; // we want wifi task running on core 0 wifi_promiscuous_filter_t filter = { // .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // only MGMT frames .filter_mask = WIFI_PROMIS_FILTER_MASK_ALL}; // we use all frames @@ -54,7 +63,7 @@ void wifi_sniffer_init(void) { ESP_ERROR_CHECK(esp_coex_preference_set( ESP_COEX_PREFER_BALANCE)); // configure Wifi/BT coexist lib - ESP_ERROR_CHECK(esp_wifi_init(&cfg)); // configure Wifi with cfg + ESP_ERROR_CHECK(esp_wifi_init(&wificfg)); // configure Wifi with cfg ESP_ERROR_CHECK( esp_wifi_set_country(&wifi_country)); // set locales for RF and channels ESP_ERROR_CHECK( @@ -65,16 +74,11 @@ void wifi_sniffer_init(void) { esp_wifi_set_promiscuous_filter(&filter)); // set MAC frame filter ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); // now switch on monitor mode -} -// Wifi channel rotation task -void switchWifiChannel(void *parameter) { - while (1) { - ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // waiting for channel switch timer - channel = - (channel % WIFI_CHANNEL_MAX) + 1; // rotate channel 1..WIFI_CHANNEL_MAX - esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); - //ESP_LOGD(TAG, "Wifi set channel %d", channel); - } - vTaskDelete(NULL); // shoud never be reached -} + // setup wifi channel rotation timer + WifiChanTimer = + xTimerCreate("WifiChannelTimer", pdMS_TO_TICKS(cfg.wifichancycle * 10), + pdTRUE, (void *)0, switchWifiChannel); + assert(WifiChanTimer); + xTimerStart(WifiChanTimer, 0); +} \ No newline at end of file