Skip to content

Commit b7850b4

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

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

shared/tinyusb/mp_usbd.h

Lines changed: 1 addition & 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"

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ static uint8_t cdc_itf_pending; // keep track of cdc interfaces which need atten
3838
static int8_t cdc_connected_flush_delay = 0;
3939
static void usbd_cdc_rx_event_callback(void);
4040

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

4248
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
4349
uintptr_t ret = 0;
@@ -70,7 +76,7 @@ uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags) {
7076
return ret;
7177
}
7278

73-
mp_uint_t mp_usbd_cdc_rx_strn(const char *buf, mp_uint_t len) {
79+
mp_uint_t mp_usbd_cdc_rx_strn(char *buf, mp_uint_t len) {
7480
return tud_cdc_read(buf, len);
7581
}
7682

@@ -229,6 +235,7 @@ const machine_usbd_cdc_obj_t machine_usbd_cdc_obj = {{&machine_usbd_cdc_type}};/
229235

230236

231237
static bool machine_usbd_cdc_irq_scheduled;// [MICROPY_HW_USB_CDC_NUM];
238+
static void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached);
232239

233240
void machine_usbd_cdc_init0(void) {
234241
// for (size_t i = 0; i < MICROPY_HW_USB_CDC_NUM; ++i) {
@@ -241,17 +248,17 @@ void machine_usbd_cdc_init0(void) {
241248
// Activate USB_CDC(0) on dupterm slot 1 for the REPL
242249
// todo auto detect appropriate slot
243250
MP_STATE_VM(dupterm_objs[1]) = MP_OBJ_FROM_PTR(&machine_usbd_cdc_obj);
244-
usb_vcp_attach_to_repl(&machine_usbd_cdc_obj, true);
251+
machine_usbd_cdc_attach_to_repl(&machine_usbd_cdc_obj, true);
245252
#endif
246253

247254
}
248255

249256
static mp_obj_t machine_usbd_cdc_irq_run(mp_obj_t self_in) {
250-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
257+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
251258
// uint8_t idx = self->cdc_idx;
252259
mp_obj_t callback = MP_STATE_PORT(machine_usbd_cdc_irq);
253260
machine_usbd_cdc_irq_scheduled = false;
254-
if (callback != mp_const_none && usbd_cdc_rx_num(self)) {
261+
if (callback != mp_const_none && tud_cdc_available()) {
255262
mp_call_function_1(callback, self_in);
256263
}
257264
return mp_const_none;
@@ -268,11 +275,12 @@ void usbd_cdc_rx_event_callback(void) {
268275
}
269276

270277
static void machine_usbd_cdc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
271-
int id = ((machine_usbd_cdc_obj_t *)MP_OBJ_TO_PTR(self_in))->cdc_itf->cdc_idx;
272-
mp_printf(print, "USBD_CDC(%u)", id);
278+
// int id = ((machine_usbd_cdc_obj_t *)MP_OBJ_TO_PTR(self_in))->cdc_itf->cdc_idx;
279+
mp_printf(print, "USBD_CDC()");
273280
}
274281

275-
void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached) {
282+
static void machine_usbd_cdc_attach_to_repl(const machine_usbd_cdc_obj_t *self, bool attached) {
283+
UNUSED(self);
276284
// self->attached_to_repl = attached;
277285
if (attached) {
278286
// Default behavior is non-blocking when attached to repl
@@ -322,7 +330,7 @@ static mp_obj_t machine_usbd_cdc_init(size_t n_args, const mp_obj_t *pos_args, m
322330

323331
// parse args
324332
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
325-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
333+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
326334
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
327335

328336
// flow control
@@ -430,7 +438,7 @@ static mp_obj_t machine_usbd_cdc_irq(size_t n_args, const mp_obj_t *pos_args, mp
430438
{ MP_QSTR_trigger, MP_ARG_INT, {.u_int = USBD_CDC_IRQ_RX} },
431439
{ MP_QSTR_hard, MP_ARG_BOOL, {.u_bool = false} },
432440
};
433-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
441+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
434442
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
435443
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
436444

@@ -481,17 +489,17 @@ static const mp_rom_map_elem_t machine_usbd_cdc_locals_dict_table[] = {
481489
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&mp_stream___exit___obj) },
482490

483491
// class constants
484-
{ MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_RTS) },
485-
{ MP_ROM_QSTR(MP_QSTR_CTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_CTS) },
492+
// { MP_ROM_QSTR(MP_QSTR_RTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_RTS) },
493+
// { MP_ROM_QSTR(MP_QSTR_CTS), MP_ROM_INT(USBD_CDC_FLOWCONTROL_CTS) },
486494
{ MP_ROM_QSTR(MP_QSTR_IRQ_RX), MP_ROM_INT(USBD_CDC_IRQ_RX) },
487495
};
488496

489497
static MP_DEFINE_CONST_DICT(machine_usbd_cdc_locals_dict, machine_usbd_cdc_locals_dict_table);
490498

491499
static mp_uint_t machine_usbd_cdc_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
492500
UNUSED(self_in);
493-
machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
494-
int ret = mp_usbd_cdc_rx_strn((byte *)buf, size);
501+
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(self_in);
502+
int ret = mp_usbd_cdc_rx_strn((char *)buf, size);
495503
if (ret == 0) {
496504
// return EAGAIN error to indicate non-blocking
497505
*errcode = MP_EAGAIN;
@@ -520,7 +528,7 @@ static mp_uint_t machine_usbd_cdc_ioctl(mp_obj_t self_in, mp_uint_t request, uin
520528
} else if (request == MP_STREAM_CLOSE) {
521529
ret = 0;
522530
} else if (request == MP_STREAM_REPL_ATTACHED) {
523-
machine_usbd_cdc_attach_to_repl(arg);
531+
machine_usbd_cdc_attach_to_repl(self, arg);
524532
ret = 0;
525533
} else {
526534
*errcode = MP_EINVAL;

shared/tinyusb/mp_usbd_cdc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ extern const mp_obj_type_t machine_usbd_cdc_type;
4646
uintptr_t mp_usbd_cdc_poll_interfaces(uintptr_t poll_flags);
4747
// void tud_cdc_rx_cb(uint8_t itf);
4848
mp_uint_t mp_usbd_cdc_tx_strn(const char *str, mp_uint_t len);
49-
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);
5050

5151
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_CDC_H

0 commit comments

Comments
 (0)