8000 ports/nrf: Consolidate stdio functions. by andrewleech · Pull Request #15158 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/nrf: Consolidate stdio functions. #15158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ INC += -I./modules/music
INC += -I./modules/ble
INC += -I./modules/board
INC += -I./modules/nrf
INC += -I../../shared/readline
INC += -I./drivers/bluetooth
INC += -I./drivers
INC += -I./drivers/bluetooth
INC += -I./drivers/usb
INC += -I../../lib/nrfx/
INC += -I../../lib/nrfx/drivers
INC += -I../../lib/nrfx/drivers/include
INC += -I../../lib/nrfx/mdk
INC += -I../../lib/nrfx/hal
INC += -I../../lib/nrfx/drivers/src/
INC += -I../../shared/readline

MCU_VARIANT_UPPER = $(shell echo $(MCU_VARIANT) | tr '[:lower:]' '[:upper:]')
MCU_SUB_VARIANT_UPPER = $(shell echo $(MCU_SUB_VARIANT) | tr '[:lower:]' '[:upper:]')
Expand Down Expand Up @@ -128,7 +129,15 @@ CFLAGS_MCU_m4 = $(CFLAGS_CORTEX_M) -mtune=cortex-m4 -mcpu=cortex-m4 -mfpu=fpv4-s

CFLAGS_MCU_m0 = $(CFLAGS_CORTEX_M) -fshort-enums -mtune=cortex-m0 -mcpu=cortex-m0 -mfloat-abi=soft

# linker wrap does not work with lto on older gcc/binutils: https://sourceware.org/bugzilla/show_bug.cgi?id=24406
GCC_VERSION = $(shell arm-none-eabi-gcc --version | sed -n -E 's:^arm.*([0-9]+\.[0-9]+\.[0-9]+).*$$:\1:p')
GCC_MAJOR_VERS = $(word 1,$(subst ., ,$(GCC_VERSION)))
ifeq ($(shell test $(GCC_MAJOR_VERS) -ge 10; echo $$?),0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewleech this looks a bit funny to me. On my Arch system I have:

arm-none-eabi-gcc (Arch Repository) 14.1.0

But GCC_VERSION here evaluates to 4.1.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah drats, it worked on a few systems I'd tested on but yes my arch server is also at 14.1.0 and I get the same thing. Silly greedy regex. Easy fix...
arm-none-eabi-gcc --version | sed -n -E 's:^arm.*([0-9]+\.[0-9]+\.[0-9]+).*$$:\1:p' ->
arm-none-eabi-gcc --version | sed -n -E 's:^arm.* ([0-9]+\.[0-9]+\.[0-9]+).*$$:\1:p'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#15553

I wish there was a way to do this in a proper programatic way, but I couldn't find one.

LTO ?= 1
else
LTO ?= 0
endif

ifeq ($(LTO),1)
CFLAGS += -flto
else
Expand Down Expand Up @@ -183,9 +192,12 @@ SRC_SHARED_C += $(addprefix shared/,\
libc/string0.c \
readline/readline.c \
runtime/pyexec.c \
runtime/stdout_helpers.c \
runtime/sys_stdio_mphal.c \
runtime/interrupt_char.c \
tinyusb/mp_usbd.c \
tinyusb/mp_usbd_cdc.c \
tinyusb/mp_usbd_descriptor.c \
timeutils/timeutils.c \
)

Expand All @@ -200,13 +212,13 @@ endif
SRC_NRFX += $(addprefix lib/nrfx/drivers/src/,\
prs/nrfx_prs.c \
nrfx_uart.c \
nrfx_uarte.c \
nrfx_uarte.c \
nrfx_adc.c \
nrfx_saadc.c \
nrfx_temp.c \
nrfx_rng.c \
nrfx_twi.c \
nrfx_twim.c \
nrfx_twim.c \
nrfx_spi.c \
nrfx_spim.c \
nrfx_rtc.c \
Expand Down Expand Up @@ -234,21 +246,17 @@ SRC_C += \
$(wildcard $(BOARD_DIR)/*.c) \

ifeq ($(MCU_SUB_VARIANT), nrf52840)
# Add support for USB using TinyUSB.

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


# If SoftDevice is selected.
ifneq ($(SD), )
# For external tinyusb drivers to enable SoftDevice mode.
CFLAGS += -DSOFTDEVICE_PRESENT
endif

SRC_C += $(addprefix drivers/usb/,\
usb_cdc.c \
usb_descriptors.c \
)
SRC_C += drivers/usb/usb_cdc.c

SRC_C += $(addprefix lib/tinyusb/src/,\
common/tusb_fifo.c \
Expand All @@ -259,6 +267,7 @@ SRC_C += $(addprefix lib/tinyusb/src/,\
portable/nordic/nrf5x/dcd_nrf5x.c \
)

LDFLAGS += -Wl,--wrap=dcd_event_handler
endif

DRIVERS_SRC_C += $(addprefix modules/,\
Expand Down Expand Up @@ -331,9 +340,9 @@ hex: $(BUILD)/$(OUTPUT_FILENAME).hex
$(BUILD)/$(OUTPUT_FILENAME).hex: $(BUILD)/$(OUTPUT_FILENAME).elf
$(OBJCOPY) -O ihex $< $@

FLASHER ?=
FLASHER ?= jlink

ifeq ($(FLASHER),)
ifeq ($(FLASHER), jlink)

ifeq ($(MCU_VARIANT), nrf91)

Expand Down
13 changes: 6 additions & 7 deletions ports/nrf/bluetooth_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

// SD specific configurations.

#ifndef MICROPY_PY_BLE_NUS
// Nordic UART Service
// If enabled, REPL will be available on this interface.
#define MICROPY_PY_BLE_NUS (0)
#endif

#if (BLUETOOTH_SD == 110)

#define MICROPY_PY_BLE (1)
#define MICROPY_PY_BLE_NUS (0)
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#define MICROPY_PY_UBLUEPY (1)
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)

#elif (BLUETOOTH_SD == 132)

#define MICROPY_PY_BLE (1)
#define MICROPY_PY_BLE_NUS (0)
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#define MICROPY_PY_UBLUEPY (1)
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
Expand All @@ -23,7 +27,6 @@
#elif (BLUETOOTH_SD == 140)

#define MICROPY_PY_BLE (1)
#define MICROPY_PY_BLE_NUS (0)
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#define MICROPY_PY_UBLUEPY (1)
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
Expand All @@ -39,8 +42,4 @@
#define MICROPY_PY_BLE (0)
#endif

#ifndef MICROPY_PY_BLE_NUS
#define MICROPY_PY_BLE_NUS (0)
#endif

#endif
1 change: 1 addition & 0 deletions ports/nrf/boards/ARDUINO_NANO_33_BLE_SENSE/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define MICROPY_PY_MACHINE_ADC (1)
#define MICROPY_PY_MACHINE_TEMP (1)

#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_HW_USB_CDC (1)
#define MICROPY_HW_HAS_LED (1)
#define MICROPY_HW_HAS_SWITCH (0)
Expand Down
4 changes: 4 additions & 0 deletions ports/nrf/boards/PCA10056/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@

#define MICROPY_HW_ENABLE_RNG (1)

#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_HW_USB_CDC (1)

#define MICROPY_HW_HAS_LED (1)
#define MICROPY_HW_LED_COUNT (4)
#define MICROPY_HW_LED_PULLUP (1)
Expand All @@ -47,6 +50,7 @@
#define MICROPY_HW_LED4 (16) // LED4

// UART config
#define MICROPY_HW_ENABLE_UART_REPL (1)
#define MICROPY_HW_UART1_RX (8)
#define MICROPY_HW_UART1_TX (6)
#define MICROPY_HW_UART1_CTS (7)
Expand Down
4 changes: 1 addition & 3 deletions ports/nrf/boards/PCA10059/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ ifeq ($(DFU),1)
BOOTLOADER=open_bootloader
BOOTLOADER_VERSION_MAJOR=1
BOOTLOADER_VERSION_MINOR=2
FLASHER=nrfutil
else
FLASHER=segger
FLASHER ?= nrfutil
endif

LD_FILES += boards/nrf52840_1M_256k.ld
Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/SEEED_XIAO_NRF52/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define MICROPY_BOARD_DEINIT XIAO_board_deinit
#define MICROPY_BOARD_ENTER_BOOTLOADER(nargs, args) XIAO_board_enter_bootloader()

#define MICROPY_HW_ENABLE_USBDEV (1)
#define MICROPY_HW_USB_CDC (1)
#define MICROPY_PY_MACHINE_UART (1)
#define MICROPY_PY_MACHINE_HW_PWM (1)
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/drivers/bluetooth/ble_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#include "mphalport.h"


#if MICROPY_HW_USB_CDC
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
#include "usb_cdc.h"
#endif

Expand Down Expand Up @@ -941,7 +941,7 @@ static void sd_evt_handler(uint32_t evt_id) {
// unhandled event!
break;
}
#if MICROPY_HW_USB_CDC
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_USB_CDC
// Forward SOC events to USB CDC driver.
usb_cdc_sd_event_handler(evt_id);
#endif
Expand Down
29 changes: 8 additions & 21 deletions ports/nrf/drivers/bluetooth/ble_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,16 @@ static ubluepy_advertise_data_t m_adv_data_uart_service;
static ubluepy_advertise_data_t m_adv_data_eddystone_url;
#endif // BLUETOOTH_WEBBLUETOOTH_REPL

int mp_hal_stdin_rx_chr(void) {
while (!ble_uart_enabled()) {
// wait for connection
int mp_ble_uart_stdin_rx_chr(void) {
if (ble_uart_enabled() && !isBufferEmpty(mp_rx_ring_buffer)) {
uint8_t byte = -1;
bufferRead(mp_rx_ring_buffer, byte);
return (int)byte;
}
while (isBufferEmpty(mp_rx_ring_buffer)) {
;
}

uint8_t byte;
bufferRead(mp_rx_ring_buffer, byte);
return (int)byte;
return -1;
}

mp_uint_t mp_hal_stdout_tx_strn(const char *str, size_t len) {
mp_uint_t mp_ble_uart_stdout_tx_strn(co F42D nst char *str, size_t len) {
// Not connected: drop output
if (!ble_uart_enabled()) return 0;

Expand Down Expand Up @@ -150,17 +146,8 @@ void ble_uart_tx_char(char c) {
(uint8_t *)&c);
}

void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
for (const char *top = str + len; str < top; str++) {
if (*str == '\n') {
ble_uart_tx_char('\r');
}
ble_uart_tx_char(*str);
}
}

#if MICROPY_PY_SYS_STDFILES
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
uintptr_t mp_ble_uart_stdio_poll(uintptr_t poll_flags) {
uintptr_t ret = 0;
if ((poll_flags & MP_STREAM_POLL_RD) && ble_uart_enabled()
&& !isBufferEmpty(mp_rx_ring_buffer)) {
Expand Down
4 changes: 4 additions & 0 deletions ports/nrf/drivers/bluetooth/ble_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ void ble_uart_advertise(void);
bool ble_uart_connected(void);
bool ble_uart_enabled(void);

uintptr_t mp_ble_uart_stdio_poll(uintptr_t poll_flags);
int mp_ble_uart_stdin_rx_chr(void);
mp_uint_t mp_ble_uart_stdout_tx_strn(const char *str, size_t len);

#endif // BLUETOOTH_SD

#endif // BLUETOOTH_LE_UART_H__
14 changes: 2 additions & 12 deletions ports/nrf/drivers/usb/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,9 @@
#ifndef MICROPY_INCLUDED_NRF_TUSB_CONFIG_H
#define MICROPY_INCLUDED_NRF_TUSB_CONFIG_H

// Common configuration

#define CFG_TUSB_MCU OPT_MCU_NRF5X
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE

#define CFG_TUSB_MEM_SECTION
#define CFG_TUSB_MEM_ALIGN TU_ATTR_ALIGNED(4)
#include "shared/tinyusb/tusb_config.h"

// Device configuration

#define CFG_TUD_ENDOINT0_SIZE (64)
#define CFG_TUD_CDC (1)
#define CFG_TUD_CDC_RX_BUFSIZE (64)
#define CFG_TUD_CDC_TX_BUFSIZE (64)
#define CFG_TUSB_MCU OPT_MCU_NRF5X

#endif // MICROPY_INCLUDED_NRF_TUSB_CONFIG_H
Loading
Loading
0