Introduction
In this blog, I discuss common keywords used in embedded system, as well as some common keywords used in other projects/products, but should be extremely cared when used in embedded system.
new and delete expression
It is not advisable to use dynamic allocation in embedded system.
- objects created with
neware stored in the heap, while globals are stored in the .data and .bss sections, and locals on the stack¹. This distinction, however, only exists in the software. At the hardware level, .data, .bss, heap and stack are just arbitrary portions of the RAM. [5] - for normal software on PC, dynamic allocation is OK.
- for embedded system, RAM is very limited, and dynamic memory allocation should be limitted.
- dynamic memory allocation may causes heap fragmentation which only OS on PC be able to manage the RAM by defragmenting it or putting unused stuff away into a paging/swap file. Embedded chips don't have such OS. [6]
volatile type qualifiers
Notes: this section was written with info from various resources, before I read this page: https://blog.mbedded.ninja/. Afterward, I highly recommend the page as the main reference [8] for this topic. You can read my page as an abridge version or head there for complete explaination.
- Compiler may optimize codes and perform reordering of code lines, but
volatileprevents such things [1]
x = 10; // write x
x = 20; // write x again. The compiler may eliminate 1st write
// but for embedded, IO memory, such write is important. - For embedded system,
volatileis needed for cases ,- representing hardware registers (or memory-mapped I/O) as variables [7], [8]
- variables which are read/written to from interrupts and also accessed outside the interrupt [8]
- using to read data from buffer, ig: read incoming data to buffers from interrupts, and then read the data back out of the buffer outside of the interrupt [8]
- sharing variables between execution contexts [7]
- Difference between
constandvolatile:
const | volatile |
|---|---|
| the keyword tells the compiler that the value is not to be changed during program execution. | the keyword tells the compiler 1) to not perform any optimization on the variables and not to assume anything about the variables against which it is declared. 2) the value of the variable declared using volatile may change at any time |
| used to prevent accidental modification of value within the program. | usually used for special memory such as I/O memory-mapped |
- a value can be both
constandvolatile: the value could be changed externally, such as via I/O, external interrupts.constonly ensures that the program doesn't modify the value within itself.
inline specifier
- In short,
inlinefunctions are used to reduced overhead in function call.- overhead are actions including storing memory address of the function calls and copies of argument to call stacks, transferring the program control to the functions, and then returing the control to the calling function.
- with
inline, the compiler inserts the whole code of inline function to the calling function when it is called. Data types are checked!
inlinewill not works if:- there is a loop within the function.
- there is a
staticvariable. - there is
switch,goto. (ifis OK).
- According to CPP Reference [3], function included in multiple source files must be
inline. - Compiler generated special member functions (default constructor, copy constructor, copy assignment operator) are `inline` [4].
- Differences between
macroandinline:
| Macros | inline |
|---|---|
| All macro functions are expanded at compile time. | Inline function are only expanded in the calling functions when the calling functions are called. |
| expression passed to macro functions are evaluated more than once | expression passed to inline functions are evaluated more than once. |
| there is no data type checking | there is strict data type checking |
References:
[1] Effective Modern C++. Item 40 Differences between atomic and volatile.
[2] Inline Functions in C++ - GeeksforGeeks
[3] inline specifier - cppreference.com
[4] Effective Modern C++. Item 17 Understanding special member function generation.
[6] c++ - Is it required to delete variables before going to sleep? - Arduino Stack Exchange
[7] microcontroller - Using volatile in embedded C development - Electrical Engineering Stack Exchange
[8] Embedded Systems And The Volatile Keyword | mbedded.ninja
