Skip to content

Commit a453b4f

Browse files
iabdalkaderdpgeorge
authored andcommitted
drivers/cyw43: Make the CYW43 Bluetooth HCI driver more portable.
This commit allows other ports to reuse the CYW43 HCI driver, by replacing all Bluetooth UART and control named pins with defines in config files and using `mpbthci` abstract functions (i.e. `mp_bluetooth_hci_*`) instead of the STM32 specific UART functions. Note: the function `cywbt_wait_cts_low` does not need to switch the CTS pin from alternate function to GPIO to read it. At least on stm32, mimxrt it's possible to just read the pin input. For example, see the STM32F7 RM0410 section 6.3.11, and the `SION` for IMXRT. So this function can also be available for other ports if the pin mode switching is removed. Signed-off-by: iabdalkader <[email protected]>
1 parent fc49524 commit a453b4f

File tree

2 files changed

+66
-40
lines changed

2 files changed

+66
-40
lines changed

drivers/cyw43/cywbt.c

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,42 +29,48 @@
2929

3030
#include "py/runtime.h"
3131
#include "py/mphal.h"
32-
#include "pin_static_af.h"
33-
#include "uart.h"
3432
#include "extmod/mpbthci.h"
3533

3634
#if MICROPY_PY_NETWORK_CYW43
3735

3836
#include "lib/cyw43-driver/src/cyw43_config.h"
3937
#include "lib/cyw43-driver/firmware/cyw43_btfw_4343A1.h"
4038

41-
// Provided by the port.
42-
extern pyb_uart_obj_t mp_bluetooth_hci_uart_obj;
43-
4439
// Provided by the port, and also possibly shared with the stack.
4540
extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
4641

4742
/******************************************************************************/
4843
// CYW BT HCI low-level driver
4944

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+
5054
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);
5256
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) {
5458
break;
5559
}
5660
mp_hal_delay_ms(1);
5761
}
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);
5964
}
65+
#endif
6066

6167
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) {
6571
MICROPY_EVENT_POLL_HOOK
6672
}
67-
buf[i] = uart_rx_char(&mp_bluetooth_hci_uart_obj);
73+
buf[i] = c;
6874
}
6975

7076
// expect a command complete event (event 0x0e)
@@ -80,11 +86,11 @@ STATIC int cywbt_hci_cmd_raw(size_t len, uint8_t *buf) {
8086
*/
8187

8288
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) {
8591
MICROPY_EVENT_POLL_HOOK
8692
}
87-
buf[i] = uart_rx_char(&mp_bluetooth_hci_uart_obj);
93+
buf[i] = c;
8894
}
8995

9096
return 0;
@@ -150,12 +156,15 @@ STATIC int cywbt_download_firmware(const uint8_t *firmware) {
150156

151157
// RF switch must select high path during BT patch boot
152158
#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);
154160
#endif
155161
mp_hal_delay_ms(10); // give some time for CTS to go high
162+
#ifdef CYW43_PIN_BT_CTS
156163
cywbt_wait_cts_low();
164+
#endif
157165
#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);
159168
#endif
160169

161170
mp_bluetooth_hci_uart_set_baudrate(115200);
@@ -168,25 +177,33 @@ STATIC int cywbt_download_firmware(const uint8_t *firmware) {
168177
int mp_bluetooth_hci_controller_init(void) {
169178
// This is called immediately after the UART is initialised during stack initialisation.
170179

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
176189

177190
#if MICROPY_HW_ENABLE_RF_SWITCH
178191
// 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
181194
#endif
182195

183196
uint8_t buf[256];
184197

185-
mp_hal_pin_low(pyb_pin_BT_REG_ON);
198+
mp_hal_pin_low(CYW43_PIN_BT_REG_ON);
186199
mp_bluetooth_hci_uart_set_baudrate(115200);
187200
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
189203
cywbt_wait_cts_low();
204+
#else
205+
mp_hal_delay_ms(100);
206+
#endif
190207

191208
// Reset
192209
cywbt_hci_cmd(0x03, 0x0003, 0, NULL);
@@ -197,7 +214,7 @@ int mp_bluetooth_hci_controller_init(void) {
197214
mp_bluetooth_hci_uart_set_baudrate(MICROPY_HW_BLE_UART_BAUDRATE_DOWNLOAD_FIRMWARE);
198215
#endif
199216

200-
cywbt_download_firmware((const uint8_t*)&cyw43_btfw_4343A1[0]);
217+
cywbt_download_firmware((const uint8_t *)&cyw43_btfw_4343A1[0]);
201218

202219
// Reset
203220
cywbt_hci_cmd(0x03, 0x0003, 0, NULL);
@@ -219,40 +236,42 @@ int mp_bluetooth_hci_controller_init(void) {
219236
// cywbt_hci_cmd(0x03, 0x0013, 248, buf);
220237

221238
// 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");
223240

224241
// 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");
226243

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
228247

229248
return 0;
230249
}
231250

232251
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);
234253

235254
return 0;
236255
}
237256

238-
#ifdef pyb_pin_BT_DEV_WAKE
257+
#ifdef CYW43_PIN_BT_DEV_WAKE
239258
STATIC uint32_t bt_sleep_ticks;
240259
#endif
241260

242261
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) {
245264
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
247266
}
248267
}
249268
#endif
250269
return 0;
251270
}
252271

253272
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);
256275
/*
257276
// this is just for info/tracing purposes
258277
static bool last_host_wake = false;
@@ -268,11 +287,11 @@ bool mp_bluetooth_hci_controller_woken(void) {
268287
}
269288

270289
int mp_bluetooth_hci_controller_wakeup(void) {
271-
#ifdef pyb_pin_BT_DEV_WAKE
290+
#ifdef CYW43_PIN_BT_DEV_WAKE
272291
bt_sleep_ticks = mp_hal_ticks_ms();
273292

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
276295
// Use delay_us rather than delay_ms to prevent running the scheduler (which
277296
// might result in more BLE operations).
278297
mp_hal_delay_us(5000); // can't go lower than this

ports/stm32/cyw43_configport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@
9393
#define CYW43_PIN_WL_REG_ON pyb_pin_WL_REG_ON
9494
#define CYW43_PIN_WL_HOST_WAKE pyb_pin_WL_HOST_WAKE
9595
#define CYW43_PIN_WL_SDIO_1 pyb_pin_WL_SDIO_1
96+
#define CYW43_PIN_WL_GPIO_1 pyb_pin_WL_GPIO_1
97+
#define CYW43_PIN_WL_GPIO_4 pyb_pin_WL_GPIO_4
98+
99+
#define CYW43_PIN_BT_REG_ON pyb_pin_BT_REG_ON
100+
#define CYW43_PIN_BT_HOST_WAKE pyb_pin_BT_HOST_WAKE
101+
#define CYW43_PIN_BT_DEV_WAKE pyb_pin_BT_DEV_WAKE
102+
#define CYW43_PIN_BT_CTS pyb_pin_BT_CTS
96103

97104
#if MICROPY_HW_ENABLE_RF_SWITCH
98105
#define CYW43_PIN_WL_RFSW_VDD pyb_pin_WL_RFSW_VDD

0 commit comments

Comments
 (0)