]> granicus.if.org Git - esp-idf/blob - components/bt/bt.c
Merge branch 'master' into feature/btdm_bluedroid
[esp-idf] / components / bt / bt.c
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include <stddef.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18
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"
27 #include "esp_task.h"
28 #include "esp_intr.h"
29 #include "esp_attr.h"
30 #include "bt.h"
31
32 #if CONFIG_BT_ENABLED
33
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);
37
38
39 #define BT_DEBUG(...)
40 #define BT_API_CALL_CHECK(info, api_call, ret) \
41 do{\
42     esp_err_t __err = (api_call);\
43     if ((ret) != __err) {\
44         BT_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\
45         return __err;\
46     }\
47 } while(0)
48
49 struct osi_funcs_t {
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]);
62 };
63
64 static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
65
66 static void IRAM_ATTR interrupt_disable(void)
67 {
68     portENTER_CRITICAL(&global_int_mux);
69 }
70
71 static void IRAM_ATTR interrupt_restore(void)
72 {
73     portEXIT_CRITICAL(&global_int_mux);
74 }
75
76 static void *IRAM_ATTR semphr_create_wrapper(uint32_t max, uint32_t init)
77 {
78     return (void *)xSemaphoreCreateCounting(max, init);
79 }
80
81 static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
82 {
83     return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
84 }
85
86 static int32_t IRAM_ATTR semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
87 {
88     return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_RATE_MS);
89 }
90
91 static void *IRAM_ATTR mutex_create_wrapper(void)
92 {
93     return (void *)xSemaphoreCreateMutex();
94 }
95
96 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
97 {
98     return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
99 }
100
101 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
102 {
103     return (int32_t)xSemaphoreGive(mutex);
104 }
105
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,
119 };
120
121 static void bt_controller_task(void *pvParam)
122 {
123     btdm_osi_funcs_register(&osi_funcs);
124     btdm_controller_init();
125 }
126
127 void bt_controller_init()
128 {
129     xTaskCreatePinnedToCore(bt_controller_task, "btController",
130                             ESP_TASK_BT_CONTROLLER_STACK, NULL,
131                             ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0);
132 }
133
134 #endif