Skip to content

Commit fc49524

Browse files
iabdalkaderdpgeorge
authored andcommitted
mimxrt: Integrate Bluetooth support with NimBLE bindings.
This commit adds the necessary functions to get NimBLE working with the mimxrt port. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 772a360 commit fc49524

File tree

7 files changed

+376
-0
lines changed

7 files changed

+376
-0
lines changed

ports/mimxrt/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ else
264264
SRC_HAL_C += hal/$(FLEXSPI_FLASH_TYPE)_config.c
265265
endif
266266

267+
ifeq ($(MICROPY_PY_BLUETOOTH),1)
268+
SRC_C += mpbthciport.c
269+
endif # MICROPY_PY_BLUETOOTH
270+
271+
ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1)
272+
SRC_C += mpnimbleport.c
273+
endif
274+
267275
# Math library source files
268276
ifeq ($(MICROPY_FLOAT_IMPL),double)
269277
LIBM_SRC_C += $(addprefix lib/libm_dbl/,\
@@ -449,6 +457,7 @@ OBJ += $(PY_O)
449457
OBJ += $(LIBM_O)
450458
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
451459
OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
460+
OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o))
452461
OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o))
453462
OBJ += $(addprefix $(BUILD)/, $(SRC_SS:.S=.o))
454463
OBJ += $(addprefix $(BUILD)/, $(SRC_TINYUSB_C:.c=.o))

ports/mimxrt/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
#include "lib/cyw43-driver/src/cyw43.h"
4848
#endif
4949
#endif
50+
51+
#if MICROPY_PY_BLUETOOTH
52+
#include "mpbthciport.h"
53+
#include "extmod/modbluetooth.h"
54+
#endif
55+
5056
#include "systick.h"
5157
#include "extmod/modnetwork.h"
5258

@@ -71,6 +77,9 @@ int main(void) {
7177

7278
systick_enable_dispatch(SYSTICK_DISPATCH_LWIP, mod_network_lwip_poll_wrapper);
7379
#endif
80+
#if MICROPY_PY_BLUETOOTH
81+
mp_bluetooth_hci_init();
82+
#endif
7483

7584
#if MICROPY_PY_NETWORK_CYW43
7685
{
@@ -135,6 +144,9 @@ int main(void) {
135144
#if MICROPY_PY_MACHINE_I2S
136145
machine_i2s_deinit_all();
137146
#endif
147+
#if MICROPY_PY_BLUETOOTH
148+
mp_bluetooth_deinit();
149+
#endif
138150
#if MICROPY_PY_NETWORK
139151
mod_network_deinit();
140152
#endif

ports/mimxrt/mpbthciport.c

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2021 Damien P. George
7+
* Copyright (c) 2023 Ibrahim Abdelkader <iabdalkader@openmv.io>
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/stream.h"
30+
#include "py/mphal.h"
31+
#include "extmod/modbluetooth.h"
32+
#include "extmod/mpbthci.h"
33+
#include "shared/runtime/softtimer.h"
34+
#include "modmachine.h"
35+
#include "mpbthciport.h"
36+
37+
#include "fsl_lpuart.h"
38+
#include CLOCK_CONFIG_H
39+
40+
#if MICROPY_PY_BLUETOOTH
41+
42+
#define DEBUG_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
43+
#define ERROR_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
44+
45+
uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
46+
47+
STATIC mp_sched_node_t mp_bluetooth_hci_sched_node;
48+
STATIC soft_timer_entry_t mp_bluetooth_hci_soft_timer;
49+
50+
STATIC void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) {
51+
mp_bluetooth_hci_poll_now();
52+
}
53+
54+
void mp_bluetooth_hci_init(void) {
55+
soft_timer_static_init(
56+
&mp_bluetooth_hci_soft_timer,
57+
SOFT_TIMER_MODE_ONE_SHOT,
58+
0,
59+
mp_bluetooth_hci_soft_timer_callback
60+
);
61+
}
62+
63+
STATIC void mp_bluetooth_hci_start_polling(void) {
64+
mp_bluetooth_hci_poll_now();
65+
}
66+
67+
void mp_bluetooth_hci_poll_in_ms(uint32_t ms) {
68+
soft_timer_reinsert(&mp_bluetooth_hci_soft_timer, ms);
69+
}
70+
71+
// For synchronous mode, we run all BLE stack code inside a scheduled task.
72+
STATIC void run_events_scheduled_task(mp_sched_node_t *node) {
73+
// This will process all buffered HCI UART data, and run any callouts or events.
74+
mp_bluetooth_hci_poll();
75+
}
76+
77+
// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to
78+
// request that processing happens ASAP in the scheduler.
79+
void mp_bluetooth_hci_poll_now(void) {
80+
mp_sched_schedule_node(&mp_bluetooth_hci_sched_node, run_events_scheduled_task);
81+
}
82+
83+
mp_obj_t mp_bthci_uart;
84+
85+
int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
86+
DEBUG_printf("mp_bluetooth_hci_uart_init\n");
87+
88+
mp_obj_t args[] = {
89+
MP_OBJ_NEW_SMALL_INT(port),
90+
MP_OBJ_NEW_QSTR(MP_QSTR_baudrate), MP_OBJ_NEW_SMALL_INT(baudrate),
91+
MP_OBJ_NEW_QSTR(MP_QSTR_timeout), MP_OBJ_NEW_SMALL_INT(200),
92+
MP_OBJ_NEW_QSTR(MP_QSTR_timeout_char), MP_OBJ_NEW_SMALL_INT(200),
93+
MP_OBJ_NEW_QSTR(MP_QSTR_txbuf), MP_OBJ_NEW_SMALL_INT(768),
94+
MP_OBJ_NEW_QSTR(MP_QSTR_rxbuf), MP_OBJ_NEW_SMALL_INT(768),
95+
};
96+
97+
mp_bthci_uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, 1, 5, args);
98+
MP_STATE_PORT(mp_bthci_uart) = mp_bthci_uart;
99+
100+
// Start the HCI polling to process any initial events/packets.
101+
mp_bluetooth_hci_start_polling();
102+
return 0;
103+
}
104+
105+
int mp_bluetooth_hci_uart_deinit(void) {
106+
DEBUG_printf("mp_bluetooth_hci_uart_deinit\n");
107+
mp_bthci_uart = MP_OBJ_NULL;
108+
return 0;
109+
}
110+
111+
int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) {
112+
DEBUG_printf("mp_bluetooth_hci_uart_set_baudrate(%lu)\n", baudrate);
113+
if (mp_bthci_uart != MP_OBJ_NULL) {
114+
// This struct is not public, so we use the base defined in board config files.
115+
// machine_uart_obj_t uart = (machine_uart_obj_t *) MP_PTR_FROM_OBJ(mp_bthci_uart);
116+
LPUART_SetBaudRate(MICROPY_HW_BLE_UART_BASE, baudrate, BOARD_BOOTCLOCKRUN_UART_CLK_ROOT);
117+
}
118+
return 0;
119+
}
120+
121+
int mp_bluetooth_hci_uart_any(void) {
122+
int errcode = 0;
123+
const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol);
124+
125+
mp_uint_t ret = proto->ioctl(mp_bthci_uart, MP_STREAM_POLL, MP_STREAM_POLL_RD, &errcode);
126+
if (errcode != 0) {
127+
ERROR_printf("Uart ioctl failed to poll UART %d\n", errcode);
128+
return -1;
129+
}
130+
return ret & MP_STREAM_POLL_RD;
131+
}
132+
133+
int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) {
134+
DEBUG_printf("mp_bluetooth_hci_uart_write\n");
135+
136+
int errcode = 0;
137+
const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol);
138+
139+
mp_bluetooth_hci_controller_wakeup();
140+
141+
if (proto->write(mp_bthci_uart, (void *)buf, len, &errcode) < 0) {
142+
ERROR_printf("mp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode);
143+
}
144+
return 0;
145+
}
146+
147+
// This function expects the controller to be in the wake state via a previous call
148+
// to mp_bluetooth_hci_controller_woken.
149+
int mp_bluetooth_hci_uart_readchar(void) {
150+
DEBUG_printf("mp_bluetooth_hci_uart_readchar\n");
151+
if (mp_bluetooth_hci_uart_any()) {
152+
int errcode = 0;
153+
uint8_t buf = 0;
154+
const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol);
155+
if (proto->read(mp_bthci_uart, (void *)&buf, 1, &errcode) < 0) {
156+
ERROR_printf("mp_bluetooth_hci_uart_readchar: failed to read UART %d\n", errcode);
157+
return -1;
158+
}
159+
return buf;
160+
} else {
161+
DEBUG_printf("mp_bluetooth_hci_uart_readchar: not ready\n");
162+
return -1;
163+
}
164+
}
165+
166+
// Default (weak) implementation of the HCI controller interface.
167+
// A driver (e.g. cywbt43.c) can override these for controller-specific
168+
// functionality (i.e. power management).
169+
MP_WEAK int mp_bluetooth_hci_controller_init(void) {
170+
DEBUG_printf("mp_bluetooth_hci_controller_init (default)\n");
171+
return 0;
172+
}
173+
174+
MP_WEAK int mp_bluetooth_hci_controller_deinit(void) {
175+
DEBUG_printf("mp_bluetooth_hci_controller_deinit (default)\n");
176+
return 0;
177+
}
178+
179+
MP_WEAK int mp_bluetooth_hci_controller_sleep_maybe(void) {
180+
DEBUG_printf("mp_bluetooth_hci_controller_sleep_maybe (default)\n");
181+
return 0;
182+
}
183+
184+
MP_WEAK bool mp_bluetooth_hci_controller_woken(void) {
185+
DEBUG_printf("mp_bluetooth_hci_controller_woken (default)\n");
186+
return true;
187+
}
188+
189+
MP_WEAK int mp_bluetooth_hci_controller_wakeup(void) {
190+
DEBUG_printf("mp_bluetooth_hci_controller_wakeup (default)\n");
191+
return 0;
192+
}
193+
194+
MP_REGISTER_ROOT_POINTER(mp_obj_t mp_bthci_uart);
195+
196+
#endif // MICROPY_PY_BLUETOOTH

ports/mimxrt/mpbthciport.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-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_MIMXRT_MPBTHCIPORT_H
27+
#define MICROPY_INCLUDED_MIMXRT_MPBTHCIPORT_H
28+
29+
// Initialise the HCI subsystem (should be called once, early on).
30+
void mp_bluetooth_hci_init(void);
31+
32+
// Poll the HCI now, or after a certain timeout.
33+
void mp_bluetooth_hci_poll_now(void);
34+
void mp_bluetooth_hci_poll_in_ms(uint32_t ms);
35+
36+
// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c).
37+
// Request new data from the uart and pass to the stack, and run pending events/callouts.
38+
// This is a low-level function and should not be called directly, use
39+
// mp_bluetooth_hci_poll_now/mp_bluetooth_hci_poll_in_ms instead.
40+
void mp_bluetooth_hci_poll(void);
41+
42+
#endif // MICROPY_INCLUDED_MIMXRT_MPBTHCIPORT_H

ports/mimxrt/mpconfigport.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ uint32_t trng_random_u32(void);
5959
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
6060
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
6161
#define MICROPY_SCHEDULER_DEPTH (8)
62+
#define MICROPY_SCHEDULER_STATIC_NODES (1)
6263
#define MICROPY_VFS (1)
6364

6465
// Control over Python builtins
@@ -118,6 +119,14 @@ uint32_t trng_random_u32(void);
118119
#define MICROPY_PY_LWIP_REENTER MICROPY_PY_PENDSV_REENTER
119120
#define MICROPY_PY_LWIP_EXIT MICROPY_PY_PENDSV_EXIT
120121

122+
#ifndef MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
123+
#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
124+
#endif
125+
126+
#ifndef MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
127+
#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (MICROPY_BLUETOOTH_NIMBLE)
128+
#endif
129+
121130
#ifndef MICROPY_PY_NETWORK_HOSTNAME_DEFAULT
122131
#define MICROPY_PY_NETWORK_HOSTNAME_DEFAULT "mpy-mimxrt"
123132
#endif

ports/mimxrt/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+
__WFI();
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy