Lec - 5 GPIO
Lec - 5 GPIO
Lec - 5 GPIO
▪ Collecting sensory data and acting in response are done through peripheral modules integrated with
the MCU. It could be in one of two forms:
1. Digital:
▪ Parallel: One bit (from switch/button) or a collection of bits forming one entity (a byte/word)
▪ Serial: A collection of bits transmitted or received on a serial link (wired/wireless)
2. Analog:
▪ Input from an analog sensor e.g., temperature sensor, or output to control a motor.
Analog inputs need ATD converter, and analog outputs need DTA converter to interface to the MCU.
3
➢ A peripheral module is viewed by the application code as a group of locations (registers) categorized as
CONTROL, STATUS, and DATA registers:
Control Registers
Status Registers
4G Address space
Data Registers
Peripheral module registers
General-purpose IO (GPIO)
Suitable to:
▪ Read switches/buttons state, hex keypad, …
6
Several points must be clear before using GPIO ports:
◦ The number of available ports in the MCU and their size (number of bits in each)
SOFTWARE ◦ Configuration:
‒ Connection between port bits to MCU pins
‒ Defining data direction: input or output:
• Is direction configurable?
• Is it defined for the port as a whole or on a port-bit basis?
• What can we do to output port bits: send data, set, clear, or toggle port bits
8
➢ Configuration:
1. Enable clock to the selected GPIO port (Done on a port basis)
1. Connect the selected port pin to the GPIO module via the multiplexer(Done on a bit basis)
➢ Operation:
▪ To read a port bit that is configured for input:
Read PORT INPUT DATA REGISTER
▪ To change port bit(s) configured for output by:
A write to PORT OUTPUT DATA REGISTER, or
Set/Clear/Toggle bits by writing ONE(s) to respective bits is the corresponding PORT
SET/CLEAR/TOGGLR REGISTER
9
KL25Z GPIO: Clock Gating
➢ By default, peripheral modules are disabled (no clock connected) to save power.
➢ System Integration Module (SIM) is used to provide clock to only the active modules.
To enable clock to a GPIO port, PORTA…PORTE, set its corresponding bit in the control register SIM_SCGC5.
10
▪ A port bit is connected to a MCU pin:
Example: (MKL25Z128VLK4 80-pin package)
PORTA bit 1 is connected to pin PTA1 (pin 27) Peripheral Modules
PORTE bit 30 is connected to pin PTA30 (pin 22)
11
KL25Z GPIO Port Bits Direction (input or output)
▪ Each GPIO bit Qi can be
QDi
configured individually to
work as:
• Input bit or
• Output bit Qi
12
1. Enabling Clock to GPIO Port
▪ The register SIM_SCGC5 is 32-bit.
Only 16 bits are shown below.
▪ Bits 9 through 13 are five separate bits
labeled PORTA through PORTE. Each bit
enables clock to the corresponding port.
For example, if we need to enable clocking of
PORTA and PORTE before accessing each of
them, we should set bits 9 and 13 in
SIM_CGC5 to 1.
13
Lower 16 bits of register SIM_SCGC5
2. Connecting a Port Pin to GPIO Module
PSOR select
Port Data Direction Register bit should be 1 PCOR select
Set
Output
Rst Port Data
PTOR select Direction Pin or
Tgl Output
Pad on
D Register Q package
When the MCU is reset, the port data direction Input Enable
bit is set to 0. (Why?)
I/O Clock
PDIR select
Port Data Pin Control
D Input Q Register
Register MUX field
Input Direction
15
KL25Z GPIO: Port Bit Circuitry
Data Bus
bit n
Address
Bus
Control
▪ Control (At startup)
Address
▪ Direction (input or output) Decoder
PDOR select
‒ Data
‒ Set PDIR select
I/O Clock
Data
16
GPIO Control and Data Registers in Memory
Six registers
17
Port Data and Control Registers
Data Bus Address
Bus
▪ PDDR:
bit n
PDDR select
▪ PDOR:
Writ Data Bus bit n to PDOR bit n. Port Data
D Direction Q
Register
▪ PSOR:
PDOR select
Set selected bits in PDOR
PSOR select
Set
▪ PCOR:
PCOR select
Rst Port Data
PTOR select Pin or
Tgl Output
Clear selected bits in PDOR D Register Q
Pad on
package
▪ PTOR
Toggle selected bits in PDOR PDIR select
I/O Clock
➢ Output:
Values are written to the GPIOx_PDOR register.
➢ Set bits:
Writing an integer N to GPIOx_PSOR will set all the port bits that correspond to all
bits that are ones in N.
Example: To set the least significant byte of PTA, write 0x000000ff to GPIOA_PSOR
(PTA->PSOR).
➢ Clear bits:
Writing an integer N to GPIOx_PCOR will clear all port bits that correspond to all
bits that are ones in N.
Example: To clear the least significant byte of PTA, write 0x000000ff to
GPIOA_PCOR (PTA->PCOR).
➢ Toggle bits:
Writing an integer N to GPIOx_PTOR will invert all port bits that correspond to all
bits that are ones in N.
Example: To invert the least significant byte of PTA, write 0x000000ff to
19 GPIOA_PTOR (PTA->PTOR).
INTERFACING
Software
20
Let’s consider a simple problem:
Basic GPIO Example Light up a LED in response to a switch
21
Basic GPIO Example
We can easily recognize two phases for this basic GPIO program:
1. Configuration:
• Configure each selected port bit for input or output (one input and two output bits)
2. Operation:
• Loop reading the switch state and updating the LEDs accordingly
22
I- Configuration: Pseudocode
Configuration is done by setting fields in control registers.
// Set MUX field in PCR of PTA1, PTA2, and PTA5 to 001 to connect pins to GPIO
set MUX field (bits 8-10 in PORTA_PCR1) to 001
set MUX field (bits 8-10 in PORTA_PCR2) to 001
set MUX field (bits 8-10 in PORTA_PCR5) to 001
// Make PTA1 and PTA2 outputs Need to know the addresses of registers:
set bits 1 and 2 of GPIOA_PDDR 1. SIM_SCGC5
2. PCR of pins PTA1, PTA2, and PTA5
3. GPIOA_PDDR,
// Make PTA5 input and the positions of the fields:
clear bit 5 of GPIOA_PDDR 1. PORTA in SIM_SCGC5
2. MUX field in PCR
Hard to remember!
23
2- Operation: Program Loop Pseudocode
24
Need to Reference Control & Data Registers
In our application, we need to reference
the control registers:
▪ SCGC5 in the SIM module
▪ PCRs of the pins used for input and output
▪ The port data direction register
25
Definitions of Pointers to Control & Data Registers
/* System Integration Module System Clock Gating Control Register 5*/
#define SIM_SCGC5 (*((volatile unsigned int*)0x40048038))
26
Accessing Hardware Registers in C
It would be tedious to have to look up and remember the addresses for all hardware control registers,
and the field positions.
➢ The CMSIS is a hardware abstraction layer for Cortex-M processors, developed and distributed by
ARM.
➢ The CMSIS-CORE component provides a C-language interface to the processor core and peripherals.
It simplifies software reuse, reduces the learning curve for developers, and reduces the time to market.
➢ It consists of:
▪ Macros and functions to perform various operations, and
▪ C data structures that map directly to registers.
27
CMSIS - Accessing Hardware Registers in C
The SIM peripheral’s control registers, for example, can be accessed in the C program by CMSIS-CORE.
➢ CMSIS-CORE lets us access the SIM control registers using a C-language data structure with a useful name (SIM).
For example, to access the SIM_SCGC5 register, we simply write SIM->SCGC5.
Note that SIM is defined as a pointer to a data structure, which is why we use the “->” to select the control
register within.
➢ There are similar data structures for all the MCU’s peripherals and their control registers.
The file MKL25Z4.h defines the access layer for CMSIS-CORE and peripherals for KL25Z4-type MCUs.
We need to be sure that all our C source files contain the directive
#include <MKL25Z4.h>
28
CMSIS – Defining GPIO As a New Data Type
▪ Header file MKL25Z4.h defines C data structure types to represent hardware registers in MCU with
CMSIS-Core and peripherals hardware abstraction layer:
30
Coding Style for Accessing Bits
▪ Easy to make mistakes dealing with literal binary and hexadecimal values.
▪ For example, we need to set bits 15 and 19 in PORTA_PDIR for output.
We need to define a 32-bit literal n with bits 15 and 9 only set, you may use:
◦ n = 0000 0000 0000 0000 1000 0010 0000 0000 or
◦ n = 0x00008200
◦ n = 8704
▪ Easier to define n by any of the following three equivalent ways:
1. Make the literal value from shifted bit positions:
n = (1UL << 9) | (1UL << 15);
▪ Set in n all the bits which are one in mask, leaving others unchanged
n |= MASK(foo);
▪ Clear in n all the bits which are zero in mask, leaving others unchanged
n &= ~MASK(foo);
32
Accessing Fields in Control Registers Using Masks
How can we read and modify fields in control registers?
33
Accessing Fields in Control Registers Using Masks (continued)
To find the current value of PORTE field:
n = (SIM->SCGC5 & SIM_SCGC5_PORTE_MASK) >> 13
To set fields PORTA and PORTE and clearing all other fields:
SIM->SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTE_MASK
To set fields PORTA and PORTE and keeping all other fields unchanged:
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTE_MASK
34
C Code
#define LED1_POS (1)
#define LED2_POS (2)
#define SW1_POS (5)
#define MASK(x) (1UL << (x))
35
Clocking Logic
Control register SIM_SCGC5 gates clocks to GPIO ports through bits 9-13.
36
Port Data
PSOR select
Set
PCOR select
Rst Port Data
PTOR select Pin or
Tgl Output
Pad on
D Register Q package
I/O Clock
PDIR select
Port Data Pin Control
D Input Q Register
Register MUX field
▪ Multiplexer used to increase configurability - what should a pin be connected with internally?
37
CMSIS C Support for PCR
38
CMSIS C Support for PCR
▪ Header file defines pointers to PORT_Type registers
/* PORT - Peripheral instance base addresses */
/** Peripheral PORTA base address */
#define PORTA_BASE (0x40049000u)
/** Peripheral PORTA base pointer */
#define PORTA ((PORT_Type *)PORTA_BASE)
39
Resulting C Code for Clock Control and Mux
40
INTERFACING
Hardware
41
Inputs: What’s a One? A Zero?
42
Outputs: What’s a One? A Zero?
Vout
transistor
▪ Above values only specified when current < 5 mA
Logic 0 out
(18 mA for high-drive pads) and VDD > 2.7 V
Iout
43
Output Example: Driving LEDs
▪ Need to limit current to a value which is safe for both LED
and MCU port driver
▪ Use current-limiting resistor
R = (VDD – VLED)/ILED
ILED = 4 mA
44
Output Example: Driving a Speaker
▪ Create a square wave with a GPIO output
void Beep(void) {
unsigned int period=20000;
while (1) {
PTC->PTOR = MASK(SPKR_POS);
Delay(period/2);
}
}
45
Electrical Characteristic Consideration for Digital I/O Interfacing
Electrical compatibility:
1. Voltage-level compatibility:
The high output level of the driving circuit should be high enough to be considered as a high for the input of the
driven circuit.
The low output level of the driving circuit should be considered as a low for the input of driven circuit.
3. Signal timing:
Signal timing is also an important factor for making sure that the digital circuit functions correctly.
The main concern about timing is whether the signal from one chip becomes valid early enough to be used by
other chips. This is a timing compatibility issue.
When the operating frequency becomes very high, the transmission line effect and ground bounce effect will need to
be considered too.
46
Additional Configuration in PCR
1. CLOCK GATING
▪ Enable the clock to PORTA and PORTB.
3. LOOP
▪ Read switch from PORTA.
▪ If PTA1 is high, set PTB19
else clear PTB19.
▪ Repeat.
48