if you know C, you are 50 % embedded capable. In this article, I will share a few tips that will make you an embedded qualified programmer.
1. Understand Memory Segments
Memory where the code runs can come one or more different areas called segments. These areas are simply the logical divisions that can contain suitable physical memory. To understand C code better, you need to be aware of a few common memory segments. Users can always define their segments in assembly code.
-------------------
| Text segment |
| |
|-------------------|
| Data segment |
| |
|-------------------|
| symbol table |
| |
|-------------------|
| Stack |
| |
|-------------------|
| HEAP |
| |
-------------------
1.a. Text (instructions)
This segment contains execution instructions. Normally, this segment is Read only. Programs are not generally allowed to modify the text segment. The code below will reside in the text segment.
int f() {
return 1;
}
1.b. Data Segment
This segment is used to hold global and static variables in a program. static variable, in a way, is a global variable except with limited visibility scope. A global variable/function normally has visible entry in the symbol table, whereas the static does not have an entry in the symbol table.
The globalCount variable below will reside in the data segment. Any change in its value anywhere will be effective.
int globalCount = 0;
void increment()
{
globalCount++;
}
1.c. Symbol Table
When the compiler compiles code, it puts all named entries (functions, global variables), into symbol table. The debugger uses this symbol table to go between an address and a function name. Its main purpose is for debugging/tracing.
1.d. Stack
Stack is where automatic variables stored when exceeding the number of register profile. Each program will have a default stacksize which is set by linker/compiler tool. In the example below, variable array will be allocated on the stack, whereas variable i is likely to be assigned with a register.
void functionF()
{
int array[20];
int i;
for (i = 0; i < 20; i++)
array[i] = i*i;
….
}
1.e. Heap
Heap is where operation like malloc and free operate on. It provides dynamic memory for the program. Heap is normally used for big memory buffer.
2. Know important keyword implications
When C programming was taught in school, there was little mentioning about implications of using different keywords. But rather, the coursework was probably focusing on the code construct, logic, etc… These are of course very essential for programming C, but little extra understanding would make any C course twice beneficial. Here are a few import keywords that an embedded software engineer needs to be aware of.
2.a. static keyword
static key word when combined with the variable tells the compiler to put the variable in the data segment, but limits the access scope to either a file or a function. In the code below, fileStatic variable sits in the data segment, and its scope is file meaning that any function within the same file can access to it. On the other hand, variable funcStatic is only available with functionF. funcStatic also sits in the global data segment.
static int fileStatic = 2;
void functionF()
{
int funcStatic = 1;
int array[20];
int i;
}
static is normally used by internal functions of a module. A typical use of static is shown in the snippet below
/* internal functions ... */
static void _internal1();
static void _internal2();
...
/* public functions */
int module1_function1();
int module2_function2();
2.b. volatile keyword
volatile keyword tells the compiler to use the variable from memory and not to optimize it with a register. It is typically used when the variable is shared between modules or it can be changed at anytime. Hence, any variable representing a memory location should be declared with volatile keyword.
volatile unsigned int * HardWareAddr = 0xFF00AABB;
/* read hardware register */
unsigned int a = *HardwareAddr;
/* write register */
*HardwareAddr = 0xABCDEF00;
3. Pointers and Function Pointers
Have a really good understanding about pointer and memory will take care of most of the embedded issues for you.
Pointers allow you to quickly access data at certain memory location direction, as seen in the section 2.b.
Function pointer (or also refered as function callback) is another important element of embedded world. Function pointers allow code to easily jump back and forth between functions. Most of the low level OS facilities involve function callback: Interrup Service Routine (ISR), OS context switch, user hook to OS facilities.
4. Closing
I hope that this little short article gives you some useful introduction to applying C to writing embedded applications.
參考: 原文出處