Fix usb_cdc.enable(console=False, data=True)

This commit is contained in:
Dan Halbert 2021-06-02 11:29:56 -04:00
parent 6bc4f80e61
commit 3930440bc5
3 changed files with 30 additions and 11 deletions

View File

@ -26,6 +26,8 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_PA18) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_PA19) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA17) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_LED),MP_ROM_PTR(&pin_PA16) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_D13),MP_ROM_PTR(&pin_PA16) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SDA),MP_ROM_PTR(&pin_PB02) },

View File

@ -147,26 +147,26 @@ static const char data_cdc_comm_interface_name[] = USB_INTERFACE_NAME " CDC2 con
static const char console_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC data";
static const char data_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC2 data";
// .idx is set later.
static usb_cdc_serial_obj_t usb_cdc_console_obj = {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
.idx = 0,
};
static usb_cdc_serial_obj_t usb_cdc_data_obj = {
.base.type = &usb_cdc_serial_type,
.timeout = -1.0f,
.write_timeout = -1.0f,
.idx = 1,
};
static bool usb_cdc_console_is_enabled;
static bool usb_cdc_data_is_enabled;
void usb_cdc_set_defaults(void) {
usb_cdc_console_is_enabled = CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT;
usb_cdc_data_is_enabled = CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT;
common_hal_usb_cdc_enable(CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT,
CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT);
}
bool usb_cdc_console_enabled(void) {
@ -241,11 +241,21 @@ bool common_hal_usb_cdc_enable(bool console, bool data) {
// Right now these objects contain no heap objects, but if that changes,
// they will need to be protected against gc.
// Assign only as many idx values as necessary. They must start at 0.
uint8_t idx = 0;
usb_cdc_console_is_enabled = console;
usb_cdc_set_console(console ? MP_OBJ_FROM_PTR(&usb_cdc_console_obj) : mp_const_none);
if (console) {
usb_cdc_console_obj.idx = idx;
idx++;
}
usb_cdc_data_is_enabled = data;
usb_cdc_set_data(data ? MP_OBJ_FROM_PTR(&usb_cdc_data_obj) : mp_const_none);
if (data) {
usb_cdc_data_obj.idx = idx;
}
return true;
}

View File

@ -77,10 +77,14 @@ void usb_init(void) {
post_usb_init();
#if MICROPY_KBD_EXCEPTION
#if MICROPY_KBD_EXCEPTION && CIRCUITPY_USB_CDC
// Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() usb_callback will be invoked when Ctrl+C is received
// This usb_callback always got invoked regardless of mp_interrupt_char value since we only set it once here
tud_cdc_set_wanted_char(CHAR_CTRL_C);
// Don't watch for ctrl-C if there is no REPL.
if (usb_cdc_console_enabled()) {
tud_cdc_set_wanted_char(CHAR_CTRL_C);
}
#endif
}
@ -103,7 +107,7 @@ void usb_set_defaults(void) {
#endif
};
// Some dynamic USB data must be saved after boot.py. How much is needed
// Some dynamic USB data must be saved after boot.py. How much is needed?
size_t usb_boot_py_data_size(void) {
size_t size = 0;
@ -151,7 +155,10 @@ void usb_background(void) {
#if CFG_TUSB_OS == OPT_OS_NONE
tud_task();
#endif
tud_cdc_write_flush();
// No need to flush if there's no REPL.
if (usb_cdc_console_enabled()) {
tud_cdc_write_flush();
}
}
}
@ -274,13 +281,13 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
* @param itf Interface index (for multiple cdc interfaces)
* @param wanted_char The wanted char (set previously)
*/
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
(void)itf; // not used
// Only called when console is enabled.
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
// Workaround for using lib/utils/interrupt_char.c
// Compare mp_interrupt_char with wanted_char and ignore if not matched
if (mp_interrupt_char == wanted_char) {
tud_cdc_read_flush(); // flush read fifo
tud_cdc_n_read_flush(itf); // flush read fifo
mp_keyboard_interrupt();
}
}