07GPIO
07GPIO
1
Overview
n How do we make a program light up LEDs in
response to a switch?
n GPIO
n Basic Concepts
n Port Circuitry
n Control Registers
n Accessing Hardware Registers in C
n Clocking and Muxing
n Circuit Interfacing
n Inputs
n Outputs
n Additional Port Configuration
Basic Concepts
n Quantity depends on
package pin count
GPIO Port Bit Circuitry in MCU
n Direction Address
Decoder
n MUX PDDR select
Output (different
D Direction Q
n Register
I/O Clock
PDIR select
Port Data Pin Control
D Input Q Register
Register MUX field
Control Registers
differently
Port Data
n Input: 0 D Direction
Register
Q
bit direction to 0
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
Writing Output Port Data
Data Bus Address
PDOR Address
Decoder
PDDR select
PDOR select
PDOR select
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
Pseudocode for Program
// Make PTA1 and PTA2 outputs
set bits 1 and 2 of GPIOA_PDDR
// Make PTA5 input
clear bit 5 of GPIOA_PDDR
// Initialize the output data values: LED 1 off, LED 2 on
clear bit 1, set bit 2 of GPIOA_PDOR
PTA->PDOR = …
Coding Style and Bit Access
n Easy to make mistakes dealing with literal binary and
hexadecimal values
n To set bits 13 and 19, use “0000 0000 0000 1000 0010 0000 0000 0000”
or “0x00082000”
n Set in n all the bits which are one in mask, leaving others
unchanged
n |= MASK(foo);
n Clear in n all the bits which are zero in mask, leaving others unchanged
n &= MASK(foo);
C Code
#define LED1_POS (1)
#define LED2_POS (2)
#define SW1_POS (5)
#define MASK(x) (1UL << (x))
while (1) {
if (PTA->PDIR & MASK(SW1_POS)) {
// switch is not pressed, then light LED 2
PTA->PDOR = MASK(LED2_POS);
} else {
// switch is pressed, so light LED 1
PTA->PDOR = MASK(LED1_POS);
}
}
Clocking Logic
Bit Port
13 PORTE
12 PORTD
11 PORTC
10 PORTB
9 PORTA
n Need to enable clock to GPIO module
n By default, GPIO modules are disabled to save power
n Writing to an unclocked module triggers a hardware fault!
n Control register SIM_SCGC5 gates clocks to GPIO ports
n Enable clock to Port A
SIM->SCGC5 |= (1UL << 9);
n Header file MKL25Z4.h has definitions
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
Register
Freescale: is the pin
mux location in this
PDOR select diagram accurate?
Connecting a GPIO Signal to a Pin
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
INTERFACING
Inputs: What’s a One? A Zero?
n Input signal’s value is
determined by voltage
Vout
n
Iout
Output Example: Driving LEDs
n Need to limit current to a value which
is safe for both LED and MCU port
driver
n Use current-limiting resistor
n R = (VDD – VLED)/ILED
n Set ILED = 4 mA
n VLED depends on type of LED (mainly
color)
n Red: ~1.8V
n Blue: ~2.7 V
void Beep(void) {
unsigned int period=20000;
while (1) {
PTC->PTOR = MASK(SPKR_POS);
Delay(period/2);
}
}
Additional Configuration in PCR