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.
This commit is contained in:
Ivan Grokhotkov 2016-09-26 00:50:57 +08:00
parent 4480ab6c8c
commit b936441b9b
9 changed files with 38 additions and 210 deletions

View File

@ -96,26 +96,11 @@ static void bt_controller_task(void *pvParam)
btdm_controller_init(); btdm_controller_init();
} }
void bt_controller_init()
static void bt_init_task(void *pvParameters)
{ {
xTaskCreatePinnedToCore(bt_controller_task, "btControllerTask", ESP_TASK_BT_CONTROLLER_STACK, NULL, ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0); xTaskCreatePinnedToCore(bt_controller_task, "btController",
ESP_TASK_BT_CONTROLLER_STACK, NULL,
if (app_startup_cb) { ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0);
app_startup_cb(app_startup_ctx);
}
vTaskDelete(NULL);
} }
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 #endif

View File

@ -15,7 +15,7 @@
#ifndef __BT_H__ #ifndef __BT_H__
#define __BT_H__ #define __BT_H__
#include "freertos/FreeRTOS.h" #include <stdint.h>
#include "esp_err.h" #include "esp_err.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -23,9 +23,12 @@ extern "C" {
#endif #endif
typedef void (* bt_app_startup_cb_t)(void *param); /**
* @brief Initialize BT controller
esp_err_t esp_bt_startup(bt_app_startup_cb_t cb, void *ctx); *
* This function should be called only once, before any other BT functions are called.
*/
void bt_controller_init();
/* @breif: vhci_host_callback /* @breif: vhci_host_callback
* used for vhci call host function to notify what host need to do * used for vhci call host function to notify what host need to do

View File

@ -80,33 +80,22 @@ config WIFI_ENABLED
Temporarily, this option is not compatible with BT stack. 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 config SYSTEM_EVENT_QUEUE_SIZE
int "system event queue size" int "System event queue size"
default 32 default 32
depends on WIFI_ENABLED
help help
Config system event queue size in different application. Config system event queue size in different application.
config SYSTEM_EVENT_TASK_STACK_SIZE config SYSTEM_EVENT_TASK_STACK_SIZE
int "system event task stack size" int "Event loop task stack size"
default 2048 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 help
Config system event task stack size in different application. Config system event task stack size in different application.

View 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 call_user_start_cpu1();
static void IRAM_ATTR user_start_cpu1(void); static void IRAM_ATTR user_start_cpu1(void);
extern void ets_setup_syscalls(void); extern void ets_setup_syscalls(void);
extern esp_err_t app_main(void *ctx); extern int main(void);
#if CONFIG_BT_ENABLED
extern void bt_app_main(void *param);
#endif
extern int _bss_start; extern int _bss_start;
extern int _bss_end; extern int _bss_end;
@ -137,11 +134,17 @@ void IRAM_ATTR user_start_cpu1(void)
static void do_global_ctors(void) static void do_global_ctors(void)
{ {
void (**p)(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)(); (*p)();
} }
} }
static void mainTask(void* args)
{
main();
vTaskDelete(NULL);
}
void user_start_cpu0(void) void user_start_cpu0(void)
{ {
esp_set_cpu_freq(); // set CPU frequency configured in menuconfig esp_set_cpu_freq(); // set CPU frequency configured in menuconfig
@ -150,28 +153,12 @@ void user_start_cpu0(void)
do_global_ctors(); do_global_ctors();
esp_ipc_init(); esp_ipc_init();
spi_flash_init(); spi_flash_init();
#ifdef CONFIG_WIFI_ENABLED
#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);
}
system_init(); system_init();
esp_event_init(NULL, NULL);
tcpip_adapter_init();
#endif #endif
xTaskCreatePinnedToCore(&mainTask, "mainTask",
#if CONFIG_WIFI_ENABLED && CONFIG_WIFI_AUTO_STARTUP ESP_TASK_MAIN_STACK, NULL,
#include "esp_wifi.h" ESP_TASK_MAIN_PRIO, NULL, 0);
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
ESP_LOGI(TAG, "Starting scheduler on PRO CPU."); ESP_LOGI(TAG, "Starting scheduler on PRO CPU.");
vTaskStartScheduler(); vTaskStartScheduler();
} }

View File

@ -51,10 +51,9 @@
/* idf task */ /* idf task */
#define ESP_TASKD_EVENT_PRIO (ESP_TASK_PRIO_MAX - 5) #define ESP_TASKD_EVENT_PRIO (ESP_TASK_PRIO_MAX - 5)
#define ESP_TASKD_EVENT_STACK CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE #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_PRIO (ESP_TASK_PRIO_MAX - 7)
#define ESP_TASK_TCPIP_STACK 2048 #define ESP_TASK_TCPIP_STACK 2048
#define ESP_TASK_BT_INIT_PRIO (ESP_TASK_PRIO_MAX - 7) #define ESP_TASK_MAIN_PRIO (ESP_TASK_PRIO_MIN + 1)
#define ESP_TASK_BT_INIT_STACK 2048 #define ESP_TASK_MAIN_STACK CONFIG_MAIN_TASK_STACK_SIZE
#endif #endif

View 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_BELOW, /**< the channel width is HT40 and the second channel is below the primary channel */
} wifi_second_chan_t; } 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 { typedef struct {
void *event_q; /**< WiFi event q handler, it's a freeRTOS queue */ void *event_q; /**< WiFi event q handler, it's a freeRTOS queue */

View File

@ -445,7 +445,6 @@ PROVIDE ( _lseek_r = 0x4000bd8c );
PROVIDE ( __lshrdi3 = 0x4000c84c ); PROVIDE ( __lshrdi3 = 0x4000c84c );
PROVIDE ( __ltdf2 = 0x40063790 ); PROVIDE ( __ltdf2 = 0x40063790 );
PROVIDE ( __ltsf2 = 0x4006342c ); PROVIDE ( __ltsf2 = 0x4006342c );
PROVIDE ( main = 0x400076c4 );
PROVIDE ( malloc = 0x4000bea0 ); PROVIDE ( malloc = 0x4000bea0 );
PROVIDE ( _malloc_r = 0x4000bbb4 ); PROVIDE ( _malloc_r = 0x4000bbb4 );
PROVIDE ( maxSecretKey_256 = 0x3ff97448 ); PROVIDE ( maxSecretKey_256 = 0x3ff97448 );
@ -1378,6 +1377,7 @@ PROVIDE ( rom_iq_est_disable = 0x40005590 );
PROVIDE ( rom_iq_est_enable = 0x40005514 ); PROVIDE ( rom_iq_est_enable = 0x40005514 );
PROVIDE ( rom_linear_to_db = 0x40005f64 ); PROVIDE ( rom_linear_to_db = 0x40005f64 );
PROVIDE ( rom_loopback_mode_en = 0x400030f8 ); PROVIDE ( rom_loopback_mode_en = 0x400030f8 );
PROVIDE ( rom_main = 0x400076c4 );
PROVIDE ( rom_meas_tone_pwr_db = 0x40006004 ); PROVIDE ( rom_meas_tone_pwr_db = 0x40006004 );
PROVIDE ( rom_mhz2ieee = 0x4000404c ); PROVIDE ( rom_mhz2ieee = 0x4000404c );
PROVIDE ( rom_noise_floor_auto_set = 0x40003bdc ); PROVIDE ( rom_noise_floor_auto_set = 0x40003bdc );

View File

@ -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

View File

@ -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); xTaskCreatePinnedToCore(&bleAdvtTask, "bleAdvtTask", 2048, NULL, 5, NULL, 0);
} }