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
3839static int8_t cdc_connected_flush_delay = 0 ;
3940static 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
4249uintptr_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
7784void 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
231238static 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
248257static 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
269278static 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`.
350360static 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
488498static MP_DEFINE_CONST_DICT (machine_usbd_cdc_locals_dict , machine_usbd_cdc_locals_dict_table ) ;
489499
490500static 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
501512static 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 ;
0 commit comments