Skip to content

Commit 3f093a6

Browse files
committed
shared/tinyusb: Add USBD_CDC class to access usb serial port.
Signed-off-by: Andrew Leech <[email protected]>
1 parent 6979f20 commit 3f093a6

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

extmod/modmachine.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "extmod/modmachine.h"
3333
#include "shared/runtime/pyexec.h"
3434

35+
#if MICROPY_HW_USB_CDC
36+
#include "shared/tinyusb/mp_usbd_cdc.h"
37+
#endif
38+
3539
#if MICROPY_PY_MACHINE_DHT_READINTO
3640
#include "drivers/dht/dht.h"
3741
#endif

extmod/os_dupterm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void mp_os_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc) {
5353
if (nlr_push(&nlr) == 0) {
5454
mp_stream_close(term);
5555
const mp_stream_p_t *stream_p = mp_get_stream_raise(term, MP_STREAM_OP_IOCTL);
56+
int errcode = 0;
5657
stream_p->ioctl(term, MP_STREAM_REPL_ATTACHED, 0, &errcode);
5758
nlr_pop();
5859
} else {

shared/tinyusb/mp_usbd.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ static inline void mp_usbd_init_tud(void) {
4545
tusb_init();
4646
tud_cdc_configure_fifo_t cfg = { .rx_persistent = 0, .tx_persistent = 1 };
4747
tud_cdc_configure_fifo(&cfg);
48+
49+
#if MICROPY_HW_USB_CDC
50+
machine_usbd_cdc_init0();
51+
#endif
4852
}
4953

5054
// Run the TinyUSB device task

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ mp_uint_t mp_usbd_cdc_rx_strn(const char *buf, mp_uint_t len) {
7575
}
7676

7777
void tud_cdc_rx_cb(uint8_t itf) {
78-
#if 0
78+
#if !MICROPY_PY_OS_DUPTERM
7979
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
8080
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
8181
cdc_itf_pending &= ~(1 << itf);
@@ -230,18 +230,19 @@ const machine_usbd_cdc_obj_t machine_usbd_cdc_obj = {{&machine_usbd_cdc_type}};/
230230

231231
static bool machine_usbd_cdc_irq_scheduled;// [MICROPY_HW_USB_CDC_NUM];
232232

233-
static void machine_usbd_cdc_init0(void) {
233+
void machine_usbd_cdc_init0(void) {
234234
// for (size_t i = 0; i < MICROPY_HW_USB_CDC_NUM; ++i) {
235235
MP_STATE_PORT(machine_usbd_cdc_irq) = mp_const_none;
236236
machine_usbd_cdc_irq_scheduled = false;
237237
// }
238238

239+
#if MICROPY_PY_OS_DUPTERM
239240
// #if MICROPY_HW_USB_CDC_REPL
240241
// Activate USB_CDC(0) on dupterm slot 1 for the REPL
241242
// todo auto detect appropriate slot
242243
MP_STATE_VM(dupterm_objs[1]) = MP_OBJ_FROM_PTR(&machine_usbd_cdc_obj);
243244
usb_vcp_attach_to_repl(&machine_usbd_cdc_obj, true);
244-
// #endif
245+
#endif
245246

246247
}
247248

@@ -349,7 +350,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(machine_usbd_cdc_isconnected_obj, machine_usbd_
349350
/// Return `True` if any characters waiting, else `False`.
350351
static mp_obj_t machine_usbd_cdc_any(mp_obj_t self_in) {
351352
UNUSED(self_in);
352-
if (ringbuf_peek(&stdin_ringbuf) != -1) {
353+
if (tud_cdc_available()) {
353354
return mp_const_true;
354355
} else {
355356
return mp_const_false;
@@ -488,8 +489,9 @@ static const mp_rom_map_elem_t machine_usbd_cdc_locals_dict_table[] = {
488489
static MP_DEFINE_CONST_DICT(machine_usbd_cdc_locals_dict, machine_usbd_cdc_locals_dict_table);
489490

490491
static mp_uint_t machine_usbd_cdc_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
492+
UNUSED(self_in);
491493
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
492-
int ret = usbd_cdc_rx(self, (byte *)buf, size, 0);
494+
int ret = mp_usbd_cdc_rx_strn((byte *)buf, size);
493495
if (ret == 0) {
494496
// return EAGAIN error to indicate non-blocking
495497
*errcode = MP_EAGAIN;
@@ -499,7 +501,7 @@ static mp_uint_t machine_usbd_cdc_read(mp_obj_t self_in, void *buf, mp_uint_t si
499501
}
500502

501503
static mp_uint_t machine_usbd_cdc_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
502-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
504+
UNUSED(self_in);
503505
int ret = mp_usbd_cdc_tx_strn((const char *)buf, size);
504506
if (ret == 0) {
505507
// return EAGAIN error to indicate non-blocking
@@ -514,13 +516,7 @@ static mp_uint_t machine_usbd_cdc_ioctl(mp_obj_t self_in, mp_uint_t request, uin
514516
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
515517
if (request == MP_STREAM_POLL) {
516518
uintptr_t flags = arg;
517-
ret = 0;
518-
if ((flags & MP_STREAM_POLL_RD) && usbd_cdc_rx_num(self) > 0) {
519-
ret |= MP_STREAM_POLL_RD;
520-
}
521-
if ((flags & MP_STREAM_POLL_WR) && usbd_cdc_tx_half_empty(self)) {
522-
ret |= MP_STREAM_POLL_WR;
523-
}
519+
ret = mp_usbd_cdc_poll_interfaces(flags);
524520
} else if (request == MP_STREAM_CLOSE) {
525521
ret = 0;
526522
} else if (request == MP_STREAM_REPL_ATTACHED) {

shared/tinyusb/mp_usbd_cdc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define MICROPY_HW_USB_CDC_DTR_RTS_BOOTLOADER (0)
3838
#endif
3939

40+
void machine_usbd_cdc_init0(void);
4041
// extern const mp_obj_type_t pyb_usb_vcp_type;
4142
extern const mp_obj_type_t machine_usbd_cdc_type;
4243
// extern void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached);

0 commit comments

Comments
 (0)