Methodology for Building Embedded Programs from Code and Toolbox for Hardware Debugging

How to Build Embedded Programs and Find Documentation

1. Define the Purpose

For example, if I want to configure a peripheral, I need to first understand how to drive this peripheral, its structure, enumerations, and what methods are available, etc., and whether there are examples to refer to.

2. Find the Core Structure

<span>esp-hal</span> library documentation at https://docs.esp-rs.org/esp-hal/ indicates that the name of the driving structure is the uppercase name of the peripheral. For instance, the driving structure for the I2C peripheral is called I2C.

pub struct I2c<'d, Dm: DriverMode> { /* private fields */ }

Scroll to the bottom of the page, under <span>Modules</span>, you can query modules as needed. On the peripherals module page, we can find the core driving structure Peripherals. This core driving structure is very important as it is the center for interaction between the peripheral and the external world. All methods implemented in this structure are used to drive this peripheral. In some cases, a peripheral may have multiple driving structures.

3. Find the Constructor Method of the Structure

Use this constructor method to instantiate or configure the peripheral, which is mostly the new method, and it takes a peripheral as a parameter, and may also take configuration as a parameter.

4. Finally

After configuring the peripheral, you will find related methods and interfaces (traits) in the driving structure, and use these methods to control the peripheral in the program.

Project Debugging

The development board connects to the host PC via USB/UART (Universal Asynchronous Receiver-Transmitter), and the connection for programming code to the microcontroller uses the same connection to send log messages.

If you want to perform online debugging (debugging while the program is running on the board), you need to use specialized hardware tools, such as hardware debuggers or emulator systems. These tools connect to the microcontroller via JTAG, SWD (Serial Wire Debug), or proprietary interfaces provided by the manufacturer.

Generally, using logs is sufficient, unless the issues are very complex, especially those related to timing, interrupts, and hardware interactions, where using online debugging can be very valuable. Through online debugging, we can set breakpoints to check the system’s state, read and write memory locations in real-time, monitor changes in specific memory addresses or variables, and view and modify CPU register values.

LEDs (Light Emitting Diodes) are also a simple and effective debugging tool, used to determine issues through the switching and blinking patterns of the LED.

Logic Analyzers are electronic instruments (virtual in Wokwi) used to observe digital signals (high and low levels) that change over time. They are very useful when you need to debug communication protocols (like I2C, SPI) or check the timing relationships of multiple pins.

Oscilloscopes are similar to logic analyzers, but they are used to observe analog signals (continuously varying voltages) and can display the real waveforms of signals (such as voltage levels, waveforms, and signal transitions). They are very useful for debugging power issues, verifying signal integrity, and analyzing timing-related issues in embedded systems.

Both logic analyzers and oscilloscopes can be used in conjunction with other debugging techniques to help identify and resolve hardware-software integration issues.

Profilers are tools used to analyze code performance. They can tell us which functions consume the most CPU time or where performance bottlenecks occur, helping you optimize your code.

Leave a Comment