应用笔记 · 2023年11月2日

Get started with ESP-IDF and ESP32-DevKitC: debugging, unit testing, project analysis

From .PlatformIO official document

The goal of this tutorial is to demonstrate how simple it is to use PlatformIO IDE for VSCode to develop, run and debug a simple Wi-Fi project with the Espressif IoT Development Framework framework for the ESP32-DevKitC board.

  • Level: Intermediate
  • Platforms: Windows, Mac OS X, Linux

Requirements:

Setting Up the Project

  1. Click on “PlatformIO Home” button on the bottom PlatformIO Toolbar:

    ../../_images/espidf-debugging-unit-testing-analysis-1.png

  2. Click on “New Project”, select Espressif ESP32 Dev Module as the development board, Espressif IoT Development Framework as the framework and a path to the project location (or use the default one):

    ../../_images/espidf-debugging-unit-testing-analysis-2.png

Adding Code to the Generated Project

  1. Create a new file main.c in src_dir folder and add the following code:

    Warning

    Make sure this new file main.c is registered as source file using idf_component_register function in src/CMakeLists.txt file:


  2. To compile the project use one of the following options:
    • Build option from the Project Tasks menu
    • Build button in PlatformIO Toolbar
    • Task Menu Tasks: Run Task... > PlatformIO: Build or in PlatformIO Toolbar
    • Command Palette View: Command Palette > PlatformIO: Build
    • Hotkeys cmd-alt-b / ctrl-alt-b:

    ../../_images/espidf-debugging-unit-testing-analysis-3.png

  3. If everything went well, we should see a successful result message in the terminal window:

    ../../_images/espidf-debugging-unit-testing-analysis-4.png

  4. To upload the firmware to the board we can use the following options:
    • Upload option from the Project Tasks menu
    • Upload button in PlatformIO Toolbar
    • Command Palette View: Command Palette > PlatformIO: Upload
    • Task Menu Tasks: Run Task... > PlatformIO: Upload
    • Hotkeys cmd-alt-u / ctrl-alt-u:

    ../../_images/espidf-debugging-unit-testing-analysis-5.png

  5. Connect the board to your computer and update the default monitor speed to 115200 in platformio.ini file:

  6. Open Serial Monitor to observe the output from the board:

    ../../_images/espidf-debugging-unit-testing-analysis-6.png

  7. If everything went well, the board should be visible as a WiFi access point:

    ../../_images/espidf-debugging-unit-testing-analysis-7.png

Debugging the Firmware

Setting Up the Hardware

In order to use Debugging, we need to connect an external JTAG probe and the board using the following pins:

ESP32 pin JTAG probe pin
3.3V Pin 1(VTref)
GPIO 9 (EN) Pin 3 (nTRST)
GND Pin 4 (GND)
GPIO 12 (TDI) Pin 5 (TDI)
GPIO 14 (TMS) Pin 7 (TMS)
GPIO 13 (TCK) Pin 9 (TCK)
GPIO 15 (TDO) Pin 13 (TDO)
  1. Specify debug_tool in “platformio.ini” (Project Configuration File). In this tutorial, Olimex ARM-USB-OCD-H debug probe is used:

  2. To start the debug session we can use the following methods:
    • Debug: Start debugging in the top menu
    • Start Debugging option in the Quick Access menu
    • Hotkey button F5:

    ../../_images/espidf-debugging-unit-testing-analysis-8.png

  3. Walk through the code using control buttons, set breakpoints, and add variables to the Watch window:

    ../../_images/espidf-debugging-unit-testing-analysis-9.png

Writing Unit Tests

Note

Functions setUp and tearDown are used to initialize and finalize test conditions. Implementations of these functions are not required for running tests but if you need to initialize some variables before you run a test, you use the setUp function and if you need to clean up variables you use tearDown function.

For the sake of simplicity, let’s create a small library called calculator, implement several basic functions additionsubtractionmultiplicationdivision and test them using PlatformIO Unit Testing solution.

  1. Create a new folder calculator in the lib_dir folder and add two new files calculator.h and calculator.c with the following contents:

    calculator.h:


    calculator.c:


  2. Create a new file test_calc.c to the folder test_dir and add basic tests for the calculator library:

  3. Let’s run tests on the board and check the results. There should be a problem with test_function_calculator_division test:

    ../../_images/espidf-debugging-unit-testing-analysis-10.png

  4. Let’s fix the incorrect expected value and run tests again. After processing the results should be correct:

    ../../_images/espidf-debugging-unit-testing-analysis-11.png

Project Inspection

For illustrative purposes, let’s imagine we need to find a function with the biggest memory footprint. Also, let’s introduce a bug to our project so Static Code Analysis can report it.

  1. Open PlatformIO Home and navigate to Inspect section, select the current project and press Inspect button:

    ../../_images/espidf-debugging-unit-testing-analysis-12.png

  2. Project statistics:

    ../../_images/espidf-debugging-unit-testing-analysis-13.png

  3. The biggest function:

    ../../_images/espidf-debugging-unit-testing-analysis-14.png

  4. Possible bugs:

    ../../_images/espidf-debugging-unit-testing-analysis-15.png

Conclusion

Now we have a project template for the ESP32-DevKitC board that we can use as boilerplate for later projects.