Brief Introduction to Embedded System
What is an Embedded System
What is a System?
A way of working, organizing or performing one or many tasks according to a fixed set of rules, program or plan
- It is also an arrangement in which all units assemble and work together according to a program or plan
- Examples: A watch (time display system), A washing machine (Automatic cloth washing system)
To Summarize, An Embedded System:
- Combine of Hardware + Software
- has embedded software in a computer hardware
- Uses Microprocessor / Microcontroller
- Not a computer (not designed to be programmed by the end user)
- computer is a general-purpose computer
- may be an independent system or a part of large system
- dedicated for either an application(s) or specific part of an application or product of a larger system
- System Functions in real time
- Must be efficient (in Energy, Code-size, Run-time, Weight and Cost)
- Program is preloaded or embedded in the ROM(s) or flash memory
- System controls the latencies of tasks, events, and then interrupts and responds. Latency means time interval between the instance of need to respond and start of the actual execution
Three main embedded components
- Embeds hardware to give computer like functionalities. The hardware includes embedded memory, peripheral and input-output devices.
- Embeds main application software generally into flash, ROM, or media card and the application software performs concurrently multiple tasks. The system most often does not have a secondary hard disk.
- Embeds a real time operating system (RTOS), which supervises the application software tasks running on the hardware and organizes the accesses to system resources according to priorities and timing constraints of tasks in the system
Embedded Systems vs General Purpose Computing
Embedded Systems | General Purpose Computing |
---|---|
Few applications that are known at design-time (Single functioned) | Broad class of applications (Multipurpose) |
Not programmable by end user | Programmable by end user |
Fixed run-time requirements | The faster the better |
Must be efficient (Size, Power, Cost, Memory, Real-time) | No Resource constrained |
User Interface involves Buttons and Sensors | User Interface involves Keyboard, Mouse, Touch Screen etc |
Embedded System Devices
Industry Control
- Arduino
- Raspberry Pi
- STM32
Mobile Platform
-
Android
-
IOS
Applications of Embedded Systems
- Coal Mine Monitoring
- Pollution Sensing
- Infrastructure Monitoring
- Wireless Mesh Routers (WMN) with embedded RTOS
History of Embedded Systems
Hardware Units in Embedded Systems
- Processor(s) and
- Basic circuit elements: power source, clock, reset, timers, memory, glue circuit for the elements linking and interfaces
- Keypad, LCD display matrix or touch screen
- IO communication elements: buses (serial and parallel), interfaces for network interface, ADC, DAC, pulse dialer, modem, Bluetooth, 802.11, …
- Interrupt handler
Processor
A processor has two essential units.
- Program Flow Control Unit (CU) —includes a fetch unit for fetching instructions from the memory
- Execution Unit (EU) — includes circuits for arithmetic and logical unit (ALU), and for instructions for a program control task, data transfer instructions, halt, interrupt, or jump to another set of instructions or call to another routine or sleep or reset
An embedded system processor chip or core can be one of the following:
- General Purpose Processor (GPP) - instruction set designed not specific to the applications
- Microprocessor - a single VLSI chip that has a CPU and some other units
- used when large embedded software is located in the external memory chips
- Microcontroller - a single-chip VLSI with limited computational power
- possesses enhanced input-output capabilities and a number of on-chip functional units
- Embedded Processor
- Fast context switching and thus lower latencies
- Microprocessor - a single VLSI chip that has a CPU and some other units
- Application Specific System Processor (ASSP) as additional processor
- Typically a set-top box (STB) processor or mpeg video-processor or network application processor
- Multiprocessor system using GPPs and Application Specific Instruction Processor (ASIP)
- Digital Signal Processor (DSP)
- For applications in image processing, multimedia, audio, video, HDTV, etc.
- used when signal-processing function need to be processed fast.
- Media Processor
- Digital Signal Processor (DSP)
- System-on-chip (SoC) - GPP or ASIP core(s) integrated into an ASIC or VLSI circuit
Instruction Set Architecture (ISA)
ISA is processor specific.
An instruction contains an opcode and optional operand(s).
- **Opcode specifies the operation to be performed **
- Operand specifies the data to be processed
E.g. Consider a 16-bit instruction: 0001 0101 0101 0110, there are some bits reserved for the opcode (instruction type: add, sub…) and the rest are for operands.
What are the trade offs when increasing the number of bits of opcode if the number of bits of the instruction remains unchanged?
More bits for opcode -> the number of different operations that the processor can perform increases
Less bits for operand -> the range of data decreases
What are the advantages and disadvantages of instructions with variable sizes, e.g. 8-bit and 16-bit?
More efficient use of memory
Much more complicated circuit
Interrupts Handler
A system may possess a number of devices and the system processor has to control and handle the requirements of each device by running an Interrupt Service Routine (ISR) for each interrupt event.
- Interrupt handles elements for the external port interrupts, IO interrupts, timer and Real-Time Clock interrupts, software interrupts and exceptions
- Interrupts may be simultaneously pending for service
Linking Embedded System Hardware
- Linking and interfacing buses and units
- Linking and interfacing circuit (also called glue circuit) for the buses by using the appropriate multiplexers, decoders, and de-multiplexers to link various system units
Buses
- Processor of a system might have to be connected to a number of other devices and systems
- A bus consists of a common set of lines to interconnect the multiple devices, hardware units and systems.
- It enables the communication between two units at any given instance
- The remaining units remain in an unconnected state during communication between these two
- A bus may be a serial bus or a parallel bus transferring one bit or multiple data bits respectively
IO Communication Units
I/O Ports and Communication Drivers: Network Ethernet or serial driver to communicate with host embedded system Expansion Facility
- Serial Buses: UART (512 kbaud/s), 1-wire CAN (33 kbps), Industrial I2C (100kbps), Serial Port (230 kbps), IEEE 1394 (400 Mbps), High Speed USB 2.0 (480 Mbps), USB 3.0 (5Gbps)
- Parallel Buses: SCSI parallel (40 Mbps), Fast SCSI (8M to 80 Mbps) , Ultra SCSI-3 (8M to 160 Mbps), PCI, PCI-X
Software Embedded into a System
- Embedded software saves as ROM image
- Conversion steps from high level language codes to assembly and then to the ROM image
- Program layers in the embedded software
- High level language used for software development is C, C++, visual C++ or Java
Memories
- ROM or EPROM
- Storing ‘Application’ program from where the processor fetches the instruction codes
- Storing codes for system booting, initializing, Initial input data and Strings
- Storing Codes for RTOS
- Storing Pointers (addresses) of various Interrupt Service Routines
- External or Internal RAM
- Stack: for automatic variables within functions (LIFO)
- Heap: for dynamic memory allocation
- EEPROM or Flash
- Storing non-volatile (long- term) results of processing
- Caches
- Storing copies of the instructions, data and branch-transfer instructions in advance from external memories and storing temporarily the results in write back caches during fast processing
ROM Image
Final stage software also called ROM image.
Just as an image is a unique sequence and arrangement of pixels
Embedded software is also a unique placement and arrangement at each ROM address of bytes for instructions and data.
- Bytes at each address defined for creating the ROM image.
- By changing this image, the same hardware platform work differently and can be used for entirely different applications or for new upgrades of the same system.
- Hardware elements between distinct systems can be identical but it is the software that makes a system unique and distinct from the other.
- ROM image may alternatively be compressed software (for example, the zip format) and data (for example, the pictures in jpg or gif format) along with the software required for decompression algorithm
Machine Language Coding
Programmer defines the addresses and the corresponding bytes or bits at each address.
- The machine code-based coding is used in configuring some specific physical device or subsystem (e.g., instruct a transceiver to transmit at specific Mbps or Gbps using a specific bus protocol and networking protocol)
- Coding in machine implementable codes is time-consuming because the programmer must first understand the processor instruction set and then memorize the instructions and their machine codes.
Assembly Language Coding
- Needed for invoking Processor Specific Instructions
- Could be time-consuming as it requires understanding of the processor and instruction set.
- A program or a small specific part coded in the assembly language using an Assembler (software used for developing codes in assembly).
- Full coding in assembly may be done only for a few simple, small-scale systems (e.g., toys, vending machines, data acquisition systems, etc.).
Four steps when using assembly language
- Assembler: translate the assembly software into the machine codes (assembling)
- Linker: linking these codes with the other required assembled codes (e.g., *.exe)
- Loader (part of the OS): relocating the codes according to the available RAM addresses
- Locator: locating the codes as a ROM image and permanently placing them at the available addresses in the ROM (e.g., Intel Hex file format) before finally burned at the ROM
Machine Language vs. Assembly Language
Machine Language is the actual bits used to control the processor in the computer
- Usually viewed as a sequence of hexadecimal numbers (typically bytes)
- The processor read these bits (instructions) in from program memory
- Machine language provides a way of entering instructions into a computer (through switches, punched tape, or a binary file).
Assembly language is more human readable view of machine language
- Instead of using numbers, the instructions and registers are given names (add, sub, R1, A, B…)
- Assembly language is very close to the machine language.
Compiler
- Generates an object file
- Using linker and locator, the file for ROM image is created for the targeted hardware
C++ and Java are other languages used for software coding
Converting a C program into ROM image:
Real-Time Operating System (RTOS)
RTOS is a SOFTWARE.
Characteristics of RTOS:
- A real-time system requires that results be produced within a specified deadline period
- An embedded system is a computing device that is part of a larger system (i.e. automobile, airliner)
- A safety-critical system is a real-time system with catastrophic results in case of failure
- A hard real-time system guarantees that real-time tasks be completed within their required deadlines
- A soft real-time system provides priority of real- time tasks over non real-time tasks
Features of Real Time Systems
Most real-time systems do not provide the features found in a standard desktop system
Why?
- Real-time systems are typically single-purpose
- Real-time systems often do not require interfacing with a user
- Features found in a desktop PC require more substantial hardware than what is typically available in a real-time system
System-on-a-Chip
Many real-time systems are designed using system-on-a-chip (SOC) strategy.
- SOC allows the CPU, memory, memory- management unit, and attached peripheral ports (I.e. USB) to be contained in a single integrated circuit
Kernel
It is the basic structural unit of any operating systems (OS)
- It can be defined as the secured unit of an OS
- The services provided by Kernel handle the following tasks:
- Process management
- Memory management
- Scheduling
- Inter process communication
- I/O, Device/driver management
- Interrupt control
Process Management
What it does?
- Create a process when there is a program which is requested to run
- Activate the process to request the required resources (CPU time and memory)
- Run the process
- Block the process when there are some requests from other processes with higher priority
- Resume the process if necessary
- Deactivate the process to release all resources used
- Delete the process after the program has been finished
Inter-Process Communication and Synchronization of Processes
A significant problem that multitasking systems must address is sharing data and hardware resources among multiple tasks.
- It is usually “unsafe” for two tasks to access the same specific data or hardware resource simultaneously
Example of multi-process synchronization is Consumer-Producer Problem.
Consumer-Producer Problem
2 Things happened in the same time:
- Producer - generate a piece of data, put it into the buffer and start again
- Consumer - consuming the data (i.e., removing it from the buffer) one piece at a time.
The problem is to make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.
To solve the problem, the 3 common approaches are:
- Temporarily masking
- Binary Semaphores
- Message Passing
Temporarily Masking
Achieved by Disabling interrupt.
- An interrupt, even if of higher priority than the present critical function, get disabled
Disadvantage of Temporarily Masking:
- increases the interrupt latency period and a deadline may be missed for an interrupt service
- For some safety devices, interrupt cannot be missed
Interrupt latency is The period of time from the arrival of an interrupt at the CPU to the start of the routine that services the interrupt.
Binary Semaphores
Semaphore is a Synchronization tool that does not require busy waiting.
Binary semaphore – integer value can range only between 0 and 1
- Simpler to implement
- Can implement a counting semaphore S as a binary semaphore
- Provides mutual exclusion
Problems might caused by Binary Semaphores
- Deadlock might occur
Message Passing
Most RTOSs offer a message passing mechanism for synchronization
Each message can contain an array or buffer of data
- Consumer will execute only (read the count) if the producer send a message to ask it to do so. Otherwise, consumer will wait
- Alternatively, the producer will continue execute only when it receive a reply from consumer indicating that consumer has finished the consumption
Scheduling in RTOS
In a typical RTOS, a task has three states:
- Running
- Ready
- Blocked
Most tasks are blocked, most of the time
Only one task per CPU is running
- In simpler systems, the ready list is usually short, two or three tasks at most
Preemptive, priority-based scheduling
More important process -> higher priority
- Support preemption: A process currently running on the CPU will be preempted (搶占) if a higher-priority process becomes available to run
Preemptive kernels allow preemption of a task running in kernel mode
- In Preemptive scheduling, Event latency should be minimized
Basically:
- Each process has a priority
- Periodic processes require the CPU at specified intervals (periods)
- p is the duration of the period
- d is the deadline by when the process must be serviced
- t is the processing time
In this example there is only 1 Process. It runs in every period and able to meet its deadline.
What will happen if a process cannot meet the deadline?
Scheduling of Tasks
There are 3 Type of Tasks in Real Time Systems.
Hard real time systems
- Means All Processes must always meet their deadlines
- System fails if deadline window is missed
Soft real time systems
- Means All Processes must try to meet their deadlines
- System does not fail if a few deadlines are missed
Soft real time systems can miss some deadlines, but eventually performance will degrade if too many are missed.
Firm real time systems
- Result has no use outside deadline window
- Tasks that fail are discarded
Firm real time systems can miss some deadlines, but eventually performance will degrade if too many are missed.
Rate Monotonic Scheduling
Priority is Assigned based on the inverse of it period
- priority =
- Shorter period = higher priority
- Longer period = lower priority
- Preemption is checked every when every Period starts / every Process ends
Example - Meeting Deadlines
2 Processes , P1 and P2
- P1: t = 20, d = 50, p = 50
- Priority = 1/50 = 0.02
- P2: t = 36, d = 100, p = 100
- Priority = 1/100 = 0.01
0.02 > 0.01, Therefore P1 has higher priority than P2
First P1 is trying to meet the deadline, then let the P2 process.
After Deadline of P1 is gone and A new Period start,
P1 has higher priority and therefore preempt the CPU.
After P1 finished, the CPU is passed back to P2 and P2 can meet it’s deadline.
Example - Not Meeting Deadlines
2 Processes , P1 and P2
- P1: t = 25, d = 50, p = 50
- Priority = 1/50 = 0.02
- P2: t = 36, d = 80, p = 80
- Priority = 1/80 = 0.0125
0.02 > 0.0125, Therefore P1 has higher priority than P2
Assume Hard real time systems
First P1 is trying to meet the deadline, then let the P2 process.
After Deadline of P1 is gone and A new Period start,
P1 has higher priority and therefore preempt the CPU.
After P1 finished, the CPU is passed back to P2.
However, this time P2 can not meet the deadline.
System fails happen.
Exercise
Assume Fail to meet deadline will not cause System failure
Earliest Deadline First Scheduling
Priorities are assigned according to deadlines:
- Priority of processes are changed during time
- The earlier the deadline, the higher the priority
- The later the deadline, the lower the priority
- Preemption is checked every when every Period starts / every Process ends
Example
2 Processes , P1 and P2
- P1: t = 25, d = 50, p = 50
- P2: t = 36, d = 80, p = 80
Nothing special.
P1 deadline earlier -> finish P1 first
P2 deadline earlier -> finish P2 first
Exercise
Proportional Share Scheduling
- T shares are allocated among all processes in the system
- An application receives N shares where N < T
- This ensures each application will receive of the total processor time
Scheduling with Latency
If latency is considered, changing the execution processes too frequently will waste too much running time
- In this case, it is better to complete one process first and hence execute the others
- On every process switching, There are latency
Example - Earliest Deadline First Scheduling (with latency)
2 Processes , P1 and P2
Latency = 10
- P1: t = 20, d = 50, p = 50
- P2: t = 30, d = 80, p = 80
Green blocks are latency.
On every process switching, There are latency of 10
P1 deadline earlier -> finish P1 first
P2 deadline earlier -> finish P2 first