]> granicus.if.org Git - esp-idf/commitdiff
Startup flow refactoring
authorIvan Grokhotkov <ivan@espressif.com>
Sun, 25 Sep 2016 16:50:57 +0000 (00:50 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Sun, 25 Sep 2016 16:50:57 +0000 (00:50 +0800)
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
components/bt/include/bt.h
components/esp32/Kconfig
components/esp32/cpu_start.c
components/esp32/include/esp_task.h
components/esp32/include/esp_wifi.h
components/esp32/ld/esp32.rom.ld
components/esp32/wifi.c [deleted file]
examples/04_ble_adv/main/app_bt.c

index efb6d34ee34dd9e479b626359650355c66fb468a..d9bc5ae08932b8fb1e8e9adb7698dac1a9f889b1 100644 (file)
@@ -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
index 8511aabdf8d4a81cc8e1ed52edffbc0cf08aca91..0dc04249396a11f1651c39221f1d1991ffd52fb4 100644 (file)
@@ -15,7 +15,7 @@
 #ifndef __BT_H__
 #define __BT_H__
 
-#include "freertos/FreeRTOS.h"
+#include <stdint.h>
 #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
index c649a0e3178984b9ec32145b179357b4f6ef6f86..a43d16d2c612d053318b95ab726042a60a11e87a 100644 (file)
@@ -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.
 
index 45a4bcec3c0ce5aadaaa627215263bc6bc8793e7..f2cec8426704028bdc07b812184c1130c3f77ed0 100644 (file)
@@ -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();
 }
index 58458106fa036b2a939d319d8e28200b5e1cd3a2..0a68e5d919eb1574549cf2457fb92f14b68e2ea4 100644 (file)
 /* 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
index a5b9d089aedd7697998e16efd20089b01098ded2..38b7bdac1ca00f3be063bda960825e1086728d01 100644 (file)
@@ -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 */
index 6f9064a3980b9b1f0403c8a493153e95578ec10c..ac1142f82d5bd1d04efdba22eccb0da2c3df586b 100644 (file)
@@ -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 (file)
index fd44d30..0000000
+++ /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 <stddef.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#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
index 011cf0c7155c563d3baa0bf9d6a4dc3e21e6b958..b2ffc774986c01fd1bdca609e29552ffa4eff745 100755 (executable)
@@ -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);
 }