Skip to content

Commit 30dfbe5

Browse files
committed
alif: Integrate cyw43 Bluetooth with NimBLE.
Signed-off-by: Damien George <[email protected]>
1 parent d6e3342 commit 30dfbe5

File tree

8 files changed

+333
-0
lines changed

8 files changed

+333
-0
lines changed

ports/alif/alif.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ ifeq ($(MICROPY_SSL_MBEDTLS),1)
142142
SRC_C += mbedtls/mbedtls_port.c
143143
endif
144144

145+
ifeq ($(MICROPY_PY_BLUETOOTH),1)
146+
SRC_C += mpbthciport.c mpnimbleport.c
147+
endif
148+
145149
ifeq ($(MICROPY_FLOAT_IMPL),float)
146150
LIBM_SRC_C += $(SRC_LIB_LIBM_C)
147151
LIBM_SRC_C += $(SRC_LIB_LIBM_SQRT_HW_C)

ports/alif/cyw43_configport.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
#include "py/mphal.h"
3333
#include "py/runtime.h"
3434
#include "extmod/modnetwork.h"
35+
#include "extmod/mpbthci.h"
3536
#include "pendsv.h"
3637

3738
#ifndef static_assert
3839
#define static_assert(expr, msg) typedef int static_assert_##__LINE__[(expr) ? 1 : -1]
3940
#endif
4041

4142
#define CYW43_USE_SPI (1)
43+
#define CYW43_ENABLE_BLUETOOTH_OVER_UART (1)
4244
#define CYW43_LWIP (1)
4345
#define CYW43_USE_STATS (0)
4446
#define CYW43_PRINTF(...) mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__)
@@ -78,6 +80,7 @@
7880
#define CYW43_HAL_PIN_PULL_NONE 0
7981

8082
#define CYW43_HAL_MAC_WLAN0 MP_HAL_MAC_WLAN0
83+
#define CYW43_HAL_MAC_BDADDR MP_HAL_MAC_BDADDR
8184

8285
#define cyw43_hal_ticks_us mp_hal_ticks_us
8386
#define cyw43_hal_ticks_ms mp_hal_ticks_ms
@@ -87,13 +90,22 @@
8790
#define cyw43_hal_pin_low mp_hal_pin_low
8891
#define cyw43_hal_pin_high mp_hal_pin_high
8992

93+
#define cyw43_hal_uart_set_baudrate mp_bluetooth_hci_uart_set_baudrate
94+
#define cyw43_hal_uart_write mp_bluetooth_hci_uart_write
95+
#define cyw43_hal_uart_readchar mp_bluetooth_hci_uart_readchar
96+
9097
#define cyw43_hal_get_mac mp_hal_get_mac
9198
#define cyw43_hal_get_mac_ascii mp_hal_get_mac_ascii
9299
#define cyw43_hal_generate_laa_mac mp_hal_generate_laa_mac
93100

94101
#define CYW43_PIN_WL_REG_ON pin_WL_REG_ON
95102
#define CYW43_PIN_WL_IRQ pin_WL_IRQ
96103

104+
#define CYW43_PIN_BT_REG_ON pin_BT_REG_ON
105+
#define CYW43_PIN_BT_HOST_WAKE pin_BT_HOST_WAKE
106+
#define CYW43_PIN_BT_DEV_WAKE pin_BT_DEV_WAKE
107+
#define CYW43_PIN_BT_CTS pin_BT_UART_CTS
108+
97109
#define cyw43_schedule_internal_poll_dispatch(func) pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, func)
98110

99111
void cyw43_post_poll_hook(void);
@@ -120,4 +132,10 @@ static inline void cyw43_hal_pin_config_irq_falling(mp_hal_pin_obj_t pin, bool e
120132
mp_hal_pin_config_irq_falling(pin, enable);
121133
}
122134

135+
#define cyw43_bluetooth_controller_init mp_bluetooth_hci_controller_init
136+
#define cyw43_bluetooth_controller_deinit mp_bluetooth_hci_controller_deinit
137+
#define cyw43_bluetooth_controller_sleep_maybe mp_bluetooth_hci_controller_sleep_maybe
138+
#define cyw43_bluetooth_controller_woken mp_bluetooth_hci_controller_woken
139+
#define cyw43_bluetooth_controller_wakeup mp_bluetooth_hci_controller_wakeup
140+
123141
#endif // MICROPY_INCLUDED_ALIF_CYW43_CONFIGPORT_H

ports/alif/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030
#include "py/mperrno.h"
3131
#include "py/mphal.h"
3232
#include "py/stackctrl.h"
33+
#include "extmod/modbluetooth.h"
3334
#include "extmod/modnetwork.h"
3435
#include "shared/readline/readline.h"
3536
#include "shared/runtime/gchelper.h"
3637
#include "shared/runtime/pyexec.h"
3738
#include "shared/runtime/softtimer.h"
3839
#include "shared/tinyusb/mp_usbd.h"
3940
#include "tusb.h"
41+
#include "mpbthciport.h"
4042
#include "mpuart.h"
4143
#include "ospi_flash.h"
4244
#include "pendsv.h"
@@ -103,6 +105,10 @@ int main(void) {
103105
mod_network_lwip_init();
104106
#endif
105107

108+
#if MICROPY_PY_BLUETOOTH
109+
mp_bluetooth_hci_init();
110+
#endif
111+
106112
#if MICROPY_PY_NETWORK_CYW43
107113
{
108114
cyw43_init(&cyw43_state);
@@ -154,6 +160,9 @@ int main(void) {
154160

155161
soft_reset_exit:
156162
mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n");
163+
#if MICROPY_PY_BLUETOOTH
164+
mp_bluetooth_deinit();
165+
#endif
157166
soft_timer_deinit();
158167
gc_sweep_all();
159168
mp_deinit();

ports/alif/mpbthciport.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2025 OpenMV LLC.
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mphal.h"
28+
#include "py/runtime.h"
29+
#include "extmod/mpbthci.h"
30+
#include "shared/runtime/softtimer.h"
31+
#include "mpbthciport.h"
32+
33+
#if MICROPY_PY_BLUETOOTH
34+
35+
uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
36+
37+
// Soft timer and scheduling node for scheduling a HCI poll.
38+
static soft_timer_entry_t mp_bluetooth_hci_soft_timer;
39+
static mp_sched_node_t mp_bluetooth_hci_sched_node;
40+
41+
// This is called by soft_timer and executes at IRQ_PRI_PENDSV.
42+
static void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) {
43+
mp_bluetooth_hci_poll_now();
44+
}
45+
46+
void mp_bluetooth_hci_init(void) {
47+
soft_timer_static_init(
48+
&mp_bluetooth_hci_soft_timer,
49+
SOFT_TIMER_MODE_ONE_SHOT,
50+
0,
51+
mp_bluetooth_hci_soft_timer_callback
52+
);
53+
}
54+
55+
static void mp_bluetooth_hci_start_polling(void) {
56+
mp_bluetooth_hci_poll_now();
57+
}
58+
59+
void mp_bluetooth_hci_poll_in_ms(uint32_t ms) {
60+
soft_timer_reinsert(&mp_bluetooth_hci_soft_timer, ms);
61+
}
62+
63+
// For synchronous mode, we run all BLE stack code inside a scheduled task.
64+
// This task is scheduled periodically via a timer, or immediately after UART RX IRQ.
65+
static void run_events_scheduled_task(mp_sched_node_t *node) {
66+
(void)node;
67+
// This will process all buffered HCI UART data, and run any callouts or events.
68+
mp_bluetooth_hci_poll();
69+
}
70+
71+
// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to
72+
// request that processing happens ASAP in the scheduler.
73+
void mp_bluetooth_hci_poll_now(void) {
74+
mp_sched_schedule_node(&mp_bluetooth_hci_sched_node, run_events_scheduled_task);
75+
}
76+
77+
/******************************************************************************/
78+
// HCI over UART
79+
80+
#include "mpuart.h"
81+
82+
static uint32_t hci_uart_id;
83+
static bool hci_uart_first_char;
84+
static uint8_t hci_rx_ringbuf_array[768];
85+
static ringbuf_t hci_rx_ringbuf = {
86+
.buf = hci_rx_ringbuf_array,
87+
.size = sizeof(hci_rx_ringbuf_array),
88+
.iget = 0,
89+
.iput = 0,
90+
};
91+
92+
int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
93+
hci_uart_id = port;
94+
hci_uart_first_char = true;
95+
96+
// Initialise the UART.
97+
mp_uart_init(hci_uart_id, baudrate, pin_BT_UART_TX, pin_BT_UART_RX, &hci_rx_ringbuf);
98+
mp_uart_set_flow(hci_uart_id, pin_BT_UART_RTS, pin_BT_UART_CTS);
99+
mp_uart_set_irq_callback(hci_uart_id, mp_bluetooth_hci_poll_now);
100+
101+
// Start the HCI polling to process any initial events/packets.
102+
mp_bluetooth_hci_start_polling();
103+
104+
return 0;
105+
}
106+
107+
int mp_bluetooth_hci_uart_deinit(void) {
108+
mp_uart_deinit(hci_uart_id);
109+
return 0;
110+
}
111+
112+
int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) {
113+
mp_uart_set_baudrate(hci_uart_id, baudrate);
114+
return 0;
115+
}
116+
117+
int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) {
118+
mp_bluetooth_hci_controller_wakeup();
119+
mp_uart_tx_data(hci_uart_id, (void *)buf, len);
120+
return 0;
121+
}
122+
123+
// This function expects the controller to be in the wake state via a previous call
124+
// to mp_bluetooth_hci_controller_woken.
125+
int mp_bluetooth_hci_uart_readchar(void) {
126+
if (mp_uart_rx_any(hci_uart_id)) {
127+
int c = mp_uart_rx_char(hci_uart_id);
128+
if (hci_uart_first_char) {
129+
if (c == 0) {
130+
return -1;
131+
}
132+
hci_uart_first_char = false;
133+
}
134+
return c;
135+
} else {
136+
return -1;
137+
}
138+
}
139+
140+
#endif // MICROPY_PY_BLUETOOTH

ports/alif/mpbthciport.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_ALIF_MPBTHCIPORT_H
27+
#define MICROPY_INCLUDED_ALIF_MPBTHCIPORT_H
28+
29+
#include "py/mpconfig.h"
30+
31+
// Initialise the HCI subsystem (should be called once, early on).
32+
void mp_bluetooth_hci_init(void);
33+
34+
// Call this to poll the HCI now.
35+
void mp_bluetooth_hci_poll_now(void);
36+
37+
// Call this to poll the HCI after a certain timeout.
38+
void mp_bluetooth_hci_poll_in_ms(uint32_t ms);
39+
40+
// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c).
41+
// Request new data from the uart and pass to the stack, and run pending events/callouts.
42+
// This is a low-level function and should not be called directly, use
43+
// mp_bluetooth_hci_poll_now/mp_bluetooth_hci_poll_in_ms instead.
44+
void mp_bluetooth_hci_poll(void);
45+
46+
#endif // MICROPY_INCLUDED_ALIF_MPBTHCIPORT_H

ports/alif/mpconfigport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ extern const struct _mp_obj_type_t mp_network_cyw43_type;
170170
MICROPY_HW_NIC_CYW43 \
171171
MICROPY_BOARD_NETWORK_INTERFACES \
172172

173+
// Bluetooth code only runs in the scheduler, no locking/mutex required.
174+
#define MICROPY_PY_BLUETOOTH_ENTER uint32_t atomic_state = 0;
175+
#define MICROPY_PY_BLUETOOTH_EXIT (void)atomic_state;
176+
#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
177+
#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (1)
178+
173179
#define MP_STATE_PORT MP_STATE_VM
174180

175181
// Miscellaneous settings

ports/alif/mpnimbleport.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Jim Mussared
7+
* Copyright (c) 2020 Damien P. George
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/runtime.h"
29+
#include "py/mperrno.h"
30+
#include "py/mphal.h"
31+
32+
#if MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
33+
34+
#define DEBUG_printf(...) // printf("mpnimbleport.c: " __VA_ARGS__)
35+
36+
#include "host/ble_hs.h"
37+
#include "nimble/nimble_npl.h"
38+
39+
#include "extmod/mpbthci.h"
40+
#include "extmod/modbluetooth.h"
41+
#include "extmod/nimble/modbluetooth_nimble.h"
42+
#include "extmod/nimble/hal/hal_uart.h"
43+
#include "mpbthciport.h"
44+
45+
// Get any pending data from the UART and send it to NimBLE's HCI buffers.
46+
// Any further processing by NimBLE will be run via its event queue.
47+
void mp_bluetooth_hci_poll(void) {
48+
if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) {
49+
// DEBUG_printf("mp_bluetooth_hci_poll_uart %d\n", mp_bluetooth_nimble_ble_state);
50+
51+
// Run any timers.
52+
mp_bluetooth_nimble_os_callout_process();
53+
54+
// Process incoming UART data, and run events as they are generated.
55+
mp_bluetooth_nimble_hci_uart_process(true);
56+
57+
// Run any remaining events (e.g. if there was no UART data).
58+
mp_bluetooth_nimble_os_eventq_run_all();
59+
}
60+
61+
if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
62+
// Call this function again in 128ms to check for new events.
63+
// TODO: improve this by only calling back when needed.
64+
mp_bluetooth_hci_poll_in_ms(128);
65+
}
66+
}
67+
68+
// --- Port-specific helpers for the generic NimBLE bindings. -----------------
69+
70+
void mp_bluetooth_nimble_hci_uart_wfi(void) {
71+
__WFE();
72+
73+
// This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK.
74+
// Do not need to run events here (it must not invoke Python code), only processing incoming HCI data.
75+
mp_bluetooth_nimble_hci_uart_process(false);
76+
}
77+
78+
#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE

0 commit comments

Comments
 (0)