From b936441b9b8188acff07989094f2c867312a2b98 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 00:50:57 +0800 Subject: [PATCH 01/14] Startup flow refactoring This change removes implicit WiFi/BT initialization from startup code. "main" task is started once essential part of startup code is complete. This task calls application-provided "int main(void)" function, which can call WiFi/BT init functions if necessary. --- components/bt/bt.c | 23 +----- components/bt/include/bt.h | 11 ++- components/esp32/Kconfig | 29 +++---- components/esp32/cpu_start.c | 37 +++------ components/esp32/include/esp_task.h | 7 +- components/esp32/include/esp_wifi.h | 21 ----- components/esp32/ld/esp32.rom.ld | 2 +- components/esp32/wifi.c | 115 ---------------------------- examples/04_ble_adv/main/app_bt.c | 3 +- 9 files changed, 38 insertions(+), 210 deletions(-) delete mode 100644 components/esp32/wifi.c diff --git a/components/bt/bt.c b/components/bt/bt.c index efb6d34ee3..d9bc5ae089 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -96,26 +96,11 @@ static void bt_controller_task(void *pvParam) btdm_controller_init(); } - -static void bt_init_task(void *pvParameters) +void bt_controller_init() { - xTaskCreatePinnedToCore(bt_controller_task, "btControllerTask", ESP_TASK_BT_CONTROLLER_STACK, NULL, ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0); - - if (app_startup_cb) { - app_startup_cb(app_startup_ctx); - } - - vTaskDelete(NULL); + xTaskCreatePinnedToCore(bt_controller_task, "btController", + ESP_TASK_BT_CONTROLLER_STACK, NULL, + ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0); } - -esp_err_t esp_bt_startup(bt_app_startup_cb_t cb, void *ctx) -{ - app_startup_cb = cb; - app_startup_ctx = ctx; - - xTaskCreatePinnedToCore(bt_init_task, "btInitTask", ESP_TASK_BT_INIT_STACK, NULL, ESP_TASK_BT_INIT_PRIO, NULL, 0); - - return ESP_OK; -} #endif diff --git a/components/bt/include/bt.h b/components/bt/include/bt.h index 8511aabdf8..0dc0424939 100644 --- a/components/bt/include/bt.h +++ b/components/bt/include/bt.h @@ -15,7 +15,7 @@ #ifndef __BT_H__ #define __BT_H__ -#include "freertos/FreeRTOS.h" +#include #include "esp_err.h" #ifdef __cplusplus @@ -23,9 +23,12 @@ extern "C" { #endif -typedef void (* bt_app_startup_cb_t)(void *param); - -esp_err_t esp_bt_startup(bt_app_startup_cb_t cb, void *ctx); +/** + * @brief Initialize BT controller + * + * This function should be called only once, before any other BT functions are called. + */ +void bt_controller_init(); /* @breif: vhci_host_callback * used for vhci call host function to notify what host need to do diff --git a/components/esp32/Kconfig b/components/esp32/Kconfig index c649a0e317..a43d16d2c6 100644 --- a/components/esp32/Kconfig +++ b/components/esp32/Kconfig @@ -80,33 +80,22 @@ config WIFI_ENABLED Temporarily, this option is not compatible with BT stack. -config WIFI_AUTO_STARTUP - bool "Start WiFi with system startup" - default "y" - depends on WIFI_ENABLED - help - By default, WiFi is started with system startup, you can turn off this - feature and start by yourself. - -config WIFI_AUTO_CONNECT - bool "Enable auto connect" - default "y" - depends on WIFI_ENABLED - help - If station is enabled, and station config is set, this will enable WiFi - station auto connect when WiFi startup. - config SYSTEM_EVENT_QUEUE_SIZE - int "system event queue size" + int "System event queue size" default 32 - depends on WIFI_ENABLED help Config system event queue size in different application. config SYSTEM_EVENT_TASK_STACK_SIZE - int "system event task stack size" + int "Event loop task stack size" default 2048 - depends on WIFI_ENABLED + help + Config system event task stack size in different application. + + +config MAIN_TASK_STACK_SIZE + int "Main task stack size" + default 4096 help Config system event task stack size in different application. diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 45a4bcec3c..f2cec84267 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -47,10 +47,7 @@ static void IRAM_ATTR user_start_cpu0(void); static void IRAM_ATTR call_user_start_cpu1(); static void IRAM_ATTR user_start_cpu1(void); extern void ets_setup_syscalls(void); -extern esp_err_t app_main(void *ctx); -#if CONFIG_BT_ENABLED -extern void bt_app_main(void *param); -#endif +extern int main(void); extern int _bss_start; extern int _bss_end; @@ -137,11 +134,17 @@ void IRAM_ATTR user_start_cpu1(void) static void do_global_ctors(void) { void (**p)(void); - for (p = &__init_array_start; p != &__init_array_end; ++p) { + for (p = &__init_array_end; p >= &__init_array_start; --p) { (*p)(); } } +static void mainTask(void* args) +{ + main(); + vTaskDelete(NULL); +} + void user_start_cpu0(void) { esp_set_cpu_freq(); // set CPU frequency configured in menuconfig @@ -150,28 +153,12 @@ void user_start_cpu0(void) do_global_ctors(); esp_ipc_init(); spi_flash_init(); - -#if CONFIG_WIFI_ENABLED - esp_err_t ret = nvs_flash_init(5, 3); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "nvs_flash_init failed, ret=%d", ret); - } - +#ifdef CONFIG_WIFI_ENABLED system_init(); - esp_event_init(NULL, NULL); - tcpip_adapter_init(); #endif - -#if CONFIG_WIFI_ENABLED && CONFIG_WIFI_AUTO_STARTUP -#include "esp_wifi.h" - esp_wifi_startup(app_main, NULL); -#elif CONFIG_BT_ENABLED -#include "bt.h" - esp_bt_startup(bt_app_main, NULL); -#else - app_main(NULL); -#endif - + xTaskCreatePinnedToCore(&mainTask, "mainTask", + ESP_TASK_MAIN_STACK, NULL, + ESP_TASK_MAIN_PRIO, NULL, 0); ESP_LOGI(TAG, "Starting scheduler on PRO CPU."); vTaskStartScheduler(); } diff --git a/components/esp32/include/esp_task.h b/components/esp32/include/esp_task.h index 58458106fa..0a68e5d919 100644 --- a/components/esp32/include/esp_task.h +++ b/components/esp32/include/esp_task.h @@ -51,10 +51,9 @@ /* idf task */ #define ESP_TASKD_EVENT_PRIO (ESP_TASK_PRIO_MAX - 5) #define ESP_TASKD_EVENT_STACK CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE -#define ESP_TASK_WIFI_STARTUP_PRIO (ESP_TASK_PRIO_MAX - 7) -#define ESP_TASK_WIFI_STARTUP_STACK 4096 #define ESP_TASK_TCPIP_PRIO (ESP_TASK_PRIO_MAX - 7) #define ESP_TASK_TCPIP_STACK 2048 -#define ESP_TASK_BT_INIT_PRIO (ESP_TASK_PRIO_MAX - 7) -#define ESP_TASK_BT_INIT_STACK 2048 +#define ESP_TASK_MAIN_PRIO (ESP_TASK_PRIO_MIN + 1) +#define ESP_TASK_MAIN_STACK CONFIG_MAIN_TASK_STACK_SIZE + #endif diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index a5b9d089ae..38b7bdac1c 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -136,27 +136,6 @@ typedef enum { WIFI_SECOND_CHAN_BELOW, /**< the channel width is HT40 and the second channel is below the primary channel */ } wifi_second_chan_t; -/** - * @brief startup WiFi driver and register application specific callback function - * - * @attention 1. This API should be called in application startup code to init WiFi driver - * @attention 2. The callback function is used to provide application specific WiFi configuration, - * such as, set the WiFi mode, register the event callback, set AP SSID etc before - * WiFi is startup - * @attention 3. Avoid to create application task in the callback, otherwise you may get wrong behavior - * @attention 4. If the callback return is not ESP_OK, the startup will fail! - * @attention 5. Before this API can be called, system_init()/esp_event_init()/tcpip_adapter_init() should - * be called firstly - * - * @param wifi_startup_cb_t cb : application specific callback function - * @param void *ctx : reserved for user - * - * @return ESP_OK : succeed - * @return others : fail - */ -typedef esp_err_t (* wifi_startup_cb_t)(void *ctx); - -esp_err_t esp_wifi_startup(wifi_startup_cb_t cb, void *ctx); typedef struct { void *event_q; /**< WiFi event q handler, it's a freeRTOS queue */ diff --git a/components/esp32/ld/esp32.rom.ld b/components/esp32/ld/esp32.rom.ld index 6f9064a398..ac1142f82d 100644 --- a/components/esp32/ld/esp32.rom.ld +++ b/components/esp32/ld/esp32.rom.ld @@ -445,7 +445,6 @@ PROVIDE ( _lseek_r = 0x4000bd8c ); PROVIDE ( __lshrdi3 = 0x4000c84c ); PROVIDE ( __ltdf2 = 0x40063790 ); PROVIDE ( __ltsf2 = 0x4006342c ); -PROVIDE ( main = 0x400076c4 ); PROVIDE ( malloc = 0x4000bea0 ); PROVIDE ( _malloc_r = 0x4000bbb4 ); PROVIDE ( maxSecretKey_256 = 0x3ff97448 ); @@ -1378,6 +1377,7 @@ PROVIDE ( rom_iq_est_disable = 0x40005590 ); PROVIDE ( rom_iq_est_enable = 0x40005514 ); PROVIDE ( rom_linear_to_db = 0x40005f64 ); PROVIDE ( rom_loopback_mode_en = 0x400030f8 ); +PROVIDE ( rom_main = 0x400076c4 ); PROVIDE ( rom_meas_tone_pwr_db = 0x40006004 ); PROVIDE ( rom_mhz2ieee = 0x4000404c ); PROVIDE ( rom_noise_floor_auto_set = 0x40003bdc ); diff --git a/components/esp32/wifi.c b/components/esp32/wifi.c deleted file mode 100644 index fd44d30d00..0000000000 --- a/components/esp32/wifi.c +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at - -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include -#include - -#include "esp_err.h" -#include "esp_wifi.h" -#include "esp_event.h" -#include "esp_task.h" - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/queue.h" -#include "freertos/semphr.h" - -#if CONFIG_WIFI_ENABLED - -static bool wifi_startup_flag = false; - -static wifi_startup_cb_t startup_cb; -static void *startup_ctx; - -#define WIFI_DEBUG(...) -#define WIFI_API_CALL_CHECK(info, api_call, ret) \ -do{\ - esp_err_t __err = (api_call);\ - if ((ret) != __err) {\ - WIFI_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\ - return __err;\ - }\ -} while(0) - - - -static void esp_wifi_task(void *pvParameters) -{ - esp_err_t err; - wifi_init_config_t cfg; - cfg.event_q = (xQueueHandle)esp_event_get_handler(); - - do { - err = esp_wifi_init(&cfg); - if (err != ESP_OK) { - WIFI_DEBUG("esp_wifi_init fail, ret=%d\n", err); - break; - } - - if (startup_cb) { - err = (*startup_cb)(startup_ctx); - if (err != ESP_OK) { - WIFI_DEBUG("startup_cb fail, ret=%d\n", err); - break; - } - } - - err = esp_wifi_start(); - if (err != ESP_OK) { - WIFI_DEBUG("esp_wifi_start fail, ret=%d\n", err); - break; - } - -#if CONFIG_WIFI_AUTO_CONNECT - wifi_mode_t mode; - bool auto_connect; - err = esp_wifi_get_mode(&mode); - if (err != ESP_OK) { - WIFI_DEBUG("esp_wifi_get_mode fail, ret=%d\n", err); - } - - err = esp_wifi_get_auto_connect(&auto_connect); - if ((mode == WIFI_MODE_STA || mode == WIFI_MODE_APSTA) && auto_connect) { - err = esp_wifi_connect(); - if (err != ESP_OK) { - WIFI_DEBUG("esp_wifi_connect fail, ret=%d\n", err); - break; - } - } -#endif - } while (0); - - if (err != ESP_OK) { - WIFI_DEBUG("wifi startup fail, deinit\n"); - esp_wifi_deinit(); - } - - vTaskDelete(NULL); -} - -esp_err_t esp_wifi_startup(wifi_startup_cb_t cb, void *ctx) -{ - if (wifi_startup_flag) { - return ESP_FAIL; - } - - startup_cb = cb; - startup_ctx = ctx; - - xTaskCreatePinnedToCore(esp_wifi_task, "wifiTask", ESP_TASK_WIFI_STARTUP_STACK, NULL, ESP_TASK_WIFI_STARTUP_PRIO, NULL, 0); - - return ESP_OK; -} -#endif diff --git a/examples/04_ble_adv/main/app_bt.c b/examples/04_ble_adv/main/app_bt.c index 011cf0c715..b2ffc77498 100755 --- a/examples/04_ble_adv/main/app_bt.c +++ b/examples/04_ble_adv/main/app_bt.c @@ -197,8 +197,9 @@ void bleAdvtTask(void *pvParameters) } } -void bt_app_main() +int main() { + bt_controller_init(); xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0); } From e1c782a206b37830560f8f43185400e996d24099 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 00:51:56 +0800 Subject: [PATCH 02/14] components/esp32,bt: fix typos in comments --- components/bt/include/bt.h | 8 ++++---- components/esp32/include/esp_event.h | 2 +- components/esp32/include/esp_task.h | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/components/bt/include/bt.h b/components/bt/include/bt.h index 0dc0424939..1e89f96aa1 100644 --- a/components/bt/include/bt.h +++ b/components/bt/include/bt.h @@ -30,7 +30,7 @@ extern "C" { */ void bt_controller_init(); -/* @breif: vhci_host_callback +/** @brief: vhci_host_callback * used for vhci call host function to notify what host need to do * * notify_host_send_available: notify host can send packet to controller @@ -42,20 +42,20 @@ typedef struct vhci_host_callback { int (*notify_host_recv)(uint8_t *data, uint16_t len); } vhci_host_callback_t; -/* @breif: API_vhci_host_check_send_available +/** @brief: API_vhci_host_check_send_available * used for check actively if the host can send packet to controller or not. * return true for ready to send, false means cannot send packet */ bool API_vhci_host_check_send_available(void); -/* @breif: API_vhci_host_send_packet +/** @brief: API_vhci_host_send_packet * host send packet to controller * param data is the packet point, the param len is the packet length * return void */ void API_vhci_host_send_packet(uint8_t *data, uint16_t len); -/* @breif: API_vhci_host_register_callback +/** @brief: API_vhci_host_register_callback * register the vhci referece callback, the call back * struct defined by vhci_host_callback structure. * param is the vhci_host_callback type variable diff --git a/components/esp32/include/esp_event.h b/components/esp32/include/esp_event.h index 0b61b70219..34358a2675 100644 --- a/components/esp32/include/esp_event.h +++ b/components/esp32/include/esp_event.h @@ -101,7 +101,7 @@ typedef union { } system_event_info_t; typedef struct { - system_event_id_t event_id; /**< even ID */ + system_event_id_t event_id; /**< event ID */ system_event_info_t event_info; /**< event information */ } system_event_t; diff --git a/components/esp32/include/esp_task.h b/components/esp32/include/esp_task.h index 0a68e5d919..bb028bf485 100644 --- a/components/esp32/include/esp_task.h +++ b/components/esp32/include/esp_task.h @@ -15,10 +15,10 @@ /* Notes: * 1. Put all task priority and stack size definition in this file * 2. If the task priority is less than 10, use ESP_TASK_PRIO_MIN + X style, - * otherwise use ESP_TASK_PRIO_MIN - X style - * 3. If this is a daemon task, the macro prifix is ESP_TASKD_, otherwise + * otherwise use ESP_TASK_PRIO_MAX - X style + * 3. If this is a daemon task, the macro prefix is ESP_TASKD_, otherwise * it's ESP_TASK_ - * 4. If the configMAX_PRIORITIES is modified, please make all prority are + * 4. If the configMAX_PRIORITIES is modified, please make all priority are * greater than 0 * 5. Make sure esp_task.h is consistent between wifi lib and idf */ From cc8dd46da27defdc04fd9ae419b4393b01845362 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Fri, 23 Sep 2016 14:46:39 +0800 Subject: [PATCH 03/14] clean up warnings For third party components (lwip and expat), compilation flags are adjusted to silence existing warnings (i have manually checked that all warnings are benign). In components/esp32, replaced use of WIFI_DEBUG with ESP_LOG functions. Only remaining warning is in FreeRTOS queue.c, and it may be a useful one. --- components/esp32/event.c | 84 ++++++++++++++++------------------- components/expat/component.mk | 2 +- components/lwip/component.mk | 2 +- 3 files changed, 41 insertions(+), 47 deletions(-) diff --git a/components/esp32/event.c b/components/esp32/event.c index c1d9d4e73b..b26a4ea304 100644 --- a/components/esp32/event.c +++ b/components/esp32/event.c @@ -27,22 +27,22 @@ #include "freertos/semphr.h" #include "tcpip_adapter.h" +#include "esp_log.h" #define ESP32_WORKAROUND 1 #if CONFIG_WIFI_ENABLED +static const char* TAG = "event"; static bool event_init_flag = false; static xQueueHandle g_event_handler = NULL; - static system_event_cb_t g_event_handler_cb; static void *g_event_ctx; -#define WIFI_DEBUG(...) #define WIFI_API_CALL_CHECK(info, api_call, ret) \ do{\ esp_err_t __err = (api_call);\ if ((ret) != __err) {\ - WIFI_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\ + ESP_LOGE(TAG, "%s %d %s ret=%d", __FUNCTION__, __LINE__, (info), __err);\ return __err;\ }\ } while(0) @@ -71,7 +71,7 @@ static system_event_handle_t g_system_event_handle_table[] = { {SYSTEM_EVENT_STA_CONNECTED, system_event_sta_connected_handle_default}, {SYSTEM_EVENT_STA_DISCONNECTED, system_event_sta_disconnected_handle_default}, {SYSTEM_EVENT_STA_AUTHMODE_CHANGE, NULL}, - {SYSTEM_EVENT_STA_GOT_IP, system_event_sta_got_ip_default}, + {SYSTEM_EVENT_STA_GOT_IP, system_event_sta_got_ip_default}, {SYSTEM_EVENT_AP_START, system_event_ap_start_handle_default}, {SYSTEM_EVENT_AP_STOP, system_event_ap_stop_handle_default}, {SYSTEM_EVENT_AP_STACONNECTED, NULL}, @@ -85,7 +85,7 @@ static esp_err_t system_event_sta_got_ip_default(system_event_t *event) extern esp_err_t esp_wifi_set_sta_ip(void); WIFI_API_CALL_CHECK("esp_wifi_set_sta_ip", esp_wifi_set_sta_ip(), ESP_OK); - printf("ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR "\n", + ESP_LOGI(TAG, "ip: " IPSTR ", mask: " IPSTR ", gw: " IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip), IP2STR(&event->event_info.got_ip.ip_info.netmask), IP2STR(&event->event_info.got_ip.ip_info.gw)); @@ -161,7 +161,7 @@ esp_err_t system_event_sta_connected_handle_default(system_event_t *event) esp_event_send(&evt); } else { - WIFI_DEBUG("invalid static ip\n"); + ESP_LOGE(TAG, "invalid static ip"); } } @@ -187,92 +187,86 @@ static esp_err_t esp_wifi_post_event_to_user(system_event_t *event) static esp_err_t esp_system_event_debug(system_event_t *event) { if (event == NULL) { - printf("Error: event is null!\n"); + ESP_LOGE(TAG, "event is null!"); return ESP_FAIL; } - WIFI_DEBUG("received event: "); switch (event->event_id) { case SYSTEM_EVENT_WIFI_READY: { - WIFI_DEBUG("SYSTEM_EVENT_WIFI_READY\n"); + ESP_LOGD(TAG, "SYSTEM_EVENT_WIFI_READY"); break; } case SYSTEM_EVENT_SCAN_DONE: { - system_event_sta_scan_done_t *scan_done; - scan_done = &event->event_info.scan_done; - WIFI_DEBUG("SYSTEM_EVENT_SCAN_DONE\nstatus:%d, number:%d\n", scan_done->status, scan_done->number); + system_event_sta_scan_done_t *scan_done = &event->event_info.scan_done; + ESP_LOGD(TAG, "SYSTEM_EVENT_SCAN_DONE, status:%d, number:%d", scan_done->status, scan_done->number); break; } case SYSTEM_EVENT_STA_START: { - WIFI_DEBUG("SYSTEM_EVENT_STA_START\n"); + ESP_LOGD(TAG, "SYSTEM_EVENT_STA_START"); break; } case SYSTEM_EVENT_STA_STOP: { - WIFI_DEBUG("SYSTEM_EVENT_STA_STOP\n"); + ESP_LOGD(TAG, "SYSTEM_EVENT_STA_STOP"); break; } case SYSTEM_EVENT_STA_CONNECTED: { - system_event_sta_connected_t *connected; - connected = &event->event_info.connected; - WIFI_DEBUG("SYSTEM_EVENT_STA_CONNECTED\nssid:%s, ssid_len:%d, bssid:%02x:%02x:%02x:%02x:%02x:%02x, channel:%d, authmode:%d\n", \ + system_event_sta_connected_t *connected = &event->event_info.connected; + ESP_LOGD(TAG, "SYSTEM_EVENT_STA_CONNECTED, ssid:%s, ssid_len:%d, bssid:%02x:%02x:%02x:%02x:%02x:%02x, channel:%d, authmode:%d", \ connected->ssid, connected->ssid_len, connected->bssid[0], connected->bssid[0], connected->bssid[1], \ connected->bssid[3], connected->bssid[4], connected->bssid[5], connected->channel, connected->authmode); break; } case SYSTEM_EVENT_STA_DISCONNECTED: { - system_event_sta_disconnected_t *disconnected; - disconnected = &event->event_info.disconnected; - WIFI_DEBUG("SYSTEM_EVENT_STA_DISCONNECTED\nssid:%s, ssid_len:%d, bssid:%02x:%02x:%02x:%02x:%02x:%02x, reason:%d\n", \ + system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected; + ESP_LOGD(TAG, "SYSTEM_EVENT_STA_DISCONNECTED, ssid:%s, ssid_len:%d, bssid:%02x:%02x:%02x:%02x:%02x:%02x, reason:%d", \ disconnected->ssid, disconnected->ssid_len, disconnected->bssid[0], disconnected->bssid[0], disconnected->bssid[1], \ disconnected->bssid[3], disconnected->bssid[4], disconnected->bssid[5], disconnected->reason); break; } case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: { - system_event_sta_authmode_change_t *auth_change; - auth_change = &event->event_info.auth_change; - WIFI_DEBUG("SYSTEM_EVENT_STA_AUTHMODE_CHNAGE\nold_mode:%d, new_mode:%d\n", auth_change->old_mode, auth_change->new_mode); + system_event_sta_authmode_change_t *auth_change = &event->event_info.auth_change; + ESP_LOGD(TAG, "SYSTEM_EVENT_STA_AUTHMODE_CHNAGE, old_mode:%d, new_mode:%d", auth_change->old_mode, auth_change->new_mode); break; } case SYSTEM_EVENT_STA_GOT_IP: { - system_event_sta_got_ip_t *got_ip; - got_ip = &event->event_info.got_ip; - WIFI_DEBUG("SYSTEM_EVENT_STA_GOTIP\n"); + system_event_sta_got_ip_t *got_ip = &event->event_info.got_ip; + ESP_LOGD(TAG, "SYSTEM_EVENT_STA_GOTIP, ip:" IPSTR ", mask:" IPSTR ", gw:" IPSTR, + IP2STR(&got_ip->ip_info.ip), + IP2STR(&got_ip->ip_info.netmask), + IP2STR(&got_ip->ip_info.gw)); break; } case SYSTEM_EVENT_AP_START: { - WIFI_DEBUG("SYSTEM_EVENT_AP_START\n"); + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_START"); break; } case SYSTEM_EVENT_AP_STOP: { - WIFI_DEBUG("SYSTEM_EVENT_AP_STOP\n"); + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STOP"); break; } case SYSTEM_EVENT_AP_STACONNECTED: { - system_event_ap_staconnected_t *staconnected; - staconnected = &event->event_info.sta_connected; - WIFI_DEBUG("SYSTEM_EVENT_AP_STACONNECTED\nmac:%02x:%02x:%02x:%02x:%02x:%02x, aid:%d\n", \ + system_event_ap_staconnected_t *staconnected = &event->event_info.sta_connected; + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STACONNECTED, mac:%02x:%02x:%02x:%02x:%02x:%02x, aid:%d", \ staconnected->mac[0], staconnected->mac[0], staconnected->mac[1], \ staconnected->mac[3], staconnected->mac[4], staconnected->mac[5], staconnected->aid); break; } case SYSTEM_EVENT_AP_STADISCONNECTED: { - system_event_ap_stadisconnected_t *stadisconnected; - stadisconnected = &event->event_info.sta_disconnected; - WIFI_DEBUG("SYSTEM_EVENT_AP_STADISCONNECTED\nmac:%02x:%02x:%02x:%02x:%02x:%02x, aid:%d\n", \ + system_event_ap_stadisconnected_t *stadisconnected = &event->event_info.sta_disconnected; + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_STADISCONNECTED, mac:%02x:%02x:%02x:%02x:%02x:%02x, aid:%d", \ stadisconnected->mac[0], stadisconnected->mac[0], stadisconnected->mac[1], \ stadisconnected->mac[3], stadisconnected->mac[4], stadisconnected->mac[5], stadisconnected->aid); break; } case SYSTEM_EVENT_AP_PROBEREQRECVED: { - system_event_ap_probe_req_rx_t *ap_probereqrecved; - ap_probereqrecved = &event->event_info.ap_probereqrecved; - WIFI_DEBUG("SYSTEM_EVENT_AP_PROBEREQRECVED\nrssi:%d, mac:%02x:%02x:%02x:%02x:%02x:%02x\n", \ + system_event_ap_probe_req_rx_t *ap_probereqrecved = &event->event_info.ap_probereqrecved; + ESP_LOGD(TAG, "SYSTEM_EVENT_AP_PROBEREQRECVED, rssi:%d, mac:%02x:%02x:%02x:%02x:%02x:%02x", \ ap_probereqrecved->rssi, ap_probereqrecved->mac[0], ap_probereqrecved->mac[0], ap_probereqrecved->mac[1], \ ap_probereqrecved->mac[3], ap_probereqrecved->mac[4], ap_probereqrecved->mac[5]); break; } default: { - printf("Error: no such kind of event!\n"); + ESP_LOGW(TAG, "no such kind of event!"); break; } } @@ -283,19 +277,19 @@ static esp_err_t esp_system_event_debug(system_event_t *event) static esp_err_t esp_system_event_handler(system_event_t *event) { if (event == NULL) { - printf("Error: event is null!\n"); + ESP_LOGE(TAG, "Error: event is null!"); return ESP_FAIL; } esp_system_event_debug(event); if ((event->event_id < SYSTEM_EVENT_MAX) && (event->event_id == g_system_event_handle_table[event->event_id].event_id)) { if (g_system_event_handle_table[event->event_id].event_handle) { - WIFI_DEBUG("enter default callback\n"); + ESP_LOGV(TAG, "enter default callback"); g_system_event_handle_table[event->event_id].event_handle(event); - WIFI_DEBUG("exit default callback\n"); + ESP_LOGV(TAG, "exit default callback"); } } else { - printf("mismatch or invalid event, id=%d\n", event->event_id); + ESP_LOGE(TAG, "mismatch or invalid event, id=%d", event->event_id); } return esp_wifi_post_event_to_user(event); @@ -310,7 +304,7 @@ static void esp_system_event_task(void *pvParameters) if (xQueueReceive(g_event_handler, &evt, portMAX_DELAY) == pdPASS) { ret = esp_system_event_handler(&evt); if (ret == ESP_FAIL) { - printf("esp wifi post event to user fail!\n"); + ESP_LOGE(TAG, "post event to user fail!"); } } } @@ -334,9 +328,9 @@ esp_err_t esp_event_send(system_event_t *event) if (pdPASS != ret) { if (event) { - printf("e=%d f\n", event->event_id); + ESP_LOGE(TAG, "e=%d f", event->event_id); } else { - printf("e null\n"); + ESP_LOGE(TAG, "e null"); } return ESP_FAIL; } diff --git a/components/expat/component.mk b/components/expat/component.mk index 69595d7b27..907b358a87 100644 --- a/components/expat/component.mk +++ b/components/expat/component.mk @@ -10,6 +10,6 @@ COMPONENT_ADD_INCLUDEDIRS := port/include include/expat COMPONENT_SRCDIRS := library port -CFLAGS += -Wno-error=address -Waddress -DHAVE_EXPAT_CONFIG_H +CFLAGS += -Wno-unused-function -DHAVE_EXPAT_CONFIG_H include $(IDF_PATH)/make/component_common.mk diff --git a/components/lwip/component.mk b/components/lwip/component.mk index 3e6b26c0f5..5d15020047 100644 --- a/components/lwip/component.mk +++ b/components/lwip/component.mk @@ -6,6 +6,6 @@ COMPONENT_ADD_INCLUDEDIRS := include/lwip include/lwip/port include/lwip/posix COMPONENT_SRCDIRS := api apps/sntp apps core/ipv4 core/ipv6 core netif port/freertos port/netif port -CFLAGS += -Wno-error=address -Waddress +CFLAGS += -Wno-address -Wno-unused-variable -Wno-unused-but-set-variable include $(IDF_PATH)/make/component_common.mk From 53de9f115fa78a3c06c858ff9daf1ced53611efb Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 01:17:32 +0800 Subject: [PATCH 04/14] Event handling refactoring This change separates definitions in esp_event.h and functions in event.c into several parts: - event structure definitions (esp_event.h) - default implementations of event handlers (event_default_handlers.c) - default implementation of event loop (event_loop.c, esp_event_loop.h) Purpose of this change is to allow applications choose their own poison: - full control of event loop at the expense of more bootstrap code - pre-defined event task firing event callbacks, but less code in app_main.c --- .../{event.c => event_default_handlers.c} | 86 +------------ components/esp32/event_loop.c | 118 ++++++++++++++++++ components/esp32/include/esp_event.h | 50 ++------ components/esp32/include/esp_event_loop.h | 81 ++++++++++++ 4 files changed, 211 insertions(+), 124 deletions(-) rename components/esp32/{event.c => event_default_handlers.c} (84%) create mode 100644 components/esp32/event_loop.c create mode 100644 components/esp32/include/esp_event_loop.h diff --git a/components/esp32/event.c b/components/esp32/event_default_handlers.c similarity index 84% rename from components/esp32/event.c rename to components/esp32/event_default_handlers.c index b26a4ea304..37ba634041 100644 --- a/components/esp32/event.c +++ b/components/esp32/event_default_handlers.c @@ -19,6 +19,7 @@ #include "esp_err.h" #include "esp_wifi.h" #include "esp_event.h" +#include "esp_event_loop.h" #include "esp_task.h" #include "freertos/FreeRTOS.h" @@ -29,14 +30,7 @@ #include "tcpip_adapter.h" #include "esp_log.h" -#define ESP32_WORKAROUND 1 - -#if CONFIG_WIFI_ENABLED -static const char* TAG = "event"; -static bool event_init_flag = false; -static xQueueHandle g_event_handler = NULL; -static system_event_cb_t g_event_handler_cb; -static void *g_event_ctx; +const char* TAG = "event"; #define WIFI_API_CALL_CHECK(info, api_call, ret) \ do{\ @@ -175,15 +169,6 @@ esp_err_t system_event_sta_disconnected_handle_default(system_event_t *event) return ESP_OK; } -static esp_err_t esp_wifi_post_event_to_user(system_event_t *event) -{ - if (g_event_handler_cb) { - return (*g_event_handler_cb)(g_event_ctx, event); - } - - return ESP_OK; -} - static esp_err_t esp_system_event_debug(system_event_t *event) { if (event == NULL) { @@ -274,7 +259,7 @@ static esp_err_t esp_system_event_debug(system_event_t *event) return ESP_OK; } -static esp_err_t esp_system_event_handler(system_event_t *event) +esp_err_t esp_event_process_default(system_event_t *event) { if (event == NULL) { ESP_LOGE(TAG, "Error: event is null!"); @@ -290,72 +275,7 @@ static esp_err_t esp_system_event_handler(system_event_t *event) } } else { ESP_LOGE(TAG, "mismatch or invalid event, id=%d", event->event_id); - } - - return esp_wifi_post_event_to_user(event); -} - -static void esp_system_event_task(void *pvParameters) -{ - system_event_t evt; - esp_err_t ret; - - while (1) { - if (xQueueReceive(g_event_handler, &evt, portMAX_DELAY) == pdPASS) { - ret = esp_system_event_handler(&evt); - if (ret == ESP_FAIL) { - ESP_LOGE(TAG, "post event to user fail!"); - } - } - } -} - -system_event_cb_t esp_event_set_cb(system_event_cb_t cb, void *ctx) -{ - system_event_cb_t old_cb = g_event_handler_cb; - - g_event_handler_cb = cb; - g_event_ctx = ctx; - - return old_cb; -} - -esp_err_t esp_event_send(system_event_t *event) -{ - portBASE_TYPE ret; - - ret = xQueueSendToBack((xQueueHandle)g_event_handler, event, 0); - - if (pdPASS != ret) { - if (event) { - ESP_LOGE(TAG, "e=%d f", event->event_id); - } else { - ESP_LOGE(TAG, "e null"); - } return ESP_FAIL; } - return ESP_OK; } - -void *esp_event_get_handler(void) -{ - return (void *)g_event_handler; -} - -esp_err_t esp_event_init(system_event_cb_t cb, void *ctx) -{ - if (event_init_flag) { - return ESP_FAIL; - } - - g_event_handler_cb = cb; - g_event_ctx = ctx; - - g_event_handler = xQueueCreate(CONFIG_SYSTEM_EVENT_QUEUE_SIZE, sizeof(system_event_t)); - - xTaskCreatePinnedToCore(esp_system_event_task, "eventTask", ESP_TASKD_EVENT_STACK, NULL, ESP_TASKD_EVENT_PRIO, NULL, 0); - return ESP_OK; -} - -#endif diff --git a/components/esp32/event_loop.c b/components/esp32/event_loop.c new file mode 100644 index 0000000000..be00e34be4 --- /dev/null +++ b/components/esp32/event_loop.c @@ -0,0 +1,118 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include "esp_err.h" +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_event_loop.h" +#include "esp_task.h" + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "freertos/semphr.h" + +#include "tcpip_adapter.h" +#include "esp_log.h" +#include "sdkconfig.h" + + +static const char* TAG = "event"; +static bool s_event_init_flag = false; +static QueueHandle_t s_event_queue = NULL; +static system_event_cb_t s_event_handler_cb = NULL; +static void *s_event_ctx = NULL; + +static esp_err_t esp_wifi_post_event_to_user(system_event_t *event) +{ + if (s_event_handler_cb) { + return (*s_event_handler_cb)(s_event_ctx, event); + } + return ESP_OK; +} + +static void esp_system_event_task(void *pvParameters) +{ + system_event_t evt; + esp_err_t ret; + + while (1) { + if (xQueueReceive(s_event_queue, &evt, portMAX_DELAY) == pdPASS) { + ret = esp_event_process_default(&evt); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "default event handler failed!"); + } + ret = esp_wifi_post_event_to_user(&evt); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "post event to user fail!"); + } + } + } +} + +system_event_cb_t esp_event_set_cb(system_event_cb_t cb, void *ctx) +{ + system_event_cb_t old_cb = s_event_handler_cb; + + s_event_handler_cb = cb; + s_event_ctx = ctx; + + return old_cb; +} + +esp_err_t esp_event_send(system_event_t *event) +{ + portBASE_TYPE ret; + + ret = xQueueSendToBack(s_event_queue, event, 0); + + if (pdPASS != ret) { + if (event) { + ESP_LOGE(TAG, "e=%d f", event->event_id); + } else { + ESP_LOGE(TAG, "e null"); + } + return ESP_FAIL; + } + + return ESP_OK; +} + +QueueHandle_t esp_event_loop_get_queue(void) +{ + return s_event_queue; +} + +esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx) +{ + if (s_event_init_flag) { + return ESP_FAIL; + } + + s_event_handler_cb = cb; + s_event_ctx = ctx; + + s_event_queue = xQueueCreate(CONFIG_SYSTEM_EVENT_QUEUE_SIZE, sizeof(system_event_t)); + + xTaskCreatePinnedToCore(esp_system_event_task, "eventTask", + ESP_TASKD_EVENT_STACK, NULL, ESP_TASKD_EVENT_PRIO, NULL, 0); + + s_event_init_flag = true; + return ESP_OK; +} + diff --git a/components/esp32/include/esp_event.h b/components/esp32/include/esp_event.h index 34358a2675..86afab2c40 100644 --- a/components/esp32/include/esp_event.h +++ b/components/esp32/include/esp_event.h @@ -105,30 +105,6 @@ typedef struct { system_event_info_t event_info; /**< event information */ } system_event_t; -/** - * @brief Application specified event callback function - * - * @param void *ctx : reserved for user - * @param system_event_t *event : event type defined in this file - * - * @return ESP_OK : succeed - * @return others : fail - */ -typedef esp_err_t (*system_event_cb_t)(void *ctx, system_event_t *event); - -/** - * @brief Set application specified event callback function - * - * @attention 1. If cb is NULL, means application don't need to handle - * If cb is not NULL, it will be call when an event is received, after the default event callback is completed - * - * @param system_event_cb_t cb : callback - * @param void *ctx : reserved for user - * - * @return system_event_cb_t : old callback - */ -system_event_cb_t esp_event_set_cb(system_event_cb_t cb, void *ctx); - /** * @brief Send a event to event task * @@ -142,28 +118,20 @@ system_event_cb_t esp_event_set_cb(system_event_cb_t cb, void *ctx); esp_err_t esp_event_send(system_event_t *event); /** - * @brief Get the event handler + * @brief Default event handler for system events * - * @attention : currently this API returns event queue handler, by this event queue, - * users can notice when WiFi has done something like scanning done, connected to AP or disconnected from AP. + * This function performs default handling of system events. + * When using esp_event_loop APIs, it is called automatically before invoking the user-provided + * callback function. * - * @param null + * Applications which implement a custom event loop must call this function + * as part of event processing. * - * @return void * : event queue pointer + * @param event pointer to event to be handled + * @return ESP_OK if an event was handled successfully */ -void *esp_event_get_handler(void); +esp_err_t esp_event_process_default(system_event_t *event); -/** - * @brief Init the event module - * Create the event handler and task - * - * @param system_event_cb_t cb : application specified event callback, it can be modified by call esp_event_set_cb - * @param void *ctx : reserved for user - * - * @return ESP_OK : succeed - * @return others : fail - */ -esp_err_t esp_event_init(system_event_cb_t cb, void *ctx); #ifdef __cplusplus } diff --git a/components/esp32/include/esp_event_loop.h b/components/esp32/include/esp_event_loop.h new file mode 100644 index 0000000000..97672aedf2 --- /dev/null +++ b/components/esp32/include/esp_event_loop.h @@ -0,0 +1,81 @@ +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef __ESP_EVENT_LOOP_H__ +#define __ESP_EVENT_LOOP_H__ + +#include +#include + +#include "esp_err.h" +#include "esp_event.h" +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Application specified event callback function + * + * @param void *ctx : reserved for user + * @param system_event_t *event : event type defined in this file + * + * @return ESP_OK : succeed + * @return others : fail + */ +typedef esp_err_t (*system_event_cb_t)(void *ctx, system_event_t *event); + +/** + * @brief Initialize event loop + * Create the event handler and task + * + * @param system_event_cb_t cb : application specified event callback, it can be modified by call esp_event_set_cb + * @param void *ctx : reserved for user + * + * @return ESP_OK : succeed + * @return others : fail + */ +esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx); + +/** + * @brief Set application specified event callback function + * + * @attention 1. If cb is NULL, means application don't need to handle + * If cb is not NULL, it will be call when an event is received, after the default event callback is completed + * + * @param system_event_cb_t cb : callback + * @param void *ctx : reserved for user + * + * @return system_event_cb_t : old callback + */ +system_event_cb_t esp_event_loop_set_cb(system_event_cb_t cb, void *ctx); + +/** + * @brief Get the queue used by event loop + * + * @attention : currently this API is used to initialize "q" parameter + * of wifi_init structure. + * + * @return QueueHandle_t : event queue handle + */ +QueueHandle_t esp_event_loop_get_queue(void); + + +#ifdef __cplusplus +} +#endif + +#endif /* __ESP_EVENT_LOOP_H__ */ From e9b54b6b456c103f7d95b6a033d7c8c65e8aae85 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 01:34:05 +0800 Subject: [PATCH 05/14] components/esp32: add ESP_ERROR_CHECK Convenience macro to do error check and assert in cases when error recovery is not expected --- components/esp32/include/esp_err.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/esp32/include/esp_err.h b/components/esp32/include/esp_err.h index 6ea176991c..4f013f91ab 100644 --- a/components/esp32/include/esp_err.h +++ b/components/esp32/include/esp_err.h @@ -15,6 +15,7 @@ #define __ESP_ERR_H__ #include +#include #ifdef __cplusplus extern "C" { @@ -31,6 +32,12 @@ typedef int32_t esp_err_t; #define ESP_ERR_INVALID_ARG 0x102 #define ESP_ERR_INVALID_STATE 0x103 +/** + * Macro which can be used to check the error code, + * and terminate the program in case the code is not ESP_OK. + * Prints the failed statement to serial output. + */ +#define ESP_ERROR_CHECK(x) do { esp_err_t rc = (x); if (rc != ESP_OK) { assert(0 && #x);} } while(0); #ifdef __cplusplus } From 5a762d9eeebff7ea191c9bd00bcd9446673f3506 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 01:35:31 +0800 Subject: [PATCH 06/14] components/esp32: clarify type of queue in wifi_init_config_t, add default init macro --- components/esp32/include/esp_wifi.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/esp32/include/esp_wifi.h b/components/esp32/include/esp_wifi.h index 38b7bdac1c..32d5cd0f67 100644 --- a/components/esp32/include/esp_wifi.h +++ b/components/esp32/include/esp_wifi.h @@ -59,7 +59,8 @@ #include #include - +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" #include "esp_err.h" #include "rom/queue.h" @@ -138,13 +139,22 @@ typedef enum { typedef struct { - void *event_q; /**< WiFi event q handler, it's a freeRTOS queue */ + QueueHandle_t event_queue; /**< WiFi event queue handle */ uint8_t rx_ba_win; /**< TBC */ uint8_t tx_ba_win; /**< TBC */ uint8_t rx_buf_cnt; /**< TBC */ uint8_t tx_buf_cnt; /**< TBC */ } wifi_init_config_t; + +#define WIFI_INIT_CONFIG_DEFAULT(event_queue_) { \ + .event_queue = event_queue_, \ + .rx_ba_win = 0, \ + .tx_ba_win = 0, \ + .rx_buf_cnt = 0, \ + .tx_buf_cnt = 0 \ +}; + /** * @brief Init WiFi * Alloc resource for WiFi driver, such as WiFi control structure, RX/TX buffer, From 10c69514b75151037560dea3ef26a67ce687042b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 01:45:12 +0800 Subject: [PATCH 07/14] components/bt: fix compilation, remove ./ from makefile --- components/bt/bt.c | 3 --- components/bt/component.mk | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/components/bt/bt.c b/components/bt/bt.c index d9bc5ae089..addea46dc4 100644 --- a/components/bt/bt.c +++ b/components/bt/bt.c @@ -36,9 +36,6 @@ extern void btdm_osi_funcs_register(void *osi_funcs); extern void btdm_controller_init(void); -static bt_app_startup_cb_t app_startup_cb; -static void *app_startup_ctx; - #define BT_DEBUG(...) #define BT_API_CALL_CHECK(info, api_call, ret) \ do{\ diff --git a/components/bt/component.mk b/components/bt/component.mk index 110d022672..8290f9f4a4 100644 --- a/components/bt/component.mk +++ b/components/bt/component.mk @@ -6,7 +6,7 @@ CURRENT_DIR=$(IDF_PATH)/components/bt -COMPONENT_ADD_INCLUDEDIRS := ./include +COMPONENT_ADD_INCLUDEDIRS := include CFLAGS += -Wno-error=unused-label -Wno-error=return-type -Wno-error=missing-braces -Wno-error=pointer-sign -Wno-error=parentheses @@ -20,6 +20,4 @@ COMPONENT_ADD_LDFLAGS := -lbt -L$(abspath lib) \ ALL_LIB_FILES := $(patsubst %,$(COMPONENT_PATH)/lib/lib%.a,$(LIBS)) $(COMPONENT_LIBRARY): $(ALL_LIB_FILES) -COMPONENT_SRCDIRS := ./ - include $(IDF_PATH)/make/component_common.mk From b190dc3e9ff85453235a638931a9314352d72fe2 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 03:05:25 +0800 Subject: [PATCH 08/14] components/lwip,esp32: fixes for C++ - put contents of a few headers into c++ guard blocks - fix off-by-one error in do_global_ctors - remove system_init from startup code (should be called from main) --- components/esp32/cpu_start.c | 5 +---- components/lwip/include/lwip/port/arch/sys_arch.h | 12 +++++++++++- components/lwip/include/lwip/port/netif/wlanif.h | 8 ++++++++ components/tcpip_adapter/include/tcpip_adapter.h | 10 ++++++++-- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index f2cec84267..5566d978a0 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -134,7 +134,7 @@ void IRAM_ATTR user_start_cpu1(void) static void do_global_ctors(void) { void (**p)(void); - for (p = &__init_array_end; p >= &__init_array_start; --p) { + for (p = &__init_array_end - 1; p >= &__init_array_start; --p) { (*p)(); } } @@ -153,9 +153,6 @@ void user_start_cpu0(void) do_global_ctors(); esp_ipc_init(); spi_flash_init(); -#ifdef CONFIG_WIFI_ENABLED - system_init(); -#endif xTaskCreatePinnedToCore(&mainTask, "mainTask", ESP_TASK_MAIN_STACK, NULL, ESP_TASK_MAIN_PRIO, NULL, 0); diff --git a/components/lwip/include/lwip/port/arch/sys_arch.h b/components/lwip/include/lwip/port/arch/sys_arch.h index be8ff72260..a863348256 100644 --- a/components/lwip/include/lwip/port/arch/sys_arch.h +++ b/components/lwip/include/lwip/port/arch/sys_arch.h @@ -38,6 +38,11 @@ #include "freertos/queue.h" #include "freertos/semphr.h" +#ifdef __cplusplus +extern "C" { +#endif + + typedef xSemaphoreHandle sys_sem_t; typedef xSemaphoreHandle sys_mutex_t; typedef xTaskHandle sys_thread_t; @@ -67,6 +72,11 @@ uint32_t system_get_time(void); void sys_delay_ms(uint32_t ms); sys_sem_t* sys_thread_sem_init(void); void sys_thread_sem_deinit(void); -sys_sem_t* sys_thread_sem_get(void); +sys_sem_t* sys_thread_sem_get(void); + +#ifdef __cplusplus +} +#endif + #endif /* __SYS_ARCH_H__ */ diff --git a/components/lwip/include/lwip/port/netif/wlanif.h b/components/lwip/include/lwip/port/netif/wlanif.h index f9e322ebda..7eb303eab4 100755 --- a/components/lwip/include/lwip/port/netif/wlanif.h +++ b/components/lwip/include/lwip/port/netif/wlanif.h @@ -10,6 +10,10 @@ #include "lwip/err.h" +#ifdef __cplusplus +extern "C" { +#endif + err_t wlanif_init(struct netif *netif); void wlanif_input(struct netif *netif, void *buffer, u16_t len, void* eb); @@ -20,4 +24,8 @@ wifi_interface_t wifi_get_interface(void *dev); void netif_reg_addr_change_cb(void* cb); +#ifdef __cplusplus +} +#endif + #endif /* _WLAN_LWIP_IF_H_ */ diff --git a/components/tcpip_adapter/include/tcpip_adapter.h b/components/tcpip_adapter/include/tcpip_adapter.h index 9cae530eff..51fb233d81 100644 --- a/components/tcpip_adapter/include/tcpip_adapter.h +++ b/components/tcpip_adapter/include/tcpip_adapter.h @@ -16,9 +16,7 @@ #define _TCPIP_ADAPTER_H_ #include - #include "rom/queue.h" - #include "esp_wifi.h" #define CONFIG_TCPIP_LWIP 1 @@ -28,6 +26,10 @@ #include "lwip/ip_addr.h" #include "apps/dhcpserver.h" +#ifdef __cplusplus +extern "C" { +#endif + #define IP2STR(ipaddr) ip4_addr1_16(ipaddr), \ ip4_addr2_16(ipaddr), \ ip4_addr3_16(ipaddr), \ @@ -129,5 +131,9 @@ wifi_interface_t tcpip_adapter_get_wifi_if(void *dev); esp_err_t tcpip_adapter_get_sta_list(struct station_info *sta_info, struct station_list **sta_list); esp_err_t tcpip_adapter_free_sta_list(struct station_list *sta_list); +#ifdef __cplusplus +} +#endif + #endif /* _TCPIP_ADAPTER_H_ */ From 62aaec630c2d60ce108f3503c0e3841b9c720998 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 12:29:00 +0800 Subject: [PATCH 09/14] components/esp32: remove "_user" part from entry points, weaken start_cpu0/1 With this change applications can override very early part of startup procedure by implementing "start_cpu0" and "start_cpu1" functions. --- components/esp32/cpu_start.c | 57 +++++++++++++++-------------- components/esp32/ld/esp32.common.ld | 2 +- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 5566d978a0..76a5e6a1c5 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -43,9 +43,13 @@ #include "esp_ipc.h" #include "esp_log.h" -static void IRAM_ATTR user_start_cpu0(void); -static void IRAM_ATTR call_user_start_cpu1(); -static void IRAM_ATTR user_start_cpu1(void); +void start_cpu0(void) __attribute__((weak, alias("start_cpu0_default"))); +void start_cpu1(void) __attribute__((weak, alias("start_cpu0_default"))); +void start_cpu0_default(void) IRAM_ATTR; +void start_cpu1_default(void) IRAM_ATTR; +static void IRAM_ATTR call_start_cpu1(); +static void do_global_ctors(void); +static void main_task(void* args); extern void ets_setup_syscalls(void); extern int main(void); @@ -64,7 +68,7 @@ static bool app_cpu_started = false; * and the app CPU is in reset. We do have a stack, so we can do the initialization in C. */ -void IRAM_ATTR call_user_start_cpu0() +void IRAM_ATTR call_start_cpu0() { //Kill wdt REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN); @@ -88,13 +92,13 @@ void IRAM_ATTR call_user_start_cpu0() ESP_EARLY_LOGI(TAG, "Pro cpu up."); #ifndef CONFIG_FREERTOS_UNICORE - ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_user_start_cpu1); + ESP_EARLY_LOGI(TAG, "Starting app cpu, entry point is %p", call_start_cpu1); SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_C_REG, DPORT_APPCPU_RUNSTALL); SET_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING); CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_A_REG, DPORT_APPCPU_RESETTING); - ets_set_appcpu_boot_addr((uint32_t)call_user_start_cpu1); + ets_set_appcpu_boot_addr((uint32_t)call_start_cpu1); while (!app_cpu_started) { ets_delay_us(100); @@ -104,11 +108,10 @@ void IRAM_ATTR call_user_start_cpu0() CLEAR_PERI_REG_MASK(DPORT_APPCPU_CTRL_B_REG, DPORT_APPCPU_CLKGATE_EN); #endif ESP_EARLY_LOGI(TAG, "Pro cpu start user code"); - user_start_cpu0(); + start_cpu0(); } - -void IRAM_ATTR call_user_start_cpu1() +void IRAM_ATTR call_start_cpu1() { asm volatile (\ "wsr %0, vecbase\n" \ @@ -118,10 +121,25 @@ void IRAM_ATTR call_user_start_cpu1() ESP_EARLY_LOGI(TAG, "App cpu up."); app_cpu_started = 1; - user_start_cpu1(); + start_cpu1(); } -void IRAM_ATTR user_start_cpu1(void) +void start_cpu0_default(void) +{ + esp_set_cpu_freq(); // set CPU frequency configured in menuconfig + uart_div_modify(0, (APB_CLK_FREQ << 4) / 115200); + ets_setup_syscalls(); + do_global_ctors(); + esp_ipc_init(); + spi_flash_init(); + xTaskCreatePinnedToCore(&main_task, "main", + ESP_TASK_MAIN_STACK, NULL, + ESP_TASK_MAIN_PRIO, NULL, 0); + ESP_LOGI(TAG, "Starting scheduler on PRO CPU."); + vTaskStartScheduler(); +} + +void start_cpu1_default(void) { // Wait for FreeRTOS initialization to finish on PRO CPU while (port_xSchedulerRunning[0] == 0) { @@ -139,24 +157,9 @@ static void do_global_ctors(void) } } -static void mainTask(void* args) +static void main_task(void* args) { main(); vTaskDelete(NULL); } -void user_start_cpu0(void) -{ - esp_set_cpu_freq(); // set CPU frequency configured in menuconfig - uart_div_modify(0, (APB_CLK_FREQ << 4) / 115200); - ets_setup_syscalls(); - do_global_ctors(); - esp_ipc_init(); - spi_flash_init(); - xTaskCreatePinnedToCore(&mainTask, "mainTask", - ESP_TASK_MAIN_STACK, NULL, - ESP_TASK_MAIN_PRIO, NULL, 0); - ESP_LOGI(TAG, "Starting scheduler on PRO CPU."); - vTaskStartScheduler(); -} - diff --git a/components/esp32/ld/esp32.common.ld b/components/esp32/ld/esp32.common.ld index 45bcc24f47..3fb4ca761c 100644 --- a/components/esp32/ld/esp32.common.ld +++ b/components/esp32/ld/esp32.common.ld @@ -1,5 +1,5 @@ /* Default entry point: */ -ENTRY(call_user_start_cpu0); +ENTRY(call_start_cpu0); SECTIONS { From 890fadc394a4e79d7dce1a50b15c891e94798082 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 12:35:09 +0800 Subject: [PATCH 10/14] components/esp32: fix renaming of esp_event_set_cb, minor clean up --- components/esp32/event_loop.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/components/esp32/event_loop.c b/components/esp32/event_loop.c index be00e34be4..4b6e21fe09 100644 --- a/components/esp32/event_loop.c +++ b/components/esp32/event_loop.c @@ -27,7 +27,6 @@ #include "freertos/queue.h" #include "freertos/semphr.h" -#include "tcpip_adapter.h" #include "esp_log.h" #include "sdkconfig.h" @@ -38,7 +37,7 @@ static QueueHandle_t s_event_queue = NULL; static system_event_cb_t s_event_handler_cb = NULL; static void *s_event_ctx = NULL; -static esp_err_t esp_wifi_post_event_to_user(system_event_t *event) +static esp_err_t esp_event_post_to_user(system_event_t *event) { if (s_event_handler_cb) { return (*s_event_handler_cb)(s_event_ctx, event); @@ -46,18 +45,16 @@ static esp_err_t esp_wifi_post_event_to_user(system_event_t *event) return ESP_OK; } -static void esp_system_event_task(void *pvParameters) +static void esp_event_loop_task(void *pvParameters) { - system_event_t evt; - esp_err_t ret; - while (1) { + system_event_t evt; if (xQueueReceive(s_event_queue, &evt, portMAX_DELAY) == pdPASS) { - ret = esp_event_process_default(&evt); + esp_err_t ret = esp_event_process_default(&evt); if (ret != ESP_OK) { ESP_LOGE(TAG, "default event handler failed!"); } - ret = esp_wifi_post_event_to_user(&evt); + ret = esp_event_post_to_user(&evt); if (ret != ESP_OK) { ESP_LOGE(TAG, "post event to user fail!"); } @@ -65,23 +62,18 @@ static void esp_system_event_task(void *pvParameters) } } -system_event_cb_t esp_event_set_cb(system_event_cb_t cb, void *ctx) +system_event_cb_t esp_event_loop_set_cb(system_event_cb_t cb, void *ctx) { system_event_cb_t old_cb = s_event_handler_cb; - s_event_handler_cb = cb; s_event_ctx = ctx; - return old_cb; } esp_err_t esp_event_send(system_event_t *event) { - portBASE_TYPE ret; - - ret = xQueueSendToBack(s_event_queue, event, 0); - - if (pdPASS != ret) { + portBASE_TYPE ret = xQueueSendToBack(s_event_queue, event, 0); + if (ret != pdPASS) { if (event) { ESP_LOGE(TAG, "e=%d f", event->event_id); } else { @@ -89,7 +81,6 @@ esp_err_t esp_event_send(system_event_t *event) } return ESP_FAIL; } - return ESP_OK; } @@ -103,13 +94,11 @@ esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx) if (s_event_init_flag) { return ESP_FAIL; } - s_event_handler_cb = cb; s_event_ctx = ctx; - s_event_queue = xQueueCreate(CONFIG_SYSTEM_EVENT_QUEUE_SIZE, sizeof(system_event_t)); - xTaskCreatePinnedToCore(esp_system_event_task, "eventTask", + xTaskCreatePinnedToCore(esp_event_loop_task, "eventTask", ESP_TASKD_EVENT_STACK, NULL, ESP_TASKD_EVENT_PRIO, NULL, 0); s_event_init_flag = true; From 123788c1ab8263510f20bd5dc8e81eed26f6d74d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 14:17:31 +0800 Subject: [PATCH 11/14] gitlab-ci: build SSC with matching branch name, if available --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7233d9ac4d..42f24cd033 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -64,6 +64,7 @@ build_ssc: script: - git clone ssh://git@gitlab.espressif.cn:27227/yinling/SSC.git - cd SSC + - git checkout ${CI_BUILD_REF_NAME} || echo "Using SSC default branch..." - make defconfig - chmod +x gen_misc_ng.sh - ./gen_misc_ng.sh From 7cef0308dc4eb18a09bcd3e4f8bcb7223599de64 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 14:48:41 +0800 Subject: [PATCH 12/14] Change application entry point name back to app_main --- components/esp32/cpu_start.c | 4 ++-- examples/04_ble_adv/main/app_bt.c | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 664b21ff64..3abebde947 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -55,7 +55,7 @@ static bool app_cpu_started = false; static void do_global_ctors(void); static void main_task(void* args); extern void ets_setup_syscalls(void); -extern int main(void); +extern int app_main(void); extern int _bss_start; extern int _bss_end; @@ -166,7 +166,7 @@ static void do_global_ctors(void) static void main_task(void* args) { - main(); + app_main(); vTaskDelete(NULL); } diff --git a/examples/04_ble_adv/main/app_bt.c b/examples/04_ble_adv/main/app_bt.c index b2ffc77498..7f5dda5ec5 100755 --- a/examples/04_ble_adv/main/app_bt.c +++ b/examples/04_ble_adv/main/app_bt.c @@ -197,9 +197,10 @@ void bleAdvtTask(void *pvParameters) } } -int main() +int app_main() { bt_controller_init(); xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0); + return 0; } From adfb9fafaafbee4184bdeb2c2097ff10a4789b82 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 15:57:54 +0800 Subject: [PATCH 13/14] components/freertos: fix a bug with an uninitialised return value introduced in d63dac0 --- components/freertos/queue.c | 1 + 1 file changed, 1 insertion(+) diff --git a/components/freertos/queue.c b/components/freertos/queue.c index 1fb0552d49..bdd413ea5e 100644 --- a/components/freertos/queue.c +++ b/components/freertos/queue.c @@ -1156,6 +1156,7 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue; } } #endif /* configUSE_QUEUE_SETS */ + xReturn = pdPASS; } else { From 991fde1f9d06aa0f08c412961bb61f5c94c084d3 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 26 Sep 2016 15:58:58 +0800 Subject: [PATCH 14/14] compoenents/esp32: don't alias start_cpu1 to start_cpu0_default --- components/esp32/cpu_start.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 3abebde947..fd46eec282 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -47,7 +47,7 @@ void start_cpu0(void) __attribute__((weak, alias("start_cpu0_default"))); void start_cpu0_default(void) IRAM_ATTR; #if !CONFIG_FREERTOS_UNICORE static void IRAM_ATTR call_start_cpu1(); -void start_cpu1(void) __attribute__((weak, alias("start_cpu0_default"))); +void start_cpu1(void) __attribute__((weak, alias("start_cpu1_default"))); void start_cpu1_default(void) IRAM_ATTR; static bool app_cpu_started = false; #endif //!CONFIG_FREERTOS_UNICORE