1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/task.h"
21 #include "freertos/queue.h"
22 #include "freertos/semphr.h"
23 #include "freertos/xtensa_api.h"
24 #include "freertos/portmacro.h"
25 #include "esp_types.h"
26 #include "esp_system.h"
34 /* not for user call, so don't put to include file */
35 extern void btdm_osi_funcs_register(void *osi_funcs);
36 extern void btdm_controller_init(void);
38 /* VHCI function interface */
39 typedef struct vhci_host_callback {
40 void (*notify_host_send_available)(void); /*!< callback used to notify that the host can send packet to controller */
41 int (*notify_host_recv)(uint8_t *data, uint16_t len); /*!< callback used to notify that the controller has a packet to send to the host*/
42 } vhci_host_callback_t;
44 extern bool API_vhci_host_check_send_available(void);
45 extern void API_vhci_host_send_packet(uint8_t *data, uint16_t len);
46 extern void API_vhci_host_register_callback(const vhci_host_callback_t *callback);
49 #define BT_API_CALL_CHECK(info, api_call, ret) \
51 esp_err_t __err = (api_call);\
52 if ((ret) != __err) {\
53 BT_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\
59 xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
60 void (*_ints_on)(unsigned int mask);
61 void (*_interrupt_disable)(void);
62 void (*_interrupt_restore)(void);
63 void (*_task_yield)(void);
64 void *(*_semphr_create)(uint32_t max, uint32_t init);
65 int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
66 int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
67 void *(*_mutex_create)(void);
68 int32_t (*_mutex_lock)(void *mutex);
69 int32_t (*_mutex_unlock)(void *mutex);
70 esp_err_t (* _read_efuse_mac)(uint8_t mac[6]);
73 static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
75 static void IRAM_ATTR interrupt_disable(void)
77 portENTER_CRITICAL(&global_int_mux);
80 static void IRAM_ATTR interrupt_restore(void)
82 portEXIT_CRITICAL(&global_int_mux);
85 static void *IRAM_ATTR semphr_create_wrapper(uint32_t max, uint32_t init)
87 return (void *)xSemaphoreCreateCounting(max, init);
90 static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
92 return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
95 static int32_t IRAM_ATTR semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
97 return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
100 static void *IRAM_ATTR mutex_create_wrapper(void)
102 return (void *)xSemaphoreCreateMutex();
105 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
107 return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
110 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
112 return (int32_t)xSemaphoreGive(mutex);
115 static struct osi_funcs_t osi_funcs = {
116 ._set_isr = xt_set_interrupt_handler,
117 ._ints_on = xt_ints_on,
118 ._interrupt_disable = interrupt_disable,
119 ._interrupt_restore = interrupt_restore,
120 ._task_yield = vPortYield,
121 ._semphr_create = semphr_create_wrapper,
122 ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
123 ._semphr_take = semphr_take_wrapper,
124 ._mutex_create = mutex_create_wrapper,
125 ._mutex_lock = mutex_lock_wrapper,
126 ._mutex_unlock = mutex_unlock_wrapper,
127 ._read_efuse_mac = esp_efuse_read_mac,
130 bool esp_vhci_host_check_send_available(void)
132 return API_vhci_host_check_send_available();
135 void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
137 API_vhci_host_send_packet(data, len);
140 void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
142 API_vhci_host_register_callback((const vhci_host_callback_t *)callback);
145 static void bt_controller_task(void *pvParam)
147 btdm_osi_funcs_register(&osi_funcs);
148 btdm_controller_init();
151 void esp_bt_controller_init()
153 xTaskCreatePinnedToCore(bt_controller_task, "btController",
154 ESP_TASK_BT_CONTROLLER_STACK, NULL,
155 ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0);