ARM Microcontroller Guide
ARM Microcontroller Guide
Peripherals
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
2
References
3
References
4
References
5
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
7
1. Overview on ARM architecture
Outline
The point consists of the following topics:
What is ARMarchitecture?
Development of the ARMArchitecture
ARM Licensing.
ARMv7 architecture profiles
8
What is ARM architecture?
Acronym of Advanced RISC Machines.
It is a 32 – bit microprocessors based on
RISC architecture.
This approach reduces costs, heat and power
use by stripping out unneeded instructions
and optimizing pathways.
Later versions of the architecture also
support a variable-length instruction set that
provides both 32- and 16-bit wide
instructions for improved code density.
9
History of ARM architecture
The British computer manufacturer Acorn Computers first developed
the Acorn RISC Machine architecture (ARM) in the 1980s to use in its
personal computers.
10
History of ARM architecture
Its first ARM-based products were coprocessor modules for the BBC
Micro series of computers.
11
History of ARM architecture
After the success of the first BBCMicroprocessor they developed the
second processor in the seriesARM1.
•Clock: 2MHz
•Registers: 16 general purpose, selectively banked
•Pipeline: three-stage
•Cache: none
•Addressing: 26-bit
12
History of ARM architecture
The ARM2 featured a 32-bit data bus, 26-bit address space and 27 32-
bit registers.
Eight bits from the program counter register were available for other
purposes;
the top six bits (available because of the 26-bit address space) served as
status flags, and the bottom two bits (available because the program
counter was alwaysword-aligned) were used for setting modes.
13
History of ARM architecture
In 1990, Acorn spun off the design team into a new company named
Advanced RISC Machines Ltd developing ARM6architecture.
The address bus was extended to 32 bits in the ARM6, but program
code still had to lie within the first 64 MBof memory in 26-bit
compatibility mode, due to the reserved bits for the status flags
14
Development of the ARM Architecture
15
Development of the ARM Architecture
Embedded Processors
16
Development of the ARM Architecture
Application Processors
17
ARM Licensing
ARMHoldings Ltd develops the architecture
and licenses it to other companies.
It provides two types of Licenses:
1- Core License:
which licensees use to create (MCUs),CPUs,
and SOCs on those cores.
The original design manufacturer combines
the ARM core with other parts to produce a
complete device.
18
ARM Licensing
ARMHoldings Ltd develops the architecture
and licenses it to other companies.
It provides two types of Licenses:
2- ArchitectureLicense:
Companies can also obtain an
ARM architectural license for designing their
own CPU cores using the ARM instruction
sets.
These cores must comply fully with the
ARM architecture.
19
ARMv7 architecture profiles
Starting of ARMv7, ARM defined three architecture
"profiles based on their capabilities and provided
applications
1-A-Profile:
the "Application" profile, implemented by 32-bit
cores in the Cortex-A series and by some non-ARM
cores.
Application profiles implement a traditional ARM
architecture with multiple modes and support a virtual
memory system architecture based on an MMU.
These profiles support both ARM andThumb
instruction sets.
20
ARMv7 architecture profiles
Starting of ARMv7, ARM defined three architecture
"profiles based on their capabilities and provided
applications
2- R-Profile:
Real-time profiles implement a traditional ARM
architecture with multiple modes and support a
protected memory system architecture based on an
MPU.
implemented by cores in the Cortex-R series
21
ARMv7 architecture profiles
Starting of ARMv7, ARM defined three
architecture "profiles based on their
capabilities and provided applications
3- M-Profile:
the "Microcontroller" profile,
implemented by most cores in the Cortex-
M series.
Microcontroller profiles implement a
programmers' model designed for fast
interrupt processing, with hardware
stacking of registers and support for writing
interrupt handlers in high-level languages.
22
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
24
2. ARM Cortex-M4 and ARM Cortex-
M3 Specifications
Outline
The point consists of the following topics:
The Cortex-M Processor family
The Cortex-M3 and Cortex-M4 processors features
Cortex-M4 block diagram
Cortex-M4Architecture
Cortex-M4 Exceptions and interrupts
25
The Cortex-M Processor family
For general data processing and I/O control tasks, the Cortex-M0
and Cortex-M0þ processors have excellent energy efficiency
But for applications with complex data processing requirements,
they may take more instructions and clock cycles.
26
The Cortex-M3 and Cortex-M4 processors features
27
The Cortex-M3 and Cortex-M4 processors features
28
The Cortex-M3 and Cortex-M4 processors features
29
The Cortex-M3 and Cortex-M4 processors features
30
The Cortex-M3 and Cortex-M4 processors features
31
Cortex-M4 block diagram
The Cortex-M3 and Cortex-M4 processors are highly configurable. allowing system-
on-chip designers to remove any optional components (Like debug support if not
required to be supported in MCU).
32
Cortex-M4 block diagram
34
Cortex-M4 Architecture
Programmer model
the processors can have privileged and unprivileged access levels.
The privileged access level can access all resources in the processor.
unprivileged access level means some memory regions are inaccessible, and
a few operations cannot be used.
The processor is also has two operational states:
Debug state:When the processor is halted (e.g., by the debugger, or after
hitting a breakpoint), it enters debug state and stops executing
instructions.
Thumb state: If the processor is running program code (Thumb), it is in
the Thumbstate.
35
Cortex-M4 Architecture
Programmer model
In the thumb state, the processor has two operational modes:
37
Cortex-M4 Architecture
Register file
38
Cortex-M4 Architecture
Instruction Set:
ClassicARM processors were supporting bothARM-32 bit Instructions
and Thum16 bit instructions
39
Cortex-M4 Architecture
Instruction Set:
ARM Cortex Instruction set introduced Thumb-2 technology, the Thumb
instruction set has been extended to support both 16-bit and 32-bit
instruction encoding.
40
Cortex-M4 Architecture
Instruction Set:
With Thumb-2 technology, the Cortex-M processor has a number of
advantages over classic ARM processors:
1. No state switching overhead, saving both execution time and instruction
space.
2. No need to specify ARM state or Thumb state in source files, making
software development easier.
3. It is easier to get the best code density, efficiency, and performance at the
same time.
4. With Thumb-2 technology, theThumb instruction set has been extended
by a wide margin when compared to a classic processor
41
Cortex-M4 Architecture
Memory System
The Cortex-M3 and M4 processors themselves do not include memories.
they come with a generic on-chip bus interface.
the microcontroller vendor will need to add the following items to the
memory system:
1. Program memory, typically flash
2. Data memory, typically SRAM
3. Peripherals
42
Cortex-M4 Architecture
Memory System
The provided bus interfaces on the Cortex-M processors are 32-bit, and
based on the Advanced Microcontroller BusArchitecture (AMBA)
standard.
43
Cortex-M4 Exceptions and interrupts
Exceptions are events that cause changes to program flow.
When one happens, the processor suspends the current executing task and
executes a part of the program called the exception handler.
After the execution of the exception handler is completed, the processor
then resumes normal program execution.
44
Cortex-M4 Exceptions and interrupts
45
Cortex-M4 Exceptions and interrupts
Nested Interrupt Vector Controller.(NIVC)
The NVIC handles the exceptions and interrupt configurations,
prioritization, and interrupt masking.
The NVIC has the following features:
1. Flexible exception and interrupt management
2. Nested exception/interrupt support.
3. Vectored exception/interrupt entry.
4. Interrupt masking.
46
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
48
3. TM4C123GH6PM Microcontroller
Peripherals.
Outline
The point consists of the following topics:
Microcontroller Features
Microcontroller High level block diagram
49
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
50
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
51
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
52
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
53
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
54
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
55
TM4C123GH6PM Microcontroller Overview
Microcontroller Features
56
TM4C123GH6PM Microcontroller Overview
Microcontroller High level block diagram
57
TM4C123GH6PM Microcontroller Overview
Microcontroller Pin Diagram
58
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
60
4. TIVA TM4C123GH6PM Launchpad
kit specifications
TIVA TM4C123GH6PM Launchpad kitspecifications
61
TIVA TM4C123GH6PM Launchpad kitspecifications
62
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
64
5. GPIO Interface with applications
GPIO Interface with applications
65
GPIO Interface with applications
66
GPIO Interface with applications
• Bit masking in both read and write operations through address lines
67
GPIO Interface with applications
68
GPIO Interface with applications
69
GPIO Interface with applications
70
GPIO Interface with applications
71
GPIO Interface with applications
72
GPIO Interface with applications
73
GPIO Interface with applications
74
GPIO Interface with applications
75
GPIO Interface with applications
76
GPIO Interface with applications
77
GPIO Interface with applications
78
GPIO Interface with applications
79
GPIO Interface with applications
80
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
82
5. Interrupts and exceptions of
TM4C123GH6PM
Interrupts and exceptions of TM4C123GH6PM
NIVC Capabilities
83
Interrupts and exceptions of TM4C123GH6PM
• In most processors, interrupt handling is fairly simple and each interrupt will
start a PUSH PROCESSOR STATE – RUN ISR – POP PROCESSOR
STATE process.
• If the interrupt handler could have seen that a second interrupt was pending,
it could have “tail-chained” into the next ISR, saving power and cycles.
84
Interrupts and exceptions of TM4C123GH6PM
• In most processors, the interrupt controller would complete the process before
starting the entire PUSH-ISR-POP process over again, wasting precious cycles
and power doing so.
• The Tiva C NVIC is able to stop the POP process, return the stack pointer to
the proper location and “tail-chain” into the next ISR with only 6 cycles.
86
Interrupts and exceptions of TM4C123GH6PM
87
Interrupts and exceptions of TM4C123GH6PM
88
Interrupts and exceptions of TM4C123GH6PM
■ Inactive.
The exception is not active and not pending.
■ Pending.
The exception is waiting to be serviced by the processor. An interrupt request
from a peripheral or from software can change the state of the corresponding
interrupt to pending.
■ Active.
An exception that is being serviced by the processor but has not completed.
Note:
■ Active and Pending.
The exception is being serviced by the processor, and there is a pending
exception from the same source.
89
Interrupts and exceptions of TM4C123GH6PM
First interrupt
handling done
Inactive
Interrupt Requested
Active
and Interrupt handling
Pending done Pending
90
Interrupts and exceptions of TM4C123GH6PM
91
Interrupts and exceptions of TM4C123GH6PM
92
Interrupts and exceptions of TM4C123GH6PM
93
Interrupts and exceptions of TM4C123GH6PM
94
Interrupts and exceptions of TM4C123GH6PM
Exception Priorities
• There are some sources with fixed priority and others with configurable
priority.
• As the priority number decreases, the priority of the exception increased.
• If two interrupts are pending , the interrupt with the higher priority will be
handled.
• If both of pending interrupts have the same priority, the interrupt with the
lower interrupt number will be handled.
• If a higher priority interrupt becomes pending while a lower priority
interrupt is active, the lower one will be preempted.
• If the newly pending interrupt is the same priority of the currently active
interrupt, the active interrupt will not be preempted regardless of both
interrupts numbers.
95
Interrupts and exceptions of TM4C123GH6PM
• If the two pending interrupts have the same group priority, the subpriority
order will determine the order of execution.
• If the two pending interrupts have the same group priority and the same
subpriority, the interrupt number will determine the order of execution.
96
Interrupts and exceptions of TM4C123GH6PM
97
Interrupts and exceptions of TM4C123GH6PM
98
Interrupts and exceptions of TM4C123GH6PM
99
Interrupts and exceptions of TM4C123GH6PM
100
Interrupts and exceptions of TM4C123GH6PM
101
Interrupts and exceptions of TM4C123GH6PM
102
Interrupts and exceptions of TM4C123GH6PM
104 .
Interrupts and exceptions of TM4C123GH6PM
104
Interrupts and exceptions of TM4C123GH6PM
105
Interrupts and exceptions of TM4C123GH6PM
106
Interrupts and exceptions of TM4C123GH6PM
109
Interrupts and exceptions of TM4C123GH6PM
• Field columns in the table refer to the bits in the INTA field. For the INTB field,
the corresponding bits are 15:13; for INTC, 23:21; and for INTD, 31:29.
110
Interrupts and exceptions of TM4C123GH6PM
111
Interrupts and exceptions of TM4C123GH6PM
112
Agenda
1. Overview on ARMarchitecture
2. ARM Cortex-M4 and ARM Cortex-M3 Specifications
3. TM4C123GH6PM Microcontroller Peripherals.
4. TIVATM4C123GH6PM Launchpad kit specifications
5. GPIO Interface with applications
6. ADC Interface with applications
7. Interrupts and exceptions ofTM4C123GH6PM.
8. SPI Interface with applications.
9. I2C Interface with applications.
10. UART Interface.
11. DMAand its applications.
12. Timers and PWM interfacing .
113
6. DMA (Direct Memory Access)
DMA (Direct Memory Access)
Introduction
• Direct memory access (DMA) is a means of having a peripheral device control
a processor's memory bus directly. DMA permits the peripheral, such as a
UART, to transfer data directly to or from memory without having each byte (or
word) handled by the processor.
Benefits:
• DMA enables more efficient use of interrupts.
• increases data throughput.
• potentially reduces hardware costs by eliminating the need for peripheral-
specific FIFO buffers.
115
DMA (Direct MemoryAccess)
116
DMA (Direct MemoryAccess)
117
DMA (Direct Memory Access)
118
DMA (Direct Memory Access)
Functional Description:
1- Channel assignments:
• Each DMA channel has up to five possible assignments.
• The Type indicates if a particular peripheral uses a single request (S), burst
request (B) or either (SB).
119
DMA (Direct MemoryAccess)
Functional Description:
1- Channel assignments:
120
DMA (Direct MemoryAccess)
Functional Description:
1- Channel assignments:
121
DMA (Direct Memory Access)
Functional Description:
2- Channel Priority:
122
DMA (Direct Memory Access)
Functional Description:
3- Arbitration size:
123
DMA (Direct Memory Access)
Functional Description:
4- Channel Configuration structure:
• The μDMA controller uses an area of system memory to store a set of channel
control structures in a table.
• Each entry in the table structure contains source and destination pointers,
transfer size, and transfer mode.
• The control table can be located anywhere in system memory, but it must be
contiguous and aligned on a 1024-byte boundary.
124
DMA (Direct Memory Access)
Functional Description:
4- Channel Configuration structure:
125
DMA (Direct Memory Access)
Functional Description:
4- Channel Configuration structure:
• Each channel may have one or two control structures in the control table: a
primary control structure and an optional alternate control structure.
• The table is organized so that all of the primary entries are in the first half of the
table, and all the alternate structures are in the second half of the table.
• The primary entry is used for simple transfer modes where transfers can be
reconfigured and restarted after each transfer is complete.
126
DMA (Direct Memory Access)
Functional Description:
5- Types of requests:
1- Single Request:
• When a single request is detected, and not a burst request, the μDMA controller
transfers one item and then stops to wait for another request.
2- Burst Request:
• When a burst request is detected, the μDMA controller transfers the number of
items that is the lesser of the arbitration size or the number of items remaining
in the transfer.
• the arbitration size should be the same as the number of data items that the
peripheral can accommodate when making a burst request.
• A burst transfer runs to completion once it is started, and cannot be interrupted,
even by a higher priority channel.
• Burst transfers complete in a shorter time than the same number of non-burst
transfers.
127
DMA (Direct MemoryAccess)
Functional Description:
5- Types of requests:
128
DMA (Direct Memory Access)
Functional Description:
6- Transfere modes:
- Stop Mode
• The μDMA controller does not perform any transfers and disables the channel if
it is enabled.
• At the end of a transfer, the μDMA controller updates the control word to set
the mode to Stop.
129
DMA (Direct Memory Access)
Functional Description:
6- Transfere modes:
- Basic Mode
• the μDMA controller performs transfers as long as there are more items to
transfer, and a transfer request is present.
• Only the number of transfers specified by the ARBSIZE field in the DMA
Channel Control Word (DMACHCTL) register is transferred on a software
request, even if there is more data to transfer.
• This mode is used with peripherals that assert a μDMA request signal
whenever the peripheral is ready for a data transfer.
• Basic mode should not be used in any situation where the request is
momentary even though the entire transfer should be completed.
• When all of the items have been transferred using Basic mode, the μDMA
controller sets the mode for that channel to Stop.
130
DMA (Direct Memory Access)
Functional Description:
6- Transfere modes:
- Auto Mode
• Auto mode is similar to Basic mode, except that once a transfer request is
received, the transfer runs to completion, even if the μDMA request is
removed.
• This mode is suitable for software-triggered transfers. Generally, Auto
mode is not used with a peripheral.
• When all the items have been transferred using Auto mode, the μDMA
controller sets the mode for that channel to Stop.
131
DMA (Direct Memory Access)
Functional Description:
6- Transfere modes:
- Ping-Pong
• Ping-Pong mode is used to support a continuous data flow to or from a
peripheral.
• To use Ping-Pong mode, both the primary and alternate data structures
must be implemented.
• The transfer is started using the primary control structure and after
complete the μDMA controller reads the alternate control structure for that
channel to continue the transfer.
• Each time this happens, an interrupt is generated, and the processor can
reload the control structure for the just-completed transfer.
132
DMA (Direct Memory Access)
Functional Description:
6- Transfere modes:
- Ping-Pong
133
DMA (Direct Memory Access)
Functional Description:
7- Transfer Size and Increment
• The μDMA controller supports transfer data sizes of 8, 16, or 32 bits.
• The source and destination data size must be the same for any given
transfer.
• The source and destination address can be auto-incremented
independently by bytes, half-words, or words, or can be set to no
increment.
• it is not necessary for the address increment to match the data size as long
as the increment is the same or larger than the data size.
134
DMA (Direct Memory Access)
Functional Description:
8- Software Request and peripheral transfer:
• Each peripheral that supports μDMA has a single request and/or burst
request signal that is asserted when the peripheral is ready to transfer data
• The request signal can be disabled or enabled using the DMA Channel
Request Mask Set (DMAREQMASKSET) and DMA Channel Request
Mask Clear (DMAREQMASKCLR) registers.
• When a μDMA transfer is complete, the μDMA controller generates an
interrupt,
• For more information on how a specific peripheral interacts with the μDMA
controller, refer to the DMA Operation section in the chapter that discusses
that peripheral.
135
DMA (Direct Memory Access)
Functional Description:
9 – Interrupts and errors:
• When a μDMA transfer is complete, the μDMA controller generates a
completion interrupt on the interrupt vector of the peripheral.
• if μDMA is used to transfer data for a peripheral and interrupts are used, then
the interrupt handler for that peripheral must be designed to handle the μDMA
transfer completion interrupt.
• If the transfer uses the software μDMA channel, then the completion interrupt
occurs on the dedicated software μDMA interrupt vector.
• When μDMA is enabled for a peripheral, the μDMA controller stops the normal
transfer interrupts for a peripheral from reaching the interrupt controller.
• when a large amount of data is transferred using μDMA, instead of receiving
multiple interrupts from the peripheral as data flows, the interrupt controller
receives only one interrupt when the transfer is complete.
• If the μDMA controller encounters a bus or memory protection error as it
attempts to perform a data transfer, it disables the μDMA channel that caused
the error and generates an interrupt on the μDMA error interrupt vector.
136
DMA (Direct MemoryAccess)
137
7. Serial Interface protocols
Serial Interface Protocols
• In parallel mode, each bit has a single wire devoted to it and all the bits are
transmitted at the same time.
• This method of transmission can move a significant amount of data in a given
period of time.
• Its disadvantage is the large number of interconnecting cables between the two
units.
• It also require special interfacing to minimize noise and distortion problems.
139
Serial Interface Protocols
141
Serial Interface Protocols
• Data Transfer is called Asynchronous when data bits are not “synchronized”
with a clock line, i.e. there is no clock line at all!
• Asynchronous data transfer has a protocol, which is usually as follows:
• The first bit is always the START bit ,followed by DATA bits followed by a STOP
bit
• A way of error detection would be added.
• The START bit is always low (0) while the STOP bit is always high (1).
142
Serial Interface Protocols
• Synchronous data transfer is when the data bits are “synchronized” with a clock
pulse.
• The basic principle is that data bit sampling (or in other words, say, ‘recording’)
is done with respect to clock pluses, as you can see in the timing diagrams.
• Since data is sampled depending upon clock pulses, and since the clock
sources are very reliable, so there is much less error in synchronous as
compared to asynchronous.
143
Serial Interface Protocols
SPI
USB
I2C
144
Serial Interface Protocols: UART
What is UART?
145
Serial Interface Protocols: UART
• When idling, or not transmitting any data, the wire voltage is held high.
• When the transmitting UART is ready to transmit a byte, it must first transmit a start The
start bit is always 0. This tells the receiver that a byte is incoming, the next bit will be the
first bit of the byte being transmitted.
• Bits are transmitted starting with the least significant digit finishing with the most
significant..
• After the 8th bit, a stop bit is transmitted which always has a value of 1. At least one stop
bit is required.
• If no more bytes are pending transmission, the transmitter will go idle with a high voltage
level same as the stop bit.
• An optimal parity bit could be added before the stop bit and its value is dependent on the
parity type (even or odd).
147
Serial Interface Protocols: UART
Zero
Idle detected
on bus
148
Serial Interface Protocols: UART
• Overrun error:
• occurs when the receiver cannot process the character that just came in before
the next one arrives.
• Framing error:
• A "framing error" occurs when the designated "start" and "stop" bits are not
found.
• Parity error:
• A Parity Error occurs when the parity of the number of 1 bits disagrees with that
specified by the parity bit. Use of a parity bit is optional, so this error will only
occur if parity-checking has been enabled.
149
Serial Interface Protocols: UART
150
Serial Interface Protocols: UART
152
Serial Interface Protocols: UART
153
Serial Interface Protocols: UART
• The transmit logic performs parallel-to-serial conversion on the data read from
the transmit FIFO.
• The control logic outputs the serial bit stream beginning with a start bit and
followed by the data bits (LSB first), parity bit, and the stop bits according to the
programmed configuration in the control registers.
• The receive logic performs serial-to-parallel conversion on the received bit
stream after a valid start pulse has been detected.
• Overrun, parity, frame error checking, and line-break detection are also
performed, and their status accompanies the data that is written to the receive
FIFO.
154
Serial Interface Protocols: UART
• For transmission, data is written into the transmit FIFO by writing data on
UART Data (UARTDR) register.
• If the UART is enabled, it causes a data frame to start transmitting.
• Data continues to be transmitted until there is no data left in the transmit FIFO.
• The BUSY bit in the UART Flag (UARTFR) register is asserted as soon as data
is written to the transmit FIFO.
• It remains asserted while data is being transmitted.
• For reception, When the receiver is idle and a start bit has been received, the
receive counter begins running and data is sampled depending on the setting of
the HSE bit (bit 5) in UARTCTL.
• The parity bit is then checked if parity mode is enabled. Data length and parity
are defined in the UARTLCRH register.
• When a full word is received, the data is stored in the receive FIFO along with
any error bits associated with that word.
156
Serial Interface Protocols: UART
• The UART has two 16x8 FIFOs; one for transmit and one for receive.
• Both FIFOs are accessed via the UART Data (UARTDR).
• Out of reset, both FIFOs are disabled and act as 1-byte-deep holding registers.
• The FIFOs are enabled by setting the FEN bit in UARTLCRH.
• FIFO status can be monitored via the UART Flag (UARTFR) register and the
UART Receive Status (UARTRSR) register.
• Hardware monitors empty, full and overrun conditions.(TXFE, TXFF, RXFE,
RXFF and OE bits).
• The trigger points at which the FIFOs generate interrupts is controlled via the
UART Interrupt FIFO Level Select (UARTIFLS) register.
• Both FIFOs can be individually configured to trigger interrupts at different levels.
Available configurations include ⅛, ¼, ½, ¾, and ⅞.
157
Serial Interface Protocols: UART
• The UART can generate interrupts when the following conditions are observed:
Overrun error, break error, parity error, frame error, receive timeout,
transmission condition met, reception condition met.
• All of the interrupt events are ORed together before being sent to the interrupt
controller, so the UART can only generate a single interrupt request to the
controller at any given time.(That means all sources UART interrupt will be
handled by a single ISR).
• This could be done using by reading the UART Masked Interrupt Status
(UARTMIS) register.
• The interrupt events that can trigger a controller-level interrupt are defined in
the UART Interrupt Mask (UARTIM) register.
• If interrupts are not used, the raw interrupt status is visible via the UART Raw
Interrupt Status (UARTRIS) register.
158
Serial Interface Protocols: SPI
What is SPI?
160
Serial Interface Protocols: SPI
161
Serial Interface Protocols: SPI
• Transmissions normally involve two shift registers of some given word size,
such as eight bits, one in the master and one in the slave; they are connected
in a virtual ring topology.
• After the register bits have been shifted out and in, the master and slave have
exchanged register values.
• If more data needs to be exchanged, the shift registers are reloaded and the
process repeats.
162
Serial Interface Protocols: SPI
163
Serial Interface Protocols: SPI
Data Order
• LSB First:
• MSB First:
164
Serial Interface Protocols: SPI
165
Serial Interface Protocols: SPI
166
Serial Interface Protocols: SPI
167
Serial Interface Protocols: SPI
168
Serial Interface Protocols: SPI
169
Serial Interface Protocols: I2C
What is I2C?
I2C Terminologies:
171
Serial Interface Protocols: I2C
• All address packets transmitted on the TWI bus are nine bits long, consisting of
seven address bits, one READ/WRITE control bit and an acknowledge bit.
• If the READ/WRITE bit is set, a read operation is to be performed, otherwise a
write operation should be performed.
• When a slave recognizes that it is being addressed, it should acknowledge by
pulling SDA low in the ninth SCL (ACK) cycle.
• the address 0000 000 is reserved for a general call.
172
Serial Interface Protocols: I2C
• All data packets transmitted on the TWI bus are nine bits long, consisting of one
data byte and an acknowledge bit.
• The MSB of the data byte is transmitted first.
173
Serial Interface Protocols: I2C
174
Serial Interface Protocols: I2C
175
Serial Interface Protocols: I2C
176
Serial Interface Protocols: I2C
177
Serial Interface Protocols: I2C
178
Serial Interface Protocols: I2C
179
8. General Purpose Timer and Pulse
Width Modulation
Introduction to Micrcontroller Free running timer
• Timer is just a register that updates its value every trigger by increment or
decrement.
• There are at least three data registers in any timer, one to hold the current
Timer value, one to hold the max number of counts to be reached and the third
is to hold a prescaler division factor.
• Any microcontroller will provide a group of modes to support the timer to
perform the time related functionality.
• Any timer is used to support, Timeout events, external events counting and
generation of time controlled signals (PWM).
181
Introduction to Micrcontroller Free running timer
The time consumed in one time iteration is calculated from the form:
Time per iteration = Number of counts per iteration X Timer per one count.
Number of counts per iteration is limited by the size of the free running timer
register.
Time per one count is limited by the oscillator frequency.
To increase the overall iteration time we can increase the time per one count
without changing the oscillator frequency.
This could be done using prescaler and post scaler.
182
Introduction to Micrcontroller Free running timer
• The the value in the prescaler register defines the number of times the clock
has to tick before the timer register experiences a single pulse.
• The scaling ratio 1:2 means that the clock has to have a LOW to HIGH
transition twice so that the output of the prescaler completes one HIGH pulse.
• The postscaler concept is much similar to the prescaler. Only here, the value of
the prescaler determines the number of times the timer register has to overflow
to produce an interrupt.
183
Introduction to Micrcontroller Free running timer
• Timer can be configured to reset after specific value saved in its compare
register.
• The overflow time is calculated from the form:
Tov = (N / Fosc) X (Number of counts)
• N is the prescaler division factor.
• Fosc is the input oscillator frequency
• Number of counts is dependent on the value saved in the compare register.
• The overflow events can be used to generate interrupts on a specific times.
184
Introduction to Micrcontroller Free running timer
185
Introduction to Micrcontroller Free running timer
Duty cycle = (number of counts till toggle / total number of counts) X 100 %
186
Introduction to Micrcontroller Free running timer
187
Introduction to Micrcontroller Free running timer
• Input capture could be used to measure the periodicity and the frequency of
external input signals.
• The measured time is calculated using the formula:
• If Second edge counts > first edge counts
• Tperiod = (Second edge counts – first edge counts) X (Fosc / N) X
(number of timer overflows – 1).
• If Second edge counts < first edge counts
• Tperiod = (Overflow counts – (first edge counts – second edge counts) X
(Fosc / N) X (number of timer overflows – 1).
188
Introduction to Micrcontroller Free running timer
189
Introduction to Micrcontroller Free running timer
190
Introduction to Micrcontroller Free running timer
191
Introduction to Micrcontroller Free running timer
- Distance measurement
192
Introduction to Micrcontroller Free running timer
193
TM4C123GH6PM GPT
The General-Purpose Timer Module (GPTM) contains six 16/32-bit GPTM
blocks and six 32/64-bit Wide GPTM blocks with the following functional
options:
16/32-bit operating modes:
• 16- or 32-bit programmable one-shot timer
• 16- or 32-bit programmable periodic timer
• 16-bit general-purpose timer with an 8-bit prescaler
• 32-bit Real-Time Clock (RTC) when using an external 32.768-KHz clock as the
input
• 16-bit input-edge count- or time-capture modes with an 8-bit prescaler
• 16-bit PWM mode with an 8-bit prescaler and software-programmable output
inversion of the
• PWM signal
194
TM4C123GH6PM GPT
The General-Purpose Timer Module (GPTM) contains six 16/32-bit GPTM
blocks and six 32/64-bit Wide GPTM blocks with the following functional
options:
32/64-bit operating modes:
• 32- or 64-bit programmable one-shot timer
• 32- or 64-bit programmable periodic timer
• 32-bit general-purpose timer with a 16-bit prescaler
• 64-bit Real-Time Clock (RTC) when using an external 32.768-KHz clock as the
input
• 32-bit input-edge count- or time-capture modes with a16-bit prescaler
• 32-bit PWM mode with a 16-bit prescaler and software-programmable output
inversion of the
• PWM signal
195
TM4C123GH6PM GPT
The General-Purpose Timer Module (GPTM) contains six 16/32-bit GPTM
blocks and six 32/64-bit Wide GPTM blocks with the following functional
options:
Other features:
■ Count up or down
■ Twelve 16/32-bit Capture Compare PWM pins (CCP)
■ Twelve 32/64-bit Capture Compare PWM pins (CCP)
■ Daisy chaining of timer modules to allow a single timer to initiate multiple timing
events
■Timer synchronization allows selected timers to start counting on the same clock
cycle
■ ADC event trigger
■ User-enabled stalling when the microcontroller asserts CPU Halt flag during
debug (excluding RTC mode)
■Ability to determine the elapsed time between the assertion of the timer interrupt
and entry into the interrupt service routine
■ Efficient transfers using Micro Direct Memory Access Controller (μDMA)
196
TM4C123GH6PM GPT
Block Diagram:
197
TM4C123GH6PM GPT
Block Diagram:
The main components of each GPTM block are:
1- Two free running up/down counters known as timer A and Timer B.
• Each one of them can work in 16 bit mode and can concatenated to together to
make it 32 bit counter or 32 bit mode and can concatenated together to make it
64 bit counter.
• The timer timeout value is controlled via the Load initialization registers
(GPTMTAILR and GPTMTBILR).
• The free running counter value is monitored by the timer shadow registers
(GPTMTAV and GPTMTBV).
2- Two prescaler units is counting till prescaler match every timer count.
• Each timer prescaler value is initialized by the prescaler registers (GPTMTAPR
and GPTMTBPR).
• The run time prescaler value is read via the prescaler value
register.(GPTMTAPV and GPTMTBPV).
198
TM4C123GH6PM GPT
Block Diagram:
The main components of each GPTM block are:
3- Two Compare match units to control the compare match interrupts.
• The value of the compare match is initialized via the compare match registers
(GPTMTAMATCHR, GPTMTBMATCHR)
199
TM4C123GH6PM GPT
GPTM signals:
200
TM4C123GH6PM GPT
GPTM signals:
Note that:
The AFSEL bit in the (GPIOAFSEL) register should be set to choose the GP Timer
function. The PMCn field in the (GPIOPCTL) register should selected to assign the
GP Timer signal to the specified GPIO port pin. The GPIODEN register should be
configured to make the GPIO pin digital.
201
TM4C123GH6PM GPT
GPTM Modes of operations:
1- One-Shot/Periodic Timer Mode:
202
TM4C123GH6PM GPT
GPTM Modes of operations:
1- One-Shot/Periodic Timer Mode:
Timer mode Count Start Start value End value After timeout
direction condition
Periodic Up Timer is 0x0000 GPTMTALIR Reload by
enabled 0x0000 and
restart counting
Periodic Down Timer is GPTMTALIR 0x0000 Reload by
enabled GPTMTAL
IR
204 and restart
TM4C123GH6PM GPT
GPTM Modes of operations:
1- One-Shot/Periodic Timer Mode:
204
TM4C123GH6PM GPT
GPTM Modes of operations:
2- Real time clock (RTC) mode:
• Concatenated versions of the Timer A and Timer B registers are configured as
an up-counter.
32.768 1 Hz
KHz from Pre divider Timer B TimerA
CPP0
• The input clock on a CCP0 input is required to be 32.768 KHz in RTC mode.
• The clock signal is then divided down to a 1-Hz rate and is passed along to the
input of the counter.
• When the timer is enabled it starting counting from a preloaded value equal
0x01.
• If the GPTMTnILR register is loaded with a new value, the counter begins
counting at that value and rolls over at the fixed value of 0xFFFFFFFF
• When the counter value reach the value saved in the compare match register it
generated RTC event interrupt.
205
TM4C123GH6PM GPT
GPTM Modes of operations:
2- Real time clock (RTC) mode:
• To ensure that the RTC value is coherent, software should follow the process
detailed in Figure
206
TM4C123GH6PM GPT
GPTM Modes of operations:
3- Input Edge-Count Mode:
207
TM4C123GH6PM GPT
GPTM Modes of operations:
3- Input Edge-Count Mode:
When up counting the number of detected counts will be the value saved in the
match registers (GPTMTnMATCHR and GPTMTnPMR) as the counter stating
from 0x00.
208
TM4C123GH6PM GPT
GPTM Modes of operations:
3- Input Edge-Count Mode:
When down counting, the timer is counting starting from the value saved in the
load registers (GPTMTnILR and GPTMTnPR) and stopped when reaching the
Match registers.
209
TM4C123GH6PM GPT
GPTM Modes of operations:
3- Input Edge-Count Mode:
Notes:
• when executing an up-count, that the value of GPTMTnPR and GPTMTnILR
must be greater than the value of GPTMTnPMR and GPTMTnMATCHR.
• When the counting matches, the timer sets the corresponding bit in the raw
interrupt status and still high until cleared by setting the corresponding bit in the
interrupt clear register.
• If interrupt is enabled, the timer also sets the masked interrupt status when
match occurred.
• After match, the timer will be reloaded by zero in up counting and by the value
saved in GPTMTnILR and GPTMTnPR registers when down counting.
• After match, the timer will stop counting as it will automatically disable the timer.
210
TM4C123GH6PM GPT
GPTM Modes of operations:
4- Input Edge-Time Mode:
• When the selected input event is detected, the current timer counter value is
captured in the GPTMTnR and GPTMTnPS register and is available to be read
by the microcontroller.
212
TM4C123GH6PM GPT
GPTM Modes of operations:
4- Input Edge-Time Mode:
Notes:
• When the selected event detected, the timer sets the corresponding bit in the
interrupt raw status register and still hold until cleared by setting the
corresponding bit in the interrupt clear register.
• If the capture mode event interrupt is enabled, the timer will set the
corresponding bit in the masked interrupt status register.
213
TM4C123GH6PM GPT
GPTM Modes of operations:
5- PWM mode:
• The timer is configured as a 24-bit or 48-bit down-counter with a start value (and thus
period) defined by the GPTMTnILR and GPTMTnPR registers.
• When the timer enabled, the counter begins counting down from until it reaches the 0x0
state.
• On the next counter cycle in periodic mode, the counter reloads its start value from the
GPTMTnILR and GPTMTnPR registers and continues counting until disabled by
software.
• The output PWM signal asserts when the counter is at the value of the PTMTnILR and
GPTMTnPR registers (its start state), and is deasserted when the counter value equals
the value in the GPTMTnMATCHR and GPTMTnPMR registers.
214
TM4C123GH6PM GPT
GPTM Modes of operations:
5- PWM mode:
Notes:
• Software has the capability of inverting the output PWM signal by setting the
TnPWML bit in the GPTMCTL register.
• he timer is capable of generating interrupts based on three types of events:
rising edge, falling edge, or both for the output signal.
• When event occurred, the timer sets the corresponding bit in the raw interrupt
status register and holds until it is cleared by setting the corresponding bit in the
interrupt clear register.
• If the capture mode event interrupt is enabled, the timer will also set the
corresponding bit in the masked interrupt status register.
• If PWM output inversion is enabled, edge detection interrupt behavior is
reversed.
215
TM4C123GH6PM GPT
GPTM timers Daisy chaining:
• The Wait-for-Trigger mode allows daisy chaining of the timer modules such that
once configured, a single timer can initiate multiple timing events using the
Timer triggers.
• Wait-for-Trigger mode is enabled by setting the TnWOT bit in the GPTMTnMR
register.
• When the TnWOT bit is set, Timer N+1 does not begin counting until the timer
in the previous position in the daisy chain (Timer N) reaches its time-out event.
• Care must be taken that the TAWOT bit is never set in GPTM0.
216
TM4C123GH6PM GPT
GPTM timers Daisy chaining:
218