Skip to content

Commit f2f7cc8

Browse files
committed
zephyr: Switch nRF52840 DK to UART console, fix USB ordering.
The nRF52840 DK's USB CDC ACM console was unreliable — device enumeration failures and stalls during raw-paste mode. Switch to UART via JLink OB (uart0 with hw-flow-control) which is always available. Move USB device stack init from mp_task (after console init) to zephyr_start.c main() (before console init) so CDC ACM UART is ready when the console subsystem opens the device. Add DTR wait for CDC ACM console boards so output isn't lost before a host connects. Reduce MICROPY_REPL_STDIN_BUFFER_MAX to 64 (raw-paste window=32 bytes) to avoid overflowing USB-UART bridge buffers at 115200 baud. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent 3b240d5 commit f2f7cc8

6 files changed

Lines changed: 44 additions & 14 deletions

File tree

ports/zephyr/boards/nrf52840dk_nrf52840.conf

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
CONFIG_NETWORKING=n
22

3-
# USB CDC ACM console
4-
CONFIG_USB_DEVICE_STACK_NEXT=y
5-
CONFIG_USBD_CDC_ACM_CLASS=y
6-
73
CONFIG_BT=y
84
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
95
CONFIG_BT_GATT_DYNAMIC_DB=y
@@ -43,7 +39,6 @@ CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
4339
CONFIG_MICROPY_HEAP_SIZE=98304
4440
CONFIG_MAIN_STACK_SIZE=16384
4541

46-
# CONFIG_DYNAMIC_THREAD=y
4742
CONFIG_THREAD_CUSTOM_DATA=y
4843
CONFIG_THREAD_MONITOR=y
4944
CONFIG_THREAD_STACK_INFO=y
@@ -52,7 +47,7 @@ CONFIG_THREAD_STACK_INFO=y
5247
# Full mbedTLS ECP causes DHKey check failure on nRF52840 native controller
5348
CONFIG_BT_LONG_WQ_STACK_SIZE=4096
5449

55-
# SMP/pairing debug via RTT
50+
# RTT logging for debug (keeps log output off the UART console)
5651
CONFIG_LOG=y
5752
CONFIG_LOG_BACKEND_RTT=y
5853
CONFIG_LOG_BACKEND_UART=n
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* Enable UART hardware flow control (RTS/CTS) for reliable raw REPL.
2+
* Pins already routed on nRF52840 DK: RTS=P0.5, CTS=P0.7.
3+
* JLink OB UART bridge supports hardware flow control.
4+
*/
5+
&uart0 {
6+
hw-flow-control;
7+
};

ports/zephyr/main.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ DT_FOREACH_STATUS_OKAY(micropython_heap, MICROPY_HEAP_DEFINE)
8585

8686
static __noinit char heap[MICROPY_HEAP_SIZE];
8787

88-
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
89-
extern int mp_usbd_init(void);
90-
#endif // defined(CONFIG_USB_DEVICE_STACK_NEXT)
91-
9288
void init_zephyr(void) {
9389
// We now rely on CONFIG_NET_APP_SETTINGS to set up bootstrap
9490
// network addresses.
@@ -139,9 +135,8 @@ int real_main(void) {
139135
usb_enable(NULL);
140136
#endif
141137

142-
#ifdef CONFIG_USB_DEVICE_STACK_NEXT
143-
mp_usbd_init();
144-
#endif
138+
// Note: CONFIG_USB_DEVICE_STACK_NEXT init is in zephyr_start.c main()
139+
// so it runs before the console is initialised (required for CDC ACM).
145140

146141
#if MICROPY_VFS && MICROPY_MODULE_FROZEN_MPY
147142
// Mount and/or create the filesystem

ports/zephyr/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#define MICROPY_ENABLE_FINALISER (MICROPY_VFS)
6060
#define MICROPY_HELPER_REPL (1)
6161
#define MICROPY_REPL_AUTO_INDENT (1)
62+
// Reduce raw-paste window to 32 bytes for UART-bridge connections (JLink OB).
63+
// Default 256 (window=128) overflows USB-UART bridge buffers at 115200 baud.
64+
#define MICROPY_REPL_STDIN_BUFFER_MAX (64)
6265
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
6366
#define MICROPY_KBD_EXCEPTION (1)
6467
#define MICROPY_PY_BUILTINS_BYTES_HEX (1)

ports/zephyr/src/usbd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,4 @@ int mp_usbd_init(void) {
190190
return 0;
191191
}
192192

193-
#endif // defined(CONFIG_USB_DEVICE_STACK_NEXT)
193+
#endif // defined(CONFIG_USB_DEVICE_STACK_NEXT) && !defined(CONFIG_CDC_ACM_SERIAL_INITIALIZE_AT_BOOT)

ports/zephyr/src/zephyr_start.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,47 @@
2424
* THE SOFTWARE.
2525
*/
2626
#include <zephyr/kernel.h>
27+
#include <zephyr/devicetree.h>
28+
#include <zephyr/drivers/uart.h>
2729
#include "zephyr_getchar.h"
2830

2931
int real_main(void);
3032
int mp_console_init(void);
3133

34+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
35+
extern int mp_usbd_init(void);
36+
#endif
37+
38+
// Check if the console device is a CDC ACM UART (USB serial).
39+
#define MP_CONSOLE_IS_CDC_ACM DT_NODE_HAS_COMPAT(DT_CHOSEN(zephyr_console), zephyr_cdc_acm_uart)
40+
41+
#if MP_CONSOLE_IS_CDC_ACM && defined(CONFIG_UART_LINE_CTRL)
42+
static void mp_wait_for_usb_dtr(void) {
43+
const struct device *dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
44+
uint32_t dtr = 0;
45+
46+
while (!dtr) {
47+
uart_line_ctrl_get(dev, UART_LINE_CTRL_DTR, &dtr);
48+
k_msleep(100);
49+
}
50+
}
51+
#endif
52+
3253
int main(void) {
54+
#if defined(CONFIG_USB_DEVICE_STACK_NEXT)
55+
mp_usbd_init();
56+
#endif
57+
58+
#if MP_CONSOLE_IS_CDC_ACM && defined(CONFIG_UART_LINE_CTRL)
59+
mp_wait_for_usb_dtr();
60+
#endif
61+
3362
#ifdef CONFIG_CONSOLE_SUBSYS
3463
mp_console_init();
3564
#else
3665
zephyr_getchar_init();
3766
#endif
67+
3868
real_main();
3969

4070
return 0;

0 commit comments

Comments
 (0)