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);
40 #define BT_API_CALL_CHECK(info, api_call, ret) \
42 esp_err_t __err = (api_call);\
43 if ((ret) != __err) {\
44 BT_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\
50 xt_handler (*_set_isr)(int n, xt_handler f, void *arg);
51 void (*_ints_on)(unsigned int mask);
52 void (*_interrupt_disable)(void);
53 void (*_interrupt_restore)(void);
54 void (*_task_yield)(void);
55 void *(*_semphr_create)(uint32_t max, uint32_t init);
56 int32_t (*_semphr_give_from_isr)(void *semphr, void *hptw);
57 int32_t (*_semphr_take)(void *semphr, uint32_t block_time_ms);
58 void *(*_mutex_create)(void);
59 int32_t (*_mutex_lock)(void *mutex);
60 int32_t (*_mutex_unlock)(void *mutex);
61 esp_err_t (* _read_efuse_mac)(uint8_t mac[6]);
64 static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
66 static void IRAM_ATTR interrupt_disable(void)
68 portENTER_CRITICAL(&global_int_mux);
71 static void IRAM_ATTR interrupt_restore(void)
73 portEXIT_CRITICAL(&global_int_mux);
76 static void *IRAM_ATTR semphr_create_wrapper(uint32_t max, uint32_t init)
78 return (void *)xSemaphoreCreateCounting(max, init);
81 static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
83 return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
86 static int32_t IRAM_ATTR semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
88 return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_RATE_MS);
91 static void *IRAM_ATTR mutex_create_wrapper(void)
93 return (void *)xSemaphoreCreateMutex();
96 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
98 return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
101 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
103 return (int32_t)xSemaphoreGive(mutex);
106 static struct osi_funcs_t osi_funcs = {
107 ._set_isr = xt_set_interrupt_handler,
108 ._ints_on = xt_ints_on,
109 ._interrupt_disable = interrupt_disable,
110 ._interrupt_restore = interrupt_restore,
111 ._task_yield = vPortYield,
112 ._semphr_create = semphr_create_wrapper,
113 ._semphr_give_from_isr = semphr_give_from_isr_wrapper,
114 ._semphr_take = semphr_take_wrapper,
115 ._mutex_create = mutex_create_wrapper,
116 ._mutex_lock = mutex_lock_wrapper,
117 ._mutex_unlock = mutex_unlock_wrapper,
118 ._read_efuse_mac = esp_efuse_read_mac,
121 static void bt_controller_task(void *pvParam)
123 btdm_osi_funcs_register(&osi_funcs);
124 btdm_controller_init();
127 void bt_controller_init()
129 xTaskCreatePinnedToCore(bt_controller_task, "btController",
130 ESP_TASK_BT_CONTROLLER_STACK, NULL,
131 ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0);