Skip to content

Commit 353c3ca

Browse files
committed
shared/tinyusb: Add USBD_CDC class to access usb serial port.
Signed-off-by: Andrew Leech <[email protected]>
1 parent 234a219 commit 353c3ca

File tree

5 files changed

+43
-27
lines changed

5 files changed

+43
-27
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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/obj.h"
3535
#include "py/objarray.h"
3636
#include "py/runtime.h"
37+
#include "mp_usbd_cdc.h"
3738

3839
#ifndef NO_QSTR
3940
#include "tusb.h"
@@ -45,6 +46,10 @@ static inline void mp_usbd_init_tud(void) {
4546
tusb_init();
4647
tud_cdc_configure_fifo_t cfg = { .rx_persistent = 0, .tx_persistent = 1 };
4748
tud_cdc_configure_fifo(&cfg);
49+
50+
#if MICROPY_HW_USB_CDC
51+
machine_usbd_cdc_init0();
52+
#endif
4853
}
4954

5055
// Run the TinyUSB device task

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "py/mphal.h"
2929
#include "py/stream.h"
3030
#include "extmod/modmachine.h"
31+
#include "shared/runtime/mpirq.h"
3132

3233
#include "mp_usbd.h"
3334
#include "mp_usbd_cdc.h"
@@ -38,6 +39,12 @@ static uint8_t cdc_itf_pending; // keep track of cdc interfaces which need atten
3839
static int8_t cdc_connected_flush_delay = 0;
3940
static void usbd_cdc_rx_event_callback(void);
4041

42+
// Constants for USBD_CDC.irq trigger.
43+
#define USBD_CDC_IRQ_RX (1)
44+
45+
#ifndef UNUSED
46+
#define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */
47+
#endif
4148

4249
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
4350
uintptr_t ret = 0;
@@ -70,12 +77,12 @@ uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
7077
return ret;
7178
}
7279

73-
mp_uint_t mp_usbd_cdc_rx_strn(const char *buf, mp_uint_t len) {
80+
mp_uint_t mp_usbd_cdc_rx_strn(char *buf, mp_uint_t len) {
7481
return tud_cdc_read(buf, len);
7582
}
7683

7784
void tud_cdc_rx_cb(uint8_t itf) {
78-
#if 0
85+
#if !MICROPY_PY_OS_DUPTERM
7986
// consume pending USB data immediately to free usb buffer and keep the endpoint from stalling.
8087
// in case the ringbuffer is full, mark the CDC interface that need attention later on for polling
8188
cdc_itf_pending &= ~(1 << itf);
@@ -229,28 +236,30 @@ const machine_usbd_cdc_obj_t machine_usbd_cdc_obj = {{&machine_usbd_cdc_type}};/
229236

230237

231238
static bool machine_usbd_cdc_irq_scheduled;// [MICROPY_HW_USB_CDC_NUM];
239+
static void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached);
232240

233-
static void machine_usbd_cdc_init0(void) {
241+
void machine_usbd_cdc_init0(void) {
234242
// for (size_t i = 0; i < MICROPY_HW_USB_CDC_NUM; ++i) {
235243
MP_STATE_PORT(machine_usbd_cdc_irq) = mp_const_none;
236244
machine_usbd_cdc_irq_scheduled = false;
237245
// }
238246

247+
#if MICROPY_PY_OS_DUPTERM
239248
// #if MICROPY_HW_USB_CDC_REPL
240249
// Activate USB_CDC(0) on dupterm slot 1 for the REPL
241250
// todo auto detect appropriate slot
242251
MP_STATE_VM(dupterm_objs[1]) = MP_OBJ_FROM_PTR(&machine_usbd_cdc_obj);
243-
usb_vcp_attach_to_repl(&machine_usbd_cdc_obj, true);
244-
// #endif
252+
machine_usbd_cdc_attach_to_repl(&machine_usbd_cdc_obj, true);
253+
#endif
245254

246255
}
247256

248257
static mp_obj_t machine_usbd_cdc_irq_run(mp_obj_t self_in) {
249-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
258+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
250259
// uint8_t idx = self->cdc_idx;
251260
mp_obj_t callback = MP_STATE_PORT(machine_usbd_cdc_irq);
252261
machine_usbd_cdc_irq_scheduled = false;
253-
if (callback != mp_const_none && usbd_cdc_rx_num(self)) {
262+
if (callback != mp_const_none && tud_cdc_available()) {
254263
mp_call_function_1(callback, self_in);
255264
}
256265
return mp_const_none;
@@ -267,11 +276,12 @@ void usbd_cdc_rx_event_callback(void) {
267276
}
268277

269278
static void machine_usbd_cdc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
270-
int id = ((machine_usbd_cdc_obj_t *)MP_OBJ_TO_PTR(self_in))->cdc_itf->cdc_idx;
271-
mp_printf(print, "USBD_CDC(%u)", id);
279+
// int id = ((machine_usbd_cdc_obj_t *)MP_OBJ_TO_PTR(self_in))->cdc_itf->cdc_idx;
280+
mp_printf(print, "USBD_CDC()");
272281
}
273282

274-
void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached) {
283+
static void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached) {
284+
UNUSED(self);
275285
// self->attached_to_repl = attached;
276286
if (attached) {
277287
// Default behavior is non-blocking when attached to repl
@@ -321,7 +331,7 @@ static mp_obj_t machine_usbd_cdc_init(size_t n_args, const mp_obj_t *pos_args, m
321331

322332
// parse args
323333
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
324-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
334+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
325335
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
326336

327337
// flow control
@@ -349,7 +359,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(machine_usbd_cdc_isconnected_obj, machine_usbd_
349359
/// Return `True` if any characters waiting, else `False`.
350360
static mp_obj_t machine_usbd_cdc_any(mp_obj_t self_in) {
351361
UNUSED(self_in);
352-
if (ringbuf_peek(&stdin_ringbuf) != -1) {
362+
if (tud_cdc_available()) {
353363
return mp_const_true;
354364
} else {
355365
return mp_const_false;
@@ -429,7 +439,7 @@ static mp_obj_t machine_usbd_cdc_irq(size_t n_args, const mp_obj_t *pos_args, mp
429439
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = USBD_CDC_IRQ_RX} },
430440
{ MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
431441
};
432-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
442+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
433443
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
434444
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
435445

@@ -480,16 +490,17 @@ static const mp_rom_map_elem_t machine_usbd_cdc_locals_dict_table[] = {
480490
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_stream___exit___obj) },
481491

482492
// class constants
483-
{ MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_RTS) },
484-
{ MP_ROM_QSTR(MP_QSTR_CTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_CTS) },
493+
// { MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_RTS) },
494+
// { MP_ROM_QSTR(MP_QSTR_CTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_CTS) },
485495
{ MP_ROM_QSTR(MP_QSTR_IRQ_RX), MP_ROM_INT(USBD_CDC_IRQ_RX) },
486496
};
487497

488498
static MP_DEFINE_CONST_DICT(machine_usbd_cdc_locals_dict, machine_usbd_cdc_locals_dict_table);
489499

490500
static mp_uint_t machine_usbd_cdc_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
491-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
492-
int ret = usbd_cdc_rx(self, (byte *)buf, size, 0);
501+
UNUSED(self_in);
502+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
503+
int ret = mp_usbd_cdc_rx_strn((char *)buf, size);
493504
if (ret == 0) {
494505
// return EAGAIN error to indicate non-blocking
495506
*errcode = MP_EAGAIN;
@@ -499,7 +510,7 @@ static mp_uint_t machine_usbd_cdc_read(mp_obj_t self_in, void *buf, mp_uint_t si
499510
}
500511

501512
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);
513+
UNUSED(self_in);
503514
int ret = mp_usbd_cdc_tx_strn((const char *)buf, size);
504515
if (ret == 0) {
505516
// return EAGAIN error to indicate non-blocking
@@ -514,17 +525,11 @@ static mp_uint_t machine_usbd_cdc_ioctl(mp_obj_t self_in, mp_uint_t request, uin
514525
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
515526
if (request == MP_STREAM_POLL) {
516527
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-
}
528+
ret = mp_usbd_cdc_poll_interfaces(flags);
524529
} else if (request == MP_STREAM_CLOSE) {
525530
ret = 0;
526531
} else if (request == MP_STREAM_REPL_ATTACHED) {
527-
machine_usbd_cdc_attach_to_repl(arg);
532+
machine_usbd_cdc_attach_to_repl(self, arg);
528533
ret = 0;
529534
} else {
530535
*errcode = MP_EINVAL;

shared/tinyusb/mp_usbd_cdc.h

Lines changed: 2 additions & 1 deletion
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);
@@ -45,6 +46,6 @@ extern const mp_obj_type_t machine_usbd_cdc_type;
4546
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags);
4647
// void tud_cdc_rx_cb(uint8_t itf);
4748
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len);
48-
mp_uint_t mp_usbd_cdc_rx_strn(const char *buf, mp_uint_t len);
49+
mp_uint_t mp_usbd_cdc_rx_strn(char *buf, mp_uint_t len);
4950

5051
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_CDC_H

0 commit comments

Comments
 (0)