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
|
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#include "py/mperrno.h"
#include "py/mphal.h"
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
#include "lib/btstack/src/btstack.h"
#include "lib/btstack/platform/embedded/btstack_run_loop_embedded.h"
#include "lib/btstack/platform/embedded/hal_cpu.h"
#include "lib/btstack/platform/embedded/hal_time_ms.h"
#include "extmod/modbluetooth_hci.h"
#include "extmod/btstack/modbluetooth_btstack.h"
// We pass the bytes directly to the UART during a send, but then notify btstack in the next poll.
STATIC bool send_done;
STATIC void (*send_handler)(void);
// btstack issues a read of len bytes, and gives us a buffer to asynchronously fill up.
STATIC uint8_t *recv_buf;
STATIC size_t recv_len;
STATIC size_t recv_idx;
STATIC void (*recv_handler)(void);
// The IRQ functionality in btstack_run_loop_embedded.c is not used, so the
// following three functions are empty.
void hal_cpu_disable_irqs(void) {
}
void hal_cpu_enable_irqs(void) {
}
void hal_cpu_enable_irqs_and_sleep(void) {
}
uint32_t hal_time_ms(void) {
return mp_hal_ticks_ms();
}
STATIC int btstack_uart_init(const btstack_uart_config_t *uart_config) {
send_done = false;
recv_len = 0;
recv_idx = 0;
recv_handler = NULL;
send_handler = NULL;
// Set up the UART periperhal.
mp_bluetooth_hci_uart_init(MICROPY_HW_BLE_UART_ID);
return 0;
}
STATIC int btstack_uart_open(void) {
// Attach IRQ and power up the HCI controller.
mp_bluetooth_hci_uart_activate();
return 0;
}
STATIC int btstack_uart_close(void) {
return 0;
}
STATIC void btstack_uart_set_block_received(void (*block_handler)(void)) {
recv_handler = block_handler;
}
STATIC void btstack_uart_set_block_sent(void (*block_handler)(void)) {
send_handler = block_handler;
}
STATIC int btstack_uart_set_baudrate(uint32_t baudrate) {
mp_bluetooth_hci_uart_set_baudrate(baudrate);
return 0;
}
STATIC int btstack_uart_set_parity(int parity) {
return 0;
}
STATIC int btstack_uart_set_flowcontrol(int flowcontrol) {
return 0;
}
STATIC void btstack_uart_receive_block(uint8_t *buf, uint16_t len) {
recv_buf = buf;
recv_len = len;
}
STATIC void btstack_uart_send_block(const uint8_t *buf, uint16_t len) {
mp_bluetooth_hci_uart_write(buf, len);
send_done = true;
}
STATIC int btstack_uart_get_supported_sleep_modes(void) {
return 0;
}
STATIC void btstack_uart_set_sleep(btstack_uart_sleep_mode_t sleep_mode) {
// printf("btstack_uart_set_sleep %u\n", sleep_mode);
}
STATIC void btstack_uart_set_wakeup_handler(void (*wakeup_handler)(void)) {
// printf("btstack_uart_set_wakeup_handler\n");
}
STATIC const btstack_uart_block_t btstack_uart_block = {
&btstack_uart_init,
&btstack_uart_open,
&btstack_uart_close,
&btstack_uart_set_block_received,
&btstack_uart_set_block_sent,
&btstack_uart_set_baudrate,
&btstack_uart_set_parity,
&btstack_uart_set_flowcontrol,
&btstack_uart_receive_block,
&btstack_uart_send_block,
&btstack_uart_get_supported_sleep_modes,
&btstack_uart_set_sleep,
&btstack_uart_set_wakeup_handler,
};
STATIC const hci_transport_config_uart_t hci_transport_config_uart = {
HCI_TRANSPORT_CONFIG_UART,
MICROPY_HW_BLE_UART_BAUDRATE,
3000000,
0,
NULL,
};
STATIC void btstack_uart_process(void) {
if (send_done) {
// If we'd done a TX in the last interval, notify btstack that it's complete.
send_done = false;
if (send_handler) {
send_handler();
}
}
// Append any new bytes to the recv buffer, notifying bstack if we've got
// the number of bytes it was looking for.
while (uart_rx_any(&mp_bluetooth_hci_uart_obj) && recv_idx < recv_len) {
recv_buf[recv_idx++] = uart_rx_char(&mp_bluetooth_hci_uart_obj);
if (recv_idx == recv_len) {
recv_idx = 0;
recv_len = 0;
if (recv_handler) {
recv_handler();
}
}
}
}
void mp_bluetooth_hci_poll(void) {
if (mp_bluetooth_btstack_state == MP_BLUETOOTH_BTSTACK_STATE_OFF) {
return;
}
// Process uart data.
bool host_wake = mp_bluetooth_hci_controller_woken();
btstack_uart_process();
if (host_wake) {
mp_bluetooth_hci_controller_sleep_maybe();
}
// Call the BTstack run loop.
btstack_run_loop_embedded_execute_once();
}
void mp_bluetooth_btstack_port_init(void) {
static bool run_loop_init = false;
if (!run_loop_init) {
run_loop_init = true;
btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
} else {
btstack_run_loop_embedded_get_instance()->init();
}
// hci_dump_open(NULL, HCI_DUMP_STDOUT);
const hci_transport_t *transport = hci_transport_h4_instance(&btstack_uart_block);
hci_init(transport, &hci_transport_config_uart);
// TODO: Probably not necessary for BCM (we have our own firmware loader),
// but might be worth investigating for other controllers in the future.
// hci_set_chipset(btstack_chipset_bcm_instance());
}
void mp_bluetooth_btstack_port_deinit(void) {
hci_power_control(HCI_POWER_OFF);
}
void mp_bluetooth_btstack_port_start(void) {
hci_power_control(HCI_POWER_ON);
}
#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_BTSTACK
|