This is the fourth part of the meta-tutorial, where I talk about designing a cheap plant watering sensor. If you did not already read the first, second and third part please do it now. These parts contain a lot information which lead to this point of the tutorial.
The third part ended with step 18, planing the final firmware. There a decision was made about the language and style of the firmware. This article will focus on the code of the firmware itself.
Step 19: Write a Preliminary Firmware
In order to be able to do some final tests with the prototypes and be able to work on the final PCB, I need a firmware which is is very close to the final one. In the Atmel Studio, I start a new C++ project in a new folder.
The first thing I do is checking the chosen compiler options for the project. Everything looks reasonable, I just add the option
--std=c++11 to the C++ compiler options to get the latest language features.
In a section below I will describe all modules I wrote and will point details about the functions. I obviously did not wrote the whole firmware sequentially in that order, instead I use a incremental approach to develop the software:
- Create empty frameworks for all modules.
- Create a header and implementation file for each module with the correct name.
- Add the header comments, the
#pragma onceand the
#includefor the own header file.
- At this point, each module should be ready, so I can easily add new functions to each module.
- Start with the hardware module.
- Write the initialisation for the hardware, like CPU speed, port directions and other important stuff.
- Layout the interface for the hardware module and prepare empty implementation blocks to be filled with code.
- At each place where code is missing, I write a comment
// FIXME!!to be reminded that there is something missing.
- Start the logic module.
- Write the main entry point of the logic.
- Call this entry point in the
main()method of the firmware.
- Add the hardware initialisation to the logic.
At this point, I have the structure of the firmware prepared as planed. This structure will lead me through the development process. Continue Reading