Embedded C-Programming: Prepared By: Mohamed Abdallah
Embedded C-Programming: Prepared By: Mohamed Abdallah
Lecture 1
Error types.
Task 1.
2
Agenda
3
What are Embedded Systems?
4
Introduction to Embedded Systems
sensor actuator
switch indicator
Introduction to Embedded Systems
Micro-Controller concepts
Introduction to Embedded Systems
Micro-Controller concepts
Z= x+ y;
If (y==3) file1.c Compiler
return 0 ;
file1.asm
ADD R16,R10
SUB R7,R3
file2.o
file3.o Assembler
0010011000
prog.hex Linker file1.o
Introduction to Embedded Systems
11
C for Embedded Systems vs. Embedded C
12
Code Compilation process
13
Code Compilation process
File.i
File.c
Preprocessor
Preprocessing
It is the first stage of compilation. It processes preprocessor directives like
include-files, conditional compilation instructions and macros.
14
Code Compilation process
Examples on preprocessor directives:
• Including files:
#include <stdio.h>
Tells the preprocessor to copy the content of file stdio.h and paste
it here.
File1.c File1.h
#include “File1.h”
void myFunc1();
int x = 10; void myFunc1();
File1.c
void myFunc1();
void myFunc1();
int x = 10; 15
Code Compilation process
Examples on preprocessor directives:
• Object-like Macro:
#define LED_PIN 10
Tells the preprocessor that whenever the symbol LED_PIN is found
inside the code, replace it with 10.
So we can type inside the code:
int x = LED_PIN; /* x will have the value 10 */
ledInit(LED_PIN); /*Initialize LED with value 10*/
#define MY_SECOND_NUMBER LED_PIN
Now MY_SECOND_NUMBER also has the value 10.
16
Code Compilation process
Examples on preprocessor directives:
Macro definition is really helpful in code maintainability and change, for
example when a specific configuration value is used in all over the code in
a lot of lines, so to change this value only one line will be changed which is
the definition line itself instead of changing the value in all lines of code.
• Conditional compilation:
#if(LED_PIN==10)
printf(“LED_PIN=10”);
#endif
The printf line will be compiled only if the macro LED_PIN is
defined with value 10.
17
Code Compilation process
Examples on preprocessor directives:
• Conditional compilation:
#define OPERATION_MODE 1
int main(void)
{
/* Mode of operation is decided before compilation */
#if(OPERATION_MODE==1)
startMode_1();
#elif(OPERATION_MODE==2)
startMode_2();
#else
/* Display Error message and stop process */
#error OPERATION_MODE must be 0 or 1
#endif
return 0;
18
}
Code Compilation process
Examples on preprocessor directives:
• Conditional compilation:
#define IN_DEBUG_MODE
void testFunction(void)
{
#ifdef IN_DEBUG_MODE
printf(“DEBUG: entered testFunction\n”);
#endif
#ifdef IN_DEBUG_MODE
printf(“DEBUG: exit testFunction\n”);
#endif
}
Debugging messages will be compiled only if IN_DEBUG_MODE is
19
defined.
Code Compilation process
Examples on preprocessor directives:
• #undef:
Used to undefine a macro.
#define IN_DEBUG_MODE
void testFunction(void)
{
#ifdef IN_DEBUG_MODE /* This will be compiled */
printf(“DEBUG: entered testFunction\n”);
#endif
Compilation
It is the second stage. It takes the output of the preprocessor with the
source code, and generates assembly source code.
22
Code Compilation process
File.i File.s
File.c
Preprocessor Compiler Assembler
File.o
Assembler stage
It is the third stage of compilation. It takes the assembly source code and
produces the corresponding object code.
23
Code Compilation process
File.i File.s
File.c
Preprocessor Compiler Assembler
File.o
Library files
File.o
Library files
File.map File.hex
25
Code Compilation process
Preprocessing
It is the first stage of compilation. It processes preprocessor directives like
include-files, conditional compilation instructions and macros.
Compilation
It is the second stage. It takes the output of the preprocessor with the
source code, and generates assembly source code.
Assembler stage
It is the third stage of compilation. It takes the assembly source code and
produces the corresponding object code.
Linking
It is the final stage of compilation. It takes one or more object files or
libraries and linker script as input and combines them to produce a single
executable file. In doing so, it resolves references to external symbols,
assigns final addresses to procedures/functions and variables, and revises
code and data to reflect new addresses (a process called relocation).
26
Error types
27
Error types
Preprocessor error
Any error that occurs during preprocessing stage, such as using undefined
macro, or finding #error directive, when this error occurs process stops
and doesn’t go to next stage.
File.c
int x = 10;
File.c
File.c
int x = 10;
File.c
29
Error types
Linker error
Any error that occurs during linking stage, such as using a function that the
linker can’t find in any of the object files, when this error occurs process
stops and no executable file is generated.
File.c
int main()
{
myFunc(); undefined reference to `myFunc'
return 0;
}
30
Error types
Logic error
It is an error in the program logic itself (program design or code flow) that
leads to wrong behavior during runtime, it is the most difficult error to find
as the compilation process succeeds but error occurs later at runtime.
File.c
int main()
{
int x = 10;
if(x > 10) /* compare is using higher than */
{
printf(“x is less than 10”); /*But logic is less than*/
funcLessThan();
}
else
{
printf(“x is higher than 10”);
funcHigherThan();
}
return 0;
} 31
Code Compilation using command line
32
Code Compilation using command line
Environment preparing
We will be using GNU Toolchain which includes:
• GNU C Compiler (GCC).
• GNU Make.
• GNU Binutils.
• GNU Debugger (GDB).
If you are using Linux OS then all of them are available without any need
to install them, but if you are using windows then install MinGW
(Minimalist GNU for Windows), download it from:
http://www.mingw.org/
Note: It is better to install it in a folder other than "Program Files“,
probably in another partition other than C:\, and make sure that
installation folder doesn’t contain any spaces.
Setup environment variable PATH to include "<MINGW_HOME>/bin"
where <MINGW_HOME> is the MinGW installed directory that you have
33
chosen in the previous step.
Code Compilation using command line
Getting started
Now open command window in your project directory to start using these
commands
Get gcc version
This command gets gcc installed version, if version is displayed then we are
ready to use gcc, but if command failed then make sure that program is
installed correctly and make sure that "<MINGW_HOME>/bin folder is
added to PATH environment variable in Windows.
D:\newProject>gcc --version
D:\newProject>gcc --help
34
Code Compilation using command line
Run executable
To run output application executable.
D:\newProject>app.exe
Preprocess only
To do preprocessing stage only.
35
Code Compilation using command line
36
Code Compilation using command line
37
Code Compilation using command line
Note that now command prompt changes to the following line waiting for
debugger commands:
(gdb)
38
Code Compilation using command line
(gdb) b file2.c:8
Start execution
To start running application till reaching first break point set.
(gdb) r
Continue execution
To continue running application from current break point till reaching next
break point.
(gdb) c
39
Code Compilation using command line
(gdb) s
(gdb) s 3
(gdb) list
40
Code Compilation using command line
(gdb) display y
Exit debugger
To exit debugger and return to command line.
(gdb) quit
Note that now command prompt changes back to the original text:
D:\newProject>
41
C language syntax quick revision
42
Task 1
43
Mohamed AbdAllah
Embedded Systems Engineer
[email protected]
44