8000 esp32/boards: Add GENERIC_C3_USB board with USB serial/JTAG support. · projectgus/micropython@a66bd7a · GitHub
[go: up one dir, main page]

Skip to content

Commit a66bd7a

Browse files
xorbitdpgeorge
authored andcommitted
esp32/boards: Add GENERIC_C3_USB board with USB serial/JTAG support.
Add a new board type for ESP32-C3 revision 3 and up that implement the USB serial/JTAG port on pin 18 and 19. This variant uses the USB serial for programming and console, leaving the UART free. - Pins 18 and 19 are correctly reserved for this variant. Also pins 14-17 are reserved for flash for any ESP32-C3 so they can't be reconfigured anymore to crash the system. - Added usb_serial_jtag.c and .h to implement this interface. - Interface was tested to work correctly together with webrepl. - Interface was tested to work correctly when sending and receiving large files with ampy. - Disconnecting terminal or USB will not hang the system when it's trying to print.
1 parent 3720a57 commit a66bd7a

File tree

9 files changed

+174
-4
lines changed

9 files changed

+174
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
set(IDF_TARGET esp32c3)
2+
3+
set(SDKCONFIG_DEFAULTS
4+
boards/sdkconfig.base
5+
boards/sdkconfig.ble
6+
boards/GENERIC_C3_USB/sdkconfig.board
7+
)
8+
9+
if(NOT MICROPY_FROZEN_MANIFEST)
10+
set(MICROPY_FROZEN_MANIFEST ${MICROPY_PORT_DIR}/boards/manifest.py)
11+
endif()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This configuration is for a generic ESP32C3 board with 4MiB (or more) of flash.
2+
3+
#define MICROPY_HW_BOARD_NAME "ESP32C3 module"
4+
#define MICROPY_HW_MCU_NAME "ESP32C3"
5+
6+
#define MICROPY_HW_ENABLE_SDCARD (0)
7+
#define MICROPY_PY_MACHINE_DAC (0)
8+
#define MICROPY_PY_MACHINE_I2S (0)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CONFIG_ESP32C3_REV_MIN_3=y
2+
CONFIG_ESP32C3_REV_MIN=3
3+
CONFIG_ESP32C3_BROWNOUT_DET=y
4+
CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_7=
5+
CONFIG_ESP32C3_BROWNOUT_DET_LVL_SEL_4=y
6+
CONFIG_ESP32C3_BROWNOUT_DET_LVL=4
7+
CONFIG_ESP_CONSOLE_UART_DEFAULT=
8+
CONFIG_ESP_CONSOLE_USB_CDC=
9+
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y

ports/esp32/machine_pin.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,17 @@ STATIC const machine_pin_obj_t machine_pin_obj[] = {
125125
{{&machine_pin_type}, GPIO_NUM_11},
126126
{{&machine_pin_type}, GPIO_NUM_12},
127127
{{&machine_pin_type}, GPIO_NUM_13},
128-
{{&machine_pin_type}, GPIO_NUM_14},
129-
{{&machine_pin_type}, GPIO_NUM_15},
130-
{{&machine_pin_type}, GPIO_NUM_16},
131-
{{&machine_pin_type}, GPIO_NUM_17},
128+
{{NULL}, -1}, // 14 FLASH
129+
{{NULL}, -1}, // 15 FLASH
130+
{{NULL}, -1}, // 16 FLASH
131+
{{NULL}, -1}, // 17 FLASH
132+
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
133+
{{NULL}, -1}, // 18 is for native USB D-
134+
{{NULL}, -1}, // 19 is for native USB D+
135+
#else
132136
{{&machine_pin_type}, GPIO_NUM_18},
133137
{{&machine_pin_type}, GPIO_NUM_19},
138+
#endif
134139
{{&machine_pin_type}, GPIO_NUM_20},
135140
{{&machine_pin_type}, GPIO_NUM_21},
136141

ports/esp32/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "shared/runtime/pyexec.h"
5959
#include "uart.h"
6060
#include "usb.h"
61+
#include "usb_serial_jtag.h"
6162
#include "modmachine.h"
6263
#include "modnetwork.h"
6364
#include "mpthreadport.h"
@@ -89,6 +90,8 @@ void mp_task(void *pvParameter) {
8990
#endif
9091
#if CONFIG_USB_ENABLED
9192
usb_init();
93+
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
94+
usb_serial_jtag_init();
9295
#else
9396
uart_init();
9497
#endif

ports/esp32/main/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(MICROPY_SOURCE_PORT
4747
${PROJECT_DIR}/main.c
4848
${PROJECT_DIR}/uart.c
4949
${PROJECT_DIR}/usb.c
50+
${PROJECT_DIR}/usb_serial_jtag.c
5051
${PROJECT_DIR}/gccollect.c
5152
${PROJECT_DIR}/mphalport.c
5253
${PROJECT_DIR}/fatfs_port.c

ports/esp32/mphalport.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "shared/runtime/pyexec.h"
5454
#include "mphalport.h"
5555
#include "usb.h"
56+
#include "usb_serial_jtag.h"
5657

5758
TaskHandle_t mp_main_task_handle;
5859

@@ -118,6 +119,8 @@ void mp_hal_stdout_tx_strn(const char *str, size_t len) {
118119
}
119120
#if CONFIG_USB_ENABLED
120121
usb_tx_strn(str, len);
122+
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
123+
usb_serial_jtag_tx_strn(str, len);
121124
#else
122125
for (uint32_t i = 0; i < len; ++i) {
123126
uart_tx_one_char(str[i]);

ports/esp32/usb_serial_jtag.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Patrick Van Oosterwijck
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_serial_jtag.h"
30+
31+
#if CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
32+
33+
#include "hal/usb_serial_jtag_ll.h"
34+
#include "esp_intr_alloc.h"
35+
#include "soc/periph_defs.h"
36+
37+
#define USB_SERIAL_JTAG_BUF_SIZE (64)
38+
39+
static uint8_t rx_buf[USB_SERIAL_JTAG_BUF_SIZE];
40+
static volatile bool terminal_connected = false;
41+
42+
static void usb_serial_jtag_isr_handler(void *arg) {
43+
uint32_t flags = usb_serial_jtag_ll_get_intsts_mask();
44+
45+
if (flags & USB_SERIAL_JTAG_INTR_SOF) {
46+
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SOF);
47+
}
48+
49+
if (flags & USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT) {
50+
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT);
51+
size_t req_len = ringbuf_free(&stdin_ringbuf);
52+
if (req_len > USB_SERIAL_JTAG_BUF_SIZE) {
53+
req_len = USB_SERIAL_JTAG_BUF_SIZE;
54+
}
55+
size_t len = usb_serial_jtag_ll_read_rxfifo(rx_buf, req_len);
56+
for (size_t i = 0; i < len; ++i) {
57+
if (rx_buf[i] == mp_interrupt_char) {
58+
mp_sched_keyboard_interrupt();
59+
} else {
60+
ringbuf_put(&stdin_ringbuf, rx_buf[i]);
61+
}
62+
}
63+
mp_hal_wake_main_task_from_isr();
64+
}
65+
}
66+
67+
void usb_serial_jtag_init(void) {
68+
usb_serial_jtag_ll_clr_intsts_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT |
69+
USB_SERIAL_JTAG_INTR_SOF);
70+
usb_serial_jtag_ll_ena_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_OUT_RECV_PKT |
71+
USB_SERIAL_JTAG_INTR_SOF);
72+
ESP_ERROR_CHECK(esp_intr_alloc(ETS_USB_SERIAL_JTAG_INTR_SOURCE, ESP_INTR_FLAG_LEVEL1,
73+
usb_serial_jtag_isr_handler, NULL, NULL));
74+
}
75+
76+
void usb_serial_jtag_tx_strn(const char *str, size_t len) {
77+
while (len) {
78+
size_t l = len;
79+
if (l > USB_SERIAL_JTAG_PACKET_SZ_BYTES) {
80+
l = USB_SERIAL_JTAG_PACKET_SZ_BYTES;
81+
}
82+
portTickType start_tick = xTaskGetTickCount();
83+
while (!usb_serial_jtag_ll_txfifo_writable()) {
84+
portTickType now_tick = xTaskGetTickCount();
85+
if (!terminal_connected || now_tick > (start_tick + pdMS_TO_TICKS(200))) {
86+
terminal_connected = false;
87+
return;
88+
}
89+
}
90+
terminal_connected = true;
91+
l = usb_serial_jtag_ll_write_txfifo((const uint8_t *)str, l);
92+
usb_serial_jtag_ll_txfifo_flush();
93+
str += l;
94+
len -= l;
95+
}
96+
}
97+
98+
#endif // CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG

ports/esp32/usb_serial_jtag.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 Patrick Van Oosterwijck
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_SERIAL_JTAG_H
27+
#define MICROPY_INCLUDED_ESP32_USB_SERIAL_JTAG_H
28+
29+
void usb_serial_jtag_init(void);
30+
void usb_serial_jtag_tx_strn(const char *str, size_t len);
31+
32+
#endif // MICROPY_INCLUDED_ESP32_USB_SERIAL_JTAG_H

0 commit comments

Comments
 (0)
0