Skip to content

Commit e70f4d3

Browse files
Adds HW Serial CDC as IDF component example (#10262)
* feat(hw_cdc): creates documentation for the example Adds a README file that exaplains the example. * feat(hw_cdc): create cmake config file Adds necessary CMakeLists.txt file to the project with the HW CDC defines that will enable it. * feat(hw_cdc): create sdkconfig.defaults Adds necessary and minimum sdkconfig settings in order to make Arduino run as IDF Compoenent. * feat(hw_cdc): create cmake config file Create the Arduino Sketch source code CMakeLists.txt file that will include all source code files and header files in the proejct. * feat(hw_cdc): create main.cpp Adds the minimum Arduino Sketch that will print "Hello World!" in the HW Serial USB CDC port. * feat(hw_cdc): create idf_component.yml Adds necessary ESP32 Registry information in order to make ESP32 Arduino to be automatically included in the project as an IDF component. * feat(hw_cdc): update main.cpp adds code to wait for the user to open the Serial Monitor * feat(hw_cdc): formating text code Applying Code style and formating. * feat(hw_cdc): adds the example to idf_component.yml Adds the example to the list of example in the ESP32 Registry. * ci(pre-commit): Apply automatic fixes * fix(chw_cdc): typo fix Fixed Typo in the documentation. * fix(hw_cdc): fixed a commentary typo Fixed commantary typo --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 7a4587b commit e70f4d3

File tree

7 files changed

+116
-0
lines changed

7 files changed

+116
-0
lines changed

Diff for: idf_component.yml

+1
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,4 @@ dependencies:
9696
require: public
9797
examples:
9898
- path: ./idf_component_examples/hello_world
99+
- path: ./idf_component_examples/hw_cdc_hello_world
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For more information about build system see
2+
# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html
3+
# The following five lines of boilerplate have to be in your project's
4+
# CMakeLists in this exact order for cmake to work correctly
5+
cmake_minimum_required(VERSION 3.16)
6+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
7+
8+
# Adds necessary definitions for compiling it using Serial symbol attached to the HW USB CDC port
9+
list(APPEND compile_definitions "ARDUINO_USB_CDC_ON_BOOT=1")
10+
list(APPEND compile_definitions "ARDUINO_USB_MODE=1")
11+
12+
project(hw_cdc_hello_world)

Diff for: idf_component_examples/hw_cdc_hello_world/README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
idf_component_register(
2+
SRC_DIRS "."
3+
INCLUDE_DIRS "."
4+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
espressif/arduino-esp32:
4+
version: "*"
5+
override_path: "../../../"
6+
pre_release: true
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <Arduino.h>
2+
3+
void setup() {
4+
// USB CDC doesn't need a baud rate
5+
Serial.begin();
6+
7+
// wait for the Serial Monitor to be open
8+
while (!Serial) {
9+
delay(100);
10+
}
11+
12+
Serial.println("\r\nStarting...\r\n");
13+
}
14+
15+
void loop() {
16+
Serial.println("Hello world!");
17+
delay(1000);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Arduino ESP32
3+
#
4+
CONFIG_AUTOSTART_ARDUINO=y
5+
# end of Arduino ESP32
6+
7+
#
8+
# FREERTOS
9+
#
10+
CONFIG_FREERTOS_HZ=1000
11+
# end of FREERTOS
12+
# end of Component config

0 commit comments

Comments
 (0)