From 9c980a3794f8809363de3fa08cd2bdbb869cb51d Mon Sep 17 00:00:00 2001 From: "Paul D.Smith" Date: Fri, 29 Dec 2023 17:30:40 +0000 Subject: [PATCH] Flashing works, key press does not. --- esp32/cloudsmets/main/cs_app.c | 100 +++++++++++++++++----------- esp32/cloudsmets/main/cs_app.h | 3 +- esp32/cloudsmets/main/cs_flash.c | 64 +++++++++++------- esp32/cloudsmets/main/cs_mqtt.c | 4 +- esp32/cloudsmets/main/cs_ota.c | 3 + esp32/cloudsmets/main/cs_web.c | 39 +++-------- esp32/cloudsmets/main/cs_wifi.c | 8 +-- esp32/cloudsmets/main/cs_zigbee.c | 53 ++++++++++----- esp32/cloudsmets/sdkconfig.defaults | 1 + 9 files changed, 155 insertions(+), 120 deletions(-) diff --git a/esp32/cloudsmets/main/cs_app.c b/esp32/cloudsmets/main/cs_app.c index 4bc3efd..eea0db7 100644 --- a/esp32/cloudsmets/main/cs_app.c +++ b/esp32/cloudsmets/main/cs_app.c @@ -105,29 +105,43 @@ static void esp32_info() "Minimum free heap size: %d bytes\n", esp_get_minimum_free_heap_size()); } +static void IRAM_ATTR intr_handler(void *arg) +{ + BaseType_t task_unblocked; + + esp_event_isr_post_to( + app_event_loop_handle, + CS_APP_EVENT, + CS_APP_EVENT_KEY_ACTION, + NULL, + 0, + &task_unblocked); +} + /** * Note that the line is pull DOWN by a key press. * We send ourselves an event to avoid any processing that might not be * possible from inside an interrupt handler. */ -static void intr_handler(void *arg) +static void key_action(void) { - time_t pressed = 0; + static time_t pressed = 0; time_t released = 0; int level; level = gpio_get_level(CLEAR_KEY); + ESP_LOGI(TAG, "Key action: %d", level); if (level) { /* Rising line; key has been released. */ - released = time(NULL); - released = released - pressed; + released = time(NULL) - pressed; if (released > FIRST_JAN_2023) { /** * Time has been set whilst we were pressing the key so we cannot * tell how long it was held for; have to just ignore it. */ + ESP_LOGI(TAG, "Invalid interval - ignore"); } else if (released > FACTORY_RESET_PRESS) { @@ -135,30 +149,49 @@ static void intr_handler(void *arg) * Factory resetting the ESP32c3 will happen on a timer to allow * the tlsr8258 to be reset first. */ - ESP_ERROR_CHECK(esp_event_isr_post_to( - app_event_loop_handle, - CS_APP_EVENT, - CS_APP_EVENT_FACTORY_RESET, + ESP_LOGI(TAG, "Full factory reset"); + ESP_ERROR_CHECK(esp_event_post_to( + zigbee_event_loop_handle, + CS_ZIGBEE_EVENT, + CS_ZIGBEE_EVENT_FACTORY_RESET, NULL, 0, - NULL)); + 10)); + + /* Delay the ESP32c3 reset to allow ZigBee time to reset first. */ + + // TODO: Enable this later... + // ESP_ERROR_CHECK(esp_timer_start_once(factory_reset_timer_handle, FACTORY_RESET_TIMER_WAIT)); } else if (released > ZIGBEE_RESET_PRESS) { - ESP_ERROR_CHECK(esp_event_isr_post_to( - app_event_loop_handle, - CS_APP_EVENT, - CS_APP_EVENT_FACTORY_RESET, + ESP_LOGI(TAG, "Full ZigBee omly reset"); + ESP_ERROR_CHECK(esp_event_post_to( + zigbee_event_loop_handle, + CS_ZIGBEE_EVENT, + CS_ZIGBEE_EVENT_FACTORY_RESET, NULL, 0, - NULL)); + 10)); + } + else + { + ESP_LOGV(TAG, "Short press: %ld", time(NULL)); + struct timeval ptime; + gettimeofday(&ptime, NULL); + ESP_LOGV(TAG, "Short press: %ld", ptime.tv_sec); + ESP_LOGV(TAG, "Short press: %ld", pressed); + ESP_LOGV(TAG, "Short press: %ld", released); } } else { /* Falling line; key has been pressed. */ pressed = time(NULL); - released = pressed; + ESP_LOGV(TAG, "Short press: %ld", pressed); + struct timeval ptime; + gettimeofday(&ptime, NULL); + ESP_LOGV(TAG, "Short press: %ld", ptime.tv_sec); } } @@ -173,7 +206,10 @@ static void init_gpio(void) .intr_type = GPIO_INTR_DISABLE }; ESP_ERROR_CHECK(gpio_config(&gpio_config_data)); - +#if 1 + ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_LEVEL1)); + ESP_ERROR_CHECK(gpio_isr_handler_add(CLEAR_KEY, intr_handler, NULL)); +#endif /* Configure the input key GPIO line used by T-ZigBee. */ gpio_config_data.pin_bit_mask = (1 << CLEAR_KEY); gpio_config_data.mode = GPIO_MODE_INPUT; @@ -181,8 +217,11 @@ static void init_gpio(void) gpio_config_data.pull_down_en = GPIO_PULLDOWN_DISABLE; gpio_config_data.intr_type = GPIO_INTR_ANYEDGE; ESP_ERROR_CHECK(gpio_config(&gpio_config_data)); +#if 0 + // TODo: Comment on using alternatives above. ESP_ERROR_CHECK(gpio_isr_register(intr_handler, NULL, ESP_INTR_FLAG_LEVEL1, NULL)); ESP_ERROR_CHECK(gpio_intr_enable(CLEAR_KEY)); +#endif } static void factory_reset_timer_cb(void *arg) @@ -198,27 +237,8 @@ static void event_handler(void *arg, esp_event_base_t event_base, { switch (event_id) { - case CS_APP_EVENT_FACTORY_RESET: - ESP_ERROR_CHECK(esp_event_post_to( - zigbee_event_loop_handle, - CS_ZIGBEE_EVENT, - CS_ZIGBEE_EVENT_FACTORY_RESET, - NULL, - 0, - 10)); - - /* Delay the ESP32c3 reset to allow ZigBee time to reset first. */ - ESP_ERROR_CHECK(esp_timer_start_once(factory_reset_timer_handle, FACTORY_RESET_TIMER_WAIT)); - break; - - case CS_APP_EVENT_ZIGBEE_RESET: - ESP_ERROR_CHECK(esp_event_post_to( - zigbee_event_loop_handle, - CS_ZIGBEE_EVENT, - CS_ZIGBEE_EVENT_FACTORY_RESET, - NULL, - 0, - 10)); + case CS_APP_EVENT_KEY_ACTION: + key_action(); break; default: @@ -303,9 +323,11 @@ void app_main(void) .task_name = cs_app_task_name, .task_priority = CS_APP_TASK_PRIORITY_DEFAULT, // TODO: Do we really need this big a stack? Do we care? - .task_stack_size = 32768, + .task_stack_size = 4096, .task_core_id = tskNO_AFFINITY }; + esp_event_loop_args.task_name = cs_app_task_name; + ESP_ERROR_CHECK(esp_event_loop_create(&esp_event_loop_args, &app_event_loop_handle)); esp_event_loop_args.task_name = cs_flash_task_name; ESP_ERROR_CHECK(esp_event_loop_create(&esp_event_loop_args, &flash_event_loop_handle)); esp_event_loop_args.task_name = cs_web_task_name; @@ -364,7 +386,7 @@ void app_main(void) NULL)); // TODO: remove this which was added for testing. -#define NOW 1703592595 +#define NOW 1703870896 struct timeval now = { .tv_sec = NOW }; settimeofday(&now, NULL); ESP_LOGV(TAG, "Sending time event"); diff --git a/esp32/cloudsmets/main/cs_app.h b/esp32/cloudsmets/main/cs_app.h index 7846e89..65d1ccb 100644 --- a/esp32/cloudsmets/main/cs_app.h +++ b/esp32/cloudsmets/main/cs_app.h @@ -1,6 +1,5 @@ typedef enum { - CS_APP_EVENT_FACTORY_RESET, /*!< Reset everything! */ - CS_APP_EVENT_ZIGBEE_RESET, /*!< Reset ZigBee (new join). */ + CS_APP_EVENT_KEY_ACTION, /*!< Reset everything! */ } cs_app_event_t; ESP_EVENT_DECLARE_BASE(CS_APP_EVENT); diff --git a/esp32/cloudsmets/main/cs_flash.c b/esp32/cloudsmets/main/cs_flash.c index 25b67d6..cc615d0 100644 --- a/esp32/cloudsmets/main/cs_flash.c +++ b/esp32/cloudsmets/main/cs_flash.c @@ -36,7 +36,7 @@ ESP_EVENT_DECLARE_BASE(MQTT_EVENTS); esp_event_loop_handle_t flash_event_loop_handle; -#define STATUS_WIFI_SOFTAP_CONNECTED 0x01 +#define STATUS_WIFI_SOFTAP_STARTED 0x01 #define STATUS_WIFI_AP_CONNECTED 0x02 #define STATUS_MQTT_CONNECTED 0x04 #define STATUS_ZIGBEE_CONNECTED 0x08 @@ -54,17 +54,15 @@ static int flashes_per_cycle = 0; static void led_on() { - esp_timer_stop(flash_timer_handle); ESP_ERROR_CHECK(gpio_set_level(BLUE_LED, 1)); } static void led_off() { - esp_timer_stop(flash_timer_handle); ESP_ERROR_CHECK(gpio_set_level(BLUE_LED, 0)); } -#define FLASH_TIME_PERIOD (uint64_t)1000 * 500 +#define FLASH_TIME_PERIOD ((uint64_t)1000 * 500) static void led_flash(int flashes) { // TODO: Much nicer if we get rid of globals. @@ -82,7 +80,7 @@ static void led_flash(int flashes) * on, off, on, off, on, off, off, off * So a sequence of X (on, off) followed by (off, off). */ -void flash_timer_cb(void *arg) +static void flash_timer_cb(void *arg) { static int count = 0; static bool lit = false; @@ -94,25 +92,26 @@ void flash_timer_cb(void *arg) } else { - if (count <= 0) + if (count) { - /* Time for next cycle but LED stays off for this cycle. */ - count = flashes_per_cycle; - } - else - { - /* Turn the LED on. */ led_on(); } count--; + if (count <= -1) + { + /* Time for next cycle but LED stays off for this cycle. */ + count = flashes_per_cycle; + } } lit = !lit; } -void update_flash_status(uint8_t status) +static void update_flash_status(uint8_t status) { + ESP_LOGV(TAG, "status: %2.2X", status); if ((status & ALL_ZIGBEE_ACTIVE) == ALL_ZIGBEE_ACTIVE) { + esp_timer_stop(flash_timer_handle); led_on(); } else if ((status & ALL_MQTT_ACTIVE) == ALL_MQTT_ACTIVE) @@ -129,6 +128,7 @@ void update_flash_status(uint8_t status) } else { + esp_timer_stop(flash_timer_handle); led_off(); } } @@ -144,18 +144,20 @@ void update_flash_status(uint8_t status) static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { - uint8_t status = 0; + static uint8_t status = 0; + + ESP_LOGV(TAG, "Event: %s, %d", event_base, event_id); if (event_base == WIFI_EVENT) { switch (event_id) { - case WIFI_EVENT_AP_STACONNECTED: - status |= STATUS_WIFI_SOFTAP_CONNECTED; + case WIFI_EVENT_AP_START: + status |= STATUS_WIFI_SOFTAP_STARTED; break; - case WIFI_EVENT_AP_STADISCONNECTED: - status &= ~STATUS_WIFI_SOFTAP_CONNECTED; + case WIFI_EVENT_AP_STOP: + status &= ~STATUS_WIFI_SOFTAP_STARTED; break; case WIFI_EVENT_STA_CONNECTED: @@ -167,7 +169,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, break; default: - ESP_LOGE(TAG, "Unexpected flash event: %d", event_id); + ESP_LOGE(TAG, "Unexpected WiFi event: %d", event_id); break; } } @@ -184,7 +186,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, break; default: - ESP_LOGE(TAG, "Unexpected flash event: %d", event_id); + ESP_LOGE(TAG, "Unexpected MQTT event: %d", event_id); break; } } @@ -201,7 +203,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, break; default: - ESP_LOGE(TAG, "Unexpected flash event: %d", event_id); + ESP_LOGE(TAG, "Unexpected ZigBee event: %d", event_id); break; } } @@ -209,12 +211,14 @@ static void event_handler(void *arg, esp_event_base_t event_base, { ESP_LOGE(TAG, "Unexpected event: %s, %d", event_base, event_id); } + ESP_LOGV(TAG, "Update flasher status"); update_flash_status(status); } void cs_flash_task(cs_flash_create_parms_t *create_parms) { - static int flashes = 0; + // TODO: Remove this or perhaps replace with config? + esp_log_level_set(TAG, ESP_LOG_VERBOSE); flash_event_loop_handle = create_parms->flash_event_loop_handle; @@ -228,20 +232,30 @@ void cs_flash_task(cs_flash_create_parms_t *create_parms) WIFI_EVENT, ESP_EVENT_ANY_ID, event_handler, - (void *)&flashes, + NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register_with( flash_event_loop_handle, MQTT_EVENTS, ESP_EVENT_ANY_ID, event_handler, - (void *)&flashes, + NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register_with( flash_event_loop_handle, CS_ZIGBEE_EVENT, ESP_EVENT_ANY_ID, event_handler, - (void *)&flashes, + NULL, NULL)); + + /** + * Create the timer that will make the LED flash. + */ + esp_timer_create_args_t timer_args = + { + .callback = flash_timer_cb, + .name = "Flash timer" + }; + ESP_ERROR_CHECK(esp_timer_create(&timer_args, &flash_timer_handle)); } \ No newline at end of file diff --git a/esp32/cloudsmets/main/cs_mqtt.c b/esp32/cloudsmets/main/cs_mqtt.c index 7f77a76..e9b0015 100644 --- a/esp32/cloudsmets/main/cs_mqtt.c +++ b/esp32/cloudsmets/main/cs_mqtt.c @@ -175,8 +175,7 @@ static void generate_sas_token() cs_string hmac_sig = {hmac_sig_value, SHA256_LEN}; unsigned char b64_hmac_sig_value[URLENCODED_BASE64_SHA256_LEN + 1]; cs_string b64_hmac_sig = {b64_hmac_sig_value, URLENCODED_BASE64_SHA256_LEN}; - // DEBUG: TODO: time_t ttl = time(NULL) + 3600; - time_t ttl = 1703596659 + 3600; + time_t ttl = time(NULL) + 3600; unsigned char *sptr; int offset; cs_string temp_str = {NULL, 0}; @@ -630,6 +629,7 @@ void cs_mqtt_task(cs_mqtt_create_parms_t *create_parms) ESP_LOGI(TAG, "Init. MQTT task"); mqtt_event_loop_handle = create_parms->mqtt_event_loop_handle; + flash_event_loop_handle = create_parms->flash_event_loop_handle; ESP_LOGI(TAG, "Register event handlers"); diff --git a/esp32/cloudsmets/main/cs_ota.c b/esp32/cloudsmets/main/cs_ota.c index e63c720..e593c9b 100644 --- a/esp32/cloudsmets/main/cs_ota.c +++ b/esp32/cloudsmets/main/cs_ota.c @@ -490,6 +490,9 @@ static esp_timer_create_args_t esp_acceptance_timer_create_args = { void cs_ota_task(cs_ota_create_parms_t *create_parms) { + // TODO: Remove this. + esp_log_level_set(TAG, ESP_LOG_VERBOSE); + ESP_LOGI(TAG, "Creating OTA task"); ota_event_loop_handle = create_parms->ota_event_loop_handle; uint16_t acceptance_interval_mins; diff --git a/esp32/cloudsmets/main/cs_web.c b/esp32/cloudsmets/main/cs_web.c index 0a8f2c4..275a9b9 100644 --- a/esp32/cloudsmets/main/cs_web.c +++ b/esp32/cloudsmets/main/cs_web.c @@ -16,15 +16,13 @@ #include "esp_wifi.h" #include "esp_http_server.h" #include "esp_netif.h" +#include "esp_netif_types.h" #include "http_parser.h" #include "cs_web.h" #include "cs_cfg.h" #define TAG cs_web_task_name -// TODO: remove this -#define MQTT - /* Task configuration */ #define CS_TASK_TICKS_TO_WAIT CONFIG_CS_TASK_TICKS_TO_WAIT @@ -42,10 +40,8 @@ extern const uint8_t web_html_start[] asm("_binary_web_html_start"); extern const uint8_t web_html_end[] asm("_binary_web_html_end"); extern const uint8_t ota_html_start[] asm("_binary_ota_html_start"); extern const uint8_t ota_html_end[] asm("_binary_ota_html_end"); -#ifdef MQTT extern const uint8_t mqtt_html_start[] asm("_binary_mqtt_html_start"); extern const uint8_t mqtt_html_end[] asm("_binary_mqtt_html_end"); -#endif extern const uint8_t style_css_start[] asm("_binary_style_css_start"); extern const uint8_t style_css_end[] asm("_binary_style_css_end"); extern const uint8_t tools_js_start[] asm("_binary_tools_js_start"); @@ -57,9 +53,7 @@ extern const uint8_t tools_js_end[] asm("_binary_tools_js_end"); #define wifi_html_len (size_t)(wifi_html_end - wifi_html_start - 1) #define web_html_len (size_t)(web_html_end - web_html_start - 1) #define ota_html_len (size_t)(ota_html_end - ota_html_start - 1) -#ifdef MQTT #define mqtt_html_len (size_t)(mqtt_html_end - mqtt_html_start - 1) -#endif #define style_css_len (size_t)(style_css_end - style_css_start - 1) #define getdata_js_len (size_t)(getdata_js_end - getdata_js_start - 1) #define tools_js_len (size_t)(tools_js_end - tools_js_start - 1) @@ -67,9 +61,7 @@ extern const uint8_t tools_js_end[] asm("_binary_tools_js_end"); /* Event loops (exccept for the default loop, used by Wifi) */ static esp_event_loop_handle_t web_event_loop_handle = NULL; static esp_event_loop_handle_t ota_event_loop_handle = NULL; -#ifdef MQTT static esp_event_loop_handle_t mqtt_event_loop_handle = NULL; -#endif /* Server configuration. */ static httpd_config_t httpd_config = HTTPD_DEFAULT_CONFIG(); @@ -158,13 +150,11 @@ static esp_err_t get_ota_html_handler(httpd_req_t *req) return httpd_resp_send(req, (char *)ota_html_start, ota_html_len); } -#ifdef MQTT static esp_err_t get_mqtt_html_handler(httpd_req_t *req) { - ESP_LOGV(TAG, "GET ota.html"); + ESP_LOGV(TAG, "GET mqtt.html"); return httpd_resp_send(req, (char *)mqtt_html_start, mqtt_html_len); } -#endif static esp_err_t get_style_css_handler(httpd_req_t *req) { @@ -310,13 +300,11 @@ static esp_err_t get_ota_json_handler(httpd_req_t *req) return get_json_handler(req, CS_CFG_NMSP_OTA, cs_cfg_ota_definitions); } -#ifdef MQTT static esp_err_t get_mqtt_json_handler(httpd_req_t *req) { ESP_LOGV(TAG, "GET mqtt.json"); return get_json_handler(req, CS_CFG_NMSP_MQTT, cs_cfg_mqtt_definitions); } -#endif static esp_err_t post_html_handler( httpd_req_t *req, @@ -461,13 +449,11 @@ static esp_err_t post_ota_html_handler(httpd_req_t *req) return post_html_handler(req, CS_CFG_NMSP_OTA, cs_cfg_ota_definitions, ota_event_loop_handle, CS_CONFIG_CHANGE, CS_CONFIG_CHANGE); } -#ifdef MQTT static esp_err_t post_mqtt_html_handler(httpd_req_t *req) { ESP_LOGV(TAG, "POST mqtt.html"); return post_html_handler(req, CS_CFG_NMSP_MQTT, cs_cfg_mqtt_definitions, mqtt_event_loop_handle, CS_CONFIG_CHANGE, CS_CONFIG_CHANGE); } -#endif /* URI handler structures for GET /... */ static httpd_uri_t uri_get_root_html = { @@ -512,14 +498,12 @@ static httpd_uri_t uri_get_ota_html = { .user_ctx = NULL }; -#ifdef MQTT static httpd_uri_t uri_get_mqtt_html = { .uri = "/mqtt.html", .method = HTTP_GET, .handler = get_mqtt_html_handler, .user_ctx = NULL }; -#endif static httpd_uri_t uri_get_style_css = { .uri = "/css/style.css", @@ -556,7 +540,6 @@ static httpd_uri_t uri_get_ota_json = { .user_ctx = NULL }; -#ifdef MQTT // TODO: Should we rename this all Azure? static httpd_uri_t uri_get_mqtt_json = { .uri = "/mqtt.json", @@ -564,7 +547,6 @@ static httpd_uri_t uri_get_mqtt_json = { .handler = get_mqtt_json_handler, .user_ctx = NULL }; -#endif /* URI handler structure for POST /uri */ static httpd_uri_t uri_post_wifi_html = { @@ -588,18 +570,15 @@ static httpd_uri_t uri_post_ota_html = { .user_ctx = NULL }; -#ifdef MQTT static httpd_uri_t uri_post_mqtt_html = { .uri = "/mqtt.html", .method = HTTP_POST, .handler = post_mqtt_html_handler, .user_ctx = NULL }; -#endif void web_start() { - /** * Read the listen port. */ @@ -649,8 +628,8 @@ static void default_event_handler(void *arg, esp_event_base_t event_base, esp_event_post_to(web_event_loop_handle, event_base, event_id, NULL, 0, 0); } -static void web_event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) +static void event_handler(void *arg, esp_event_base_t event_base, + int32_t event_id, void *event_data) { /* STA has gotten an IP address so make sure we are listening on it. */ ESP_LOGI(TAG, "Restarting HTTP server."); @@ -720,9 +699,7 @@ void cs_web_task(cs_web_create_parms_t *create_parms) /* Save the event handles that we need to send notifications to. */ web_event_loop_handle = create_parms->web_event_loop_handle; ota_event_loop_handle = create_parms->ota_event_loop_handle; -#ifdef MQTT mqtt_event_loop_handle = create_parms->mqtt_event_loop_handle; -#endif /** * TODO: Is this really true? Remove this if not. @@ -748,28 +725,28 @@ void cs_web_task(cs_web_create_parms_t *create_parms) web_event_loop_handle, IP_EVENT, IP_EVENT_STA_GOT_IP, - web_event_handler, + event_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register_with( web_event_loop_handle, WIFI_EVENT, WIFI_EVENT_AP_START, - web_event_handler, + event_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register_with( web_event_loop_handle, CS_CONFIG_EVENT, CS_CONFIG_CHANGE, - &web_event_handler, + event_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register_with( web_event_loop_handle, IP_EVENT, ESP_NETIF_IP_EVENT_GOT_IP, - &web_event_handler, + event_handler, NULL, NULL)); diff --git a/esp32/cloudsmets/main/cs_wifi.c b/esp32/cloudsmets/main/cs_wifi.c index cc5aa45..9366ae3 100644 --- a/esp32/cloudsmets/main/cs_wifi.c +++ b/esp32/cloudsmets/main/cs_wifi.c @@ -107,26 +107,26 @@ static void wifi_event_handler(void *arg, esp_event_base_t event_base, case WIFI_EVENT_AP_START: // esp_wifi_connect(); ESP_LOGI(TAG, "SoftAP: Started"); + + // TODO: Make ticks more realistic and use a #define. + esp_event_post_to(flash_event_loop_handle, event_base, event_id, NULL, 0, 10); break; case WIFI_EVENT_AP_STOP: ESP_LOGI(TAG, "SoftAP: Stopped"); + esp_event_post_to(flash_event_loop_handle, event_base, event_id, NULL, 0, 10); break; case WIFI_EVENT_AP_STACONNECTED: ap_conn_event = (wifi_event_ap_staconnected_t *) event_data; ESP_LOGI(TAG, "SoftAP: station "MACSTR" joined, AID=%d", MAC2STR(ap_conn_event->mac), ap_conn_event->aid); - - // TODO: Make ticks more realistic and use a #define. - esp_event_post_to(flash_event_loop_handle, event_base, event_id, NULL, 0, 10); break; case WIFI_EVENT_AP_STADISCONNECTED: ap_disconn_event = (wifi_event_ap_stadisconnected_t *) event_data; ESP_LOGI(TAG, "SoftAP: Station "MACSTR" left, AID=%d", MAC2STR(ap_disconn_event->mac), ap_disconn_event->aid); - esp_event_post_to(flash_event_loop_handle, event_base, event_id, NULL, 0, 10); break; case WIFI_EVENT_STA_CONNECTED: diff --git a/esp32/cloudsmets/main/cs_zigbee.c b/esp32/cloudsmets/main/cs_zigbee.c index 40ebe5b..cde9e90 100644 --- a/esp32/cloudsmets/main/cs_zigbee.c +++ b/esp32/cloudsmets/main/cs_zigbee.c @@ -19,6 +19,7 @@ #include "esp_wifi.h" #include "esp_netif.h" #include "esp_timer.h" +#include "soc/soc_caps.h" #include "driver/uart.h" #include "driver/gpio.h" @@ -29,6 +30,7 @@ #include "cs_time.h" #include "cs_string.h" #include "cs_zigbee.h" +#include "pd_err.h" /** * The following header files are lifted straight from the Telink SDK. @@ -201,10 +203,6 @@ static void zbhci_message(uint16_t command_id, uint16_t payload_length, uint8_t * We have a valid message; might need to tell the LED flasher and certianly * need to indicate we are connected. */ - if (!receiving_events) - { - ESP_ERROR_CHECK(esp_event_post_to(flash_event_loop_handle, CS_ZIGBEE_EVENT, CS_ZIGBEE_EVENT_CONNECTED, NULL, 0, 10)); - } receiving_events = true; switch (command_id) @@ -395,14 +393,17 @@ static void request_time_timer_cb(void *arg) */ static void connected_timer_cb(void *arg) { - if (!receiving_events) + static bool previous_receiving_events = false; + + if (!receiving_events && previous_receiving_events) { esp_event_post_to(flash_event_loop_handle, CS_ZIGBEE_EVENT, CS_ZIGBEE_EVENT_DISCONNECTED, NULL, 0, 10); } - /** - * If we receive data before the next time pop, 'receiving_events' will be - * set 'true'. - */ + else if (receiving_events && !previous_receiving_events) + { + ESP_ERROR_CHECK(esp_event_post_to(flash_event_loop_handle, CS_ZIGBEE_EVENT, CS_ZIGBEE_EVENT_CONNECTED, NULL, 0, 10)); + } + previous_receiving_events = receiving_events; receiving_events = false; } @@ -422,8 +423,9 @@ static void build_commands(void) msg->pData[0] = ZBHCI_MSG_END_FLAG; } -void cs_zigbee_task(cs_zigbee_create_parms_t *create_parms) +static void uart_rx_task(void *arg) { + cs_zigbee_create_parms_t *create_parms = (cs_zigbee_create_parms_t *)arg; uint32_t bytes_to_read = RX_BUFFER_SIZE; uint32_t bytes_present = 0; uint32_t bytes_read; @@ -433,7 +435,6 @@ void cs_zigbee_task(cs_zigbee_create_parms_t *create_parms) // TODO: Remove this or perhaps replace with config? esp_log_level_set(TAG, ESP_LOG_VERBOSE); - ESP_LOGI(TAG, "Init. ZigBee task"); zigbee_event_loop_handle = create_parms->zigbee_event_loop_handle; mqtt_event_loop_handle = create_parms->mqtt_event_loop_handle; @@ -457,6 +458,7 @@ void cs_zigbee_task(cs_zigbee_create_parms_t *create_parms) NULL, NULL)); + ESP_LOGI(TAG, "Configure UART"); /** * Processing below follows the order show in example: * peripherals/uart/uart_events/main/uart_events_example_main.c @@ -471,12 +473,10 @@ void cs_zigbee_task(cs_zigbee_create_parms_t *create_parms) QueueHandle_t uart_queue_handle; // TODO: Understand this. -#define TLSR8258_MIN_MSG_SIZE 1 -#define TLSR8258_MAX_MSG_SIZE 2 -#define UART_QUEUE_DEPTH 5 +#define UART_QUEUE_DEPTH 8 /* Configure interupts, just for RX. */ ESP_ERROR_CHECK(uart_driver_install( - UART_NUM_0, TLSR8258_MAX_MSG_SIZE, TLSR8258_MAX_MSG_SIZE, UART_QUEUE_DEPTH, &uart_queue_handle, 0)); + UART_TO_TLSR8258, 2 * UART_FIFO_LEN, 2 * UART_FIFO_LEN, UART_QUEUE_DEPTH, &uart_queue_handle, 0)); /* Configure UART parameters. */ ESP_ERROR_CHECK(uart_param_config(UART_TO_TLSR8258, &uart_config)); @@ -549,7 +549,7 @@ QueueHandle_t uart_queue_handle; { bPtr = buffer + bytes_present; bytes_to_read = RX_BUFFER_SIZE - bytes_present; - bytes_read = uart_read_bytes(UART_NUM_0, bPtr, bytes_to_read, TICKS_TO_WAIT); + bytes_read = uart_read_bytes(UART_TO_TLSR8258, bPtr, bytes_to_read, TICKS_TO_WAIT); if (bytes_present == -1) { ESP_LOGE(TAG, "Rx error."); @@ -573,4 +573,23 @@ QueueHandle_t uart_queue_handle; } } } -} \ No newline at end of file +} + +void cs_zigbee_task(cs_zigbee_create_parms_t *create_parms) +{ + /* Local copy of create_parms as they are "gone" before the thread starts! */ + static cs_zigbee_create_parms_t zb_create_parms; + /** + * Now we start an FreeRTOS thread that just sits in a loop, receiving data. + */ + // TODO: What stack size do we really want? + zb_create_parms.zigbee_event_loop_handle = create_parms->zigbee_event_loop_handle; + zb_create_parms.mqtt_event_loop_handle = create_parms->mqtt_event_loop_handle; + zb_create_parms.flash_event_loop_handle = create_parms->flash_event_loop_handle; + + // TODO: What is a sensible stack size? + PD_ERROR_CHECK(xTaskCreate(uart_rx_task, "uart_rx_task", 4096, &zb_create_parms, 10, NULL)); +} + + + diff --git a/esp32/cloudsmets/sdkconfig.defaults b/esp32/cloudsmets/sdkconfig.defaults index bd666b6..4a8acaa 100644 --- a/esp32/cloudsmets/sdkconfig.defaults +++ b/esp32/cloudsmets/sdkconfig.defaults @@ -8,6 +8,7 @@ CONFIG_CS_WEB_LISTEN_PORT=80 CONFIG_CS_OTA_ENABLED=0 CONFIG_ETH_USE_SPI_ETHERNET=n CONFIG_HTTPD_MAX_REQ_HDR_LEN=1024 +CONFIG_ESP_COREDUMP_ENABLE_TO_UART=y CONFIG_LOG_DEFAULT_LEVEL_WARN=y CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_NONE=y CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE=y