No more errors when using flash memory! Is a dream within reach with a little effort? Yes, at least you can make your developer's life much easier by preventing errors from the outset or simplifying the system in which you're trying to find a bug. It's not a new idea, but how can it be implemented? In such situations, the simulation or emulation of individual hardware components can be a real lifesaver.
In November 2022, we wrote in the blog post, “Simulation? Why, I have hardware.”, has already dealt in detail with the advantages of simulations in embedded software development and, in addition to numerous application cases, has also mentioned the emulation of flash or EEPROM memories.
The following blog post uses a concrete example to show how such emulation of flash memory hardware could help to find errors faster and develop software in a more targeted and better tested manner.
Houston, we have a problem
This, or perhaps slightly less dramatic ones, pop into your head when a system error pops up out of the proverbial nowhere, and at first glance, everything seems odd. This was also the case in our example. It involved an embedded system with external flash memory, which is regularly written to and read from. It's only annoying when the data read doesn't always match the previously written data, but rather discrepancies occur. What's clear is that the error lies in the details; in most cases, everything works as it should, and it only involves a few bytes among the hundreds of thousands on the chip.
From theory to practice: approach
In order to get to grips with such or similar errors when dealing with flash memories, certain sections of the logic can be abstracted and recreated through emulation.
The first step is to isolate and reproduce the error. For example, the part of the code responsible for writing and reading the flash memory, i.e., for communication, can be extracted from the project's source code and rebuilt so that it can run in a conventional development environment on a PC. Depending on the complexity, this will require a considerable amount of additional code, which, for example, provides the necessary data structures, classes, or the data to be written.
The core idea, however, is to isolate the problem, so in the second step, the methods responsible for hardware access to the flash are replaced with simple file access. The contents of the flash are thus mapped to a file. Instead of actually accessing the hardware, the desired memory accesses are now simulated.
If you were looking for a memory problem, it's now clear, by process of elimination, whether the error originated in the hardware or the software. Once this has been determined, the error can be narrowed down and found through debugging or further isolation and simplification. Thanks to emulation, significantly more robust software can now be developed for troubleshooting, as specific hardware errors can be easily simulated, and (unit) testing and debugging can be made more comfortable. These and other advantages resulting from preventative simulation can therefore also be interesting for subsequent or new developments.
Success through simulation: Benefits at a glance
The advantages of using simulations during development rather than only when an error occurs include:
- Simplified (unit) testing and debugging
- Verification of memory layouts (behavior at block boundaries)
- Checking long-term behavior (e.g. memory overflow of ring buffer implementations)
- Easy simulation of hardware errors such as flipping bits, defective memory blocks, etc.
- Better portability through layering, abstraction and encapsulation
Conclusion
Simulating hardware components is an extremely valuable practice in embedded software development. Of course, not every hardware component is as easy to simulate as in our example, simply replicating a flash memory. However, this approach greatly simplifies developers' work, and clever abstraction and simulation allow projects to progress faster and identify potential problems early on.
In any case, it's worth considering the possibilities of simulation right at the start of the project and developing creative solutions. We're happy to help, so don't hesitate to contact us with any questions.
