Skip to content

Commit 26751c9

Browse files
authored
Merge pull request #4848 from dhalbert/usb_cdc-data-only-fixes
Fix usb_cdc.enable(console=False, data=True)
2 parents 850ede7 + 95cbf3f commit 26751c9

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-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: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ 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+
// Console will always be itf 0.
87+
tud_cdc_set_wanted_char(CHAR_CTRL_C);
88+
}
8489
#endif
8590
}
8691

@@ -103,7 +108,7 @@ void usb_set_defaults(void) {
103108
#endif
104109
};
105110

106-
// Some dynamic USB data must be saved after boot.py. How much is needed
111+
// Some dynamic USB data must be saved after boot.py. How much is needed?
107112
size_t usb_boot_py_data_size(void) {
108113
size_t size = 0;
109114

@@ -151,7 +156,13 @@ void usb_background(void) {
151156
#if CFG_TUSB_OS == OPT_OS_NONE
152157
tud_task();
153158
#endif
154-
tud_cdc_write_flush();
159+
// No need to flush if there's no REPL.
160+
#if CIRCUITPY_USB_CDC
161+
if (usb_cdc_console_enabled()) {
162+
// Console will always be itf 0.
163+
tud_cdc_write_flush();
164+
}
165+
#endif
155166
}
156167
}
157168

@@ -205,6 +216,7 @@ void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
205216
// DTR = false is counted as disconnected
206217
if (!dtr) {
207218
cdc_line_coding_t coding;
219+
// Use whichever CDC is itf 0.
208220
tud_cdc_get_line_coding(&coding);
209221

210222
if (coding.bit_rate == 1200) {
@@ -274,13 +286,13 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
274286
* @param itf Interface index (for multiple cdc interfaces)
275287
* @param wanted_char The wanted char (set previously)
276288
*/
277-
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
278-
(void)itf; // not used
279289

290+
// Only called when console is enabled.
291+
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) {
280292
// Workaround for using lib/utils/interrupt_char.c
281293
// Compare mp_interrupt_char with wanted_char and ignore if not matched
282294
if (mp_interrupt_char == wanted_char) {
283-
tud_cdc_read_flush(); // flush read fifo
295+
tud_cdc_n_read_flush(itf); // flush read fifo
284296
mp_keyboard_interrupt();
285297
}
286298
}

0 commit comments

Comments
 (0)