Unit 2 : EMBEDDED C PROGRAMMING
1. Memory And I/O Devices Interfacing: - 13 marks
Memory Interfacing: Connecting external memory devices to the
microprocessor.
I/O Interfacing: Connecting input/output devices to the microprocessor for data
transfer and control.
Memory and I/O Interfacing Several memory chips and I/O devices are
connected to a microprocessor.The following figure shows a schematic diagram
to interface memory chips and I/O devices to a microprocessor.
EXTERNAL ROM (PROGRAM MEMORY) INTERFACING :
Port 0 is used as multiplexed data & address lines. It gives lower order (A7-A0) 8bit
address in initial T cycle & higher order (A8-A15) used as data bus.
8 bit address is latched using external latch & ALE signal from 8051.
Figure shows how to access or interface ROM to 8051.
Port 2 provides higher order (A15-A8) 8 bit address.
PSEN is used to activate the output enable signal of external ROM/EPROM .
EXTERNAL RAM (DATA MEMORY) INTERFACING :
Figure shows how to connect or interface external RAM (data memory) to 8051.
Port 0 is used as multiplexed data & address lines.
Address lines are decoded using external latch & ALE signal from 8051 to
providelower order (A7-A0) address lines.
Port 2 gives higher order address lines.
RD & WR signals from 8051 selects the memory read & memory write operations
respectively.
2. Programming Embedded Systems in C. – 13 marks
Embedded C is an extension of C language and it is used to develop micro- controller- based
applications. The extensions in the Embedded C language from normal C Programming
Language are the I/O Hardware Addressing, fixed-point arithmetic operations, accessing
address spaces, etc.
Embedded C Program has five layers of Basic Structures. They are:
Comment:
Pre-processor directives:
Global Declaration:
Local Declaration:
Main function
Keywords in Embedded C
A Keyword is a special word with a special meaning to the compiler (a C Compiler for
example, is a software that is used to convert program written in C to Machine Code).
For example, if we take the Keil’s Cx51 Compiler , where they are some of the keywords:
• bit • sbit • sfr • small • large
Difference between Embedded C and C
Parameters C Embedded C
C is a general-purpose Embedded C is simply an
programming language, extension of C language and
which can be used to design it is used to develop micro-
General
any type of desktop-based controller-based applications.
application. It is nothing but an extension of
It is a type of high-level C.
language.
Embedded C is a fully
C language is a
Dependency hardware-dependent language.
hardware- independent
language. Embedded C is OS-
independent.
C compilers are OS-
dependent.
Embedded C Programming with Keil Language :
Embedded C is most popular programming language in software field for developing electronic
gadgets. Each processor used in electronic system is associated with embedded software.
Embedded C programming plays a key role in performing specific function by the processor. In
day-to-day life we used many electronic devices such as mobile phone, washing machine, digital
camera, etc.
These all device working is based on microcontroller that are programmed by embedded C.
In embedded system programming C code is preferred over other language.
Due to the following reasons:
o Easy to understand
o High Reliability
o Portability
o Scalability
What is an Embedded System?
An Embedded System is a combination of Hardware and Software. My desktop computer also has
hardware and software.
An Embedded S ystem is more of an application oriented system i.e. it is dedicated to perform a
single task .
An example for embedded system, which we use daily, is a Wireless Router.
Data Types in Embedded C:
Data Types in C Programming Language (or any programming language for that matter) help us
declaring variables in the program.
The following are the extra data types in Embedded C associated with the Keil’s Cx51 Compiler. •
bit • sbit • sfr • sfr16
Example of Embedded C Program
The following image shows the circuit diagram for the example circuit. It contains an 8051 based
Microcontroller (AT89S52) along with its basic components (like RESET Circuit, Oscillator
Circuit, etc.) and components for blinking LEDs (LEDs and Resistors).
In order to write the Embedded C Program for the above circuit, we will use the Keil C Compiler.
This compiler is a part of the Keil µVision IDE. The program is shown below.
3.RTOS - 13 Marks :
Real-time operating systems (RTOS) are used in environments where a large number of
events, mostly external to the computer system, must be accepted and processed in a short
time or within certain deadlines. such applications are industrial control, telephone switching
equipment, flight control, and real-time simulations.
Need For RTOS :
RTOS is designed to respond to events quickly and perform under heavy loads, it can be slower at big
tasks when compared to another OS.
The main goal of an RTOS is to perform critical tasks on time. It ensures that certain processes are
finished within strict deadlines, making it perfect for situations where timing is very important. It is also
good at handling multiple tasks at once..
Types of Real-Time Operating System
The real-time operating systems can be of 3 types –
Hard Real-Time Operating System:
These operating systems guarantee that critical tasks are completed within a range of time. For example,
a robot is hired to weld a car body.
Soft Real-Time Operating System:
This operating system provides some relaxation in the time limit. For example – Multimedia systems,
digital audio systems, etc. Explicit, programmer-defined, and controlled processes are encountered in
real-time systems.
Firm Real-time Operating System
RTOS of this type have to follow deadlines as well. In spite of its small impact, missing a deadline can
have unintended consequences, including a reduction in the quality of the product. Example: Multimedia
applications.
Uses of RTOS
Defense systems like RADAR .
Air traffic control system.
Networked multimedia systems.
Medical devices like pacemakers.
Stock trading applications.
Different Between RegularOS and Real-Timeoperating systems
Regular OS Real-Time OS (RTOS)
Complex Simple
Best effort Guaranteed response
Fairness Strict Timing constraints
Regular OS Real-Time OS (RTOS)
Average Bandwidth Minimum and maximum limits
Terms used in RTOS:
Task
Job
Release time of a job
Execution time of a job.
Deadline of a job.
Processors.
Features of RTOS:
Occupy very less memory
Consume fewer resources
Response times are highly predictable
Unpredictable environment
Components of Real Time Operating System:
The Scheduler:
Symmetric Multiprocessing (SMP)
Function Library:
Memory Management.
Fast dispatch latency
User-defined data objects and classes.
Advantages :
Maximum Consumption
Task Shifting
Focus On Application.
Real-Time Operating System In Embedded System:
Error Free
Memory Allocation:
Disadvantages:
Limited Tasks:
Use Heavy System Resources:
Complex Algorithms :
Device Driver And Interrupt Signals:
Thread Priority:
Minimum Switching:
4.Multiple Tasks And Multiple Processes – 2marks
Define multitasking.
Multitasking is the process of scheduling and switching the CPU
between several tasks. A single CPU switches its attention between
several sequential tasks. It maximizes the utilization of the CPU and
also provides modular construction of application.
******************************************************************
5.Context Switching – 13marks
Context switching in an operating system involves saving the context or state
of a running process so that it can be restored later, and then loading the
context or state of another. process and run it.
Context Switching refers to the process/method used by the system to change
the process from one state to another using the CPUs present in the system to
perfo rm its job.
Example of Context Switching:
Suppose in the OS there (N) numbers of processes are stored in a Process
Control Block(PCB). like The process is running using the CPU to do its job.
While a process is running, other processes with the highest priority queue up
to use the CPU to complete their job.
Need of Context Switching:
One process does not directly switch to another within the system.
Context switching enables all processes to share a single CPU to finish
their execution and store the status of the system’s tasks.
Context switching only allows a single CPU to handle multiple
processes requests parallelly without the need for any additional
processors.
Context Switching Triggers:
Interrupts
Multitasking
User/Kernel switch
What is Process Control Block(PCB)?
The Process Control block(PCB) is also known as a Task Control Block. it
represents a process in the Operating System. A process control block (PCB) is
a data structure used by a computer to store all information about a process.
It is also called the descriptive process. When a process is created (started or
installed), the operating system creates a process manager.
State Diagram of Context Switching:
Working Process Context Switching
So the context switching of two processes, the priority-based process occurs
in the ready queue of the process control block. These are the following steps.
The state of the current process must be saved for rescheduling.
The process state contains records, credentials, and operating
system-specific information stored on the PCB or switch.
The PCB can be stored in a single layer in kernel memory or in a
custom OS file.
A handle has been added to the PCB to have the system ready to run.
The operating system aborts the execution of the current process and
selects a process from the waiting list by tuning its PCB.
Load the PCB’s program counter and continue execution in the
selected process.
Graph:
Advantages:
It allows multiple programs to run on a single CPU, creating the
illusion of multitasking. It helps in managing resources
efficiently, ensuring that the CPU isn't idle while waiting for slow
operations (like reading from disk).
The ability of context switching to switch rapidly between tasks
is a core capability of a multitasking operating system (OS) such
as Windows, Linux or macOS.
Context switching enables multiple processes to share a single
CPU, while making it appear as though the CPU is executing
multiple processes simultaneously .
Disadvantages :
Overhead: Context switching incurs overhead due to the need
to save and restore the state of processes. ...
Time-consuming: The process of saving and restoring the state
of a process takes time, which can impact the responsiveness of
the system.
****************************************************************************
Priority Based Scheduling Policies – 13 marks
Earliest-Deadline-First Scheduling:
Earliest Deadline First (EDF) is one of the best known algorithms for realtime
processing. It is an optimal dynamic algorithm. In dynamic priority algorithms,
the priority of a task can change during its execution. It produces a valid
schedule whenever one exists.
EDF is a preemptive scheduling algorithm that dispatches the process with
the earliest deadline. If an arriving process has an earlier deadline than the
running process, the system preempts the running process and dispatches
the arriving process.
A task with a shorter deadline has a higher priority. It executes a job with the
earliest deadline. Tasks cannot be scheduled by rate monotonic algorithm.
EDF is optimal among all scheduling algorithms not keeping the processor idle
at certain times. Upper bound of process utilization is 100 %.
The task set can be scheduled by a number of policies including fixed priority
or dynamic priority algorithm.
The success of a real-time system depends on whether all the jobs of all the
tasks can be guaranteed to complete their executions before their deadlines.
Task Arrival Duration Deadline Implementation of earliest
deadline first : Is it really not
T1 0 10 33 feasible to implement EDF
T2 4 3 28 scheduling .
T3 5 10 29
dhfn
EDF properties :
1. EDF is optimal with respect to feasibility (i.e. schedulability).
2. EDF is optimal with respect to minimizing the maximum lateness.
Advantages:
1. It is optimal algorithm.
2. Periodic, aperiodic and sporadic tasks are scheduled using EDF algorithm.
3. Gives best CPU utilization.
Disadvantages:
1. Needs priority queue for storing deadlines
2. Needs dynamic priorities
3. Typically no OS support and Behaves badly under overload.
****************************************************************************
Rate Monotonic Scheduling – 13 marks
Rate Monotonic Priority Assignment (RM) is a so called static priority round
robin scheduling algorithm.
In this algorithm, priority is increases with the rate at which a process mustbe
scheduled. The process of lowest period will get the highest priority.
The priorities are assigned to tasks before execution and do not change over
time. RM scheduling is preemptive, i.e., a task can be preempted by a task
with higher priority.
In RM algorithms, the assigned priority is never modified during runtime of the
system.
RM assigns priorities simply in accordance with its periods, i.e. thepriority is
as higher as shorter is the period which means as higher is the activationrate.
So RM is a scheduling algorithm for periodic task sets.
Rate Monotonic Scheduling (RMS)
Tasks that need to run more frequently are given higher priority.
It is useful for tasks that repeat at set intervals (like every second or
millisecond).
Great for predictable systems but needs careful setup to avoid missed
deadlines.
Each periodic task is assigned a priority inversely based on its period
:
The shorter the period, the higher the priority.
The longer the period, the lower the priority.
The algorithm was proven under the following assumptions :
Tasks are periodic.
Each task must be completed before the next request occurs.
All tasks are independent.
Run time of each task request is constant.
Any non-periodic task in the system has no required deadlines
RMS is optimal among all fixed priority scheduling algorithms for
scheduling periodic tasks where the deadlines of the tasks equal their
periods.
Advantages :
1. Simple to understand. 2. Easy to implement. 3. Stable algorithm.
Disadvantages :
Lower CPU utilization.
Only deal with independent tasks.
Non-precise schedulability analysis
Comparison between RMS and EDF
Parameters RMS EDF
Priorities Static Dynamic
Works with OS with fixed priorities Yes No
Uses full computational power of processor No Yes
Possible to exploit full computational power No Yes
Priority Inversion:
Priority inversion occurs when a low-priority job executes while some ready
higher-priority job waits .
*******************************************************************************