8000 esp32: Add MICROPY_HW_ENABLE_USBDEV macro. · micropython/micropython@efe88a2 · GitHub
[go: up one dir, main page]

Skip to content

Commit efe88a2

Browse files
committed
esp32: Add MICROPY_HW_ENABLE_USBDEV macro.
This splits off enabling the native USB peripheral from enabling the MICROPY_HW_USB_CDC option. Although currently setting MICROPY_HW_ENABLE_USBDEV 1 and MICROPY_HW_USB_CDC 0 results in a non-functional USB device, as there's no other driver support. This brings the esp32 port further in line with rp2 port and the (upcoming) support for using MicroPython's TinyUSB with esp32. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 97a5a49 commit efe88a2

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

ports/esp32/main.c

Copy file name to clipboard
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void mp_task(void *pvParameter) {
101101
#endif
102102
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
103103
usb_serial_jtag_init();
104-
#elif MICROPY_HW_USB_CDC
104+
#elif MICROPY_HW_ENABLE_USBDEV
105105
usb_init();
106106
#endif
107107
#if MICROPY_HW_ENABLE_UART_REPL

ports/esp32/mpconfigport.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,22 @@ typedef long mp_off_t;
260260
// board specifics
261261
#define MICROPY_PY_SYS_PLATFORM "esp32"
262262

263-
// Enable stdio over native USB peripheral CDC via TinyUSB
263+
// Enable native USB support
264+
#ifndef MICROPY_HW_ENABLE_USBDEV
265+
#define MICROPY_HW_ENABLE_USBDEV (SOC_USB_OTG_SUPPORTED)
266+
#endif
267+
268+
// Enable stdio over native USB peripheral CDC (via TinyUSB)
264269
#ifndef MICROPY_HW_USB_CDC
265-
#define MICROPY_HW_USB_CDC (SOC_USB_OTG_SUPPORTED)
270+
#define MICROPY_HW_USB_CDC (MICROPY_HW_ENABLE_USBDEV)
266271
#endif
267272

268273
// Enable stdio over USB Serial/JTAG peripheral
269-
#define MICROPY_HW_ESP_USB_SERIAL_JTAG (SOC_USB_SERIAL_JTAG_SUPPORTED && !MICROPY_HW_USB_CDC)
274+
#ifndef MICROPY_HW_ESP_USB_SERIAL_JTAG
275+
#define MICROPY_HW_ESP_USB_SERIAL_JTAG (SOC_USB_SERIAL_JTAG_SUPPORTED && !MICROPY_HW_ENABLE_USBDEV)
270276
#endif
271277

272-
#if MICROPY_HW_USB_CDC && MICROPY_HW_ESP_USB_SERIAL_JTAG
278+
#if MICROPY_HW_ENABLE_USBDEV && MICROPY_HW_ESP_USB_SERIAL_JTAG
273279
#error "Invalid build config: Can't enable both native USB and USB Serial/JTAG peripheral"
274280
#endif
275281

ports/esp32/usb.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
#include "py/mphal.h"
2929
#include "usb.h"
3030

31-
#if MICROPY_HW_USB_CDC
31+
#if MICROPY_HW_ENABLE_USBDEV
3232

3333
#include "esp_timer.h"
3434
#ifndef NO_QSTR
3535
#include "tinyusb.h"
3636
#include "tusb_cdc_acm.h"
3737
#endif
3838

39+
#if MICROPY_HW_USB_CDC
40+
3941
#define CDC_ITF TINYUSB_CDC_ACM_0
4042

4143
static uint8_t usb_rx_buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE];
@@ -64,11 +66,25 @@ static void usb_callback_rx(int itf, cdcacm_event_t *event) {
6466
}
6567
}
6668

69+
void usb_tx_strn(const char *str, size_t len) {
70+
// Write out the data to the CDC interface, but only while the USB host is connected.
71+
uint64_t timeout = esp_timer_get_time() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT_MS * 1000);
72+
while (tud_cdc_n_connected(CDC_ITF) && len && esp_timer_get_time() < timeout) {
73+
size_t l = tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, len);
74+
str += l;
75+
len -= l;
76+
tud_cdc_n_write_flush(CDC_ITF);
77+
}
78+
}
79+
80+
#endif // MICROPY_HW_USB_CDC
81+
6782
void usb_init(void) {
6883
// Initialise the USB with defaults.
6984
tinyusb_config_t tusb_cfg = {0};
7085
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
7186

87+
#if MICROPY_HW_USB_CDC
7288
// Initialise the USB serial interface.
7389
tinyusb_config_cdcacm_t acm_cfg = {
7490
.usb_dev = TINYUSB_USBDEV_0,
@@ -86,18 +102,7 @@ void usb_init(void) {
86102
#endif
87103
};
88104
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
89-
90-
}
91-
92-
void usb_tx_strn(const char *str, size_t len) {
93-
// Write out the data to the CDC interface, but only while the USB host is connected.
94-
uint64_t timeout = esp_timer_get_time() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT_MS * 1000);
95-
while (tud_cdc_n_connected(CDC_ITF) && len && esp_timer_get_time() < timeout) {
96-
size_t l = tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, len);
97-
str += l;
98-
len -= l;
99-
tud_cdc_n_write_flush(CDC_ITF);
100-
}
105+
#endif
101106
}
102107

103-
#endif // MICROPY_HW_USB_CDC
108+
#endif // MICROPY_HW_ENABLE_USBDEV

0 commit comments

Comments
 (0)
0