8000 esp32: Add support for USB with CDC ACM. · micropython/micropython@c81d048 · GitHub
[go: up one dir, main page]

Skip to content

Commit c81d048

Browse files
committed
esp32: Add support for USB with CDC ACM.
The REPL will be available on the USB serial port. Signed-off-by: Damien George <damien@micropython.org>
1 parent 66a86a0 commit c81d048

File tree

6 files changed

+145
-1
lines changed

6 files changed

+145
-1
lines changed

ports/esp32/boards/sdkconfig.usb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_USB_ENABLED=y
2+
CONFIG_USB_CDC_ENABLED=y
3+
CONFIG_USB_CDC_RX_BUFSIZE=256
4+
CONFIG_USB_CDC_TX_BUFSIZE=256

ports/esp32/main.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
#include "lib/mp-readline/readline.h"
5656
#include "lib/utils/pyexec.h"
5757
#include "uart.h"
58+
#include "usb.h"
5859
#include "modmachine.h"
5960
#include "modnetwork.h"
6061
#include "mpthreadport.h"
@@ -77,7 +78,11 @@ void mp_task(void *pvParameter) {
7778
#if MICROPY_PY_THREAD
7879
mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t));
7980
#endif
81+
#if CONFIG_USB_ENABLED
82+
usb_init();
83+
#else
8084
uart_init();
85+
#endif
8186
machine_init();
8287

8388
// TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0.

ports/esp32/main/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ set(MICROPY_SOURCE_DRIVERS
3737
set(MICROPY_SOURCE_PORT
3838
${PROJECT_DIR}/main.c
3939
${PROJECT_DIR}/uart.c
40+
${PROJECT_DIR}/usb.c
4041
${PROJECT_DIR}/gccollect.c
4142
${PROJECT_DIR}/mphalport.c
4243
${PROJECT_DIR}/fatfs_port.c
@@ -126,6 +127,7 @@ if(IDF_TARGET STREQUAL "esp32")
126127
list(APPEND IDF_COMPONENTS esp32)
127128
elseif(IDF_TARGET STREQUAL "esp32s2")
128129
list(APPEND IDF_COMPONENTS esp32s2)
130+
list(APPEND IDF_COMPONENTS tinyusb)
129131
endif()
130132

131133
# Register the main IDF component.
@@ -185,6 +187,10 @@ if(IDF_VERSION_MINOR GREATER_EQUAL 2)
185187
# so add them explicitly.
186188
list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/${IDF_TARGET}/include)
187189
list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/soc/soc/include)
190+
if(IDF_VERSION_MINOR GREATER_EQUAL 3)
191+
list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/tinyusb/additions/include)
192+
list(APPEND MICROPY_CPP_INC_EXTRA ${IDF_PATH}/components/tinyusb/tinyusb/src)
193+
endif()
188194
endif()
189195

190196
# Include the main MicroPython cmake rules.

ports/esp32/mphalport.c

+6Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@
4343
#include "lib/timeutils/timeutils.h"
4444
#include "lib/utils/pyexec.h"
4545
#include "mphalport.h"
46+
#include "usb.h"
4647

4748
TaskHandle_t mp_main_task_handle;
4849

49-
STATIC uint8_t stdin_ringbuf_array[256];
50+
STATIC uint8_t stdin_ringbuf_array[260];
5051
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
5152

5253
// Check the ESP-IDF error code and raise an OSError if it's not ESP_OK.
@@ -110,9 +111,13 @@ void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
110111
if (release_gil) {
111112
MP_THREAD_GIL_EXIT();
112113
}
114+
#if CONFIG_USB_ENABLED
115+
usb_tx_strn(str, len);
116+
#else
113117
for (uint32_t i = 0; i < len; ++i) {
114118
uart_tx_one_char(str[i]);
115119
}
120+
#endif
116121
if (release_gil) {
117122
MP_THREAD_GIL_ENTER();
118123
}

ports/esp32/usb.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "usb.h"
30+
31+
#if CONFIG_USB_ENABLED
32+
33+
#include "tinyusb.h"
34+
#include "tusb_cdc_acm.h"
35+
36+
#define CDC_ITF TINYUSB_CDC_ACM_0
37+
38+
static uint8_t usb_rx_buf[CONFIG_USB_CDC_RX_BUFSIZE];
39+
40+
static void usb_callback_rx(int itf, cdcacm_event_t *event) {
41+
// TODO: what happens if more chars come in during this function, are they lost?
42+
for (;;) {
43+
size_t len = 0;
44+
esp_err_t ret = tinyusb_cdcacm_read(itf, usb_rx_buf, sizeof(usb_rx_buf), &len);
45+
if (ret != ESP_OK) {
46+
break;
47+
}
48+
if (len == 0) {
49+
break;
50+
}
51+
for (size_t i = 0; i < len; ++i) {
52+
if (usb_rx_buf[i] == mp_interrupt_char) {
53+
mp_keyboard_interrupt();
54+
} else {
55+
ringbuf_put(&stdin_ringbuf, usb_rx_buf[i]);
56+
}
57+
}
58+
}
59+
}
60+
61+
void usb_init(void) {
62+
// Initialise the USB with defaults.
63+
tinyusb_config_t tusb_cfg = {0};
64+
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
65+
66+
// Initialise the USB serial interface.
67+
tinyusb_config_cdcacm_t amc_cfg = {
68+
.usb_dev = TINYUSB_USBDEV_0,
69+
.cdc_port = CDC_ITF,
70+
.rx_unread_buf_sz = 256,
71+
.callback_rx = &usb_callback_rx,
72+
.callback_rx_wanted_char = NULL,
73+
.callback_line_state_changed = NULL,
74+
.callback_line_coding_changed = NULL
75+
};
76+
ESP_ERROR_CHECK(tusb_cdc_acm_init(&amc_cfg));
77+
}
78+
79+
void usb_tx_strn(const char *str, size_t len) {
80+
while (len) {
81+
size_t l = len;
82+
if (l > CONFIG_USB_CDC_TX_BUFSIZE) {
83+
l = CONFIG_USB_CDC_TX_BUFSIZE;
84+
}
85+
tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, l);
86+
tinyusb_cdcacm_write_flush(CDC_ITF, pdMS_TO_TICKS(1000));
87+
str += l;
88+
len -= l;
89+
}
90+
}
91+
92+
#endif // CONFIG_USB_ENABLED

ports/esp32/usb.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_ESP32_USB_H
27+
#define MICROPY_INCLUDED_ESP32_USB_H
28+
29+
void usb_init(void);
30+
void usb_tx_strn(const char *str, size_t len);
31+
32+
#endif // MICROPY_INCLUDED_ESP32_USB_H

0 commit comments

Comments
 (0)
0