This is an old revision of the document!
Table of Contents
Embedded Overview
Overview of embedded system development.
Key Parameters
- Memory
- Storage
- Power
- Processing power
Integral Types
On ARM processors pass in 4 or less parameters to functions, they will be put in registers and save stack space!
- Smaller data types are better
- use <stding.h>
- Use unit8_t not char, etc. More portable.
- Some processors short == int. Don't assume bit size
Floating Point
IEEE754 standard
- float 32 bits, 6 decimal places
- double 64 bits, 15 decimal places
- long double 80 bits, 19 decimal places
FPU - Floating point unit Does not take extra power Takes less procesisng power Make sure you need floats More memory needed when switching thread contents
Map File
Look at the *.map file at the end to see program memory usage ZI = Zero initialized data RW Data - read/write data
Bit manipulation
Binary can be like: 0c000100 Remember XOR! Great Macro:
#define MASK(0) ((uint8_t(1<<x))
Bit Fields
Better use of memory.
typedef union {
struct{
unsigned monday : 1;
unsigned tuesday : 1;
unsigned wednesday : 1;
unsigned thursday : 1;
unsigned friday : 1;
unsigned saturday : 1;
unsigned sunday : 1;
} days;
uint8_t byte_sized;
} weekdays;
weekdays active_days;
active_days.days.monday = 0;
Qualifiers
Volatile
Being specific about variables
Volatile – tells the compiler that the variable may change at any time by some source other than the code it’s compiling. This may avoid the compiler optimizing out a section of code that appears to never get execute due to a conditional.
- Blocking delays
- Loops that loop to delay
- Use volatile to avoid optimizing loop out
const
- Stored in ROM, use less space.
- #defines are simply replaces in-line by the compiler, canbloat.
- #defines are not visible to the debugger
- Try to use const if you can
Function Alternatives
- Functions use a lot of memory for the stack
- Lookup tables and inline functions use ROM
- Lookup tables are easy on the CPU
- Lookup tables are const arrays. Use const
- Limitations: values are discrete so you may have to use interpolation
Macro Functions
#define square_macro(x) x*x * part of the pre-processor
Error!
Square_macro(2+3) == 11, wrong answer! * Macros should be one line * Always in-line function. Bloat may occur, function would be better
Inline Functions
- Almost identical to regular functions
- Add the “inline” keyword
- The compiler may decide not to inline
- You can force the compiler to always inline
To force inlining:
Int square_AI(unsigned char x)__attribute__((always_inline));
Int square_AI(unsigned char x) {
Return x*x;
}
To suggest inlining:
Int inline square_SI(unsigned char x){
Return x*x;
}
Inline advantages over macros:
- Compiler checks parameter types
- Debugging is easier
Floating Point Alternatives
- Note that for a float 100,000,000.00 + 1.00 = 100,000,000.00!
- Use fixed point when you can anticipate the range
- Fixed point can be faster than float on processors without an FPU