8000 shared/tinyusb: Add USBD_CDC class to access usb serial port. · micropython/micropython@9d8632f · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d8632f

Browse files
committed
shared/tinyusb: Add USBD_CDC class to access usb serial port.
Signed-off-by: Andrew Leech <andrew@alelec.net>
1 parent b096abd commit 9d8632f

File tree

1 file changed

+0
-116
lines changed

1 file changed

+0
-116
lines changed

shared/tinyusb/mp_usbd_cdc.c

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -372,120 +372,6 @@ static mp_obj_t machine_usbd_cdc_any(mp_obj_t self_in) {
372372
}
373373
static MP_DEFINE_CONST_FUN_OBJ_1(machine_usbd_cdc_any_obj, machine_usbd_cdc_any);
374374

375-
/// \method send(data, *, timeout=5000)
376-
/// Send data over the USB CDC:
377-
///
378-
/// - `data` is the data to send (an integer to send, or a buffer object).
379-
/// - `timeout` is the timeout in milliseconds to wait for the send.
380-
///
381-
/// Return value: number of bytes sent.
382-
// static const mp_arg_t machine_usbd_cdc_send_args[] = {
383-
// { MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
384-
// { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
385-
// };
386-
// #define MACHINE_USBD_CDC_SEND_NUM_ARGS MP_ARRAY_SIZE(machine_usbd_cdc_send_args)
387-
388-
// static mp_obj_t machine_usbd_cdc_send(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
389-
// // parse args
390-
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(args[0]);
391-
// mp_arg_val_t vals[MACHINE_USBD_CDC_SEND_NUM_ARGS];
392-
// mp_arg_parse_all(n_args - 1, args + 1, kw_args, MACHINE_USBD_CDC_SEND_NUM_ARGS, machine_usbd_cdc_send_args, vals);
393-
394-
// // get the buffer to send from
395-
// mp_buffer_info_t bufinfo;
396-
// uint8_t data[1];
397-
// pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data);
398-
399-
// // send the data
400-
// int ret = usbd_cdc_tx(self, bufinfo.buf, bufinfo.len, vals[1].u_int);
401-
402-
// return mp_obj_new_int(ret);
403-
// }
404-
// static MP_DEFINE_CONST_FUN_OBJ_KW(machine_usbd_cdc_send_obj, 1, machine_usbd_cdc_send);
405-
406-
/// \method recv(data, *, timeout=5000)
407-
///
408-
/// Receive data on the bus:
409-
///
410-
/// - `data` can be an integer, which is the number of bytes to receive,
411-
/// or a mutable buffer, which will be filled with received bytes.
412-
/// - `timeout` is the timeout in milliseconds to wait for the receive.
413-
///
414-
/// Return value: if `data` is an integer then a new buffer of the bytes received,
415-
/// otherwise the number of bytes read into `data` is returned.
416-
// static mp_obj_t machine_usbd_cdc_recv(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
417-
// // parse args
418-
// machine_usbd_cdc_obj_t *self = MP_OBJ_TO_PTR(args[0]);
419-
// mp_arg_val_t vals[MACHINE_USBD_CDC_SEND_NUM_ARGS];
420-
// mp_arg_parse_all(n_args - 1, args + 1, kw_args, MACHINE_USBD_CDC_SEND_NUM_ARGS, machine_usbd_cdc_send_args, vals);
421-
422-
// // get the buffer to receive into
423-
// vstr_t vstr;
424-
// mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
425-
426-
// // receive the data
427-
// int ret = usbd_cdc_rx(self, (uint8_t *)vstr.buf, vstr.len, vals[1].u_int);
428-
429-
// // return the received data
430-
// if (o_ret != MP_OBJ_NULL) {
431-
// return mp_obj_new_int(ret); // number of bytes read into given buffer
432-
// } else {
433-
// vstr.len = ret; // set actual number of bytes read
434-
// return mp_obj_new_bytes_from_vstr(&vstr); // create a new buffer
435-
// }
436-
// }
437-
// static MP_DEFINE_CONST_FUN_OBJ_KW(machine_usbd_cdc_recv_obj, 1, machine_usbd_cdc_recv);
438-
439-
static mp_obj_t machine_usbd_cdc_recv(uint n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
440-
enum { ARG_data, ARG_timeout };
441-
static const mp_arg_t allowed_args[] = {
442-
{ MP_QSTR_dest, MP_ARG_INT | MP_ARG_OBJ | MP_ARG_REQUIRED, {.u_obj = MP_OBJ_NULL} },
443-
{ MP_QSTR_timeout, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 5000 } },
444-
};
445-
446-
// Parse args.
447-
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
448-
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
449-
450-
vstr_t vstr;
451-
mp_obj_t o_ret = mp_const_none;
452-
if (mp_obj_is_int(args[ARG_data].u_obj)) {
453-
// allocate a new bytearray of given length
454-
vstr_init_len(&vstr, mp_obj_get_int(args[ARG_data].u_obj));
455-
} else {
456-
// get the existing buffer
457-
mp_buffer_info_t bufinfo;
458-
mp_get_buffer_raise(args[ARG_data].u_obj, &bufinfo, MP_BUFFER_WRITE);
459-
vstr.buf = bufinfo.buf;
460-
vstr.len = bufinfo.len;
461-
o_ret = args[ARG_data].u_obj;
462-
}
463-
size_t read = 0;
464-
uint32_t start = HAL_GetTick();
465-
size_t timeout = args[ARG_timeout].u_int;
466-
while (read < vstr.len && (HAL_GetTick() - start < timeout)) {
467-
468-
// int ret = machine_usbd_cdc_read(pos_args[0], vstr.buf, vstr.len - read, &errcode);
469-
int ret = mp_usbd_cdc_rx_strn((char *)vstr.buf + read, vstr.len - read);
470-
471-
if (ret == 0) {
472-
// mp_usbd_task();
473-
} else {
474-
read += ret;
475-
}
476-
mp_event_wait_ms(timeout);
477-
}
478-
479-
// return the received data
480-
if (o_ret != MP_OBJ_NULL) {
481-
return mp_obj_new_int(read); // number of bytes read into given buffer
482-
} else {
483-
vstr.len = read; // set actual number of bytes read
484-
return mp_obj_new_bytes_from_vstr(&vstr); // create a new buffer
485-
}
486-
}
487-
MP_DEFINE_CONST_FUN_OBJ_KW(machine_usbd_cdc_recv_obj, 2, machine_usbd_cdc_recv);
488-
489375
// irq(handler=None, trigger=IRQ_RX, hard=False)
490376
static mp_obj_t machine_usbd_cdc_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
491377
enum { ARG_handler, ARG_trigger, ARG_hard };
@@ -531,8 +417,6 @@ static const mp_rom_map_elem_t machine_usbd_cdc_locals_dict_table[] = {
531417
// { MP_ROM_QSTR(MP_QSTR_setinterrupt), MP_ROM_PTR(&machine_usbd_cdc_setinterrupt_obj) },
532418
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&machine_usbd_cdc_isconnected_obj) },
533419
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_usbd_cdc_any_obj) },
534-
// { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&machine_usbd_cdc_send_obj) },
535-
{ MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&machine_usbd_cdc_recv_obj) },
536420
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
537421
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
538422
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},

0 commit comments

Comments
 (0)
0