8000 ports/nrf: Use shared/tinyusb cdc functions. · micropython/micropython@5bedc84 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bedc84

Browse files
committed
ports/nrf: Use shared/tinyusb cdc functions.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent 7ac3338 commit 5bedc84

File tree

8 files changed

+99
-266
lines changed

8 files changed

+99
-266
lines changed

ports/nrf/Makefile

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ INC += -I./modules/ble
8181
INC += -I./modules/board
8282
INC += -I./modules/nrf
8383
INC += -I../../shared/readline
84+
INC += -I../../shared/tinyusb
8485
INC += -I./drivers/bluetooth
8586
INC += -I./drivers
8687
INC += -I../../lib/nrfx/
@@ -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 \
188-
tinyusb/mp_cdc_common.c \
190+
tinyusb/mp_usbd.c \
191+
tinyusb/mp_usbd_cdc.c \
192+
tinyusb/mp_usbd_descriptor.c \
189193
timeutils/timeutils.c \
190194
)
191195

@@ -238,7 +242,6 @@ ifeq ($(MCU_SUB_VARIANT), nrf52840)
238242
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.
@@ -247,7 +250,6 @@ endif
247250

248251
SRC_C += $(addprefix drivers/usb/,\
249252
usb_cdc.c \
250-
usb_descriptors.c \
251253
)
252254

253255
SRC_C += $(addprefix lib/tinyusb/src/,\
@@ -259,6 +261,11 @@ SRC_C += $(addprefix lib/tinyusb/src/,\
259261
portable/nordic/nrf5x/dcd_nrf5x.c \
260262
)
261263

264+
CFLAGS += \
265+
-DCFG_TUSB_MCU=OPT_MCU_NRF5X \
266+
267+
LDFLAGS += -Wl,--wrap=dcd_event_handler
268+
262269
endif
263270

264271
DRIVERS_SRC_C += $(addprefix modules/,\

ports/nrf/drivers/bluetooth/ble_uart.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,6 @@ void ble_uart_tx_char(char c) {
150150
(uint8_t *)&c);
151151
}
152152

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-
162153
#if MICROPY_PY_SYS_STDFILES
163154
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
164155
uintptr_t ret = 0;

ports/nrf/drivers/usb/tusb_config.h

Lines changed: 0 additions & 44 deletions
This file was deleted.

ports/nrf/drivers/usb/usb_cdc.c

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#include "py/runtime.h"
3838
#include "shared/runtime/interrupt_char.h"
3939
#include "shared/tinyusb/mp_usbd.h"
40+
#include "shared/tinyusb/mp_usbd_cdc.h"
41+
#include "extmod/misc.h"
42+
#include <hal/nrf_ficr.h>
4043

4144
#ifdef BLUETOOTH_SD
4245
#include "nrf_sdm.h"
@@ -46,12 +49,10 @@
4649

4750
extern void tusb_hal_nrf_power_event(uint32_t event);
4851

49-
static void cdc_task(bool tx);
52+
// static void cdc_task(bool tx);
5053

51-
static uint8_t rx_ringbuf_array[1024];
52-
static uint8_t tx_ringbuf_array[1024];
53-
static volatile ringbuf_t rx_ringbuf;
54-
static volatile ringbuf_t tx_ringbuf;
54+
static uint8_t stdin_ringbuf_array[1024];
55+
ringbuf_t stdin_ringbuf;
5556

5657
static void board_init(void) {
5758
// Config clock source.
@@ -105,62 +106,47 @@ static void board_init(void) {
105106
#endif
106107
}
107108

108-
static bool cdc_rx_any(void) {
109-
return rx_ringbuf.iput != rx_ringbuf.iget;
110-
}
111-
112-
static int cdc_rx_char(void) {
113-
return ringbuf_get((ringbuf_t*)&rx_ringbuf);
114-
}
115-
116-
static bool cdc_tx_any(void) {
117-
return tx_ringbuf.iput != tx_ringbuf.iget;
118-
}
119-
120-
static int cdc_tx_char(void) {
121-
return ringbuf_get((ringbuf_t*)&tx_ringbuf);
109+
void mp_usbd_port_get_serial_number(char *serial_buf) {
110+
uint32_t deviceid[2];
111+
deviceid[0] = nrf_ficr_deviceid_get(NRF_FICR, 0);
112+
deviceid[1] = nrf_ficr_deviceid_get(NRF_FICR, 1);
113+
MP_STATIC_ASSERT(sizeof(deviceid) * 2 <= MICROPY_HW_USB_DESC_STR_MAX);
114+
mp_usbd_hex_str(serial_buf, (uint8_t *)deviceid, sizeof(deviceid));
122115
}
123116

124-
static void cdc_task(bool tx)
125-
{
126-
if ( tud_cdc_connected() ) {
127-
// connected and there are data available
128-
while (tud_cdc_available()) {
129-
int c = tud_cdc_read_char();
130-
if (c == mp_interrupt_char) {
131-
rx_ringbuf.iget = 0;
132-
rx_ringbuf.iput = 0;
133-
mp_sched_keyboard_interrupt();
134-
} else {
135-
ringbuf_put((ringbuf_t*)&rx_ringbuf, c);
136-
}
137-
}
138-
139-
if (tx) {
140-
int chars = 0;
141-
while (cdc_tx_any()) {
142-
if (chars < 64) {
143-
tud_cdc_write_char(cdc_tx_char());
144-
chars++;
145-
} else {
146-
chars = 0;
147-
tud_cdc_write_flush();
148-
}
149-
}
150-
151-
tud_cdc_write_flush();
152-
}
153-
}
117+
static bool cdc_rx_any(void) {
118+
return stdin_ringbuf.iput != stdin_ringbuf.iget;
154119
}
155120

156-
static void usb_cdc_loop(void) {
157-
tud_task();
158-
cdc_task(true);
121+
static int cdc_rx_char(void) {
122+
return ringbuf_get((ringbuf_t*)&stdin_ringbuf);
159123
}
160124

161-
void tud_cdc_rx_cb(uint8_t itf) {
162-
cdc_task(false);
163-
}
125+
// static void cdc_task(bool tx)
126+
// {
127+
// if ( tud_cdc_connected() ) {
128+
// // connected and there are data available
129+
// while (tud_cdc_available()) {
130+
// int c = tud_cdc_read_char();
131+
// if (c == mp_interrupt_char) {
132+
// stdin_ringbuf.iget = 0;
133+
// stdin_ringbuf.iput = 0;
134+
// mp_sched_keyboard_interrupt();
135+
// } else {
136+
// ringbuf_put((ringbuf_t*)&stdin_ringbuf, c);
137+
// }
138+
// }
139+
// }
140+
// }
141+
142+
// static void mp_usbd_task(void) {
143+
// tud_task();
144+
// // cdc_task(true);
145+
// }
146+
147+
// void tud_cdc_rx_cb(uint8_t itf) {
148+
// cdc_task(false);
149+
// }
164150

165151
int usb_cdc_init(void)
166152
{
@@ -176,15 +162,10 @@ int usb_cdc_init(void)
176162
initialized = true;
177163
}
178164

179-
rx_ringbuf.buf = rx_ringbuf_array;
180-
rx_ringbuf.size = sizeof(rx_ringbuf_array);
181-
rx_ringbuf.iget = 0;
182-
rx_ringbuf.iput = 0;
183-
184-
tx_ringbuf.buf = tx_ringbuf_array;
185-
tx_ringbuf.size = sizeof(tx_ringbuf_array);
186-
tx_ringbuf.iget = 0;
187-
tx_ringbuf.iput = 0;
165+
stdin_ringbuf.buf = stdin_ringbuf_array;
166+
stdin_ringbuf.size = sizeof(stdin_ringbuf_array);
167+
stdin_ringbuf.iget = 0;
168+
stdin_ringbuf.iput = 0;
188169

189170
mp_usbd_init();
190171

@@ -208,7 +189,7 @@ void usb_cdc_sd_event_handler(uint32_t soc_evt) {
208189
uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
209190
uintptr_t ret = 0;
210191
if (poll_flags & MP_STREAM_POLL_RD) {
211-
usb_cdc_loop();
192+
mp_usbd_task();
212193
if (cdc_rx_any()) {
213194
ret |= MP_STREAM_POLL_RD;
214195
}
@@ -221,7 +202,7 @@ uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
221202

222203
int mp_hal_stdin_rx_chr(void) {
223204
for (;;) {
224-
usb_cdc_loop();
205+
mp_usbd_task();
225206
if (cdc_rx_any()) {
226207
return cdc_rx_char();
227208
}
@@ -231,24 +212,31 @@ int mp_hal_stdin_rx_chr(void) {
231212
return 0;
232213
}
233214

215+
// Send string of given length
234216
mp_uint_t mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
235-
for (const char *top = str + len; str < top; str++) {
236-
ringbuf_put((ringbuf_t*)&tx_ringbuf, *str);
237-
usb_cdc_loop();
217+
mp_uint_t ret 106A4 = len;
218+
bool did_write = false;
219+
#if MICROPY_HW_ENABLE_UART_REPL
220+
mp_uart_write_strn(str, len);
221+
did_write = true;
222+
#endif
223+
224+
#if MICROPY_HW_USB_CDC
225+
mp_uint_t cdc_res = mp_usbd_cdc_tx_strn(str, len);
226+
if (cdc_res > 0) {
227+
did_write = true;
228+
ret = MIN(cdc_res, ret);
238229
}
239-
return len;
240-
}
230+
#endif
241231

242-
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
243-
244-
for (const char *top = str + len; str < top; str++) {
245-
if (*str == '\n') {
246-
ringbuf_put((ringbuf_t*)&tx_ringbuf, '\r');
247-
usb_cdc_loop();
248-
}
249-
ringbuf_put((ringbuf_t*)&tx_ringbuf, *str);
250-
usb_cdc_loop();
232+
#if MICROPY_PY_OS_DUPTERM
233+
int dupterm_res = mp_os_dupterm_tx_strn(str, len);
234+
if (dupterm_res >= 0) {
235+
did_write = true;
236+
ret = MIN((mp_uint_t)dupterm_res, ret);
251237
}
238+
#endif
239+
return did_write ? ret : 0;
252240
}
253241

254242
void USBD_IRQHandler(void) {

0 commit comments

Comments
 (0)
0