As a seasoned developer who has dedicated my youth to development boards, I completely understand your frustration when you find yourself cursing at hardware in front of the screen! Today, I want to share some insider tips that have helped me reduce overtime.
Don’t Clash with Hardware – Be a “Blame Shifter”
Have you ever encountered a situation where your program suddenly misbehaves? You spend three days meticulously checking the code line by line, only to discover that the hardware layout is different from the schematic provided to the software!!!

In the embedded field, a software engineer who can’t shift blame is not a good cook! You are shifting the blame, not the responsibility! Once you elegantly shift the blame, conduct a self-check, and proactively follow up on the solution, maintaining a professional and positive attitude, the project manager will see that you not only know how to shift blame but can also ensure the project progresses smoothly.
The next time the project manager asks you, “Why was the feature launch delayed?” you can simply smile and say, “There were some hardware issues, and we are actively coordinating.” At that moment, the project manager can only express through their eyes, “It really is a hardware issue!”

Learn to “Peek” at Hardware – Directly Read Registers
Sometimes, directly reading and writing registers can be your “trump card” – it allows you to bypass the limitations of library functions and directly control the hardware. In Linux systems, if you want to access a certain register, you typically need to write a driver to map the register address to the virtual address space before you can operate on it.
But what if you just want to check the value of a certain register without going through such a complicated compilation process? Don’t worry, Linux has already prepared a convenient tool for us – devmem. With devmem, you can directly read and write registers in user space without writing a driver.
For example, to read the value of a 32-bit register at 0x50300000, you just need to run the following command:
devmem 0x50300000 32
If you want to write the value 0x00000005 to the 32-bit register at 0x50300000, you can do it like this:
devmem 0x50300000 32 0x00000005
Code Should Be Like LEGO – Modular Design
The boss says, “Change the sensor manufacturer for this project, and it will go live next Monday!” You look at the 2000 lines of main.c and feel overwhelmed…
In fact, for some standard structures but with different initialization details for sensors, you can consider writing their operations as modules, encapsulating init, start, and other operation functions for each different manufacturer’s sensor. For example:
First, you need to define a generic sensor operation structure (like sensor_ops), which contains function pointers for initialization, starting, stopping, etc.
Then, define the specific operation implementations for different manufacturers:
static int sensor_a_init(void) {
printf("Initializing Sensor A\n");
return 0;
}
static int sensor_a_start(void) {
printf("Starting Sensor A\n");
return 0;
}
...
sensor_ops_t sensor_a_ops = {
.init = sensor_a_init,
.start = sensor_a_start,
.stop = sensor_a_stop,
.read = sensor_a_read,
};
Finally, you can select the operation interface for different manufacturers based on the actual situation:
sensor_ops_t *current_sensor_ops;
void select_sensor(int vendor) {
if (vendor == 0) {
current_sensor_ops = &sensor_a_ops;
} elseif (vendor == 1) {
current_sensor_ops = &sensor_b_ops;
}
}
void main(void) {
int selected_vendor = 0;
select_sensor(selected_vendor);
current_sensor_ops->init();
current_sensor_ops->start();
}
With this approach, when you need to replace the sensor manufacturer, you only need to add a new sensor operation implementation and choose to use the new operation structure in the code. The entire code structure is clear and easy to maintain.
Always Use Pointers Correctly
Pointers are a very useful and powerful feature in the language, but they can lead to errors. A common mistake is using invalid pointer values in the code. For example, if it points to memory that has been dynamically allocated but has been freed.
Using invalid pointers can have negative effects that only become apparent after a long time and are difficult to detect. If you routinely set pointers to NULL after use, any subsequent erroneous use will result in an immediate error, making it easy to localize.
When using pointers, be sure to avoid the following types of pointers: null pointers (not pointing to any valid memory address, value is NULL), wild pointers (pointing to invalid or unknown memory addresses, value is not NULL), and out-of-bounds pointers (exceeding the legal range of memory addresses).
Initialize pointers immediately to NULL, perform a non-null check before use, and set to NULL immediately after freeing memory.
Smart Use of Dynamic Log Levels
In embedded systems, the primary means of troubleshooting is through logs. Logs not only improve the readability and maintainability of the code but also help developers accurately locate issues.
Adding logs excessively during debugging and then having to delete them after going live can be exhausting. The key to dynamic log levels is the ability to adjust the log level based on the runtime environment. For example, in a production environment, you can set the log level to ERROR through environment variables, while in a development environment, you can set it to DEBUG.
You can also use log rotation and log cleanup mechanisms to help you automatically manage the size and number of log files, avoiding disk space issues caused by overly large log files. Many logging libraries support this feature.
END
Lastly, I want to mention that the 21ic Forum (bbs.21ic.com) is recruiting original authors, with a maximum reward of 500 yuan per article. We welcome all netizens to actively submit articles! Click to learn more about the event details.
Previous Highlights:
-
Building a Factory in the U.S.? “Luxshare Precision” Just Made a Statement
-
“AITO” Car Show Out of Control, Latest Investigation Results Released
-
Humanoid Robot Company Defaults on Salaries, Office Vacated!
Scan the QR code to follow the video account.
Please click ใโกใ to give the editor a thumbs up!