|
| 1 | +| Supported Targets | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S3 | |
| 2 | +| ----------------- | -------- | -------- | -------- | -------- | |
| 3 | + |
| 4 | +# _HW Serial USB CDC example_ |
| 5 | + |
| 6 | +This is the simplest buildable example made to be used as a template for new projects running Arduino-ESP32 as an ESP-IDF component that will redefine the `Serial` interface to be attached to the USB CDC Hardware Serial port.\ |
| 7 | +See [arduino-esp32](https://components.espressif.com/components/espressif/arduino-esp32) in ESP Registry. |
| 8 | + |
| 9 | +## How to use example |
| 10 | + |
| 11 | +After cloning this repository, go to the `hw_cdc_hello_world` folder and select the target by executing\ |
| 12 | +`idf.py set-target <SoC_target>`.\ |
| 13 | +`<SoC_target>` can be one of the installed IDF version supported targets. |
| 14 | + |
| 15 | +It is possible to just clone this folder be executing\ |
| 16 | +`idf.py create-project-from-example "espressif/arduino-esp32^3.0.5:hw_cdc_hello_world"` |
| 17 | + |
| 18 | +For IDF 5.1.x and forward, the list of targets that support Hardware USB CDC are, at least: esp32s3, esp32c3, esp32c6 and esp32h2.\ |
| 19 | +Then just run command: `idf.py build` or `idf.py -p USB_PORT flash monitor`. |
| 20 | + |
| 21 | +Usually, it is necessary to make the ESP32 SoC to enter in `Download Mode` before uploading the firmware.\ |
| 22 | +After that, just press `RESET/EN` to start the new firmware. |
| 23 | + |
| 24 | +## Example folder contents |
| 25 | + |
| 26 | +The project **hw_serial_example** contains one source file in C++ language [main.cpp](main/main.cpp). The file is located in folder [main](main). |
| 27 | + |
| 28 | +ESP-IDF projects are built using CMake. The project building configuration is contained in `CMakeLists.txt` |
| 29 | +file that provide a set of directives and instructions describing the project's source files and targets |
| 30 | +(executable, library, or both). |
| 31 | + |
| 32 | +Below is the minimum list of files in the project folder. |
| 33 | + |
| 34 | +``` |
| 35 | +├── CMakeLists.txt Global project CMake configuration file |
| 36 | +├── sdkconfig.defaults sdkconfig setting for an Arduino project |
| 37 | +├── main |
| 38 | +│ ├── CMakeLists.txt Arduino sketch CMake configuration file |
| 39 | +│ ├── idf_component.yml List of IDF components necessary to build the project |
| 40 | +│ └── main.cpp Arduino Sketch code - don't forget to add "#include <Arduino.h>" on it |
| 41 | +└── README.md This is the file you are currently reading |
| 42 | +``` |
| 43 | + |
| 44 | +## Configuring the Hardware USB CDC Serial |
| 45 | + |
| 46 | +ESP32 Arduino has two macro defined symbols that control what `Serial` symbol will represent. |
| 47 | +Default `Serial` is the UART0 from `HardwareSerial` class. |
| 48 | + |
| 49 | +`Serial` can be changed to be attached to the HW Serial JTAG port fro the SoC. |
| 50 | +In order to make it work, it is necessary to define 2 symbols: `ARDUINO_USB_CDC_ON_BOOT` and `ARDUINO_USB_MODE` to `1`. |
| 51 | +This is achieved by adding a couple lines to the [Project Folder CMakeLists.txt](CMakeLists.txt) file. |
| 52 | + |
| 53 | + |
| 54 | +``` |
| 55 | +# Adds necessary definitions for compiling it using Serial symbol attached to the HW USB CDC port |
| 56 | +list(APPEND compile_definitions "ARDUINO_USB_CDC_ON_BOOT=1") |
| 57 | +list(APPEND compile_definitions "ARDUINO_USB_MODE=1") |
| 58 | +
|
| 59 | +``` |
| 60 | + |
| 61 | +Those two lines will add a `-DSYMBOL=VAL` when compiling every source code file. |
| 62 | + |
| 63 | +In order to make sure that it is actually working correctly, the [sketch](main/main.cpp) will execute `Serial.begin();` with no baudrate, which only works for USB CDC. |
0 commit comments