Part of the clean up process takes the whole thing back to the beginning, assuming nothing, and only using baby steps.
Making a platform.
Nothing ever gets built without the tools. Embedded applications are the same, but the tools are not hammers and screw drivers. The tools of embedded programming are functions that have had the bugs worked out of them. That perform particular tasks in an efficient and highly predictable manner. These tools are able to be ported from project to project with minimal modification and only a superficial cleaning to fit the specific use. Because embedded applications are a little bit software and a little bit hardware, often a generic platform is also carried from application to application because the tools, often, are optimized for the platform. I have worked with many platforms, and many software development environments, from assembly language on an early PC (the old 8088 with two 5.25” floppy drives), to quad core ARM systems with LAN using high level languages like C, Java, and Python. All along the way I have developed tools to perform tasks, and periodically, I have taken the tools out of the box and refit them to the new environment.
Hammers:
So what kinds of tools are in my tool box? Serial communication! Top priority number one most used functionality. Ported to so many machines and so many languages, I have lost count. Luckily, since back in the early days of 6800, 6502, Z80, and 8080, the communication module has remained mostly unchanged. The only change has only been what the registers were named, the function has always been the same. This means the code has not had to change significantly, other than register naming and how interrupts are managed. Also, writing the code in C has given me a highly portable and super clean operator for managing serial communications.
Next on the list is timers. A real time system needs to have a real time base. It is never known when a program is running (short of counting cycles), how much time has elapsed in a program from one function to another. Especially if there are lots of interrupt routines, and the functions are under the control of another team of programmers. The way to manage time is to use a constant time base, either with an interrupt based counter, or with some form of real time clock hardware. The second is usually battery backed to provide a real clock function, with date information as well. Interrupt based counters are most often using some form of capture/compare timer functionality, with a periodic interrupt and a counter to give the number of tics and converting to hours, minutes, and seconds (and possibly day, month, year).
The next group of tools is mostly dependent on what is installed in the platform, but fall into categories; data collection, which can be A/D measurement or switch state, graphics, things like plot, draw, filled shapes, and axis rotation, and math functions for creation of sin, cos, log, or root values. I also have some specialized tools for the work I generally do, like; table interpolation, and a general data stream parser.
When you have a handful of established tools, it is easier to look at a problem and determine which tools will be used in the solution. With this in mind, it is a good rule that more tools is always better, because, “if the only tool you have is a hammer, all your problems look like nails” becomes the box you put yourself into.
Wood:
There are a number of processor chips on the market, and depending on what is needed, points the direction for which to select. Embedded programming has an established bundle of guidelines when it comes to selecting processors. Tool sets meaning; compiler, Integrated Development Environment (IDE), In Circuit Emulator (ICE), programmer (for loading code), and the most important, specification sheets. The other selection criteria is a clear migration path, so the chip manufacturer won’t stop production just before you are ready to start yours, and in case you outgrow the current chip, or the speed just isn’t there, there is a better version that will run the current code with only slight modification.
Manufacturers have development platforms which are outstanding for early code development, but often paint you in a corner for the hardware design. Occasionally this is good, but often the function pins you had wanted to use for a specific purpose is unavailable because a platform function has forced it a certain way.
I am currently a fan of the Atmel line of processors, because they provide a good migration path for going larger, they are a well-established manufacturer, so they are not likely to go away in the near future (before I can retire). They have 8, 16, and 32 bit Micro Processor Units (MPUs), and have multiple core processor types. They also have a compiler and IDE that are available from the website for free download, a large number of development platforms, and ICE capability using Joint Test Action Group (JTAG) ICE devices. The platforms are also available as education based test units from Arduino (open source hardware), that provide good basic platform with very little extra baggage, meaning most of the pins are available directly via headers.
Project Start:
The development platform selected is an Arduino Nano, using an in circuit AVR programmer. The Atmel 328 is a small easily managed processor with a good array of features, well suited for a learning platform. The IDE is AVR Studio from the Atmel site (only requires you to enter some information so they can send you product updates via email), not exactly “free” like beer. The next piece of ground work is programming style. Coding style is like a fingerprint. Each person has their own style and technique, usually based on years of practice. I have a particular style and because I am the writer, I get to use my style. The reader might have a different style and since if you get 20 programmers in a room you will see at least 21 programming styles, I am not going to try and change. There will be a significant amount of source provided, particularly in the tools section. It is probably best now to establish some programming “rules” (really more of a guideline):
- Floating point is for people, not computers, and should be avoided in real time systems wherever possible.
- Self documenting code is a nice theory, so comment as much as possible. And no, you won’t remember later.
- Real time and control systems are event driven so interrupts rule, and mainline is small.
- Watch for patterns, a generic function beats out cut and paste, always!
- Time is finite, you can’t make more of it, even if it would be handy.
More rules will pop up as the discussion progresses. Returning to style, here is a style point that will probably rub many of the Electrical Engineers out there the wrong way (hammer/nail issues). I try not to use state machines for my programming. I do know what they are, and how they work and how handy they can be for real time design. But, I have seen them misused often and badly enough times, they have become a method to avoid instead of embrace. With that in mind, here is a very effective means for making a state machine that encourages proper methodology instead of abuse. Newer C allows use of function pointers, so this is available in C as well as C++. Code begins here… I will explain style elements in just a bit.
typedef SWord (*func)(void); // declare a general function pointer func stateMachine[] = { // make a list of functions one for each state init, // state 0 is exit, also initialize (return is the next state) state1, // each state operation returns a state number of the next state to run state2, // states should be enumerated so naming can be used … stateN // N states in the diagram }; int main() { SWord state = 0; // initialize first // each state operation returns the next state while ((state = stateMachine[state]()) != 0); return state; // should always be zero, right? }
Style stuff…
SWord is used instead of int beacause, based on the system and compiler, an int can be whatever the word size for the processor happens to be. I developed a “types.h” include file with naming that always means the same thing, and make small adjustments based on the particular processor / compiler pairing. Simply described, UWord and SWord are for 16 bit numbers, unsigned and signed versions. UByte and SBtye are 8 bit, and ULong and SLong, are 32 bits. I also keep the Holy Grail for embedded programming in my types.h file. It is the method for accessing hardware addresses from high level C code.
#define REG_W(a) *((volatile UWord* const)(a)) #define ARR_W(a) ((UWord* const)(a))
These are the 16 bit versions of the mechanism; the first is for a register or a physical location in memory referenced by “a”, the second is for addressing an array of 16 bit locations in memory at address “a”. By defining a structure, and using the same method, it is possible to declare a memory mapped device in the same way. Enough for now, here is my basic types.h that will be used for the examples.
Please note: the lack of comments. Because this file is updated for each compiler change, it is always touched. If my memory gets that bad, I will have to leave the business.
/************************************************************************************************ types.h History: [JAC] Original Creation when the earth was cooling Copyright(c) 2016 Chaney Firmware ************************************************************************************************/ #pragma once #include <avr/io.h> #include <avr/interrupt.h> #include <stdbool.h> #include <stdio.h> #define VER_MAJ 0 #define VER_MIN 0 #define VER_REV 0 /************************************************************************************************ Version number is maintained as a single 16 bit number of bit fields bits Field 12:15 MAJOR (number between 0 and 15) 5:11 MINOR (number between 0 and 99) 0:4 REVISION (number between 0 and 31) ************************************************************************************************/ #define OFS_MAJ 12 #define OFS_MIN 5 #define OFS_REV 0 #define VERS_NO ((VER_MAJ<<OFS_MAJ)|(VER_MIN<<OFS_MIN)|(VER_REV<<OFS_REV)) typedef unsigned char UByte; typedef char SByte; typedef unsigned short UWord; typedef short SWord; typedef unsigned long ULong; typedef long SLong; #define REG_B(a) *((volatile UByte* const)(a)) #define REG_W(a) *((volatile UWord* const)(a)) #define REG_L(a) *((volatile ULong* const)(a)) #define REG_Bs(a) *((volatile SByte* const)(a)) #define REG_Ws(a) *((volatile SWord* const)(a)) #define REG_Ls(a) *((volatile SLong* const)(a)) #define ARR_B(a) ((UByte* const)(a)) #define ARR_W(a) ((UWord* const)(a)) #define ARR_L(a) ((ULong* const)(a)) #define ARR_Bs(a) ((SByte* const)(a)) #define ARR_Ws(a) ((SWord* const)(a)) #define ARR_Ls(a) ((SLong* const)(a)) #ifdef forever #undef forever #endif #define forever for(;;) #define bit0 (1<<0) #define bit1 (1<<1) #define bit2 (1<<2) #define bit3 (1<<3) #define bit4 (1<<4) #define bit5 (1<<5) #define bit6 (1<<6) #define bit7 (1<<7) #define bit8 (1<<8) #define bit9 (1<<9) #define bit10 (1<<10) #define bit11 (1<<11) #define bit12 (1<<12) #define bit13 (1<<13) #define bit14 (1<<14) #define bit15 (1<<15) /* end of file */
Installments are going to have to be divided up by function making a whole bunch of chapters for just getting the platform working. A/D conversion, UART, timers, and EEPROM management are the next sections.
Top Comments