Вы находитесь на странице: 1из 51

1

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

 C is much more flexible than other high-level programming languages:


• C is a structured language.
• C is a relatively small language.
• C has very loose data typing.
• C easily supports low-level bit-wise data manipulation.
• C is sometimes referred to as a “high-level assembly language”.
► When compared to assembly language programming:
• Code written in C can be more reliable.
• Code written in C can be more scalable.
• Code written in C can be more portable between different platforms.
• Code written in C can be easier to maintain.
• Code written in C can be more productive
13

How to make Code more


Readable
1.Commenting 14
2.memory-mapped devices 15

Documenting the source code is helpful not only


for your future reference but for those who come
after you. For instance, if you're working on an
embedded system, you need to have a memory
map indicating where all the memory-mapped
devices can be found. Listing 8 shows an example
of a memory map.
Review: The “super loop” software 16

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

 Each .h file should be “stand alone”


▫ It should declare, #define, and typedef anything needed by
prototypes and include any .h files it needs to avoid compiler errors

 In our example prototypes for CircleArea() and Circumference are


placed in circleUtils.h ▫ circleUtils.h included in circleUtils.c ▫
circleUtils.h included in any other .c file that uses CircleArea()
Guarding Header Files 21
Guarding Example 22
Separate Compilation 23

 If code is separated into multiple .c files


▫ Must compile each .c file
▫ Combine resulting .o files to create executable
Program Organization 24
Scope/Lifetime 25

 Variable “scope” refers to part of the program


that may access the variable
▫ Local, global, etc…
• Variable “lifetime” refers to time in which a
variable occupies memory
• Both determined by how and where variable is
defined
Storage classes 26

 In C language, each variable has a storage class which decides


scope, visibility and lifetime of that variable. The following storage
classes are most oftenly used in C programming,
 Automatic variables
 External variables
 Static variables
 Register variables

Automatic variables 27

 A variable declared inside a function without any storage class


specification, is by default an automatic variable. They are created
when a function is called and are destroyed automatically when the
function exits. Automatic variables can also be called local variables
because they are local to a function. By default they are
assigned garbage value by the compiler.
External or Global variable 28

 A variable that is declared outside any function is a Global


variable. Global variables remain available throughout the entire
program. One important thing to remember about global variable is
that their values can be changed by any function in the program.

Here the global variable number is available to all three


functions.
extern keyword 29

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

 Register variable inform the compiler to store the variable in register


instead of memory. Register variable has faster access than normal
variable. Frequently used variables are kept in register. Only few
variables can be placed inside register.
 NOTE : We can never get the address of such variables.
 Syntax :
Volatile Type Qualifier 35
How to define u8 ,u32,…. 36
Modularity 37
38

 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

 The #pragma directive gives special instructions to the compiler. The


#pragma directive is especially useful in embedded C
programming and can tell the compiler to allocate a certain
variable in RAM or EEPROM. It can also tell the compiler to insert a
snippet of assembly language code.
 The GNU GCC compiler, which is a popular compiler for various
embedded architectures such as ARM and AVR, also uses attributes
as an alternative syntax to the #pragma directive.
#pragma GCC dependency
allows you to check the relative dates of the current file and another file. If the other file is more recent than the current file, a warning is issued.
This is useful if the current file is derived from the other file,
and should be regenerated. The other file is searched for using the normal include search path.
Optional trailing text can be used to give more information in the warning message.
C Startup 40

 It is not possible to directly execute C code, when the processor


comes out of reset. Since, unlike assembly language, C programs
need some basic pre-requisites to be satisfied. This section will
describe the pre-requisites and how to meet the pre-requisites.
 We will take the example of C program that calculates the sum of
an array as an example. And by the end of this section, we will be
able to perform the necessary setup, transfer control to the C code
and execute it.
Listing 12. Sum of Array in C 41
Before transferring control to C code, the 42
following have to be setup correctly.
 Stack
 Global variables
Initialized
Uninitialized
 Read-only data
Stack 43

 C uses the stack for storing local


(auto) variables, passing function
arguments, storing return address,
etc. So it is essential that the stack
be setup correctly, before
transferring control to C code.
 Stacks are highly flexible in the
ARM architecture, since the
implementation is completely left
to the software.
Stack 44

 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

 When C code is compiled, the compiler places initialized global


variables in the .data section. So just as with the assembly, the .data
has to be copied from Flash to RAM.

 The C language guarantees that all uninitialized global variables will


be initialized to zero. When C programs are compiled, a separate
section called .bss is used for uninitialized variables. Since the value
of these variables are all zeroes to start with, they do not have to be
stored in Flash. Before transferring control to C code, the memory
locations corresponding to these variables have to be initialized to
zero.
Read-only Data 46

 GCC places global variables marked as const in a separate section,


called .rodata. The .rodata is also used for storing string constants.

 Since contents of .rodata section will not be modified, they can be


placed in Flash. The linker script has to modified to accomodate this.
Startup 47
Code
Linker Script for C code 48
The startup code has the following 49

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

Вам также может понравиться