LPC&RTOS - Converted3
LPC&RTOS - Converted3
Pilani Campus
LPC23XX General Purpose Input / Output ports LPC23XX General Purpose Input / Output ports
Bit-level set and clear registers allow a single instruction set or clear of any Each enabled interrupt contributes to a Wakeup signal that can be used to bring
number of bits in one port. the part out of Power-down mode.
Direction control of individual bits. Registers provide software a view of pending rising edge interrupts, pending
All I/O default to inputs after reset. falling edge interrupts, and overall pending GPIO interrupts.
Backward compatibility with other earlier devices is maintained with legacy GPIO0 and GPIO2 interrupts share the same VIC slot with the External
registers appearing at the original addresses on the APB bus. Interrupt 3 event.
Interrupt generating digital ports Applications
PORT0 and PORT2 provide an interrupt for each port pin. General purpose I/O.
Each interrupt can be programmed to generate an interrupt on a rising edge, a Driving LEDs, or other indicators.
falling edge, or both. Controlling off-chip devices.
Edge detection is asynchronous, so it may operate when clocks are not Sensing digital inputs, detecting edges.
present, such as during Power-down mode. Bringing the part out of Power Down mode.
With this feature, level-triggered interrupts are not needed.
P0.12
P0.19
P1.20
P1.21
LPC 2XXX
0 0 0 F F 0 0 0 0 0 3 0 0 0 0 0
Data Pins P0.12 to Port
Port P0.19 as a P1.20
Output Pin to LCD and
D0-D7 P1.21 as
RS and
EN and
as a O/P
pin
IODIR0= 0x000FF000
Source: LPC23XX User manual IODIR1= 0x00300000
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Description Signal
LCD LPC2xxx
LCD D0 P0.12
LCD D1 P0.13
LCD D2 P0.14
LCD D3 P0.15
LCD D4 P0.16
LCD D5 P0.17
LCD D6 P0.18
LCD D7 P0.19
LCD RS P1.20
LCD EN P1.21
IOSET0=cmddata;
• RS=0 • RS=1
Send Command
• Command • Data
EN=High (1) IOSET1=0x00200000;
• En=1 • En=1
• DELAY • DELAY DELAY
IOCLR1=0x00200000;
BITS Pilani, Pilani Campus BITS Pilani, Pilani Campus
Interfacing of LPC 2XXX with
16x2 LCD
Data Mode:
IOCLR0=0X000FF000;
RS=1; IOSET1=0X00100000;
outdata=outdata<<12;
Thank you
IOSET0=outdata;
Send Data
DELAY
EN=Low(0) IOCLR1=0x00200000;
BITS Pilani
Pilani Campus
Interrupts Interrupts
LPC23XX Vectored Interrupt Controller (VIC): Interrupt structure of LPC23xx
Features The VIC is a component from the ARM prime cell.
ARM PrimeCell Vectored Interrupt Controller VIC module is a highly optimized interrupt controller.
Mapped to AHB address space for fast access The VIC is used to handle all the on-chip interrupt sources from
Supports 32 vectored IRQ interrupts peripherals.
16 programmable interrupt priority levels Each of the on-chip interrupt sources is connected to the VIC on
Fixed hardware priority within each programmable priority level a fixed channel: your application software can connect each of
these channels to the CPU interrupt lines (FIQ, IRQ) in one of
Hardware priority level masking
three ways.
Any input can be assigned as an FIQ interrupt
Software interrupt generation
Interrupts Interrupts
The VIC allows each interrupt to be handled as an FIQ interrupt, The ARM processor core has two interrupt inputs called Interrupt
a vectored IRQ interrupt Request (IRQ) and Fast Interrupt reQuest (FIQ).
FIQ is the fastest followed by vectored IRQ with non-vectored VIC takes 32 interrupt request inputs and programmably assigns
IRQ being the slowest. them as FIQ or vectored IRQ types.
Fast Interrupt reQuest (FIQ) requests have the highest priority.
If more than one request is assigned to FIQ, the VIC ORs the
requests to produce the FIQ signal to the ARM processor.
Vectored IRQ’s, which include all interrupt requests that are not
classified as FIQs, have a programmable interrupt priority.
The VIC ORs the requests from all of the vectored IRQs to
produce the IRQ signal to the ARM processor.
Interrupts Interrupts
Block diagram of the Vectored Interrupt Controller VIC register map
VICIRQStatus - IRQ Status Register: This register reads out the
state of those interrupt requests that are enabled and classified as
IRQ.
VICFIQStatus - FIQ Status Requests: This register reads out the
state of those interrupt requests that are enabled and classified as
FIQ.
VICRawIntr - Raw Interrupt Status Register: This register reads
out the state of the 32 interrupt requests / software interrupts,
regardless of enabling or classification.
VICIntSelect - Interrupt Select Register: This register classifies
each of the 32 interrupt requests as contributing to FIQ or IRQ.
Interrupts Interrupts
FIQ Interrupt Once you have selected an FIQ source the interrupt can be
Any interrupt source may be assigned as the FIQ interrupt. enabled in the VIC Interrupt Enable Register.
The VIC Interrupt Select Register has a unique bit for each Configuring the VIC, the peripheral generating the interrupt must
interrupt. be configured and its own interrupt registers enabled.
Setting this bit connects the selected channel to the FIQ interrupt. Once an FIQ interrupt is generated, the processor will change to
In an ideal system we would only have one FIQ interrupt. FIQ Mode and vector to FIQ vector.
However setting multiple bits in the Interrupt Select Register will Place a jump to your ISR routine at this location in order to serve
enable multiple FIQ interrupt sources. the interrupt.
On entry the interrupt source can be determined by examining
the VIC FIQ Status Register and the appropriate code executed.
Several FIQ sources slows entry into the ISR code.
Interrupts Interrupts
Leaving an FIQ Interrupt Vectored IRQ
Before you exit the ISR code you must make sure that any The VIC provides a programmable hardware lookup table which
delivers the address of the C function to run for a given interrupt
interrupt status flags in the peripheral have been cleared. source.
If this is not done you will get continuous interrupts until the flag The VIC contains 32 slots for vectored addressing.
is cleared. Each slot contains a Vector Address Register and a Vector Priority
To clear the flag: write a logic 1 not a logic 0. Register.
The Vector Priority Register allows you to assign a priority to each
interrupt slot.
It supports 16 priority levels, 15 being the lowest priority and 0 the
highest.
After reset the priority of all the VIC slots is set to 15, and the
individual priority can be elevated by the user.
Interrupts Interrupts
Vector Address Register must be initialised with the address of Leaving An IRQ Interrupt
the appropriate C function to run when the interrupt associated Interrupt status flags are cleared in the peripheral which
with the slot occurs. generated the request.
So whenever an interrupt configured as a vectored interrupt is At the end of the interrupt you must do a dummy write to the
generated, the address of its ISR will be loaded into a fixed Vector Address Register.
memory location called the Vector Address Register. This signals the end of the interrupt to the VIC, and any pending
The ARM7 CPU is going through its normal entry into the IRQ IRQ interrupt will be asserted.
Mode and will jump to IRQ interrupt vector.
Steps:
1. Convert Timer0 interrupt to an IRQ interrupt
2. Set Vectored IRQ slot 4 to highest priority
3. Pass address of the IRQ into the VIC Slot
4. Enable Timer0 as IRQ interrupt Thank you
5. In IRQ interrupt handler:
1. Clear interrupt flag
2. VICAddress register must be written (with any value) at
the end of an ISR
BITS Pilani
Pilani Campus
3 BITS pilani, Deemed to be University under Section 3, UGC Act 4 BITS pilani, Deemed to be University under Section 3, UGC Act
I2C I2C
The two wires forming an I²C bus are bidirectional open-drain I2C bus signals are SDA (Serial Data) and SCL (Serial Clock) .
lines, named Serial Data Line (SDA) and Serial Clock Line (SCL) Both the signals are bidirectional in nature.
respectively. The bus is also a ‘WIRED AND’ bus, which implies that if any of
The I²C protocol specifies that these two lines need to be pulled up the device transmits a ‘0’ , ‘0’ will appear on the bus.
with resistors.
Being a protocol based on just two wires, there should be a way to
address an individual slave device on the same bus.
For this reason, I²C defines that each slave device provides a
unique slave address for the given bus.
The address may be 7- or 10-bit wide
The protocol is also byte oriented protocol as a transmitting
device expects to receive an acknowledgement from receiving
device after every byte of data transfer.
5 BITS pilani, Deemed to be University under Section 3, UGC Act 6 BITS pilani, Deemed to be University under Section 3, UGC Act
I2C I2C
The I²C Protocol
In the I²C protocol all transactions are always initiated and
completed by the master.
This is one of the few rules of this communication protocol to keep
in mind while programming I²C devices.
All messages exchanged over the I²C bus are broken up into two
types of frame: an address frame, where the master indicates to
which slave the message is being sent, and one or more data
frames, which are 8-bit data messages passed from master to slave
or vice versa.
Data is placed on the SDA line after SCL goes low, and it is
sampled after the SCL line goes high.
7 BITS pilani, Deemed to be University under Section 3, UGC Act 8 BITS pilani, Deemed to be University under Section 3, UGC Act
I2C I2C
The time between clock edges and data read/write is defined by
devices on the bus and it vary from chip to chip.
Both SDA and SCL are bidirectional lines, connected to a positive
supply voltage via a current-source or pull-up resistors.
When the bus is free, both lines are HIGH.
The output stages of devices connected to the bus must have an START and STOP Condition
open-drain or open-collector to perform the wired-AND function.
All transactions begin with a START and are terminated by a
The bus capacitance limits the number of interfaces connected to the STOP.
bus.
A HIGH to LOW transition on the SDA line while SCL is HIGH
For a single master application, the master’s SCL output can be a defines a START condition.
push-pull driver design if there are no devices on the bus that would
stretch the clock. A LOW to HIGH transition on the SDA line while SCL is HIGH
defines a STOP condition.
9 BITS pilani, Deemed to be University under Section 3, UGC Act 10 BITS pilani, Deemed to be University under Section 3, UGC Act
I2C I2C
START and STOP conditions are always generated by the master. Byte Format
The bus is considered to be busy after the START condition. Every word transmitted on the SDA line must be eight bits long, and
The bus is considered to be free again a certain time after the STOP this also includes the address frame.
condition. The number of bytes that can be transmitted per transfer is
The bus stays busy if a repeated START (also called RESTART unrestricted.
condition) is generated instead of a STOP condition. Each byte must be followed by an Acknowledge (ACK) bit.
In this case, the START and RESTART conditions are functionally Data is transferred with the Most Significant Bit (MSB) first.
identical. If a slave cannot receive or transmit another complete byte of data
until it has performed some other function, for example servicing an
internal interrupt, it can hold the clock line SCL LOW to force the
master into a wait state.
Data transfer continues when the slave is ready for another byte of
data and releases clock line SCL.
11 BITS pilani, Deemed to be University under Section 3, UGC Act 12 BITS pilani, Deemed to be University under Section 3, UGC Act
I2C I2C
Address Frame The ACK signal is defined as follows:
The address frame is always first in any new communication The transmitter releases the SDA line during the acknowledge
sequence. clock pulse so that the receiver can pull the SDA line LOW and it
For a 7-bit address, the address is clocked out most significant bit remains stable LOW during the HIGH period of this clock pulse.
(MSB) first, followed by a R/W bit indicating whether this is a read When SDA remains HIGH during this ninth clock pulse, this is
(1) or write (0) operation. defined as the Not Acknowledge (NACK) signal.
The addressed slave should respond with an ACK bit. The master can then generate either a STOP condition to abort the
Acknowledge (ACK) and Not Acknowledge (NACK) transfer, or a RESTART condition to start a new transfer.
The ACK takes place after every byte. The ACK bit allows the There are five conditions leading to the generation of a NACK:
receiver to signal the transmitter that the byte was successfully 1. No receiver is present on the bus with the transmitted address so
received and another byte may be sent. there is no device to respond with an acknowledge.
The master generates all clock pulses over the SCL line, including
the ACK ninth clock pulse.
13 BITS pilani, Deemed to be University under Section 3, UGC Act 14 BITS pilani, Deemed to be University under Section 3, UGC Act
I2C I2C
2. The receiver is unable to receive or transmit because it is Data Frames
performing some real-time function and is not ready to start After the address frame has been sent, data can begin being
communication with the master. transmitted.
3. During the transfer, the receiver gets data or commands that it The master will simply continue generating clock pulses on SCL at
does not understand. a regular interval, and the data will be placed on SDA by either the
4. During the transfer, the receiver cannot receive any more data master or the slave, depending on whether the R/W bit indicated a
bytes. read or write operation.
5. A master-receiver must signal the end of the transfer to the slave Usually, the first or the first two bytes contains the address of the
transmitter. slave register to write to/read from.
For example, for I²C EEPROMs the first two bytes following the
address frame represent the address of the memory location
involved in the transaction.
15 BITS pilani, Deemed to be University under Section 3, UGC Act 16 BITS pilani, Deemed to be University under Section 3, UGC Act
19 BITS pilani, Deemed to be University under Section 3, UGC Act 20 BITS pilani, Deemed to be University under Section 3, UGC Act
21 BITS pilani, Deemed to be University under Section 3, UGC Act BITS pilani, Deemed to be University under Section 3, UGC Act
Device Addressing Write Protocol
The 24C256 EEPROM requires an 8-bit device address word To write a single or multiple bytes of data into the EEPROM, we
following a start condition to enable the chip for a read or write need to follow the protocol
operation. Refer circuit schematic for A0A1A2
BITS pilani, Deemed to be University under Section 3, UGC Act BITS pilani, Deemed to be University under Section 3, UGC Act
SCL Duty Cycle Register Low Half Word. Determines the low time of the I2C
I2SCLL clock. I2nSCLL and I2nSCLHtogether determine the clock frequency generated
by anI2C master.
BITS pilani, Deemed to be University under Section 3, UGC Act BITS pilani, Deemed to be University under Section 3, UGC Act
BITS pilani, Deemed to be University under Section 3, UGC Act 28 BITS pilani, Deemed to be University under Section 3, UGC Act
SPI SPI
The structure of a typical SPI bus A typical SPI bus is formed by four signals, even if it is possible to
drive some SPI devices with just three I/Os (in this case we talk about
3-wire SPI):
• SCK: this signal I/O is used to generate the clock to synchronize
data transfer over the SPI bus.
It is generated by the master device, and this means that in an SPI
bus every transfer is always started by the master.
Different from the I²C specification, the SPI is intrinsically faster
and the SPI clock speed is usually several MHz.
Nowadays is quite common to find SPI devices able to exchange
data at a rate up to 100MHz.
SPI protocol allows to devices with different communication
speeds to coexist over the same bus.
29 BITS pilani, Deemed to be University under Section 3, UGC Act 30 BITS pilani, Deemed to be University under Section 3, UGC Act
SPI SPI
• MOSI: the name of this signal I/O stands for Master Output Slave Different from the I²C protocol, the SPI does not use slave
Input, and it is used to send data from the master device to a slave . addresses to select devices, but it demands this operation to a
Different from the I²C bus, where just one wire is used to exchange physical line that is asserted LOW to perform a selection.
data both the ways, the SPI protocol defines two distinct lines to In a typical SPI bus only one slave device can be active at same
exchange data between master and slaves. time by asserting low its SS line.
• MISO: it stands for Master Input Slave Output and it corresponds This is the reason why devices with different communication speed
to the I/O line used to send data from a slave device to the master. can coexist on the same bus.
• SSn: it stands for Slave Select and in a typical SPI bus there exist
‘n’ separated lines used to address the specific SPI devices
involved in a transaction.
31 BITS pilani, Deemed to be University under Section 3, UGC Act 32 BITS pilani, Deemed to be University under Section 3, UGC Act
SPI SPI
Clock Polarity and Phase SPI bus modes according CPOL and CPHA configuration
In addition to setting the bus clock frequency, the master and slaves
must also agree on the clock polarity and phase with respect to the
data exchanged over MOSI and MISO lines. Mode CPOL CPHA
SPI Specification by Motorola names these two settings as CPOL 0 0 0
(Clock polarity ) and CPHA (clock phase) respectively, and most
silicon vendors have adopted that convention. 1 0 1
The combinations of polarity and phase are often referred to as SPI 2 1 0
bus modes which are commonly numbered according Table. 3 1 1
The most common mode are mode 0 and mode 3, but the majority
of slave devices support at least a couple of bus modes
33 BITS pilani, Deemed to be University under Section 3, UGC Act 34 BITS pilani, Deemed to be University under Section 3, UGC Act
SPI SPI
The timing diagram is shown in Figure, and it is further described At CPOL=0 the base value of the clock is zero, i.e. the active state
below: is 1 and idle state is 0.
• For CPHA=0, data is captured on the SCK rising edge (LOW →
HIGH transition) and data is output on a falling edge (HIGH →
LOW clock transition).
• For CPHA=1, data is captured on the SCK falling edge and data is
output on a rising edge.
At CPOL=1 the base value of the clock is one (inversion of
CPOL=0), i.e. the active state is 0 and idle state is 1.
• For CPHA=0, data is captured on SCK falling edge and data is
output on a rising edge.
• For CPHA=1, data is captured on SCK rising edge and data is
output on a falling edge.
35 BITS pilani, Deemed to be University under Section 3, UGC Act 36 BITS pilani, Deemed to be University under Section 3, UGC Act
37 BITS pilani, Deemed to be University under Section 3, UGC Act BITS pilani, Deemed to be University under Section 3, UGC Act
SPI bus features
8. Developed by Motorola and supported by various uC products. Bus Configuration and SPI Protocol of Multiple slaves
Multiple slave devices can be connected in parallel or daisy chained
9. In SPI mode, the serial 8-bit synchronous data transmission and utilizing the same SPI bus.
reception is possible simultaneously.
Parallel Configuration
10. Multiple masters and multiple slaves are allowed on the bus. For the parallel connection, each device on the bus should have a
separate CS line, while SCK, SDI and SDO lines are connected in
11. Master initiates the data frame. Master generates the clock (SCK) parallel.
and selects the slave device and then data transfer in both
directions takes place.
BITS pilani, Deemed to be University under Section 3, UGC Act BITS pilani, Deemed to be University under Section 3, UGC Act
BITS pilani, Deemed to be University under Section 3, UGC Act BITS pilani, Deemed to be University under Section 3, UGC Act
The current limiting resistor between the Data pin and DI/DO
limits any current flows when both the microcontroller and device
are driving the line.
3 BITS pilani, Deemed to be University under Section 3, UGC Act BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
5 6
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
7 8
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Evolution of Programming From Source to Executable
Source Code Object Code
Assembly Language
– Mnemonic codes
E59F1010 LDR R1, num1 Preprocessor Linker
E59F0008 LDR R0, num2 Libraries
E0815000 ADD R5, R1, R0 Preprocessed Code Executable Code
E58F5008 STR R5, sum
9 10
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Project creation – Target device An IDE gives a single focal point for the application
select & configure the toolsettings
development for
1. Creating source file through a project manager.
Source file creation usingassembler, C, 2. Rich featured source code.
RTOSenvironment & application build
up using projectmanager 3. Organization of source file into a project.
Object code 4. Provision of database for many devices.
5. a)Compiles, points errors and interactively correct the errors.
Error Correction b)Assembles and links to application.
c)Links a device data sheets, user guides & development tool
Object files
manuals.
Testing after linking
Absolute File
O.K. 13 14
ToPROM
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Integrated DevelopmentEnvironment (IDE) Tool Chain for Building Embedded Software
Cross-Compilers
Native tools are good for the host, but to port/locate embedded code
to target, the host must have a tool-chain that includes a cross-
compiler, one which runs on the host but produces code for the
target processor
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What is an Operating System? What is an Operating System?
Primary software modules within an operating system that implement Full Featured Operating System provides additional libraries of
such control functions, device drivers, rich communication packages, human
Scheduler
Dispatcher
computer interface.
Intertask communication
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Complex tasks
Found in all types of applications
Examples: Microsoft Word; Apache web server;
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Multiple Processes Task Scheduling
Sequence Diagram A schedule is set up to specify when, under what conditions, and
At any instant in time, only one process is actively executing; it for how long each task will be given the use of the CPU (and other
resources)
said to be in run state
The criteria for deciding which task is to run next are collectively
The other processes are in ready or in waiting state called a scheduling strategy, which generally falls into three
categories:
Multiprogramming
each task continues until it performs an operation that requires waiting for an external
event
Real-Time
tasks with specified temporal deadlines are guaranteed to complete before those
deadlines expire
Time sharing
running task is required to give up the CPU so that another task may get a turn
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Address Space of a Process Address Space of a Process
Address Space of a Process
When a process is created by the OS, it is given a portion of the
physical memory in which to work
The set of addresses delimiting that code and the data memory,
proprietary to each process, is called its address space
Processes are segregated Supervisor
Supervisor mode Mode access
User mode – limited to a subset of instructions
User
A process may create or spawn child processes (each with its own mode
data address space, data, status, and stack) access
A process may create multiple threads (each with its own stack and
status information)
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Single-Process Multiple-Threads
Multiple Threads
During partitioning and functional decomposition of the function Single-Process Multiple-Threads
intended to be performed by an ES identify which actions
would benefit from parallel execution
For example, allocate a sub job for each type of I/O
Each of the sub jobs has its own thread of execution
Such a system is called a single-process multithread design
Threads are not independent of each other (unlike processes or
tasks)
Threads can access any address within the process, including other threads’ stacks All four categories of multitasking operating system:
An OS that supports tasks with multiple threads is called a – Single process single thread
– Multiprocess single thread
multithreaded operating system – Single process multiple threads
– Multiprocess multiple threads
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Processes (tasks) vs. Threads Complete software system with two processes
• At the minimum, a process or task needs the following:
1. The code or firmware, the instructions
–These are in the memory and have addresses
Shared among
2. The data that the code is manipulating member
–The data starts in the memory and may be moved to registers. Threads
the data has addresses
3. CPU and associated physical registers
4. A stack Proprietary to
each Thread
5. Status information
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Reentrant Code
Context switch between threads can be substantially simpler and Reentrant Code
faster than between processes Child processes (and their threads) share the same firmware
When switching between threads much less information must be memory area two different threads can execute the same
saved and restored function
An operating system that supports tasks with multiple threads Functions using only local variables are inherently reentrant
referred to as a multithreaded operating system Functions using global variables, variables local to the process,
Can easily extend design to support multiple processes can further variables passed by reference, or shared resources are not
decompose each process into multiple subtasks such a system reentrant
called multiprocess – multithread design Any shared functions must be designed to be reentrant
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
• Multitasking / Multithreading system supports multiple tasks When threads operating independently our systems have few if
• Important job in multitasking system any Conflicts, Chances for corruption, Contentions
Exchanging data between tasks Real systems must deal with all such problems resources and
Synchronizing tasks inter thread communication must take place in robust manner
Sharing resources Interaction may be direct or indirect Must be synchronized and
co-ordinated want to prevent race conditions outcome of task or
computation depends upon order in which tasks execute
Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool” Source: James. K. Peckol, “Embedded System Design – A Contemporary Design Tool”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Thank you
Embedded System Design
BITS Pilani Prof. Manoj S Kakade
Dept of EEE
Pilani Campus
55
1
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani Contents
Pilani Campus
Reference Book:
Trevor Martin,“The Designer’s Guide to the Cortex-M Processor
EEEZG512/ESZG512/MELZG526/SEZG516 Family”, Second Edition, Elsevier Ltd.
Embedded System Design
Contact Session 16
2
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
While we always return from our “C” function, once started an RTOS
thread must contain a loop so that it never terminates and thus runs
forever.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Threads Threads
When a thread is created, it is also allocated its own thread ID. Context switch:
This is a variable which acts as a handle for each thread and is
used when we want to manage the activity of the thread.
osThreadId id1,id2,id3;
Each thread has its own stack for saving its data during a context switch.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Threads Starting the RTOS
Threads may be in one of three states: void thread1 (void);
Running -The currently running thread void thread2 (void);
Ready -Threads ready to run osThreadId thrdID1, thrdID2;
Wait -Blocked threads waiting for an OS event void main (void)
{
osKernelInitialize ();
IODIR1 5 0x00FF0000; // Do any C code you want
Init_Thread(); //Create a Thread
osKernelStart(); //Start the RTOS
}
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Threads
Threads of equal priority will be scheduled in a round-robin A First CMSIS-RTOS Project – Demo in Keil
fashion.
High priority tasks will preempt low priority tasks and enter the
running state “on demand.”
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Once the thread structure has been defined the thread can be created using
the osThreadCreate() API call.
Then the thread is created from within the application code; this is often
done within the main thread but can be at any point in the code.
thread1_id 5 osThreadCreate(osThread(thread1), NULL);
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Thread Management and Priority
Once the threads are running, there are a small number of OS Multiple Instances
system calls which are used to manage the running threads. One of the interesting possibilities of an RTOS is that you can create
It is also then possible to elevate or lower a thread’s priority either multiple running instances of the same base thread code.
from another function or from within its own code. First we create the thread structure and set the number of thread instances
to two:
osStatus osThreadSetPriority(threadID, priority);
osThreadDef(thread1, osPriorityNormal, 2, 0);
osPriority osThreadGetPriority(threadID);
Then we can create two instances of the thread assigned to different
thread handles. A parameter is also passed to allow each instance to
Example: identify which UART it is responsible for.
We will look at assigning different priorities to threads ThreadID_1_0 = osThreadCreate(osThread(thread1), UART1);
ThreadID_1_1 = osThreadCreate(osThread(thread1), UART2);
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Time Management
RTOS also provides some timing services which can be accessed
Example: through RTOS system calls.
We will look at creating one thread and then create multiple run-time Time Delay
instances of the same thread. The most basic of these timing services is a simple timer delay function.
void osDelay ( uint32_t millisec )
This call will place the calling thread into the WAIT_DELAY state for
the specified number of milliseconds.
The scheduler will pass execution to the next thread in the READY
state
When the timer expires, the thread will leave the wait_delay state and
move to the READY state.
The thread will resume running when the scheduler moves it to the
RUNNING state.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
This defines a name for the timer and the name of the call back Example:
function. The timer must then be instantiated in an RTOS thread. We will configure a number of virtual timers to trigger a callback
osTimerId timer0_handle 5 osTimerCreate (timer(timer0), function at various frequencies.
osTimerPeriodic, (void *)0);
This creates the timer and defines it as a periodic timer or a single
Idle Demon
shot timer (osTimerOnce). The final parameter passes an
argument to the call back function when the timer expires. If during our RTOS program we have no thread running and no
thread ready to run (eg, they are all waiting on delay functions)
osTimerStart ( timer0_handle,0x100);
then the RTOS will use the spare run-time to call an “Idle
The timer can then be started at any point in a thread the timer Demon” that is again located in the RTX_Conf_CM.c file.
start function invokes the timer by its handle and defines a count
This idle code is in effect a low priority thread within the RTOS
period in milliseconds.
which only runs when nothing else is ready.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Inter-Thread Communication
The CMSIS-RTOS API supports inter-thread communication with
signals, semaphores, mutexes, mailboxes, and message queues.
Signals
CMSIS-RTOS Keil RTX supports up to 16 signal flags for each
thread.
These signals are stored in the thread control block.
It is possible to halt the execution of a thread until a particular
signal flag or group of signal flags are set by another thread in the
system A thread may be placed into a waiting state until a pattern of flags is set
by another thread. When this happens, it will return to the ready state
and wait to be scheduled by the kernel
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
The signal wait system calls will suspend execution of the thread Example:
and place it into the wait_event state. We will look at using signals to trigger activity between two threads.
Execution of the thread will not start until all the flags set in the While this is a simple program it introduces the concept of
signal wait API call have been set. synchronizing the activity of threads together.
It is also possible to define a periodic timeout after which the
waiting thread will move back to the ready state, so that it can
resume execution when selected by the scheduler.
A value of 0xFFFF defines an infinite timeout period.
osEvent osSignalWait ( int32_t signals,uint32_t millisec);
If the signals variable is set to zero when osSignalWait is called
then setting any flag will cause the thread to resume execution.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Semaphores
Semaphores are a method of synchronizing activity between two or Semaphores help to control access to program resources.
more threads.
A semaphore is a container that holds a number of tokens.
As a thread executes, it will reach an RTOS call to acquire a
semaphore token.
If the semaphore contains one or more tokens, the thread will
continue executing and the number of tokens in the semaphore will
be decremented by one.
If there are currently no tokens in the semaphore, the thread will be
placed in a waiting state until a token becomes available.
At any point in its execution, a thread may add a token to the
semaphore causing its token count to increment by one
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Mutex
To use a semaphore in the CMSIS-RTOS you must first declare a Mutex stands for “Mutual Exclusion.”
semaphore container: A mutex is a specialized version of semaphore.
osSemaphoreId sem1; Like a semaphore, a mutex is a container for tokens.
osSemaphoreDef(sem1); A mutex can only contain one token which cannot be created or
Then within a thread the semaphore container can be initialized with destroyed.
a number of tokens. The principle use of a mutex is to control access to a chip resource such
sem1 = osSemaphoreCreate(osSemaphore(sem1), as a peripheral. For this reason a mutex token is binary and bounded.
SIX_TOKENS); Apart from this it really works in the same way as a semaphore.
First of all we must declare the mutex container and initialize the mutex:
Example: osMutexId uart_mutex;
We will look at the configuration of a semaphore and use it to signal osMutexDef (uart_mutex);
between two tasks.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Data Exchange
CMSIS-RTOS provides two methods of data transfer between The second form of data transfer is a mail queue.
threads. This is very similar to a message queue except that it transfers
The first method is a message queue which creates a buffered data blocks of data rather than a single integer
“pipe” between two threads.
The message queue is designed to transfer integer values
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Message and mail queues both provide a method for transferring Examples:
data between threads. Message Queue
This allows you to view your design as a collection of objects We will look at defining a message queue between two threads and
(threads) interconnected by data flows. then use it to send process data.
The data flow is implemented by message and mail queues.
This provides both a buffered transfer of data and a well-defined Mailbox
communication interface between threads. Demonstrates configuration of a mailbox and using it to post
messages between tasks.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Scheduling Options Preemptive Scheduling
CMSIS-RTOS allows you to build an application with three If the round-robin option is disabled in the RTX_Config_CM.c
different kernel scheduling options. file, each thread must be declared with a different priority.
These are round-robin scheduling, preemptive scheduling, and When the RTOS is started and the threads are created, the thread
cooperative multitasking. with the highest priority will run
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
This thread will run until it blocks, that is, it is forced to wait for Round-Robin Scheduling
an event flag, semaphore, or other object. A round-robin-based scheduling scheme can be created by
When it blocks, the next ready thread with the highest priority enabling the round-robin option in the RTX_Conf_CM.c file and
will be scheduled and will run until it blocks, or a higher priority declaring each thread with the same priority
thread becomes ready to run.
So with preemptive scheduling we build a hierarchy of thread
execution, with each thread consuming variable amounts of run-
time.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
In this scheme, each thread will be allotted a fixed amount of run- Round-Robin Preemptive Scheduling
time before execution is passed to the next ready thread. The default scheduling option for the Keil RTX is round-robin
If a thread blocks before its timeslice has expired, execution will preemptive.
be passed to the next ready thread. For most applications this is the most useful option and you
should use this scheduling scheme unless there is a strong reason
to do otherwise.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Cooperative Multitasking
A final scheduling option is cooperative multitasking.
In this scheme, round-robin scheduling is disabled and each
thread has the same priority.
This means that the first thread to run will run forever unless it
blocks.
Then execution will pass to the next ready thread
Threads can block on any of the standard OS objects, but there is
also an additional OS call, osThreadYeild(), that schedules a In a cooperative RTOS each thread will run until it reaches a
thread to the ready state and passes execution to the next ready blocking OS call or uses the osThreadYield() call.
thread.
Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family” Source: Trevor Martin, “The Designer’s Guide to the Cortex-M Processor Family”
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956 BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956