Module 2 C ARMKortex - GPIO
Module 2 C ARMKortex - GPIO
1
Overview
How do we make a program light up LEDs in response to a switch?
GPIO
Basic Concepts
Port Circuitry
Control Registers
Accessing Hardware Registers in C
Clocking and Muxing
Circuit Interfacing
Inputs
Outputs
3
KL25Z GPIO
Ports
Quantity depends on
package pin count
4
Freedom KL25Z
5
Memory Map
6
7
Ports
clear
8
Clocking Logic
Bit Port
Need to enable clock to GPIO module 13 PORTE
By default, GPIO modules are disabled to save power 12 PORTD
Writing to an unclocked module triggers a hardware fault! 11 PORTC
Control register SIM_SCGC5 gates clocks to GPIO ports 10 PORTB
9 PORTA
SIM_SCGC5 (System Clock Gating Control Register 5)
Enable clock to Port A
SIM->SCGC5 |= (1UL << 9);
Header file MKL25Z4.h has definitions
SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK;
9
Port Data
D Direction Q
Register
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
10
Pin Control Register
11
Structure Declarations
Like other elements of C programming, the structure must be declared before it can be used.
The declaration specifies the tagname of the structure and the names and types of the individual
members. The following example has three members: one 8-bit integer and two word pointers
struct theport{
unsigned char mask; // defines which bits are active
unsigned long volatile *addr; // pointer to its address
unsigned long volatile *ddr;}; // pointer to its direction reg
The above declaration does not create any variables or allocate any space. Therefore to use a
structure we must define a global or local variable of this type. The tagname (theport) along with
the keyword struct can be used to define variables of this new data type:
struct theport PortA,PortB,PortE;
The above line defines the three variables and allocates 9 bytes for each of variable. Because
the pointers will be 32-bit aligned the compiler will skip three bytes between mask and addr, so
each object will occupy 12 bytes. If you knew you needed just three copies of structures of this
type, you could have defined them as
struct theport{
unsigned char mask; // defines which bits are active
unsigned long volatile *addr;
12 unsigned long volatile *ddr;}PortA,PortB,PortE;
Continued
Definitions like the above are hard to extend, so to improve code reuse we can
use typedef to actually create a new data type (called port in the example below)
that behaves syntactically like char int short etc.
struct theport{
unsigned char mask; // defines which bits are active
unsigned long volatile *addr; // address
unsigned long volatile *ddr;}; // direction reg
typedef struct theport port_t;
port_t PortA,PortB,PortE;
Once we have used typedef to create port_t, we don't need access to the name
theport anymore. Consequently, some programmers use to following short-cut:
typedef struct {
unsigned char mask; // defines which bits are active
unsigned long volatile *addr; // address
unsigned long volatile *ddr;}port_t; // direction reg
port_t PortA,PortB,PortE;
13
CMSIS C Support for PCR
CMSIS: Common Microcontroller Software Interface Standard
14
CMSIS C Support for PCR
Header file defines pointers to PORT_Type registers
15
GPIO Port Bit Circuitry in MCU
Control Data Bus
bit n
Address
Bus
Direction
MUX Address
Decoder
PDDR select
I/O Clock
PDIR select
Port Data Pin Control
D Input Q Register
Register MUX field
16
Control Registers
17
PDDR: Port Data Direction Address
Data Bus
Output: 1
Port Data
Reset clears port bit D Direction
Register
Q
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
18
Writing Output Port Data Address
Data Bus
bit n Bus
Direct: write value to PDOR
Toggle: write 1 to PTOR Address
Decoder
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
19
Reading Input Port Data Address
Data Bus
Port Data
D Direction Q
Register
PDOR select
PSOR select
Set
PCOR select
Rst Port Data
PTOR select Pin or
Tgl Output
Register Pad on
D Q package
I/O Clock
PDIR select
Port Data Pin Control
D Input Q Register
Register MUX field
20
PTA2
Pseudocode for Program PTA5
// Make PTA1 and PTA2 outputs PTA1
set bits 1 and 2 of GPIOA_PDDR
// Make PTA5 input
clear bit 5 of GPIOA_PDDR
// Initialize the output data values: LED 2 off, LED 2 on
clear bit 1, set bit 2 of GPIOA_PDOR
// read switch, light LED accordingly
do forever {
if bit 5 of GPIOA_PDIR is 1 {
// switch is not pressed, then light LED 2
set bit 2 of GPIOA_PDOR
clear bit 1 of GPIO_PDOR
} else {
// switch is pressed, so light LED 1
set bit 1 of GPIOA_PDOR
clear bit 2 of GPIO_PDOR
}
}
21
CMSIS - Accessing Hardware Registers in C
Header file MKL25Z4.h defines C data structure types to represent hardware registers
in MCU with CMSIS-Core hardware abstraction layer
22
Accessing Hardware Registers in C (2)
PTA->PDOR = …
23
Coding Style and Bit Access
Easy to make mistakes dealing with literal binary and hexadecimal values
“To set bits 13 and 19, use 0000 0000 0000 1000 0010 0000 0000 0000 or 0x00082000”
24
Using Masks
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 one in mask, leaving others unchanged
n &= ~MASK(foo);
25
Resulting C Code for Clock Control and Mux
26
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);
}
}
27
Inputs and Outputs, Ones and Zeros, Voltages and Currents
INTERFACING
28
Inputs: What’s a One? A Zero?
Input signal’s value is determined
by voltage
29
Outputs: What’s a One? A Zero?
Nominal output voltages
1: VDD-0.5 V to VDD
0: 0 to 0.5 V
Note: Output voltage depends on current drawn
by load on pin
Need to consider source-to-drain resistance in the
transistor
Above values only specified when current < 5 mA
(18 mA for high-drive pads) and VDD > 2.7 V Logic 1 out
As with many other low-power ARM chips, the
KL25Z can only provide (source) or sink a very
Vout
limited amount of current: up to 5 mA per pin and
no more than 100mA across all pins at a time
If you source or sink more current than 5mA Logic 0 out
continuously or 20mA instantaneously, you will
damage the board.
Iout
30
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
Set ILED = 4 mA
VLED depends on type of LED (mainly color)
Red: ~1.8V
Blue: ~2.7 V
Solve for R given VDD = ~3.0 V
Red: 300 W
Blue: 75 W
Demonstration code in Basic Light Switching
Example
31
Output Example: Driving a Speaker
Create a square wave with a GPIO output
Use capacitor to block DC value
Use resistor to reduce volume if needed
Write to port toggle output register (PTOR) to
simplify code
void Beep(void) {
unsigned int period=20000;
while (1) {
PTC->PTOR = MASK(SPKR_POS);
Delay(period/2);
}
}
32
Additional Configuration in PCR
33
Connecting External Switches
34
LCD Interfacing
35
Code (Hex) Command to LCD Instruction Register
Commands 1 Clear display screen
2 Return cursor home
6 Increment cursor (shift cursor to right)
F Display on, cursor blinking
80 Force cursor to beginning of 1st line
C0 Force cursor to beginning of 2nd line
38 2 lines and 5x7 character (8-bit data, D0 to D7)
28 2 lines and 5x7 character (4-bit data, D4 to D7)
80 81 82 83 84 8F 80 81 82 83 84 93
C0 C1 C2 C3 C4 CF C0 C1 C2 C3 C4 D3
94 95 96 97 98 A7
D4 D5 D6 D7 D8 E7
20x1 LCD
80 81 82 83 84 93
80 81 82 83 84 93 80 81 82 83 84 A7
C0 C1 C2 C3 C4 D3 C0 C1 C2 C3 C4 E7
36
Operation
VCC, VSS, and VEE: While VCC and VSS provide +5V power supply and ground, respectively,
VEE is used for controlling the LCD contrast.
RS (Register Select): There are two registers inside the LCD and the RS pin is used for their
selection as follows. If RS = 0, the instruction command code register is selected, allowing the user
to send a command such as clear display, cursor at home, and so on (or query the busy status bit
of the controller). If RS =1, the data register is selected, allowing the user to send data to be
displayed on the LCD (or to retrieve data from the LCD controller).
R/W (Read/Write): R/W input allows the user to write information into the LCD controller or read
information from it. R/W = 1 when reading and R/W = 0 when writing.
E (Enable): The enable pin is used by the LCD to latch information presented to its data pins.
When data is supplied to data pins, a pulse (Low-to-High-to-Low) must be applied to this pin in
order for the LCD to latch in the data present at the data pins. This pulse must be a minimum of
230 ns wide, according to Hitachi datasheet.
D0–D7: The 8-bit data pins are used to send information to the LCD or read the contents of the
LCD’s internal registers.
37
Timing
38
LCD Operation Contd
We can monitor the busy flag and issue data when it is ready.
To check the busy flag, we must read the command register (R/W = 1, RS = 0).
The busy flag is the D7 bit of that register.
Therefore, if R/W = 1, RS = 0.
When D7 = 1 (busy flag = 1), the LCD is busy taking care of internal operations
and will not accept any new information. When D7 = 0, the LCD is ready to
receive new information.
39
Keypad
To detect the key pressed, the microprocessor drives
all rows low.
Then, it reads the columns.
If the data read from the columns is D7–D4 = 1111, no
key has been pressed and the process continues until
a key press is detected.
However, if one of the column bits has a zero, this
means that a key was pressed.
Starting from the top row, the microprocessor drives
one row low at a time; then it reads the columns.
If the data read is all 1s, no key in that row is pressed
and the process is moved to the next row.
This process continues until a row is identified with a
zero in one of the columns.
40