]> granicus.if.org Git - esp-idf/blob - components/bt/bt.c
Fix panic register dump format
[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 /* 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;
43
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);
47
48 #define BT_DEBUG(...)
49 #define BT_API_CALL_CHECK(info, api_call, ret) \
50 do{\
51     esp_err_t __err = (api_call);\
52     if ((ret) != __err) {\
53         BT_DEBUG("%s %d %s ret=%d\n", __FUNCTION__, __LINE__, (info), __err);\
54         return __err;\
55     }\
56 } while(0)
57
58 struct osi_funcs_t {
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]);
71 };
72
73 static portMUX_TYPE global_int_mux = portMUX_INITIALIZER_UNLOCKED;
74
75 static void IRAM_ATTR interrupt_disable(void)
76 {
77     portENTER_CRITICAL(&global_int_mux);
78 }
79
80 static void IRAM_ATTR interrupt_restore(void)
81 {
82     portEXIT_CRITICAL(&global_int_mux);
83 }
84
85 static void *IRAM_ATTR semphr_create_wrapper(uint32_t max, uint32_t init)
86 {
87     return (void *)xSemaphoreCreateCounting(max, init);
88 }
89
90 static int32_t IRAM_ATTR semphr_give_from_isr_wrapper(void *semphr, void *hptw)
91 {
92     return (int32_t)xSemaphoreGiveFromISR(semphr, hptw);
93 }
94
95 static int32_t IRAM_ATTR semphr_take_wrapper(void *semphr, uint32_t block_time_ms)
96 {
97     return (int32_t)xSemaphoreTake(semphr, block_time_ms / portTICK_PERIOD_MS);
98 }
99
100 static void *IRAM_ATTR mutex_create_wrapper(void)
101 {
102     return (void *)xSemaphoreCreateMutex();
103 }
104
105 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
106 {
107     return (int32_t)xSemaphoreTake(mutex, portMAX_DELAY);
108 }
109
110 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
111 {
112     return (int32_t)xSemaphoreGive(mutex);
113 }
114
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,
128 };
129
130 bool esp_vhci_host_check_send_available(void)
131 {
132     return API_vhci_host_check_send_available();
133 }
134
135 void esp_vhci_host_send_packet(uint8_t *data, uint16_t len)
136 {
137     API_vhci_host_send_packet(data, len);
138 }
139
140 void esp_vhci_host_register_callback(const esp_vhci_host_callback_t *callback)
141 {
142     API_vhci_host_register_callback((const vhci_host_callback_t *)callback);
143 }
144
145 static void bt_controller_task(void *pvParam)
146 {
147     btdm_osi_funcs_register(&osi_funcs);
148     btdm_controller_init();
149 }
150
151 void esp_bt_controller_init()
152 {
153     xTaskCreatePinnedToCore(bt_controller_task, "btController",
154                             ESP_TASK_BT_CONTROLLER_STACK, NULL,
155                             ESP_TASK_BT_CONTROLLER_PRIO, NULL, 0);
156 }
157
158 #endif