summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriabdalkader <i.abdalkader@gmail.com>2023-05-26 15:39:36 +0200
committerDamien George <damien@micropython.org>2023-09-14 23:50:47 +1000
commit5b774517dca780a074057e38e9511ef41a9100f7 (patch)
tree178e43e9ffac9a8f82ee8c15b0e4082b69064a9a
parentd89a0606e0e008ca8a7bb6af905330eb512ea06c (diff)
renesas-ra: Add Bluetooth support using NimBLE.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
-rw-r--r--ports/renesas-ra/Makefile8
-rw-r--r--ports/renesas-ra/main.c11
-rw-r--r--ports/renesas-ra/mpbthciport.c189
-rw-r--r--ports/renesas-ra/mpbthciport.h44
-rw-r--r--ports/renesas-ra/mpconfigport.h9
-rw-r--r--ports/renesas-ra/mpnimbleport.c78
-rw-r--r--ports/renesas-ra/mpnimbleport.h30
7 files changed, 369 insertions, 0 deletions
diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile
index a8cc6d191..9accc760b 100644
--- a/ports/renesas-ra/Makefile
+++ b/ports/renesas-ra/Makefile
@@ -342,6 +342,14 @@ SRC_C += $(addprefix $(BOARD_DIR)/ra_gen/,\
vector_data.c \
)
+ifeq ($(MICROPY_PY_BLUETOOTH),1)
+SRC_C += mpbthciport.c
+endif
+
+ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1)
+SRC_C += mpnimbleport.c
+endif
+
SRC_O += \
$(STARTUP_FILE) \
$(SYSTEM_FILE)
diff --git a/ports/renesas-ra/main.c b/ports/renesas-ra/main.c
index f23539625..65ff76bb7 100644
--- a/ports/renesas-ra/main.c
+++ b/ports/renesas-ra/main.c
@@ -63,6 +63,10 @@
#include "rtc.h"
#include "storage.h"
#include "tusb.h"
+#if MICROPY_PY_BLUETOOTH
+#include "mpbthciport.h"
+#include "extmod/modbluetooth.h"
+#endif
#define RA_EARLY_PRINT 1 /* for enabling mp_print in boardctrl. */
@@ -264,6 +268,10 @@ int main(void) {
tusb_init();
#endif
+ #if MICROPY_PY_BLUETOOTH
+ mp_bluetooth_hci_init();
+ #endif
+
MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state);
soft_reset:
@@ -382,6 +390,9 @@ soft_reset_exit:
mp_printf(&mp_plat_print, "MPY: soft reboot\n");
}
+ #if MICROPY_PY_BLUETOOTH
+ mp_bluetooth_deinit();
+ #endif
soft_timer_deinit();
timer_deinit();
uart_deinit_all();
diff --git a/ports/renesas-ra/mpbthciport.c b/ports/renesas-ra/mpbthciport.c
new file mode 100644
index 000000000..5092f8709
--- /dev/null
+++ b/ports/renesas-ra/mpbthciport.c
@@ -0,0 +1,189 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2023 Arduino SA
+ * Copyright (c) 2018-2021 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/stream.h"
+#include "py/mphal.h"
+#include "extmod/modbluetooth.h"
+#include "extmod/mpbthci.h"
+#include "shared/runtime/softtimer.h"
+#include "modmachine.h"
+#include "mpbthciport.h"
+#include "uart.h"
+
+#if MICROPY_PY_BLUETOOTH
+
+#define debug_printf(...) // mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
+#define error_printf(...) mp_printf(&mp_plat_print, "mpbthciport.c: " __VA_ARGS__)
+
+uint8_t mp_bluetooth_hci_cmd_buf[4 + 256];
+
+STATIC mp_sched_node_t mp_bluetooth_hci_sched_node;
+STATIC soft_timer_entry_t mp_bluetooth_hci_soft_timer;
+
+STATIC void mp_bluetooth_hci_soft_timer_callback(soft_timer_entry_t *self) {
+ mp_bluetooth_hci_poll_now();
+}
+
+void mp_bluetooth_hci_init(void) {
+ soft_timer_static_init(
+ &mp_bluetooth_hci_soft_timer,
+ SOFT_TIMER_MODE_ONE_SHOT,
+ 0,
+ mp_bluetooth_hci_soft_timer_callback
+ );
+}
+
+STATIC void mp_bluetooth_hci_start_polling(void) {
+ mp_bluetooth_hci_poll_now();
+}
+
+void mp_bluetooth_hci_poll_in_ms(uint32_t ms) {
+ soft_timer_reinsert(&mp_bluetooth_hci_soft_timer, ms);
+}
+
+// For synchronous mode, we run all BLE stack code inside a scheduled task.
+STATIC void run_events_scheduled_task(mp_sched_node_t *node) {
+ // This will process all buffered HCI UART data, and run any callouts or events.
+ mp_bluetooth_hci_poll();
+}
+
+// Called periodically (systick) or directly (e.g. UART RX IRQ) in order to
+// request that processing happens ASAP in the scheduler.
+void mp_bluetooth_hci_poll_now(void) {
+ mp_sched_schedule_node(&mp_bluetooth_hci_sched_node, run_events_scheduled_task);
+}
+
+mp_obj_t mp_bthci_uart;
+
+int mp_bluetooth_hci_uart_init(uint32_t port, uint32_t baudrate) {
+ debug_printf("mp_bluetooth_hci_uart_init\n");
+
+ mp_obj_t args[] = {
+ MP_OBJ_NEW_SMALL_INT(port),
+ MP_OBJ_NEW_QSTR(MP_QSTR_baudrate), MP_OBJ_NEW_SMALL_INT(baudrate),
+ MP_OBJ_NEW_QSTR(MP_QSTR_timeout), MP_OBJ_NEW_SMALL_INT(200),
+ MP_OBJ_NEW_QSTR(MP_QSTR_timeout_char), MP_OBJ_NEW_SMALL_INT(200),
+ MP_OBJ_NEW_QSTR(MP_QSTR_rxbuf), MP_OBJ_NEW_SMALL_INT(768),
+ MP_OBJ_NEW_QSTR(MP_QSTR_flow), MP_OBJ_NEW_SMALL_INT(UART_HWCONTROL_RTS | UART_HWCONTROL_CTS),
+ };
+
+ // This object is tracked in machine_uart_obj_all root pointer.
+ mp_bthci_uart = MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, make_new)((mp_obj_t)&machine_uart_type, 1, 5, args);
+
+ // Start the HCI polling to process any initial events/packets.
+ mp_bluetooth_hci_start_polling();
+ return 0;
+}
+
+int mp_bluetooth_hci_uart_deinit(void) {
+ debug_printf("mp_bluetooth_hci_uart_deinit\n");
+ mp_bthci_uart = MP_OBJ_NULL;
+ return 0;
+}
+
+int mp_bluetooth_hci_uart_set_baudrate(uint32_t baudrate) {
+ debug_printf("mp_bluetooth_hci_uart_set_baudrate(%lu)\n", baudrate);
+ machine_uart_obj_t *self = MP_OBJ_TO_PTR(mp_bthci_uart);
+ ra_sci_set_baud((uint32_t)self->uart_id, baudrate);
+ return 0;
+}
+
+int mp_bluetooth_hci_uart_any(void) {
+ int errcode = 0;
+ const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol);
+
+ mp_uint_t ret = proto->ioctl(mp_bthci_uart, MP_STREAM_POLL, MP_STREAM_POLL_RD, &errcode);
+ if (errcode != 0) {
+ error_printf("Uart ioctl failed to poll UART %d\n", errcode);
+ return -1;
+ }
+ return ret & MP_STREAM_POLL_RD;
+}
+
+int mp_bluetooth_hci_uart_write(const uint8_t *buf, size_t len) {
+ debug_printf("mp_bluetooth_hci_uart_write\n");
+
+ int errcode = 0;
+ const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol);
+
+ mp_bluetooth_hci_controller_wakeup();
+
+ if (proto->write(mp_bthci_uart, (void *)buf, len, &errcode) < 0) {
+ error_printf("mp_bluetooth_hci_uart_write: failed to write to UART %d\n", errcode);
+ }
+ return 0;
+}
+
+// This function expects the controller to be in the wake state via a previous call
+// to mp_bluetooth_hci_controller_woken.
+int mp_bluetooth_hci_uart_readchar(void) {
+ debug_printf("mp_bluetooth_hci_uart_readchar\n");
+ if (mp_bluetooth_hci_uart_any()) {
+ int errcode = 0;
+ uint8_t buf = 0;
+ const mp_stream_p_t *proto = (mp_stream_p_t *)MP_OBJ_TYPE_GET_SLOT(&machine_uart_type, protocol);
+ if (proto->read(mp_bthci_uart, (void *)&buf, 1, &errcode) < 0) {
+ error_printf("mp_bluetooth_hci_uart_readchar: failed to read UART %d\n", errcode);
+ return -1;
+ }
+ return buf;
+ } else {
+ debug_printf("mp_bluetooth_hci_uart_readchar: not ready\n");
+ return -1;
+ }
+}
+
+// Default (weak) implementation of the HCI controller interface.
+// A driver (e.g. cywbt43.c) can override these for controller-specific
+// functionality (i.e. power management).
+MP_WEAK int mp_bluetooth_hci_controller_init(void) {
+ debug_printf("mp_bluetooth_hci_controller_init (default)\n");
+ return 0;
+}
+
+MP_WEAK int mp_bluetooth_hci_controller_deinit(void) {
+ debug_printf("mp_bluetooth_hci_controller_deinit (default)\n");
+ return 0;
+}
+
+MP_WEAK int mp_bluetooth_hci_controller_sleep_maybe(void) {
+ debug_printf("mp_bluetooth_hci_controller_sleep_maybe (default)\n");
+ return 0;
+}
+
+MP_WEAK bool mp_bluetooth_hci_controller_woken(void) {
+ debug_printf("mp_bluetooth_hci_controller_woken (default)\n");
+ return true;
+}
+
+MP_WEAK int mp_bluetooth_hci_controller_wakeup(void) {
+ debug_printf("mp_bluetooth_hci_controller_wakeup (default)\n");
+ return 0;
+}
+
+#endif // MICROPY_PY_BLUETOOTH
diff --git a/ports/renesas-ra/mpbthciport.h b/ports/renesas-ra/mpbthciport.h
new file mode 100644
index 000000000..e64c3817b
--- /dev/null
+++ b/ports/renesas-ra/mpbthciport.h
@@ -0,0 +1,44 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2023 Arduino SA
+ * Copyright (c) 2021 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.
+ */
+
+#ifndef MICROPY_INCLUDED_MIMXRT_MPBTHCIPORT_H
+#define MICROPY_INCLUDED_MIMXRT_MPBTHCIPORT_H
+
+// Initialise the HCI subsystem (should be called once, early on).
+void mp_bluetooth_hci_init(void);
+
+// Poll the HCI now, or after a certain timeout.
+void mp_bluetooth_hci_poll_now(void);
+void mp_bluetooth_hci_poll_in_ms(uint32_t ms);
+
+// Must be provided by the stack bindings (e.g. mpnimbleport.c or mpbtstackport.c).
+// Request new data from the uart and pass to the stack, and run pending events/callouts.
+// This is a low-level function and should not be called directly, use
+// mp_bluetooth_hci_poll_now/mp_bluetooth_hci_poll_in_ms instead.
+void mp_bluetooth_hci_poll(void);
+
+#endif // MICROPY_INCLUDED_MIMXRT_MPBTHCIPORT_H
diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h
index eb21db843..27a0f21e5 100644
--- a/ports/renesas-ra/mpconfigport.h
+++ b/ports/renesas-ra/mpconfigport.h
@@ -100,6 +100,7 @@
#endif
#define MICROPY_USE_INTERNAL_ERRNO (1)
#define MICROPY_SCHEDULER_DEPTH (8)
+#define MICROPY_SCHEDULER_STATIC_NODES (1)
#define MICROPY_VFS (1)
// control over Python builtins
@@ -268,6 +269,14 @@ static inline mp_uint_t disable_irq(void) {
#define MICROPY_THREAD_YIELD()
#endif
+#ifndef MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
+#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
+#endif
+
+#ifndef MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS
+#define MICROPY_PY_BLUETOOTH_ENABLE_L2CAP_CHANNELS (MICROPY_BLUETOOTH_NIMBLE)
+#endif
+
// We need an implementation of the log2 function which is not a macro
#define MP_NEED_LOG2 (1)
diff --git a/ports/renesas-ra/mpnimbleport.c b/ports/renesas-ra/mpnimbleport.c
new file mode 100644
index 000000000..904b1a804
--- /dev/null
+++ b/ports/renesas-ra/mpnimbleport.c
@@ -0,0 +1,78 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019-2023 Jim Mussared
+ * Copyright (c) 2020-2023 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_NIMBLE
+
+#define DEBUG_printf(...) // printf("mpnimbleport.c: " __VA_ARGS__)
+
+#include "host/ble_hs.h"
+#include "nimble/nimble_npl.h"
+
+#include "extmod/mpbthci.h"
+#include "extmod/modbluetooth.h"
+#include "extmod/nimble/modbluetooth_nimble.h"
+#include "extmod/nimble/hal/hal_uart.h"
+#include "mpbthciport.h"
+
+// Get any pending data from the UART and send it to NimBLE's HCI buffers.
+// Any further processing by NimBLE will be run via its event queue.
+void mp_bluetooth_hci_poll(void) {
+ if (mp_bluetooth_nimble_ble_state >= MP_BLUETOOTH_NIMBLE_BLE_STATE_WAITING_FOR_SYNC) {
+ // DEBUG_printf("mp_bluetooth_hci_poll_uart %d\n", mp_bluetooth_nimble_ble_state);
+
+ // Run any timers.
+ mp_bluetooth_nimble_os_callout_process();
+
+ // Process incoming UART data, and run events as they are generated.
+ mp_bluetooth_nimble_hci_uart_process(true);
+
+ // Run any remaining events (e.g. if there was no UART data).
+ mp_bluetooth_nimble_os_eventq_run_all();
+ }
+
+ if (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF) {
+ // Call this function again in 128ms to check for new events.
+ // TODO: improve this by only calling back when needed.
+ mp_bluetooth_hci_poll_in_ms(128);
+ }
+}
+
+// --- Port-specific helpers for the generic NimBLE bindings. -----------------
+
+void mp_bluetooth_nimble_hci_uart_wfi(void) {
+ __WFI();
+
+ // This is called while NimBLE is waiting in ble_npl_sem_pend, i.e. waiting for an HCI ACK.
+ // Do not need to run events here (it must not invoke Python code), only processing incoming HCI data.
+ mp_bluetooth_nimble_hci_uart_process(false);
+}
+
+#endif // MICROPY_PY_BLUETOOTH && MICROPY_BLUETOOTH_NIMBLE
diff --git a/ports/renesas-ra/mpnimbleport.h b/ports/renesas-ra/mpnimbleport.h
new file mode 100644
index 000000000..d2d42b9cb
--- /dev/null
+++ b/ports/renesas-ra/mpnimbleport.h
@@ -0,0 +1,30 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020-2023 Jim Mussared
+ *
+ * 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.
+ */
+
+#ifndef MICROPY_INCLUDED_MIMXRT_NIMBLE_PORT_H
+#define MICROPY_INCLUDED_MIMXRT_NIMBLE_PORT_H
+
+#endif // MICROPY_INCLUDED_MIMXRT_NIMBLE_PORT_H