Embedded C: Eng - Keroles Shenouda
Embedded C: Eng - Keroles Shenouda
Embedded C: Eng - Keroles Shenouda
EMBEDDED C
ENG.KEROLES SHENOUDA
2
What will get on C Programming
Data
Structure
Embedded
C
Advanced
C
C
3
Embedded C Programming
List all preprocessor directives in c 4
programming language.
DIFFERENCE BETWEEN C AND 5
EMBEDDED C
Compilers for C (ANSI C) typically generate OS dependant
executables. Embedded C requires compilers to create files to be
downloaded to the microcontrollers/microprocessors where it needs
to run. Embedded compilers give access to all resources which is
not provided in compilers for desktop computer applications.
DIFFERENCE BETWEEN C AND 6
EMBEDDED C
C is used for desktop computers, while embedded C is for
microcontroller based applications. Accordingly, C has the luxury to
use resources of a desktop PC like memory, OS, etc. While
programming on desktop systems, we need not bother about
memory. However, embedded C has to use with the limited
resources (RAM, ROM, I/Os) on an embedded processor. Thus,
program code must fit into the available program memory. If code
exceeds the limit, the system is likely to crash.
DIFFERENCE BETWEEN C AND 7
EMBEDDED C
Embedded systems often have the real-time constraints, which is
usually not there with desktop computer applications.
Embedded systems often do not have a console, which is available
in case of desktop applications.
So, what basically is different while programming with embedded
C is the mindset; for embedded applications, we need to optimally
use the resources, make the program code efficient, and satisfy real
time constraints, if any. All this is done using the basic constructs,
syntaxes, and function libraries of ‘C’.
Embedded C Constrains 8
Memory
Power
Size
Cost
Timing
CPU
SW Should be 9
Portable
Maintainable
Optimized
Reusable
Readable
Embedded versus Desktop 10
Programming
Main characteristics of an Embedded programming environment:
• Limited ROM.
• Limited RAM.
• Limited stack space.
• Hardware oriented programming.
• Critical timing (Interrupt Service Routines, tasks, …).
• Many different pointer kinds (far / near / rom / uni / paged / …).
• Special keywords and tokens (@, interrupt, tiny, …)
Assembly versus C 11
Why Change to C 12
architecture
Problem
What is the minimum software environment you need to create an
embedded C program?
Solution
Review: An introduction to 17
schedulers
Many embedded systems must carry out tasks at particular instants
of time. More specifically, we have two kinds of activity to
perform:
• Periodic tasks, to be performed (say) once every 100 ms,
and - less commonly -
• One-shot tasks, to be performed once after a delay of (say)
50 ms.
Function Reuse 18
Header File 19
Header File 20
The extern keyword is used before a variable to inform the compiler that this
variable is declared somewhere else.
The extern declaration does not allocate storage for variables.
Problem when extern is not used 30
Example Using extern in same file 31
Static variables 32
A static variable tells the compiler to persist the variable until the
end of program. Instead of creating and destroying a variable
every time when it comes into and goes out of scope, static is
initialized only once and remains into existence till the end of
program. A static variable can either be internal or external
depending upon the place of declaraction. Scope of internal
static variable remains inside the function in which it is
defined. External static variables remain restricted to scope of file in
each they are declared.
They are assigned 0 (zero) as default value by the compiler.
Static variables 33
Register variable 34
UART.c
APP
UART_init ()
{
…… F1(); ECU
pDATA_recieve = F1 ;
} HAL
UART
ISR()
{ if (pDATA_recieve != NULL)
HW
pDATA_recieve (uDR);
} UART.h
Void (* pDATA_recieve )(char);
#pragma 39
So all that has to be done in the startup code is to point r13 at the
highest RAM address, so that the stack can grow downwards
(towards lower addresses). For the connex board this can be
acheived using the following ARM instruction.
Global Variables 45
parts
1.exception vectors
2.code to copy the .data from Flash to RAM
3.code to zero out the .bss
4.code to setup the stack pointer
5.branch to main
Startup Assembly 50
51