|
| 1 | +# Flashing multiple partitions over USB CDC ACM interface |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This example demonstrates how to flash an ESP32-S3 from another ESP32-S3 or an ESP32-S2 MCU using the `esp_serial_flash` component API. Binaries to be flashed from host MCU to another Espressif SoC can be found in `binaries` folder and are converted into C-array during build process. |
| 6 | + |
| 7 | +> **Note:** The `esp32_usb_cdc_acm` port requires ESP-IDF v4.4 or newer to build |
| 8 | +
|
| 9 | +Following steps are performed in order to re-program target's memory: |
| 10 | + |
| 11 | +1. The system is started |
| 12 | +2. The USB CDC ACM driver is initialized and a task to handle USB events is created |
| 13 | +3. As soon as an ESP32-S3 is connected to the bus, a connection is opened with it using `loader_port_esp32_usb_cdc_acm_init()`. If the target USB Serial/JTAG peripheral is not active (e.g the device firmware is using the USB OTG peripheral), it is necessary to manually put it in download mode. |
| 14 | +4. The flasher connection is started with the connected ESP32-S3 by calling `esp_loader_connect()`. |
| 15 | +5. Binary file is opened and its size is acquired, as it has to be known before flashing. |
| 16 | +6. Then `esp_loader_flash_start()` is called to enter flashing mode and erase amount of memory to be flashed. |
| 17 | +7. `esp_loader_flash_write()` function is called repeatedly until the whole binary image is transferred. |
| 18 | +8. After completion, the device can be manually reset and another ESP32-S3 can be connected to perform the flashing on. |
| 19 | + |
| 20 | +> **Note:** The USB CDC ACM device of the ESP32-S3 does not support changing the baudrate, so the argument to `esp_loader_connect()` is irrelevant |
| 21 | +
|
| 22 | +## USB host driver usage |
| 23 | + |
| 24 | +This example makes use of the Espressif [USB Host Driver](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/usb_host.html). |
| 25 | +In addition to initializing the host driver, a FreeRTOS task needs to be created to handle host usb events. |
| 26 | + |
| 27 | +A binary semaphore is used as a lock for the connected device, and a callback is registered with the loader port which releases the lock so that a new device can be connected only after disconnection. |
| 28 | + |
| 29 | +## Hardware Required |
| 30 | + |
| 31 | +* One ESP32-S3 target board and one ESP32-S3 or ESP32-S2 board, with each board having USB connections |
| 32 | +* An USB OTG adapter for the host board |
| 33 | +* One or two USB cables for power supply and programming. |
| 34 | + |
| 35 | +> **Note:** The USB connector on most ESP32-S3 and ESP32-S2 boards cannot supply power to the target, so a separate power connection is required |
| 36 | +
|
| 37 | +## Building and flashing |
| 38 | + |
| 39 | +To run the example, type the following command: |
| 40 | + |
| 41 | +```CMake |
| 42 | +idf.py -p PORT flash monitor |
| 43 | +``` |
| 44 | + |
| 45 | +(To exit the serial monitor, type ``Ctrl-]``.) |
| 46 | + |
| 47 | +See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects. |
| 48 | + |
| 49 | +## Configuration |
| 50 | + |
| 51 | +For details about available configuration option, please refer to top level [README.md](../../README.md). |
| 52 | +Compile definitions can be specified on command line when running `idf.py`, for example: |
| 53 | + |
| 54 | +``` |
| 55 | +idf.py build -DMD5_ENABLED=1 |
| 56 | +``` |
| 57 | +Binaries to be flashed are placed in separate folder (binaries.c) for each possible target and converted to C-array. Without explicitly enabling MD5 check, flash integrity verification is disabled by default. |
| 58 | + |
| 59 | +## Example output |
| 60 | + |
| 61 | +Here is the example's console output: |
| 62 | + |
| 63 | +``` |
| 64 | +... |
| 65 | +I (541) main_task: Calling app_main() |
| 66 | +I (541) usb_flasher: Installing USB Host |
| 67 | +I (571) usb_flasher: Installing the USB CDC-ACM driver |
| 68 | +I (571) usb_flasher: Opening CDC ACM device 0x303A:0x1001... |
| 69 | +Connected to target |
| 70 | +I (1121) usb_flasher: Loading bootloader... |
| 71 | +Erasing flash (this may take a while)... |
| 72 | +Start programming |
| 73 | +Progress: 100 % |
| 74 | +Finished programming |
| 75 | +I (1681) usb_flasher: Loading partition table... |
| 76 | +Erasing flash (this may take a while)... |
| 77 | +Start programming |
| 78 | +Progress: 100 % |
| 79 | +Finished programming |
| 80 | +I (1771) usb_flasher: Loading app... |
| 81 | +Erasing flash (this may take a while)... |
| 82 | +Start programming |
| 83 | +Progress: 100 % |
| 84 | +Finished programming |
| 85 | +I (5011) usb_flasher: Done! |
| 86 | +``` |
0 commit comments