embedded software fundamentals - how does code get
TRANSCRIPT
Embedded SoftwareFundamentals
How does code get converted into ones and zeroes?
Kizito NKURIKIYEYEZU,Ph.D.
Reading materialRead the following resources available on the course platforms
1 Chapter 1 of White, E. (2011). Making Embedded Systems:Design Patterns for Great Software. " O’Reilly Media, Inc.".
2 Chapter 3 of Bertolotti, I. C., & Hu, T. (2017). Embedded SoftwareDevelopment. Amsterdam University Press.
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 1 / 22
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 2 / 22
Components of embeddedsoftware development
Host MachineDevelopmentEnvironmentsCompiler ToolchainDebuggersDevelopment KitsVersion Control
FIG 1. Components of an embeddeddevelopment
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 3 / 22
Modules of a typical embeddedsoftware
The software is organized inlayersEach layer assumes specificfunctionalityModules are described inC-files (.c)Definitions are described inheader files (.h)Functions interact with othermodulesEventually interact withHardware
FIG 2. Layers of an embedded systemsoftware
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 4 / 22
Embedded system software inlayers
Device DriversInterface to hardwarelayersHardware AbstractionLayer (HAL)
Code BootingReal-time operation system(RTOS)
Abstracts High from LowlevelsSchedulingResource management
Libraries for shared codeKizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 5 / 22
Hardware Abstraction
Low level and bare-MetalFirmwareHardware Abstraction LayerPlatform IndependenceHigh quality and portablesoftware
MaintainableTestablePortableRobustEfficientConsistent
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 6 / 22
Embedded programminglanguages
FIG 3. Top embedded programming languagesASPENCORE. (2017). 2017 Embedded Markets Study Integrating IoT andAdvanced Technology Designs, Application Development & ProcessingEnvironments. April, 1–102.
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 7 / 22
Why C?
Availability of compilers for almostany MCUSmall executableDeterministic resource use (e.g.,no dynamic memory allocation)Efficient Memory ManagementTiming-centric operationsDirection Hardware/IO ControlOptimized executionNote: Modern C++ is as efficientas C and I believe it will slowlyreplace C in the future. For detailssee Kormanyos, C. (2018).Real-time C++: efficientobject-oriented and templatemicrocontroller programming
FIG 4. C can be used even onvery small micro-controllersThe ATtiny20-UUR is an AVRmicro-controller that is smallerthan a grain of rice. It is an8-Bit IC that runs at 12MHz2KB (1K x 16) FLASH and12-WLCSP (1.56x1.4)
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 8 / 22
Embedded softwaredevelopment process
FIG 5. Embedded System Development PlatformThe host machine contains the build environment for an embedded system. Itcontains a cross compiler and a cross debugger. The debug allowscommunication between the target processor through a special processorinterface, the JTAG
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 9 / 22
FIG 6. Computer and target processor
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 10 / 22
FIG 7. Software toolsThe software tools include compiler toolchain (e.g., AVR GCC, gdb make files),linker, emulators, simulators, SDK, text editors/IDE, version control, etc
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 11 / 22
FIG 8. Detailed embedded C compilation processThe C preprocessor transform the program before actual compilation. Thecompiler translate the source code into opcode (object files) for the targetprocessor. The linker combine these object files and resolve all of theunresolved symbols. The locator assign physical memory addresses to each ofthe code and data and produce an output file containing a binary memoryimage that can be loaded into the target ROM.
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 12 / 22
FIG 9. The role of a preprocessorThe C preprocessor is the macro preprocessor for the C compiler. Thepreprocessor provides the ability for the inclusion of header files, macroexpansions, conditional compilation, and line control.
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 13 / 22
FIG 10. The role of a linkerThe linker combines all of objects files into a single executable object codeuses symbols to reference other functions/variables
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 14 / 22
FIG 11. Linear detailed embedded C compilation processThe compiler translate the source code into opcode (object files) for the targetprocessor. The linker combine these object files and resolve all of theunresolved symbols. The locator assign physical memory addresses to each ofthe code and data and produce an output file containing a binary memoryimage that can be loaded into the target ROM.
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 15 / 22 Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 16 / 22
Introduction to BuildSystems using AVR GNU
Toolsets
Translation of C code into machinecode
:0C000000B89A91E088B3892788BBFCCF38:00000001FF
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 17 / 22
Translation of C code into machinecodeGCC compiles a C/C++ program into executable in 4 steps:
1 Pre-processing—via the AVR GNU C Preprocessor(avr-cpp), which includes the headers (#include) andexpands the macros (#define).
avr-cpp -mmcu=attiny13 blink.c > blink.iThe resultant intermediate file blink.i contains the expandedsource code.
2 Compilation—the compiler compiles the pre-processedsource code into assembly code for a specific processor.
avr-gcc -S blink.i >blink.sThe -S option specifies to produce assembly code, instead ofobject code. The resultant assembly file is "blink.s".
3 Assembly —the assembler (avr-as) converts the assemblycode into machine code in the object file "hello.o".
avr-as -o blink.o blink.sKizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 18 / 22
Translation of C code into machinecode
4 Linker: Finally, the linker links the object code with the librarycode to produce an executable and linkable format (.elf) file"blink.elf".
avr-gcc blink.o -o blink.elfThis generates an .elf file isn’t directly executable by theMCU. Thus, one needs to extract the machine code from it inthe Intel Hex format
avr-objcopy -O ihex -R .eeprom blink.elf blink.ihexNotes:
You can see the detailed compilation process by enabling -v(verbose) option. For example,
avr-gcc -v -mmcu=attiny13 -o blink.bin blink.cYou can Generate all intermidiate files
avr-gcc -mmcu=attiny13 -save-temps blink.cYou should always enable optimization with the -Osparameter
avr-gcc -v -Os -mmcu=attiny13 -save-temps blink.c
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 19 / 22
Building automation
The need for building automation
Building can be tediousMany GCC flagsMany independentcommandsMany build targetsMany supportedarchitecturesMany source files
Building manually cancause consistency issueswaste development time
Real world software iscomplex. For example, theLinux kernel contains:
More than 23,000 .c filesMore than 18,000 headerfileMore than 1,400assembly filesHow would you compilerthis manually?
In most cases, one can usean Integrated developmentenvironment (IDE) toautomate this process.
Why use an automatic build system?Build Management Software (or Build Automation) provides asimple and consistent method for producing a target executable
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 20 / 22
Build Management Software
Automated the process ofPreprocessingAssemblingCompilingLinkingRelocatingUpload the machine codeto the microcontroller
GNU Toolset performs alloperations using makeReal world make files arecomplex1, but are oftenpreferred to using IDE2
1https://www.gnu.org/software/make/manual/html_node/Complex-Makefile.html2https://www.embeddedrelated.com/showthread/comp.arch.embedded/252000-1.php
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 21 / 22
Example make file
Kizito NKURIKIYEYEZU, Ph.D. Embedded Software Fundamentals September 26, 2021 22 / 22