29
29
30
30
#include "py/runtime.h"
31
31
#include "py/mphal.h"
32
- #include "pin_static_af.h"
33
- #include "uart.h"
34
32
#include "extmod/mpbthci.h"
35
33
36
34
#if MICROPY_PY_NETWORK_CYW43
37
35
38
36
#include "lib/cyw43-driver/src/cyw43_config.h"
39
37
#include "lib/cyw43-driver/firmware/cyw43_btfw_4343A1.h"
40
38
41
- // Provided by the port.
42
- extern pyb_uart_obj_t mp_bluetooth_hci_uart_obj ;
43
-
44
39
// Provided by the port, and also possibly shared with the stack.
45
40
extern uint8_t mp_bluetooth_hci_cmd_buf [4 + 256 ];
46
41
47
42
/******************************************************************************/
48
43
// CYW BT HCI low-level driver
49
44
45
+ #ifdef CYW43_PIN_BT_CTS
46
+ // This code is not portable and currently only builds on stm32 port.
47
+
48
+ #include "pin_static_af.h"
49
+ #include "uart.h"
50
+
51
+ // Provided by the port.
52
+ extern pyb_uart_obj_t mp_bluetooth_hci_uart_obj ;
53
+
50
54
STATIC void cywbt_wait_cts_low (void ) {
51
- mp_hal_pin_config (pyb_pin_BT_CTS , MP_HAL_PIN_MODE_INPUT , MP_HAL_PIN_PULL_UP , 0 );
55
+ mp_hal_pin_config (CYW43_PIN_BT_CTS , MP_HAL_PIN_MODE_INPUT , MP_HAL_PIN_PULL_UP , 0 );
52
56
for (int i = 0 ; i < 200 ; ++ i ) {
53
- if (mp_hal_pin_read (pyb_pin_BT_CTS ) == 0 ) {
57
+ if (mp_hal_pin_read (CYW43_PIN_BT_CTS ) == 0 ) {
54
58
break ;
55
59
}
56
60
mp_hal_delay_ms (1 );
57
61
}
58
- mp_hal_pin_config_alt (pyb_pin_BT_CTS , MP_HAL_PIN_MODE_ALT , MP_HAL_PIN_PULL_UP , AF_FN_UART , mp_bluetooth_hci_uart_obj .uart_id );
62
+ mp_hal_pin_config_alt (CYW43_PIN_BT_CTS , MP_HAL_PIN_MODE_ALT ,
63
+ MP_HAL_PIN_PULL_UP , AF_FN_UART , mp_bluetooth_hci_uart_obj .uart_id );
59
64
}
65
+ #endif
60
66
61
67
STATIC int cywbt_hci_cmd_raw (size_t len , uint8_t * buf ) {
62
- uart_tx_strn ( & mp_bluetooth_hci_uart_obj , (void * )buf , len );
63
- for (int i = 0 ; i < 6 ; ++ i ) {
64
- while (! uart_rx_any ( & mp_bluetooth_hci_uart_obj ) ) {
68
+ mp_bluetooth_hci_uart_write ( (void * )buf , len );
69
+ for (int c , i = 0 ; i < 6 ; ++ i ) {
70
+ while (( c = mp_bluetooth_hci_uart_readchar ()) == -1 ) {
65
71
MICROPY_EVENT_POLL_HOOK
66
72
}
67
- buf [i ] = uart_rx_char ( & mp_bluetooth_hci_uart_obj ) ;
73
+ buf [i ] = c ;
68
74
}
69
75
70
76
// expect a command complete event (event 0x0e)
@@ -80,11 +86,11 @@ STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) {
80
86
*/
81
87
82
88
int sz = buf [2 ] - 3 ;
83
- for (int i = 0 ; i < sz ; ++ i ) {
84
- while (! uart_rx_any ( & mp_bluetooth_hci_uart_obj ) ) {
89
+ for (int c , i = 0 ; i < sz ; ++ i ) {
90
+ while (( c = mp_bluetooth_hci_uart_readchar ()) == -1 ) {
85
91
MICROPY_EVENT_POLL_HOOK
86
92
}
87
- buf [i ] = uart_rx_char ( & mp_bluetooth_hci_uart_obj ) ;
93
+ buf [i ] = c ;
88
94
}
89
95
90
96
return 0 ;
@@ -150,12 +156,15 @@ STATIC int cywbt_download_firmware(const uint8_t *firmware) {
150
156
151
157
// RF switch must select high path during BT patch boot
152
158
#if MICROPY_HW_ENABLE_RF_SWITCH
153
- mp_hal_pin_config (pyb_pin_WL_GPIO_1 , MP_HAL_PIN_MODE_INPUT , MP_HAL_PIN_PULL_UP , 0 );
159
+ mp_hal_pin_config (CYW43_PIN_WL_GPIO_1 , MP_HAL_PIN_MODE_INPUT , MP_HAL_PIN_PULL_UP , 0 );
154
160
#endif
155
161
mp_hal_delay_ms (10 ); // give some time for CTS to go high
162
+ #ifdef CYW43_PIN_BT_CTS
156
163
cywbt_wait_cts_low ();
164
+ #endif
157
165
#if MICROPY_HW_ENABLE_RF_SWITCH
158
- mp_hal_pin_config (pyb_pin_WL_GPIO_1 , MP_HAL_PIN_MODE_INPUT , MP_HAL_PIN_PULL_DOWN , 0 ); // Select chip antenna (could also select external)
166
+ // Select chip antenna (could also select external)
167
+ mp_hal_pin_config (CYW43_PIN_WL_GPIO_1 , MP_HAL_PIN_MODE_INPUT , MP_HAL_PIN_PULL_DOWN , 0 );
159
168
#endif
160
169
161
170
mp_bluetooth_hci_uart_set_baudrate (115200 );
@@ -168,25 +177,33 @@ STATIC int cywbt_download_firmware(const uint8_t *firmware) {
168
177
int mp_bluetooth_hci_controller_init (void ) {
169
178
// This is called immediately after the UART is initialised during stack initialisation.
170
179
171
- mp_hal_pin_output (pyb_pin_BT_REG_ON );
172
- mp_hal_pin_low (pyb_pin_BT_REG_ON );
173
- mp_hal_pin_input (pyb_pin_BT_HOST_WAKE );
174
- mp_hal_pin_output (pyb_pin_BT_DEV_WAKE );
175
- mp_hal_pin_low (pyb_pin_BT_DEV_WAKE );
180
+ mp_hal_pin_output (CYW43_PIN_BT_REG_ON );
181
+ mp_hal_pin_low (CYW43_PIN_BT_REG_ON );
182
+ #ifdef CYW43_PIN_BT_HOST_WAKE
183
+ mp_hal_pin_input (CYW43_PIN_BT_HOST_WAKE );
184
+ #endif
185
+ #ifdef CYW43_PIN_BT_DEV_WAKE
186
+ mp_hal_pin_output (CYW43_PIN_BT_DEV_WAKE );
187
+ mp_hal_pin_low (CYW43_PIN_BT_DEV_WAKE );
188
+ #endif
176
189
177
190
#if MICROPY_HW_ENABLE_RF_SWITCH
178
191
// TODO don't select antenna if wifi is enabled
179
- mp_hal_pin_config (pyb_pin_WL_GPIO_4 , MP_HAL_PIN_MODE_OUTPUT , MP_HAL_PIN_PULL_NONE , 0 ); // RF-switch power
180
- mp_hal_pin_high (pyb_pin_WL_GPIO_4 ); // Turn the RF-switch on
192
+ mp_hal_pin_config (CYW43_PIN_WL_GPIO_4 , MP_HAL_PIN_MODE_OUTPUT , MP_HAL_PIN_PULL_NONE , 0 ); // RF-switch power
193
+ mp_hal_pin_high (CYW43_PIN_WL_GPIO_4 ); // Turn the RF-switch on
181
194
#endif
182
195
183
196
uint8_t buf [256 ];
184
197
185
- mp_hal_pin_low (pyb_pin_BT_REG_ON );
198
+ mp_hal_pin_low (CYW43_PIN_BT_REG_ON );
186
199
mp_bluetooth_hci_uart_set_baudrate (115200 );
187
200
mp_hal_delay_ms (100 );
188
- mp_hal_pin_high (pyb_pin_BT_REG_ON );
201
+ mp_hal_pin_high (CYW43_PIN_BT_REG_ON );
202
+ #ifdef CYW43_PIN_BT_CTS
189
203
cywbt_wait_cts_low ();
204
+ #else
205
+ mp_hal_delay_ms (100 );
206
+ #endif
190
207
191
208
// Reset
192
209
cywbt_hci_cmd (0x03 , 0x0003 , 0 , NULL );
@@ -197,7 +214,7 @@ int mp_bluetooth_hci_controller_init(void) {
197
214
mp_bluetooth_hci_uart_set_baudrate (MICROPY_HW_BLE_UART_BAUDRATE_DOWNLOAD_FIRMWARE );
198
215
#endif
199
216
200
- cywbt_download_firmware ((const uint8_t * )& cyw43_btfw_4343A1 [0 ]);
217
+ cywbt_download_firmware ((const uint8_t * )& cyw43_btfw_4343A1 [0 ]);
201
218
202
219
// Reset
203
220
cywbt_hci_cmd (0x03 , 0x0003 , 0 , NULL );
@@ -219,40 +236,42 @@ int mp_bluetooth_hci_controller_init(void) {
219
236
// cywbt_hci_cmd(0x03, 0x0013, 248, buf);
220
237
221
238
// Configure sleep mode
222
- cywbt_hci_cmd (0x3f , 0x27 , 12 , (const uint8_t * )"\x01\x02\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00" );
239
+ cywbt_hci_cmd (0x3f , 0x27 , 12 , (const uint8_t * )"\x01\x02\x02\x00\x00\x00\x01\x00\x00\x00\x00\x00" );
223
240
224
241
// HCI_Write_LE_Host_Support
225
- cywbt_hci_cmd (3 , 109 , 2 , (const uint8_t * )"\x01\x00" );
242
+ cywbt_hci_cmd (3 , 109 , 2 , (const uint8_t * )"\x01\x00" );
226
243
227
- mp_hal_pin_high (pyb_pin_BT_DEV_WAKE ); // let sleep
244
+ #ifdef CYW43_PIN_BT_DEV_WAKE
245
+ mp_hal_pin_high (CYW43_PIN_BT_DEV_WAKE ); // let sleep
246
+ #endif
228
247
229
248
return 0 ;
230
249
}
231
250
232
251
int mp_bluetooth_hci_controller_deinit (void ) {
233
- mp_hal_pin_low (pyb_pin_BT_REG_ON );
252
+ mp_hal_pin_low (CYW43_PIN_BT_REG_ON );
234
253
235
254
return 0 ;
236
255
}
237
256
238
- #ifdef pyb_pin_BT_DEV_WAKE
257
+ #ifdef CYW43_PIN_BT_DEV_WAKE
239
258
STATIC uint32_t bt_sleep_ticks ;
240
259
#endif
241
260
242
261
int mp_bluetooth_hci_controller_sleep_maybe (void ) {
243
- #ifdef pyb_pin_BT_DEV_WAKE
244
- if (mp_hal_pin_read (pyb_pin_BT_DEV_WAKE ) == 0 ) {
262
+ #ifdef CYW43_PIN_BT_DEV_WAKE
263
+ if (mp_hal_pin_read (CYW43_PIN_BT_DEV_WAKE ) == 0 ) {
245
264
if (mp_hal_ticks_ms () - bt_sleep_ticks > 500 ) {
246
- mp_hal_pin_high (pyb_pin_BT_DEV_WAKE ); // let sleep
265
+ mp_hal_pin_high (CYW43_PIN_BT_DEV_WAKE ); // let sleep
247
266
}
248
267
}
249
268
#endif
250
269
return 0 ;
251
270
}
252
271
253
272
bool mp_bluetooth_hci_controller_woken (void ) {
254
- #ifdef pyb_pin_BT_HOST_WAKE
255
- bool host_wake = mp_hal_pin_read (pyb_pin_BT_HOST_WAKE );
273
+ #ifdef CYW43_PIN_BT_HOST_WAKE
274
+ bool host_wake = mp_hal_pin_read (CYW43_PIN_BT_HOST_WAKE );
256
275
/*
257
276
// this is just for info/tracing purposes
258
277
static bool last_host_wake = false;
@@ -268,11 +287,11 @@ bool mp_bluetooth_hci_controller_woken(void) {
268
287
}
269
288
270
289
int mp_bluetooth_hci_controller_wakeup (void ) {
271
- #ifdef pyb_pin_BT_DEV_WAKE
290
+ #ifdef CYW43_PIN_BT_DEV_WAKE
272
291
bt_sleep_ticks = mp_hal_ticks_ms ();
273
292
274
- if (mp_hal_pin_read (pyb_pin_BT_DEV_WAKE ) == 1 ) {
275
- mp_hal_pin_low (pyb_pin_BT_DEV_WAKE ); // wake up
293
+ if (mp_hal_pin_read (CYW43_PIN_BT_DEV_WAKE ) == 1 ) {
294
+ mp_hal_pin_low (CYW43_PIN_BT_DEV_WAKE ); // wake up
276
295
// Use delay_us rather than delay_ms to prevent running the scheduler (which
277
296
// might result in more BLE operations).
278
297
mp_hal_delay_us (5000 ); // can't go lower than this
0 commit comments