Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include "esp_private/periph_ctrl.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "soc/lldesc.h"
#include "esp_private/gdma.h"
#include "hal/uhci_ll.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "esp_log.h"
tag
#define UART_HCI_NUM
#define UART_RX_THRS
#define GPIO_UART_TXD_OUT
#define GPIO_UART_RXD_IN
#define GPIO_UART_RTS_OUT
#define GPIO_UART_CTS_IN
#define GPIO_OUTPUT_PIN_SEL
#define GPIO_INPUT_PIN_SEL
uart_txrxchannel
uart_env_tag
uart_env
s_uhci_hw
s_rx_channel
s_tx_channel
s_hci_uart_tl_funcs
hci_uart_tl_init()
hci_uart_tl_deinit()
hci_uart_tl_recv_async(uint8_t *, uint32_t, esp_vhci_host_callback_t, void *)
hci_uart_tl_send_async(uint8_t *, uint32_t, esp_vhci_host_callback_t, void *)
hci_uart_tl_flow_on()
hci_uart_tl_flow_off()
hci_uart_tl_finish_transfers()
hci_uart_tl_rx_eof_callback(gdma_channel_handle_t, gdma_event_data_t *, void *)
hci_uart_tl_tx_eof_callback(gdma_channel_handle_t, gdma_event_data_t *, void *)
uart_gpio_set()
uhci_uart_install()
app_main()
Files
loading...
SourceVuESP-IDF Framework and Examplescontroller_hci_uart_esp32c3_and_esp32s3 samplemain/main.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 *//* ... */ #include <string.h> #include "esp_private/periph_ctrl.h" // for enabling UHCI module, remove it after UHCI driver is released #include "driver/gpio.h" #include "driver/uart.h" #include "soc/lldesc.h" #include "esp_private/gdma.h" #include "hal/uhci_ll.h" #include "nvs_flash.h" #include "esp_bt.h" #include "esp_log.h"10 includes static const char *tag = "UHCI"; #define UART_HCI_NUM (1) #define UART_RX_THRS (120) #define GPIO_UART_TXD_OUT (4) #define GPIO_UART_RXD_IN (5) #define GPIO_UART_RTS_OUT (6) #define GPIO_UART_CTS_IN (7) #define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_UART_TXD_OUT) | (1ULL<<GPIO_UART_RTS_OUT)) #define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_UART_RXD_IN) | (1ULL<<GPIO_UART_CTS_IN))8 defines // Operation functions for HCI UART Transport Layer static bool hci_uart_tl_init(void); static void hci_uart_tl_deinit(void); static void hci_uart_tl_recv_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg); static void hci_uart_tl_send_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg); static void hci_uart_tl_flow_on(void); static bool hci_uart_tl_flow_off(void); static void hci_uart_tl_finish_transfers(void); struct uart_txrxchannel { esp_bt_hci_tl_callback_t callback; void *arg; lldesc_t link; }{ ... }; struct uart_env_tag { struct uart_txrxchannel tx; struct uart_txrxchannel rx; }{ ... }; struct uart_env_tag uart_env; static volatile uhci_dev_t *s_uhci_hw = &UHCI0; static gdma_channel_handle_t s_rx_channel; static gdma_channel_handle_t s_tx_channel; static esp_bt_hci_tl_t s_hci_uart_tl_funcs = { ._magic = ESP_BT_HCI_TL_MAGIC_VALUE, ._version = ESP_BT_HCI_TL_VERSION, ._reserved = 0, ._open = (void *)hci_uart_tl_init, ._close = (void *)hci_uart_tl_deinit, ._finish_transfers = (void *)hci_uart_tl_finish_transfers, ._recv = (void *)hci_uart_tl_recv_async, ._send = (void *)hci_uart_tl_send_async, ._flow_on = (void *)hci_uart_tl_flow_on, ._flow_off = (void *)hci_uart_tl_flow_off, }{...}; static bool hci_uart_tl_init(void) { return true; }{ ... } static void hci_uart_tl_deinit(void) { }{ ... } static IRAM_ATTR void hci_uart_tl_recv_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg) { assert(buf != NULL); assert(size != 0); assert(callback != NULL); uart_env.rx.callback = callback; uart_env.rx.arg = arg; memset(&uart_env.rx.link, 0, sizeof(lldesc_t)); uart_env.rx.link.buf = buf; uart_env.rx.link.size = size; s_uhci_hw->pkt_thres.thrs = size; gdma_start(s_rx_channel, (intptr_t)(&uart_env.rx.link)); }{ ... } static IRAM_ATTR void hci_uart_tl_send_async(uint8_t *buf, uint32_t size, esp_bt_hci_tl_callback_t callback, void *arg) { assert(buf != NULL); assert(size != 0); assert(callback != NULL); uart_env.tx.callback = callback; uart_env.tx.arg = arg; memset(&uart_env.tx.link, 0, sizeof(lldesc_t)); uart_env.tx.link.length = size; uart_env.tx.link.buf = buf; uart_env.tx.link.eof = 1; gdma_start(s_tx_channel, (intptr_t)(&uart_env.tx.link)); }{ ... } static void hci_uart_tl_flow_on(void) { }{ ... } static bool hci_uart_tl_flow_off(void) { return true; }{ ... } static void hci_uart_tl_finish_transfers(void) { }{ ... } static IRAM_ATTR bool hci_uart_tl_rx_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data) { assert(dma_chan == s_rx_channel); assert(uart_env.rx.callback != NULL); esp_bt_hci_tl_callback_t callback = uart_env.rx.callback; void *arg = uart_env.rx.arg; // clear callback pointer uart_env.rx.callback = NULL; uart_env.rx.arg = NULL; // call handler callback(arg, ESP_BT_HCI_TL_STATUS_OK); // send notification to Bluetooth Controller task esp_bt_h4tl_eif_io_event_notify(1); return true; }{ ... } static IRAM_ATTR bool hci_uart_tl_tx_eof_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data) { assert(dma_chan == s_tx_channel); assert(uart_env.tx.callback != NULL); esp_bt_hci_tl_callback_t callback = uart_env.tx.callback; void *arg = uart_env.tx.arg; // clear callback pointer uart_env.tx.callback = NULL; uart_env.tx.arg = NULL; // call handler callback(arg, ESP_BT_HCI_TL_STATUS_OK); // send notification to Bluetooth Controller task esp_bt_h4tl_eif_io_event_notify(1); return true; }{ ... } static void uart_gpio_set(void) { gpio_config_t io_output_conf = { .intr_type = GPIO_INTR_DISABLE, //disable interrupt .mode = GPIO_MODE_OUTPUT, // output mode .pin_bit_mask = GPIO_OUTPUT_PIN_SEL, // bit mask of the output pins .pull_down_en = 0, // disable pull-down mode .pull_up_en = 0, // disable pull-up mode }{...}; gpio_config(&io_output_conf); gpio_config_t io_input_conf = { .intr_type = GPIO_INTR_DISABLE, //disable interrupt .mode = GPIO_MODE_INPUT, // input mode .pin_bit_mask = GPIO_INPUT_PIN_SEL, // bit mask of the input pins .pull_down_en = 0, // disable pull-down mode .pull_up_en = 0, // disable pull-down mode }{...}; gpio_config(&io_input_conf); uart_set_pin(UART_HCI_NUM, GPIO_UART_TXD_OUT, GPIO_UART_RXD_IN, GPIO_UART_RTS_OUT, GPIO_UART_CTS_IN); }{ ... } void uhci_uart_install(void) { periph_module_enable(PERIPH_UHCI0_MODULE); periph_module_reset(PERIPH_UHCI0_MODULE); periph_module_enable(PERIPH_UART1_MODULE); periph_module_reset(PERIPH_UART1_MODULE); uart_gpio_set(); // configure UART1 uart_config_t uart_config = { .baud_rate = CONFIG_EXAMPLE_HCI_UART_BAUDRATE, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, #ifdef CONFIG_EXAMPLE_HCI_UART_FLOW_CTRL_ENABLE .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS, #else .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, #endif .rx_flow_ctrl_thresh = UART_RX_THRS, .source_clk = UART_SCLK_DEFAULT, }{...}; ESP_ERROR_CHECK(uart_param_config(UART_HCI_NUM, &uart_config)); // install DMA driver gdma_channel_alloc_config_t tx_channel_config = { .flags.reserve_sibling = 1, .direction = GDMA_CHANNEL_DIRECTION_TX, }{...}; ESP_ERROR_CHECK(gdma_new_ahb_channel(&tx_channel_config, &s_tx_channel)); gdma_channel_alloc_config_t rx_channel_config = { .direction = GDMA_CHANNEL_DIRECTION_RX, .sibling_chan = s_tx_channel, }{...}; ESP_ERROR_CHECK(gdma_new_ahb_channel(&rx_channel_config, &s_rx_channel)); gdma_connect(s_tx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_UHCI, 0)); gdma_connect(s_rx_channel, GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_UHCI, 0)); gdma_strategy_config_t strategy_config = { .auto_update_desc = false, .owner_check = false }{...}; gdma_apply_strategy(s_tx_channel, &strategy_config); gdma_apply_strategy(s_rx_channel, &strategy_config); gdma_rx_event_callbacks_t rx_cbs = { .on_recv_eof = hci_uart_tl_rx_eof_callback }{...}; gdma_register_rx_event_callbacks(s_rx_channel, &rx_cbs, NULL); gdma_tx_event_callbacks_t tx_cbs = { .on_trans_eof = hci_uart_tl_tx_eof_callback }{...}; gdma_register_tx_event_callbacks(s_tx_channel, &tx_cbs, NULL); // configure UHCI uhci_ll_init(s_uhci_hw); uhci_ll_set_eof_mode(s_uhci_hw, UHCI_RX_LEN_EOF); // disable software flow control s_uhci_hw->escape_conf.val = 0; uhci_ll_attach_uart_port(s_uhci_hw, 1); }{ ... } void app_main(void) { esp_err_t ret; /* Initialize NVS — it is used to store PHY calibration data */ ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); }{...} ESP_ERROR_CHECK( ret ); uhci_uart_install(); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); bt_cfg.hci_tl_funcs = &s_hci_uart_tl_funcs; ret = esp_bt_controller_init(&bt_cfg); if (ret != ESP_OK) { ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret)); return; }{...} ret = esp_bt_controller_enable(ESP_BT_MODE_BLE); if (ret != ESP_OK) { ESP_LOGE(tag, "Bluetooth Controller initialize failed: %s", esp_err_to_name(ret)); return; }{...} ESP_LOGI(tag, "HCI messages can be communicated over UART%d:\n" "--PINs: TxD %d, RxD %d, RTS %d, CTS %d\n" "--Baudrate: %d", UART_HCI_NUM, GPIO_UART_TXD_OUT, GPIO_UART_RXD_IN, GPIO_UART_RTS_OUT, GPIO_UART_CTS_IN, CONFIG_EXAMPLE_HCI_UART_BAUDRATE); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.