0% found this document useful (0 votes)
45 views

Embedded System Diploma Sameh Afifi

The document discusses key concepts in embedded C programming including the differences between embedded C and C, important macros, the programming process involving preprocessing, compiling, assembling and linking, linker scripts, makefiles, cross development, simulators vs emulators, polling vs interrupts, and layered architecture.

Uploaded by

MohamedYounes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Embedded System Diploma Sameh Afifi

The document discusses key concepts in embedded C programming including the differences between embedded C and C, important macros, the programming process involving preprocessing, compiling, assembling and linking, linker scripts, makefiles, cross development, simulators vs emulators, polling vs interrupts, and layered architecture.

Uploaded by

MohamedYounes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Embedded System diploma

Sameh Afifi
Difference between embedded C and C
Important Macros
Super loop
Programming process
Preprocessor
Compiler
Assembler
Linker
Linker script
Makefile
Cross development
Simulator vs Emulator
Polling vs Interrupts
Layered architecture
© 2019 Embedded system Sameh Afifi 2
Difference between embedded C and C
• Embedded C language is most frequently used to program the microcontroller.
• Earlier, many embedded applications were developed using assembly level programming.
However, they did not provide portability. This disadvantage was overcome by the advent of
various high level languages like C.
• Embedded C is an extension to the C language with some additional header files. These
header files may change from controller to controller.
• 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.
• Compilers for C (ANSI C) typically generate OS dependent executables. Embedded C requires
compilers to create files to be downloaded to the microcontrollers/microprocessors where it
needs to run.
© 2019 Embedded system Sameh Afifi 3
Important Macros
#define SETBIT(REG,BIT) (REG |= 1 << BIT)

#define CLRBIT(REG,BIT) (REG &= ~(1 << BIT))

#define TOGBIT(REG,BIT) (REG ^= 1 << BIT)

#define READBIT(REG,BIT) ((Reg >> BIT) & 1)

© 2019 Embedded system Sameh Afifi 4


Super loop

© 2019 Embedded system Sameh Afifi 5


The programming process
• Make file which generated by IDE handle all this process
simulator

.lib files

file1.C .obj1
file2.C File1.s .obj2 .HEX
File1.i .obj3 .elf
main.C Pre-
Compiler Assembler Linker
main.h processor
File2.i File2.s
file2.h
Main.i Main.s
file1.h
Emulator
Real
hardware

© 2019 Embedded system Sameh Afifi 6


Preprocessor
A program first passes through the preprocessor.
The preprocessor goes through a program and prepares it to
be read by the compiler.
The preprocessor includes the contents of other programmer
specified files, manipulates the program text, and passes on
instructions about the particular computer for which the
compiler will be translating.
Commands used in preprocessor are called preprocessor
directives and they begin with “#” symbol.
The extension of the output is .i (intermediate)

© 2019 Embedded system Sameh Afifi 7


Guarding Header Files

© 2019 Embedded system Sameh Afifi 8


Compiler
Compilers translate high level programming language instructions into assemble.
They perform the same task for high level languages that an assembler performs for
assembly language, translating program instructions from a language such as C to an
equivalent set of instructions in machine language.
Three different components are responsible for changing C instructions into their machine
language equivalents.
The file that have an extern variables or function get a symbol
The extension of the output after this stage is .s

© 2019 Embedded system Sameh Afifi 9


Assembler
Assembly language programs are not directly executable, they must be translated to
machine language.

This translation is done using a program called an assembler.

The extension of the output after this stage is .o

© 2019 Embedded system Sameh Afifi 10


The Linker
• All the object files resulting from the assembler in step one must be combined.
• The object files themselves are individually incomplete, most notably in that some of the
internal variable and function references have not yet been resolved.
• The job of the linker is to combine these object files and, in the process, to resolve all of the
unresolved symbols.
• The output of the linker is a new object file that contains all of the code and data from the
input object files and is in the same object file format.
• If one object file contains an unresolved reference to a variable named foo, and a variable
with that same name is declared in one of the other object files, the linker will match them.
• If the same symbol is declared in more than one object file, the linker is unable to proceed. It
will likely complain to the programmer (by displaying an error message) and exit.

© 2019 Embedded system Sameh Afifi 11


The Linker

• Reference for the compilation process part:


• https://www.oreilly.com/library/view/programming-embedded-systems/0596009836/ch04.html

© 2019 Embedded system Sameh Afifi 12


Linker script
• Linker scripts are text files.
• Contain the start address and size of:
• .bss section in SRAM
• .stack section in SRAM
• .data section in SRAM
• .rodata section in SRAM
• .data section in flash eeprom
• .rodata section in flash eeprom
• .vt (ivt) section in flash eeprom
• .text section in flash eeprom

• Check this link for more info:


http://www.scoberlin.de/content/media/http/informatik/gcc_docs/ld_3.html

© 2019 Embedded system Sameh Afifi 13


Make file
• If you have multiple source files in c, c++ and others language and want
to compile them from Terminal Command, it is hard to write every time.
• To solve such kind of problem, we use Makefile because during the
compilation of large project we need to write numbers of source files as
well as linker flags are required, that are not so easy to write again and
again.
• Makefile is a tool to simplify or to organize code for
compilation. Makefile is a set of commands (similar to terminal
commands) with variable names and targets to create object file and to
remove them. In a single make file we can create multiple targets to
compile and to remove object, binary files. You can compile your project
(program) any number of times by using Makefile.Contain

© 2019 Embedded system Sameh Afifi 14


Make file
• Makefile Contain the below:
• All paths of the folder that contain header files
• All paths of the .c files related to the project
• Paths of the compiler/ assembler/ linker.
• Compiler flags
• Command to build/ compile and run the project

• Check this link for more info:


https://www.includehelp.com/c-programming-questions/what-is-makefile.aspx

© 2019 Embedded system Sameh Afifi 15


Cross Development
• Cross compiler:
• Runs on one type of computer and produces
machine code for a different type of computer.
• Native compiler:
• Generate code for the same machine
• Cross development tools:
• After a program is compiled it must be tested using a
simulator or an emulator.
• Note that:
• Atmel AVR Toolchain is a collection of tools/libraries
used to create applications for AVR microcontrollers.
• This collection includes compiler, assembler, linker and
Standard C & math libraries.

© 2019 Embedded system Sameh Afifi 16


Simulator
A simulator is a software program which allows a
developer to run a program designed for one type
of machine (the target machine) on another (the
development machine). The simulator simulates the
running conditions of the target machine on the
development machine.

A simulation is a system that behaves similar


to something else

© 2019 Embedded system Sameh Afifi 17


Emulator
An emulation is a system that behaves exactly like something else

An emulator or in-circuit emulator is a hardware device which behaves like a target machine.

It is often called a real time tool because it can react to events as the target microcontroller

would.

© 2019 Embedded system Sameh Afifi 18


Startup code
• The startup code can be used to perform many different tasks and from the CPU’s point of
view it’s just a standard program.
• It is called startup code(boot code) because it’s executed before the main() function of the
user’s application.
• In its most basic form the boot code is responsible for preparing the C run-time environment
for the user’s application by performing the following operations:
1. Disable the global interrupt
2. Copying the initialized variables (.data) from ROM to RAM
3. Setting all bytes from the .bss segment (it contains global and static variables without initial
values) to zero or any value you want.
4. Setting up the processor’s stack pointer (allocating space and initialization)
5. Copying the .rodata to SRAM
6. Enable the global interrupt
7. Calling main()

© 2019 Embedded system Sameh Afifi 19


Boot loader code
• Bootloader is application which allow you to erase main application and load new version of
this application back to flash.
• In typical case, we switch between bootloader and main application trough MCU reset.
• Therefore very small part of bootloader could be executed even before _Startup() function
and just decide whether Bootloader or main application will be executed. After that,
appropriate _Startup() function and application or bootloader code will be executed.
• Check this links:
• https://www.beningo.com/understanding-the-microcontroller-boot-process/
• https://www.edgefx.in/know-about-boot-loader-technique-for-programming-microcontroller/
• https://open4tech.com/boot-process-microcontroller/

© 2019 Embedded system Sameh Afifi 20


SYNCHRONOUS vs ASYNCHRONOUS API
• SYNCHRONOUS
• You are in a queue to get a movie ticket. You cannot get one until everybody in front of you
gets one, and the same applies to the people queued behind you.
• Blocks caller till operation done by functions is completed. Caller is blocked till function
returns at the operation end.
• SYNCHRONOUS EXAMPLE. Any process consisting of multiple tasks where the tasks must be
executed in sequence.
• ASYNCHRONOUS
• You are in a restaurant with many other people. You order your food. Other people can also
order their food, they don't have to wait for your food to be cooked and served to you
before they can order. In the kitchen restaurant workers are continuously cooking, serving,
and taking orders. People will get their food served as soon as it is cooked.
• Functions starts operation requested by caller. function return does not indicate operation
finish. Caller will be notified later when the operation finishes.

© 2019 Embedded system Sameh Afifi 21


Polling vs. Interrupts
• Interrupt:
Interrupt is the signal sent to the micro to mark the event that requires immediate attention.
Interrupt is “requesting" the processor to stop performing the current program and to
“make time” to execute a special code. Whenever any device needs its service, the device
notifies the microcontroller by sending it an interrupt signal.
Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and
serves the device. The program which is associated with the interrupt is called the interrupt
service routine (ISR) or interrupt handler

© 2019 Embedded system Sameh Afifi 22


Polling vs. Interrupts

© 2019 Embedded system Sameh Afifi 23


Polling vs. Interrupts

© 2019 Embedded system Sameh Afifi 24


Interrupt Vector Table (IVT)
An interrupt vector table (IVT) is a data structure that associates a list of interrupt handlers
with a list of interrupt requests in a table of interrupt vectors.
An entry in the interrupt vector is the address of the interrupt handler.
Constant table in ROM.
Special addresses with respect to CPU.
Each interrupt has specific address in the interrupt vector table for it’s ISR.
This specific address should be programmed to have the address of ISR of the corresponding
interrupt.
• Note: to use the AVR interrupt functionality, avr/interrupt.h header file should be included

© 2019 Embedded system Sameh Afifi 25


Interrupt Vector Table (IVT)

© 2019 Embedded system Sameh Afifi 26


Interrupts
• Interrupt Sources
• Hardware Interrupts
• Signal to a PIN like reset interrupt or external
interrupt.
• Timer Overflow Interrupt.
• Serial Port Interrupt.
• ADC Interrupt.
• Software Interrupts
• interrupts are commands given by the
programmer, such as the SWI instruction for
the Motorola µCs

© 2019 Embedded system Sameh Afifi 27


Nested interrupts
After any interrupt occurs, the I-bit is cleared automatically when entering the ISR and set to
1 automatically when leaving the ISR.
If nested interrupts is enabled, the interrupt behavior will be as follows

© 2019 Embedded system Sameh Afifi 28


Interrupt Service Routine (ISR)
• Interrupt Service Routine (ISR) or Interrupt Handler
Piece of code that should be execute when an interrupt is triggered.
Usually each interrupt has its own ISR. Its address in ROM is usually save in interrupt vector
table.
should be deterministic and short as possible, as it usually set a flag to a specific task
indicating a certain event is happened, or save small data in buffer.

© 2019 Embedded system Sameh Afifi 29


Interrupts
Interrupt latency is the time between the generation of an interrupt by a device and the
servicing of the device which generated the interrupt.
 Interrupt latency may be affected by:
• Interrupt controllers.
• Interrupt masking.
• OS interrupt handling methods.
Interrupt Response: Interrupt latency + Time to save the CPU context.
Interrupt Recovery: Is defined as the time required for the processor to return to the
interrupted code.

© 2019 Embedded system Sameh Afifi 30


Interrupts

© 2019 Embedded system Sameh Afifi 31


Steps for handling an interrupt
1. An event is happened so a an interrupt request is generated and stored in interrupt
controller module in microcontroller and trigger the processor
2. The CPU completes the execution of the current instruction then Context switching started.
3. In context switching the following data are stored in stack:
• The address of the next instruction (the contents of the PC).
• Status register.
• All the CPU GPRs.
4. Microcontroller CPU jump to Interrupt Vector Table to get the ISR address of the triggered
interrupt and load the address of the ISR into PC.
5. The CPU start to execute the ISR.
6. The data that was stored on the stack in step 3 is reloaded
7. The CPU then continue executing the main program.

Prepared by: Sameh Afifi 32


Steps for handling an interrupt

Prepared by: Sameh Afifi 33


Interrupt interview Questions
What is ISR?
An ISR(Interrupt Service Routine) is an interrupt handler, a callback subroutine which is called when a
interrupt is encountered.
What is return type of ISR?
ISR does not return anything. An ISR returns nothing because there is no caller in the code to read the
returned values.
What is interrupt latency?
Interrupt latency is the time required for an ISR responds to an interrupt.
How to reduce interrupt latency?
Interrupt latency can be minimized by writing short ISR routine and by not delaying interrupts for
more time.
And reduce the critical section code

© 2019 Embedded system Sameh Afifi 34


Interrupt interview Questions
Can we use any function inside ISR?
We can use function inside ISR as long as that function is not invoked from other portion of the code
Can we use printf inside ISR?
Printf function in ISR is not supported because printf function is not reentrant, takes a lot of time and
can affect the speed of an ISR up to a great extent.
Can we put breakpoint inside ISR?
Putting a break point inside ISR is not a good idea because debugging will take some time and a
difference of half or more second will lead to different behavior of hardware.

Prepared by: Sameh Afifi 35


Reminder: Don’t hesitate to contact with me for anything 

[email protected]

https://www.facebook.com/groups/EmbeddedSystem.SA/

https://www.linkedin.com/in/sameh-afifi-8389173a/

We can't solve problems by using the same kind of thinking we used when we created them.
And Warnings is the silent Killer.

© 2019 Embedded system Sameh Afifi 36


Restricted © 2019 Embedded system matrial sameh afifi 37

You might also like