ARM University Module - Digital Input Output
ARM University Module - Digital Input Output
1
Module Syllabus
GPIO Controller
Digital IO Examples
Using LED
2
Basic Programming – Digital IO
3
Voltages and Logic Values
In digital devices, logic values (‘1’ or ‘0’) are represented as electrical voltages
For example, the external pins of Nucleo F401RE platform use 3.3 volts for logic ‘1’, and 0 volts for logic ‘0’
Digital logic can however have different meanings in different contexts depending on the interpretation
we give to different voltage levels
4
GPIO Design
Normally, the external pins are not directly accessible, but accessed via a peripheral
called General-purpose input/output (GPIO)
Used for general purpose, no special usage defined
Widely used for most applications
The direction of input/ output is controlled by the direction register
A mask register is often used to mask out certain bits
Function Select
5
GPIO Design
To reduce power consumption clocks are turned off for particular areas
Default setting – clock is disabled
To use a peripheral or a resource clock has to be enabled for each item individually
6
GPIO Registers
For example, in the Nucleo F401RE MCU, there are eight GPIO peripherals named from PORTA-PORTH, and each
of them has 10 registers, which include:
Port Mode Register (MODER) - configure the I/O direction mode (input/output/alternate/analog)
Output Type Register (TYPER) – configure the output type of the I/O port (push-pull/open-drain)
Output Speed Register (OSPEEDR) – configure the I/O output speed (2/25/50/100 MHz)
Pull-Up/Pull-Down Register (PUPDR) – configure the I/O pull-up or pull-down (no pull-up, pull-down/pull-up/pull-down)
Input Data Register (IDR) – contain the input value of the corresponding I/O port
Output Data Register (ODR) – can be read and written by software (ODR bits can be individually set and reset by writing to the BSRR)
Bit Set/Reset Register (BSRR) – can be used for atomic bit set/reset
Configuration Lock Register (LCKR) – used to lock the configuration of the port bits when a correct write sequence is applied to bit 16
7
Using Pointer to Access GPIO
Normally, you can use a pointer to directly access a GPIO peripheral in either C or assembly. For example in C, to light an
LED on PB_10:
RCC_AHB1ENR |= 0x02;
GPIOB_MODER |= 0x00100000;
GPIOB_PUPDR |= 0x00200000;
GPIOB_OSPEEDR |= 0x00200000;
GPIOB_ODR |= 0x0400;
This solution is fine for simple applications. However, if multiple instantiations of the same type of peripheral are available at the
same time, we will need to define registers for each peripheral, which makes code maintenance difficult
On the other hand, since each register is defined as a separate pointer, each register access requires a 32-bit address constant.
As a result, the program image will consume a larger memory space.
8
Define Data Structure for Peripherals
To further simplify the code and reduce its length, we can:
Define the peripheral register set as a data structure,
For example:
typedef struct {
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEEDR;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
volatile unsigned int BSRRL;
volatile unsigned int BSRRH;
volatile unsigned int LCKR;
volatile unsigned int AFR[2];
} GPIO_TypeDef;
9
Define Data Structure for Peripherals
10
Define Data Structure for Peripherals
With such arrangement:
The same register data structure for the peripheral can be shared between multiple instantiations
With further modification, the functions developed for one peripheral can be shared between multiple instantiations by
passing the base pointer to the function, for example:
11
Digital IO Example: LEDs
Light-Emitting Diode (LED)
Emits light when switched on
Simplest way to indicate the status of logic
Also widely used in applications such as automotive lighting, general lighting, traffic signals etc.
LED LED
Digital Output
GND
Digital output sources current to LED Digital output sinks current from LED
12
Digital IO Example: 7-Segment Display
Use 7 segments and a dot to display numerals or letters
Widely used in digital electronic devices, such as digital clocks, electronic meters
Simple control, easy to debug
Different values can be represented by different combinations of 8-bit segments (including dot), for example:
Display 0 1 2 3 4
8-bit value 00111111 00000110 01011011 01001111 01100110
Display 5 6 7 8 9
8-bit value 01101101 01111101 00000111 01111111 01101111
13
Digital IO Example: Infrared Emitter/Detector
Infrared emitter (LED)
A light-emitting diode that emits invisible infrared (IR) when conducting e.g. when connected to digital output:
Vcc 3.3V
Digital Output
R
R
14
Useful Resources
Cortex-M4 Technical Reference Manual:
http://infocenter.arm.com/help/topic/com.arm.doc.ddi0439d/DDI0439D_cortex_m4_processor_r0p1_trm.pdf
15