Skip to content

Commit 1907569

Browse files
pi-anldpgeorge
authored andcommittedJul 26, 2024
nrf: Consolidate all stdio functions.
Consolidate CDC, UART and NUS stdio interfaces into the one handler. Allows any/all of them to be enabled separately. Updates UART REPL to use similar define to other platforms: `MICROPY_HW_ENABLE_UART_REPL`. USB now uses the shared/tinyusb CDC implementation. Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent e1fe62f commit 1907569

File tree

16 files changed

+155
-336
lines changed

16 files changed

+155
-336
lines changed
 

‎ports/nrf/Makefile

+13-12
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,16 @@ INC += -I./modules/music
8080
INC += -I./modules/ble
8181
INC += -I./modules/board
8282
INC += -I./modules/nrf
83-
INC += -I../../shared/readline
84-
INC += -I./drivers/bluetooth
8583
INC += -I./drivers
84+
INC += -I./drivers/bluetooth
85+
INC += -I./drivers/usb
8686
INC += -I../../lib/nrfx/
8787
INC += -I../../lib/nrfx/drivers
8888
INC += -I../../lib/nrfx/drivers/include
8989
INC += -I../../lib/nrfx/mdk
9090
INC += -I../../lib/nrfx/hal
9191
INC += -I../../lib/nrfx/drivers/src/
92+
INC += -I../../shared/readline
9293

9394
MCU_VARIANT_UPPER = $(shell echo $(MCU_VARIANT) | tr '[:lower:]' '[:upper:]')
9495
MCU_SUB_VARIANT_UPPER = $(shell echo $(MCU_SUB_VARIANT) | tr '[:lower:]' '[:upper:]')
@@ -183,9 +184,12 @@ SRC_SHARED_C += $(addprefix shared/,\
183184
libc/string0.c \
184185
readline/readline.c \
185186
runtime/pyexec.c \
187+
runtime/stdout_helpers.c \
186188
runtime/sys_stdio_mphal.c \
187189
runtime/interrupt_char.c \
190+
tinyusb/mp_usbd.c \
188191
tinyusb/mp_usbd_cdc.c \
192+
tinyusb/mp_usbd_descriptor.c \
189193
timeutils/timeutils.c \
190194
)
191195

@@ -200,13 +204,13 @@ endif
200204
SRC_NRFX += $(addprefix lib/nrfx/drivers/src/,\
201205
prs/nrfx_prs.c \
202206
nrfx_uart.c \
203-
nrfx_uarte.c \
207+
nrfx_uarte.c \
204208
nrfx_adc.c \
205209
nrfx_saadc.c \
206210
nrfx_temp.c \
207211
nrfx_rng.c \
208212
nrfx_twi.c \
209-
nrfx_twim.c \
213+
nrfx_twim.c \
210214
nrfx_spi.c \
211215
nrfx_spim.c \
212216
nrfx_rtc.c \
@@ -234,21 +238,17 @@ SRC_C += \
234238
$(wildcard $(BOARD_DIR)/*.c) \
235239

236240
ifeq ($(MCU_SUB_VARIANT), nrf52840)
241+
# Add support for USB using TinyUSB.
237242

238-
INC += -I./drivers/usb
239243
INC += -I../../lib/tinyusb/src
240244

241-
242245
# If SoftDevice is selected.
243246
ifneq ($(SD), )
244247
# For external tinyusb drivers to enable SoftDevice mode.
245248
CFLAGS += -DSOFTDEVICE_PRESENT
246249
endif
247250

248-
SRC_C += $(addprefix drivers/usb/,\
249-
usb_cdc.c \
250-
usb_descriptors.c \
251-
)
251+
SRC_C += drivers/usb/usb_cdc.c
252252

253253
SRC_C += $(addprefix lib/tinyusb/src/,\
254254
common/tusb_fifo.c \
@@ -259,6 +259,7 @@ SRC_C += $(addprefix lib/tinyusb/src/,\
259259
portable/nordic/nrf5x/dcd_nrf5x.c \
260260
)
261261

262+
LDFLAGS += -Wl,--wrap=dcd_event_handler
262263
endif
263264

264265
DRIVERS_SRC_C += $(addprefix modules/,\
@@ -331,9 +332,9 @@ hex: $(BUILD)/$(OUTPUT_FILENAME).hex
331332
$(BUILD)/$(OUTPUT_FILENAME).hex: $(BUILD)/$(OUTPUT_FILENAME).elf
332333
$(OBJCOPY) -O ihex $< $@
333334

334-
FLASHER ?=
335+
FLASHER ?= jlink
335336

336-
ifeq ($(FLASHER),)
337+
ifeq ($(FLASHER), jlink)
337338

338339
ifeq ($(MCU_VARIANT), nrf91)
339340

‎ports/nrf/bluetooth_conf.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
33

44
// SD specific configurations.
55

6+
#ifndef MICROPY_PY_BLE_NUS
7+
// Nordic UART Service
8+
// If enabled, REPL will be available on this interface.
9+
#define MICROPY_PY_BLE_NUS (0)
10+
#endif
11+
612
#if (BLUETOOTH_SD == 110)
713

814
#define MICROPY_PY_BLE (1)
9-
#define MICROPY_PY_BLE_NUS (0)
1015
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
1116
#define MICROPY_PY_UBLUEPY (1)
1217
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
1318

1419
#elif (BLUETOOTH_SD == 132)
1520

1621
#define MICROPY_PY_BLE (1)
17-
#define MICROPY_PY_BLE_NUS (0)
1822
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
1923
#define MICROPY_PY_UBLUEPY (1)
2024
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
@@ -23,7 +27,6 @@
2327
#elif (BLUETOOTH_SD == 140)
2428

2529
#define MICROPY_PY_BLE (1)
26-
#define MICROPY_PY_BLE_NUS (0)
2730
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
2831
#define MICROPY_PY_UBLUEPY (1)
2932
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
@@ -39,8 +42,4 @@
3942
#define MICROPY_PY_BLE (0)
4043
#endif
4144

42-
#ifndef MICROPY_PY_BLE_NUS
43-
#define MICROPY_PY_BLE_NUS (0)
44-
#endif
45-
4645
#endif

‎ports/nrf/boards/ARDUINO_NANO_33_BLE_SENSE/mpconfigboard.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define MICROPY_PY_MACHINE_ADC (1)
1919
#define MICROPY_PY_MACHINE_TEMP (1)
2020

21+
#define MICROPY_HW_ENABLE_USBDEV (1)
2122
#define MICROPY_HW_USB_CDC (1)
2223
#define MICROPY_HW_HAS_LED (1)
2324
#define MICROPY_HW_HAS_SWITCH (0)

‎ports/nrf/boards/PCA10056/mpconfigboard.h

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
#define MICROPY_HW_ENABLE_RNG (1)
3939

40+
#define MICROPY_HW_ENABLE_USBDEV (1)
41+
#define MICROPY_HW_USB_CDC (1)
42+
4043
#define MICROPY_HW_HAS_LED (1)
4144
#define MICROPY_HW_LED_COUNT (4)
4245
#define MICROPY_HW_LED_PULLUP (1)
@@ -47,6 +50,7 @@
4750
#define MICROPY_HW_LED4 (16) // LED4
4851

4952
// UART config
53+
#define MICROPY_HW_ENABLE_UART_REPL (1)
5054
#define MICROPY_HW_UART1_RX (8)
5155
#define MICROPY_HW_UART1_TX (6)
5256
#define MICROPY_HW_UART1_CTS (7)

‎ports/nrf/boards/PCA10059/mpconfigboard.mk

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ ifeq ($(DFU),1)
99
BOOTLOADER=open_bootloader
1010
BOOTLOADER_VERSION_MAJOR=1
1111
BOOTLOADER_VERSION_MINOR=2
12-
FLASHER=nrfutil
13-
else
14-
FLASHER=segger
12+
FLASHER ?= nrfutil
1513
endif
1614

1715
LD_FILES += boards/nrf52840_1M_256k.ld

‎ports/nrf/boards/SEEED_XIAO_NRF52/mpconfigboard.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define MICROPY_BOARD_DEINIT XIAO_board_deinit
3333
#define MICROPY_BOARD_ENTER_BOOTLOADER(nargs, args) XIAO_board_enter_bootloader()
3434

35+
#define MICROPY_HW_ENABLE_USBDEV (1)
3536
#define MICROPY_HW_USB_CDC (1)
3637
#define MICROPY_PY_MACHINE_UART (1)
3738
#define MICROPY_PY_MACHINE_HW_PWM (1)

‎ports/nrf/drivers/bluetooth/ble_drv.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "mphalport.h"
4040

4141

42-
#if MICROPY_HW_USB_CDC
42+
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
4343
#include "usb_cdc.h"
4444
#endif
4545

@@ -941,7 +941,7 @@ static void sd_evt_handler(uint32_t evt_id) {
941941
// unhandled event!
942942
break;
943943
}
944-
#if MICROPY_HW_USB_CDC
944+
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
945945
// Forward SOC events to USB CDC driver.
946946
usb_cdc_sd_event_handler(evt_id);
947947
#endif

‎ports/nrf/drivers/bluetooth/ble_uart.c

+8-21
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,16 @@ static ubluepy_advertise_data_t m_adv_data_uart_service;
9797
static ubluepy_advertise_data_t m_adv_data_eddystone_url;
9898
#endif // BLUETOOTH_WEBBLUETOOTH_REPL
9999

100-
int mp_hal_stdin_rx_chr(void) {
101-
while (!ble_uart_enabled()) {
102-
// wait for connection
100+
int mp_ble_uart_stdin_rx_chr(void) {
101+
if (ble_uart_enabled() && !isBufferEmpty(mp_rx_ring_buffer)) {
102+
uint8_t byte = -1;
103+
bufferRead(mp_rx_ring_buffer, byte);
104+
return (int)byte;
103105
}
104-
while (isBufferEmpty(mp_rx_ring_buffer)) {
105-
;
106-
}
107-
108-
uint8_t byte;
109-
bufferRead(mp_rx_ring_buffer, byte);
110-
return (int)byte;
106+
return -1;
111107
}
112108

113-
mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
109+
mp_uint_t mp_ble_uart_stdout_tx_strn(const char *str, size_t len) {
114110
// Not connected: drop output
115111
if (!ble_uart_enabled()) return 0;
116112

@@ -150,17 +146,8 @@ void ble_uart_tx_char(char c) {
150146
(uint8_t *)&c);
151147
}
152148

153-
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
154-
for (const char *top = str + len; str < top; str++) {
155-
if (*str == '\n') {
156-
ble_uart_tx_char('\r');
157-
}
158-
ble_uart_tx_char(*str);
159-
}
160-
}
161-
162149
#if MICROPY_PY_SYS_STDFILES
163-
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
150+
uintptr_t mp_ble_uart_stdio_poll(uintptr_t poll_flags) {
164151
uintptr_t ret = 0;
165152
if ((poll_flags & MP_STREAM_POLL_RD) && ble_uart_enabled()
166153
&& !isBufferEmpty(mp_rx_ring_buffer)) {

‎ports/nrf/drivers/bluetooth/ble_uart.h

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ void ble_uart_advertise(void);
3737
bool ble_uart_connected(void);
3838
bool ble_uart_enabled(void);
3939

40+
uintptr_t mp_ble_uart_stdio_poll(uintptr_t poll_flags);
41+
int mp_ble_uart_stdin_rx_chr(void);
42+
mp_uint_t mp_ble_uart_stdout_tx_strn(const char *str, size_t len);
43+
4044
#endif // BLUETOOTH_SD
4145

4246
#endif // BLUETOOTH_LE_UART_H__

‎ports/nrf/drivers/usb/tusb_config.h

+2-12
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,9 @@
2626
#ifndef MICROPY_INCLUDED_NRF_TUSB_CONFIG_H
2727
#define MICROPY_INCLUDED_NRF_TUSB_CONFIG_H
2828

29-
// Common configuration
30-
31-
#define CFG_TUSB_MCU OPT_MCU_NRF5X
32-
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
33-
34-
#define CFG_TUSB_MEM_SECTION
35-
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
29+
#include "shared/tinyusb/tusb_config.h"
3630

3731
// Device configuration
38-
39-
#define CFG_TUD_ENDOINT0_SIZE (64)
40-
#define CFG_TUD_CDC (1)
41-
#define CFG_TUD_CDC_RX_BUFSIZE (64)
42-
#define CFG_TUD_CDC_TX_BUFSIZE (64)
32+
#define CFG_TUSB_MCU OPT_MCU_NRF5X
4333

4434
#endif // MICROPY_INCLUDED_NRF_TUSB_CONFIG_H

0 commit comments

Comments
 (0)