diff --git a/components/bt/bt.c b/components/bt/bt.c new file mode 100644 index 0000000000..4b623ca20f --- /dev/null +++ b/components/bt/bt.c @@ -0,0 +1,117 @@ +// 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 "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" +#include "freertos/semphr.h" +#include "freertos/xtensa_api.h" +#include "freertos/portmacro.h" +#include "esp_types.h" +#include "esp_system.h" +#include "esp_intr.h" +#include "esp_attr.h" +#include "bt.h" + +#if CONFIG_BT_ENABLED + +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{\ + esp_err_t __err = (api_call);\ + if ((ret) != __err) {\ + BT_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\ + return __err;\ + }\ +} while(0) + +extern void btdm_controller_init(void); +extern void API_osi_funcs_register(void *osi_funcs); + +struct osi_funcs_t { + xt_handler (*_set_isr)(int n, xt_handler f, void *arg); + void (*_ints_on)(unsigned int mask); + void (*_interrupt_disable)(void); + void (*_interrupt_restore)(void); + void (*_task_yield)(void); + void *(*_semphr_create)(uint32_t max, uint32_t init); + void *(*_semphr_give_from_isr)(void *semphr, void *hptw); + void *(*_semphr_take)(void *semphr, uint32_t block_time); + esp_err_t (* _read_efuse_mac)(uint8_t mac[6]); +}; + +static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED; + +static inline void IRAM_ATTR interrupt_disable(void) +{ + portENTER_CRITICAL(&global_int_mux); +} + +static inline void IRAM_ATTR interrupt_restore(void) +{ + portEXIT_CRITICAL(&global_int_mux); +} + +static inline void *IRAM_ATTR semphr_take_wrapped(void *semphr, uint32_t block_time) +{ + return (void *)xSemaphoreTake(semphr, block_time); +} + +static struct osi_funcs_t osi_funcs = { + ._set_isr = xt_set_interrupt_handler, + ._ints_on = xt_ints_on, + ._interrupt_disable = interrupt_disable, + ._interrupt_restore = interrupt_restore, + ._task_yield = vPortYield, + ._semphr_create = xQueueCreateCountingSemaphore, + ._semphr_give_from_isr = (void *)xQueueGiveFromISR, + ._semphr_take = semphr_take_wrapped, + ._read_efuse_mac = system_efuse_read_mac, +}; + +static void bt_controller_task(void *pvParam) +{ + API_osi_funcs_register(&osi_funcs); + btdm_controller_init(); +} + + +static void bt_init_task(void *pvParameters) +{ + xTaskCreatePinnedToCore(bt_controller_task, "btControllerTask", BT_CONTROLLER_TASK_STACK_SIZE, NULL, BT_CONTROLLER_TASK_PRIO, NULL, 0); + + if (app_startup_cb) + 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", BT_INIT_TASK_STACK_SIZE, NULL, BT_INIT_TASK_PRIO, NULL, 0); + + return ESP_OK; +} +#endif diff --git a/components/bt/component.mk b/components/bt/component.mk new file mode 100644 index 0000000000..110d022672 --- /dev/null +++ b/components/bt/component.mk @@ -0,0 +1,25 @@ +# +# Component Makefile +# + +#COMPONENT_ADD_INCLUDEDIRS := + +CURRENT_DIR=$(IDF_PATH)/components/bt + +COMPONENT_ADD_INCLUDEDIRS := ./include + +CFLAGS += -Wno-error=unused-label -Wno-error=return-type -Wno-error=missing-braces -Wno-error=pointer-sign -Wno-error=parentheses + +LIBS := btdm_app + +COMPONENT_ADD_LDFLAGS := -lbt -L$(abspath lib) \ + $(addprefix -l,$(LIBS)) \ + $(LINKER_SCRIPTS) + + +ALL_LIB_FILES := $(patsubst %,$(COMPONENT_PATH)/lib/lib%.a,$(LIBS)) +$(COMPONENT_LIBRARY): $(ALL_LIB_FILES) + +COMPONENT_SRCDIRS := ./ + +include $(IDF_PATH)/make/component_common.mk diff --git a/components/bt/include/bt.h b/components/bt/include/bt.h new file mode 100644 index 0000000000..04bb466c45 --- /dev/null +++ b/components/bt/include/bt.h @@ -0,0 +1,30 @@ +#ifndef __BT_H__ +#define __BT_H__ + +#include "freertos/FreeRTOS.h" +#include "esp_err.h" + +#define BT_TASK_PRIO_MAX (configMAX_PRIORITIES) +#define BT_TASK_PRIO_MIN (0) + +/* bt init */ +#define BT_INIT_TASK_PRIO (BT_TASK_PRIO_MAX-1) +#define BT_INIT_TASK_STACK_SIZE (2048) +/* controller */ +#define BT_CONTROLLER_TASK_PRIO (BT_TASK_PRIO_MAX-3) +#define BT_CONTROLLER_TASK_STACK_SIZE (4096) + +typedef void (* bt_app_startup_cb_t)(void *param); + +esp_err_t esp_bt_startup(bt_app_startup_cb_t cb, void *ctx); + +typedef struct vhci_host_callback { + void (*notify_host_send_available)(void); + int (*notify_host_recv)(uint8_t *data, uint16_t len); +} vhci_host_callback_t; + +extern bool API_vhci_host_check_send_available(void); +extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len); +extern void API_vhci_host_register_callback(const vhci_host_callback_t *callback); + +#endif /* __BT_H__ */ diff --git a/components/bt/lib/libbtdm_app.a b/components/bt/lib/libbtdm_app.a new file mode 100644 index 0000000000..4c93a93b0f Binary files /dev/null and b/components/bt/lib/libbtdm_app.a differ diff --git a/components/esp32/cpu_start.c b/components/esp32/cpu_start.c index 85f4ace512..45a4bcec3c 100644 --- a/components/esp32/cpu_start.c +++ b/components/esp32/cpu_start.c @@ -48,6 +48,9 @@ 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 _bss_start; extern int _bss_end; @@ -161,7 +164,10 @@ void user_start_cpu0(void) #if CONFIG_WIFI_ENABLED && CONFIG_WIFI_AUTO_STARTUP #include "esp_wifi.h" - esp_wifi_startup(app_main, NULL); + 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 diff --git a/components/esp32/include/soc/soc.h b/components/esp32/include/soc/soc.h index 0b8cdfecba..4ffdfb069e 100755 --- a/components/esp32/include/soc/soc.h +++ b/components/esp32/include/soc/soc.h @@ -260,14 +260,14 @@ /************************************************************************************************************* * Intr num Level Type PRO CPU usage APP CPU uasge * 0 1 extern level WMAC Reserved - * 1 1 extern level BT/BLE Host Reserved + * 1 1 extern level BT/BLE Host VHCI Reserved * 2 1 extern level FROM_CPU FROM_CPU * 3 1 extern level TG0_WDT Reserved * 4 1 extern level WBB - * 5 1 extern level Reserved + * 5 1 extern level BT Controller * 6 1 timer FreeRTOS Tick(L1) FreeRTOS Tick(L1) * 7 1 software Reserved Reserved - * 8 1 extern level Reserved + * 8 1 extern level BLE Controller * 9 1 extern level * 10 1 extern edge Internal Timer * 11 3 profiling diff --git a/components/esp32/lib b/components/esp32/lib index 9f26b9a190..ab3c510e51 160000 --- a/components/esp32/lib +++ b/components/esp32/lib @@ -1 +1 @@ -Subproject commit 9f26b9a190e6a6ca42656685df9287253badfa46 +Subproject commit ab3c510e51f312d919df6830efbc04c6de9cfd2a