From 5bbe4bee02a3b12418a34a554804fae2c693e070 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 30 Oct 2020 12:24:16 +0100 Subject: [PATCH] new _ASSERT function --- include/globals.h | 11 +++++++++++ include/main.h | 2 +- src/display.cpp | 17 ++++++++--------- src/gpsread.cpp | 2 +- src/irqhandler.cpp | 2 +- src/lmic_config.h | 5 ++--- src/lorawan.cpp | 6 +++--- src/main.cpp | 28 +++++++++++++++------------- src/mqttclient.cpp | 2 +- src/spislave.cpp | 2 +- src/timekeeper.cpp | 2 +- src/wifiscan.cpp | 4 ++-- 12 files changed, 47 insertions(+), 36 deletions(-) diff --git a/include/globals.h b/include/globals.h index 324c5328..2e4320d5 100644 --- a/include/globals.h +++ b/include/globals.h @@ -1,5 +1,6 @@ #ifndef _GLOBALS_H #define _GLOBALS_H +#endif // The mother of all embedded development... #include @@ -49,6 +50,16 @@ (xSemaphoreTake(I2Caccess, pdMS_TO_TICKS(DISPLAYREFRESH_MS)) == pdTRUE) #define I2C_MUTEX_UNLOCK() (xSemaphoreGive(I2Caccess)) +// pseudo system halt function, useful to prevent writeloops to NVRAM +#ifndef _ASSERT +#define _ASSERT(cond) \ + if ((cond) == 0) { \ + ESP_LOGE(TAG, "FAILURE in %s:%d", __FILE__, __LINE__); \ + mask_user_IRQ(); \ + for (;;) \ + ; \ + } + enum sendprio_t { prio_low, prio_normal, prio_high }; enum timesource_t { _gps, _rtc, _lora, _unsynced }; diff --git a/include/main.h b/include/main.h index acb64e95..397d6ad6 100644 --- a/include/main.h +++ b/include/main.h @@ -21,4 +21,4 @@ #include "timekeeper.h" #include "corona.h" -#endif +#endif \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index c8d88694..a29cd974 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -67,7 +67,7 @@ void dp_setup(int contrast) { MY_DISPLAY_INVERT, USE_HW_I2C, MY_DISPLAY_SDA, MY_DISPLAY_SCL, MY_DISPLAY_RST, OLED_FREQUENCY); // use standard I2C bus at 400Khz - assert(rc != OLED_NOT_FOUND); + _ASSERT (rc != OLED_NOT_FOUND); // set display buffer obdSetBackBuffer(&ssoled, displaybuf); @@ -265,10 +265,10 @@ void dp_drawPage(time_t t, bool nextpage) { else dp_printf("WIFI:off"); if (cfg.blescan) - if (!cfg.enscount) - dp_printf("BLTH:%-5d", macs_ble); - else - dp_printf(" CWA:%-5d", cwa_report()); + if (!cfg.enscount) + dp_printf("BLTH:%-5d", macs_ble); + else + dp_printf(" CWA:%-5d", cwa_report()); else dp_printf(" BLTH:off"); #elif ((WIFICOUNTER) && (!BLECOUNTER)) @@ -279,10 +279,9 @@ void dp_drawPage(time_t t, bool nextpage) { #elif ((!WIFICOUNTER) && (BLECOUNTER)) if (cfg.blescan) { dp_printf("BLTH:%-5d", macs_ble); - if (cfg.enscount) - dp_printf("(CWA:%d)", cwa_report()); - } - else + if (cfg.enscount) + dp_printf("(CWA:%d)", cwa_report()); + } else dp_printf("BLTH:off"); #else dp_printf("Sniffer disabled"); diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 75206e59..00e3c90a 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -150,7 +150,7 @@ time_t get_gpstime(void) { // GPS serial feed FreeRTos Task void gps_loop(void *pvParameters) { - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + _ASSERT((uint32_t)pvParameters == 1); // FreeRTOS check while (1) { diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index a169f4b1..82b1690c 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -6,7 +6,7 @@ static const char TAG[] = __FILE__; // irq handler task, handles all our application level interrupts void irqHandler(void *pvParameters) { - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + _ASSERT((uint32_t)pvParameters == 1); // FreeRTOS check uint32_t InterruptStatus; diff --git a/src/lmic_config.h b/src/lmic_config.h index 00df54d5..992e4a18 100644 --- a/src/lmic_config.h +++ b/src/lmic_config.h @@ -22,9 +22,8 @@ #define LMIC_USE_INTERRUPTS 1 #endif -// avoid lmic warning if we don't configure radio because we don't have one -#define CFG_sx1276_radio 1 -#if ! (defined(CFG_sx1272_radio) || defined(CFG_sx1276_radio)) +// avoid lmic warning if we don't configure radio in case we haven't one +#if !(defined(CFG_sx1272_radio) || defined(CFG_sx1276_radio)) #define CFG_sx1276_radio 1 #endif diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 34cc4a0b..d7567d50 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -227,7 +227,7 @@ void showLoraKeys(void) { // LMIC send task void lora_send(void *pvParameters) { - configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check + _ASSERT((uint32_t)pvParameters == 1); // FreeRTOS check MessageBuffer_t SendBuffer; @@ -292,7 +292,7 @@ void lora_stack_reset() { } esp_err_t lora_stack_init(bool do_join) { - assert(SEND_QUEUE_SIZE); + _ASSERT(SEND_QUEUE_SIZE > 0); LoraSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); if (LoraSendQueue == 0) { ESP_LOGE(TAG, "Could not create LORA send queue. Aborting."); @@ -382,7 +382,7 @@ void lora_queuereset(void) { xQueueReset(LoraSendQueue); } // LMIC lorawan stack task void lmictask(void *pvParameters) { - configASSERT(((uint32_t)pvParameters) == 1); + _ASSERT((uint32_t)pvParameters == 1); // setup LMIC stack os_init_ex(&myPinmap); // initialize lmic run-time environment diff --git a/src/main.cpp b/src/main.cpp index d787f97a..cabfa1e7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -65,8 +65,8 @@ PMUIRQ -> PMU chip gpio fired by software (Ticker.h) TIMESYNC_IRQ -> setTimeSyncIRQ() CYCLIC_IRQ -> setCyclicIRQ() -SENDCYCLE_IRQ -> setSendIRQ() -BME_IRQ -> setBMEIRQ() +SENDCYCLE_IRQ -> setSendIRQ() +BME_IRQ -> setBMEIRQ() MQTT_IRQ -> setMqttIRQ() ClockTask (Core 1), see timekeeper.cpp @@ -118,7 +118,7 @@ void setup() { // create some semaphores for syncing / mutexing tasks I2Caccess = xSemaphoreCreateMutex(); // for access management of i2c bus - assert(I2Caccess != NULL); + _ASSERT(I2Caccess != NULL); I2C_MUTEX_UNLOCK(); // disable brownout detection @@ -203,7 +203,7 @@ void setup() { #endif // read (and initialize on first run) runtime settings from NVRAM - assert(loadConfig()); // includes initialize if necessary + _ASSERT(loadConfig()); // includes initialize if necessary // now that we are powered, we scan i2c bus for devices i2c_scan(); @@ -217,7 +217,7 @@ void setup() { #endif #ifdef BOARD_HAS_PSRAM - assert(psramFound()); + _ASSERT(psramFound()); ESP_LOGI(TAG, "PSRAM found and initialized"); strcat_P(features, " PSRAM"); #endif @@ -343,20 +343,20 @@ void setup() { #if (HAS_LORA) strcat_P(features, " LORA"); // kick off join, except we come from sleep - assert(lora_stack_init(RTC_runmode == RUNMODE_WAKEUP ? false : true) == + _ASSERT(lora_stack_init(RTC_runmode == RUNMODE_WAKEUP ? false : true) == ESP_OK); #endif // initialize SPI #ifdef HAS_SPI strcat_P(features, " SPI"); - assert(spi_init() == ESP_OK); + _ASSERT(spi_init() == ESP_OK); #endif // initialize MQTT #ifdef HAS_MQTT strcat_P(features, " MQTT"); - assert(mqtt_init() == ESP_OK); + _ASSERT(mqtt_init() == ESP_OK); #endif #if (HAS_SDCARD) @@ -395,7 +395,7 @@ void setup() { // initialize RTC #ifdef HAS_RTC strcat_P(features, " RTC"); - assert(rtc_init()); + _ASSERT(rtc_init()); #endif #if defined HAS_DCF77 @@ -446,11 +446,13 @@ void setup() { strcat_P(features, " BMP180"); #endif if (bme_init()) - ESP_LOGI(TAG, "Starting BME sensor..."); + ESP_LOGI(TAG, "BME sensor initialized"); + else + ESP_LOGE(TAG, "BME sensor could not be initialized"); #endif // starting timers and interrupts - assert(irqHandlerTask != NULL); // has interrupt handler task started? + _ASSERT(irqHandlerTask != NULL); // has interrupt handler task started? ESP_LOGI(TAG, "Starting Timers..."); // display interrupt @@ -505,7 +507,7 @@ void setup() { #endif ESP_LOGI(TAG, "Starting Timekeeper..."); - assert(timepulse_init()); // setup pps timepulse + _ASSERT(timepulse_init()); // setup pps timepulse timepulse_start(); // starts pps and cyclic time sync #endif // TIME_SYNC_INTERVAL @@ -520,4 +522,4 @@ void setup() { } // setup() -void loop() { vTaskDelete(NULL); } +void loop() { vTaskDelete(NULL); } \ No newline at end of file diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index c17fd049..25d7ee04 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -23,7 +23,7 @@ esp_err_t mqtt_init(void) { mqttClient.begin(MQTT_SERVER, MQTT_PORT, netClient); mqttClient.onMessageAdvanced(mqtt_callback); - assert(SEND_QUEUE_SIZE); + _ASSERT(SEND_QUEUE_SIZE > 0); MQTTSendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); if (MQTTSendQueue == 0) { ESP_LOGE(TAG, "Could not create MQTT send queue. Aborting."); diff --git a/src/spislave.cpp b/src/spislave.cpp index 4d3a75ab..3e581318 100644 --- a/src/spislave.cpp +++ b/src/spislave.cpp @@ -104,7 +104,7 @@ void spi_slave_task(void *param) { } esp_err_t spi_init() { - assert(SEND_QUEUE_SIZE); + _ASSERT(SEND_QUEUE_SIZE > 0); SPISendQueue = xQueueCreate(SEND_QUEUE_SIZE, sizeof(MessageBuffer_t)); if (SPISendQueue == 0) { ESP_LOGE(TAG, "Could not create SPI send queue. Aborting."); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 051f77c3..3354b45d 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -245,7 +245,7 @@ void clock_init(void) { &ClockTask, // task handle 1); // CPU core - assert(ClockTask); // has clock task started? + _ASSERT(ClockTask != NULL); // has clock task started? } // clock_init void clock_loop(void *taskparameter) { // ClockTask diff --git a/src/wifiscan.cpp b/src/wifiscan.cpp index aa7a7041..dd8ce4a1 100644 --- a/src/wifiscan.cpp +++ b/src/wifiscan.cpp @@ -45,7 +45,7 @@ IRAM_ATTR void wifi_sniffer_packet_handler(void *buff, // Software-timer driven Wifi channel rotation callback function void switchWifiChannel(TimerHandle_t xTimer) { - configASSERT(xTimer); + _ASSERT(xTimer != NULL); channel = (channel % WIFI_CHANNEL_MAX) + 1; // rotate channel 1..WIFI_CHANNEL_MAX esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); @@ -82,7 +82,7 @@ void wifi_sniffer_init(void) { } void switch_wifi_sniffer(uint8_t state) { - assert(WifiChanTimer); + _ASSERT(WifiChanTimer != NULL); if (state) { // switch wifi sniffer on ESP_ERROR_CHECK(esp_wifi_start());