Skip to content

Commit 3930440

Browse files
committed
Fix usb_cdc.enable(console=False, data=True)
1 parent 6bc4f80 commit 3930440

File tree

3 files changed

+30
-11
lines changed

3 files changed

+30
-11
lines changed

ports/atmel-samd/boards/metro_m4_express/pins.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
2626
{ MP_OBJ_NEW_QSTR(MP_QSTR_D10), MP_ROM_PTR(&pin_PA18) },
2727
{ MP_OBJ_NEW_QSTR(MP_QSTR_D11), MP_ROM_PTR(&pin_PA19) },
2828
{ MP_OBJ_NEW_QSTR(MP_QSTR_D12), MP_ROM_PTR(&pin_PA17) },
29+
30+
{ MP_OBJ_NEW_QSTR(MP_QSTR_LED),MP_ROM_PTR(&pin_PA16) },
2931
{ MP_OBJ_NEW_QSTR(MP_QSTR_D13),MP_ROM_PTR(&pin_PA16) },
3032

3133
{ MP_OBJ_NEW_QSTR(MP_QSTR_SDA),MP_ROM_PTR(&pin_PB02) },

shared-module/usb_cdc/__init__.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,26 @@ static const char data_cdc_comm_interface_name[] = USB_INTERFACE_NAME " CDC2 con
147147
static const char console_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC data";
148148
static const char data_cdc_data_interface_name[] = USB_INTERFACE_NAME " CDC2 data";
149149

150+
// .idx is set later.
151+
150152
static usb_cdc_serial_obj_t usb_cdc_console_obj = {
151153
.base.type = &usb_cdc_serial_type,
152154
.timeout = -1.0f,
153155
.write_timeout = -1.0f,
154-
.idx = 0,
155156
};
156157

157158
static usb_cdc_serial_obj_t usb_cdc_data_obj = {
158159
.base.type = &usb_cdc_serial_type,
159160
.timeout = -1.0f,
160161
.write_timeout = -1.0f,
161-
.idx = 1,
162162
};
163163

164164
static bool usb_cdc_console_is_enabled;
165165
static bool usb_cdc_data_is_enabled;
166166

167167
void usb_cdc_set_defaults(void) {
168-
usb_cdc_console_is_enabled = CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT;
169-
usb_cdc_data_is_enabled = CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT;
168+
common_hal_usb_cdc_enable(CIRCUITPY_USB_CDC_CONSOLE_ENABLED_DEFAULT,
169+
CIRCUITPY_USB_CDC_DATA_ENABLED_DEFAULT);
170170
}
171171

172172
bool usb_cdc_console_enabled(void) {
@@ -241,11 +241,21 @@ bool common_hal_usb_cdc_enable(bool console, bool data) {
241241
// Right now these objects contain no heap objects, but if that changes,
242242
// they will need to be protected against gc.
243243

244+
// Assign only as many idx values as necessary. They must start at 0.
245+
uint8_t idx = 0;
244246
usb_cdc_console_is_enabled = console;
245247
usb_cdc_set_console(console ? MP_OBJ_FROM_PTR(&usb_cdc_console_obj) : mp_const_none);
248+
if (console) {
249+
usb_cdc_console_obj.idx = idx;
250+
idx++;
251+
}
246252

247253
usb_cdc_data_is_enabled = data;
248254
usb_cdc_set_data(data ? MP_OBJ_FROM_PTR(&usb_cdc_data_obj) : mp_const_none);
255+
if (data) {
256+
usb_cdc_data_obj.idx = idx;
257+
}
258+
249259

250260
return true;
251261
}

supervisor/shared/usb/usb.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,14 @@ void usb_init(void) {
7777

7878
post_usb_init();
7979

80-
#if MICROPY_KBD_EXCEPTION
80+
#if MICROPY_KBD_EXCEPTION && CIRCUITPY_USB_CDC
8181
// Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() usb_callback will be invoked when Ctrl+C is received
8282
// This usb_callback always got invoked regardless of mp_interrupt_char value since we only set it once here
83-
tud_cdc_set_wanted_char(CHAR_CTRL_C);
83+
84+
// Don't watch for ctrl-C if there is no REPL.
85+
if (usb_cdc_console_enabled()) {
86+
tud_cdc_set_wanted_char(CHAR_CTRL_C);
87+
}
8488
#endif
8589
}
8690

@@ -103,7 +107,7 @@ void usb_set_defaults(void) {
103107
#endif
104108
};
105109

106-
// Some dynamic USB data must be saved after boot.py. How much is needed
110+
// Some dynamic USB data must be saved after boot.py. How much is needed?
107111
size_t usb_boot_py_data_size(void) {
108112
size_t size = 0;
109113

@@ -151,7 +155,10 @@ void usb_background(void) {
151155
#if CFG_TUSB_OS == OPT_OS_NONE
152156
tud_task();
153157
#endif
154-
tud_cdc_write_flush();
158+
// No need to flush if there's no REPL.
159+
if (usb_cdc_console_enabled()) {
160+
tud_cdc_write_flush();
161+
}
155162
}
156163
}
157164

@@ -274,13 +281,13 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
274281
* @param itf Interface index (for multiple cdc interfaces)
275282
* @param wanted_char The wanted char (set previously)
276283
*/
277-
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
278-
(void)itf; // not used
279284

285+
// Only called when console is enabled.
286+
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
280287
// Workaround for using lib/utils/interrupt_char.c
281288
// Compare mp_interrupt_char with wanted_char and ignore if not matched
282289
if (mp_interrupt_char == wanted_char) {
283-
tud_cdc_read_flush(); // flush read fifo
290+
tud_cdc_n_read_flush(itf); // flush read fifo
284291
mp_keyboard_interrupt();
285292
}
286293
}

0 commit comments

Comments
 (0)