Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .github/workflows/esp32-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ jobs:
- esp-idf-target: "esp32s3"
idf-version: 'v5.5.4'
jit: false
- esp-idf-target: "esp32s3"
idf-version: 'v5.2.7'
usb-serial: 'ON'
jit: false
- esp-idf-target: "esp32s3"
idf-version: 'v5.5.4'
usb-serial: 'ON'
jit: false
- esp-idf-target: "esp32s3"
idf-version: 'v5.2.7'
usb-cdc: 'ON'
jit: false
- esp-idf-target: "esp32s3"
idf-version: 'v5.5.4'
usb-cdc: 'ON'
Expand Down Expand Up @@ -126,10 +134,7 @@ jobs:
echo 'CONFIG_USE_USB_SERIAL=y' >> sdkconfig.defaults.in
fi
if [[ "${USB_CDC}" == "ON" ]]; then
printf '%s\n' \
'CONFIG_TINYUSB_CDC_ENABLED=y' \
'CONFIG_AVM_ENABLE_USB_CDC_PORT_DRIVER=y' \
>> sdkconfig.defaults.in
echo 'CONFIG_AVM_ENABLE_USB_CDC_PORT_DRIVER=y' >> sdkconfig.defaults.in
fi
export IDF_TARGET=${{matrix.esp-idf-target}}
if [ "${{ matrix.jit }}" = "true" ]; then
Expand All @@ -141,6 +146,7 @@ jobs:
fi

- name: Print size info with idf.py
# idf.py size has known failures on IDF v5.2.7, so we skip it for this version
if: matrix.idf-version != 'v5.2.7'
shell: bash
working-directory: ./src/platforms/esp32/
Expand Down
2 changes: 1 addition & 1 deletion doc/src/programmers-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -2022,7 +2022,7 @@ The Erlang-side API is the platform-specific `usb_cdc` module ([`libs/avm_esp32/

| Platform | Notes |
|----------|-------|
| ESP32 | Enable `CONFIG_USE_USB_SERIAL` and `CONFIG_AVM_ENABLE_USB_CDC_PORT_DRIVER` in `menuconfig`. The ESP-IDF `esp_tinyusb` component must be installed. |
| ESP32 | Enable `CONFIG_AVM_ENABLE_USB_CDC_PORT_DRIVER` in `menuconfig` (it pulls in TinyUSB CDC automatically). The ESP-IDF `esp_tinyusb` managed component must be installed: `idf.py add-dependency "espressif/esp_tinyusb^2.0.0"` (or list it in `main/idf_component.yml`). `CONFIG_USE_USB_SERIAL` is a separate option that routes the system console over USB CDC. |
| RP2040/RP2350 | Set `-DAVM_USB_CDC_PORT_DRIVER_ENABLED=ON` in CMake. You must also disable stdio over USB (`pico_enable_stdio_usb(AtomVM 0)`) so that the CDC interface is available for the port driver. |
| STM32 | Set `-DAVM_USB_CDC_PORT_DRIVER_ENABLED=ON` in CMake. TinyUSB is fetched automatically by default; set the `TINYUSB_PATH` environment variable to use a local checkout. |

Expand Down
8 changes: 7 additions & 1 deletion src/platforms/esp32/components/avm_builtins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ if(CONFIG_AVM_ENABLE_DAC_NIF)
endif()

if(CONFIG_AVM_ENABLE_USB_CDC_PORT_DRIVER)
if(IDF_VERSION_MAJOR GREATER_EQUAL 6 OR (IDF_VERSION_MAJOR EQUAL 5 AND IDF_VERSION_MINOR GREATER_EQUAL 3))
if(TARGET idf::espressif__esp_tinyusb)
target_link_libraries(${COMPONENT_LIB} PRIVATE idf::espressif__esp_tinyusb)
else()
message(FATAL_ERROR
"CONFIG_AVM_ENABLE_USB_CDC_PORT_DRIVER requires the espressif/esp_tinyusb "
"managed component. Add it with "
"'idf.py add-dependency \"espressif/esp_tinyusb^2.0.0\"' or by listing it "
"in main/idf_component.yml.")
endif()
endif()

Expand Down
10 changes: 6 additions & 4 deletions src/platforms/esp32/components/avm_builtins/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ config AVM_ENABLE_UART_PORT_DRIVER

config AVM_ENABLE_USB_CDC_PORT_DRIVER
bool "Enable USB CDC port driver"
depends on TINYUSB_CDC_ENABLED
depends on IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
select TINYUSB_CDC_ENABLED
default n
help
Enable the AtomVM usb_cdc port driver. This requires native USB CDC
support through TinyUSB and should be enabled only when the firmware
is intended to expose a USB CDC data port.
Enable the AtomVM usb_cdc port driver and TinyUSB CDC support. This
requires the espressif/esp_tinyusb managed component and should be
enabled only when the firmware is intended to expose a USB CDC data
port.

config AVM_ENABLE_OTP_CRYPTO_NIFS
bool "Enable OTP Crypto NIFs"
Expand Down
12 changes: 6 additions & 6 deletions src/platforms/esp32/components/avm_builtins/usb_cdc_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <freertos/queue.h>

#include <tinyusb.h>
#include <tusb_cdc_acm.h>
#include <tinyusb_cdc_acm.h>

#include "atom.h"
#include "bif.h"
Expand Down Expand Up @@ -298,12 +298,12 @@ static Context *usb_cdc_driver_create_port(GlobalContext *global, term opts)
.callback_line_state_changed = NULL,
.callback_line_coding_changed = NULL,
};
esp_err_t err = tusb_cdc_acm_init(&acm_cfg);
esp_err_t err = tinyusb_cdcacm_init(&acm_cfg);
if (err == ESP_OK) {
cdc_data->owns_itf = true;
} else if (err == ESP_ERR_INVALID_STATE) {
// The interface was already initialized (e.g. by the console).
// tusb_cdc_acm_init ignored our acm_cfg, so register our RX
// tinyusb_cdcacm_init ignored our acm_cfg, so register our RX
// callback explicitly — otherwise we'd never see incoming bytes.
esp_err_t rerr = tinyusb_cdcacm_register_callback(itf, CDC_EVENT_RX, &usb_cdc_rx_callback);
if (rerr != ESP_OK) {
Expand Down Expand Up @@ -332,7 +332,7 @@ static Context *usb_cdc_driver_create_port(GlobalContext *global, term opts)
if (xQueueAddToSet(cdc_data->rxqueue, event_set) != pdPASS) {
ESP_LOGE(TAG, "Failed to add USB CDC queue to event set.");
if (cdc_data->owns_itf) {
tusb_cdc_acm_deinit(itf);
tinyusb_cdcacm_deinit(itf);
} else {
tinyusb_cdcacm_unregister_callback(itf, CDC_EVENT_RX);
}
Expand All @@ -350,7 +350,7 @@ static Context *usb_cdc_driver_create_port(GlobalContext *global, term opts)
ESP_LOGE(TAG, "Failed to create context");
xQueueRemoveFromSet(cdc_data->rxqueue, event_set);
if (cdc_data->owns_itf) {
tusb_cdc_acm_deinit(itf);
tinyusb_cdcacm_deinit(itf);
} else {
tinyusb_cdcacm_unregister_callback(itf, CDC_EVENT_RX);
}
Expand Down Expand Up @@ -562,7 +562,7 @@ static void usb_cdc_driver_do_close(Context *ctx, GenMessage gen_message)
SMP_MUTEX_UNLOCK(s_open_lock);

if (cdc_data->owns_itf) {
tusb_cdc_acm_deinit(cdc_data->itf);
tinyusb_cdcacm_deinit(cdc_data->itf);
} else {
tinyusb_cdcacm_unregister_callback(cdc_data->itf, CDC_EVENT_RX);
}
Expand Down
13 changes: 4 additions & 9 deletions src/platforms/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,13 @@ menu "AtomVM configuration"
Reboot the ESP32 device if the start/0 entrypoint does not return the `ok` atom.

config USE_USB_SERIAL
bool "Enable USB CDC Serial"
bool "Enable USB CDC serial console"
depends on IDF_TARGET_ESP32S2 || IDF_TARGET_ESP32S3
default n
select TINYUSB_CDC_ENABLED
help
Enable USB CDC Serial functionality.

config TINYUSB_CDC_ENABLED
bool "Enable TinyUSB CDC (requires 'idf.py add-dependency esp_tinyusb')"
default y
depends on USE_USB_SERIAL
help
Enable TinyUSB CDC functionality if USE_USB_SERIAL is enabled.
Route the AtomVM console over USB CDC. This requires the
espressif/esp_tinyusb managed component.

config JIT_ENABLED
bool "Enable just in time compilation"
Expand Down
8 changes: 4 additions & 4 deletions src/platforms/esp32/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
#ifdef CONFIG_USE_USB_SERIAL
void init_usb_serial(void);
#include "tinyusb.h"
#include "tinyusb_cdc_acm.h"
#include "tinyusb_console.h"
#include "tinyusb_default_config.h"
#include "tusb_cdc_acm.h"
#include "tusb_console.h"
#endif

#include "esp32_sys.h"
Expand Down Expand Up @@ -155,9 +155,9 @@ void init_usb_serial()
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));

tinyusb_config_cdcacm_t acm_cfg = { 0 }; // the configuration uses default values
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
ESP_ERROR_CHECK(tinyusb_cdcacm_init(&acm_cfg));

esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb
ESP_ERROR_CHECK(tinyusb_console_init(TINYUSB_CDC_ACM_0)); // log to usb

ESP_LOGI(TAG, "USB initialization: done.");
}
Expand Down
Loading