ARM STM32L476 Parallel IO
ARM STM32L476 Parallel IO
STMicroelectronics
STM32L476xx
microcontroller GPIO
Source:
STM32L476xx Datasheet
STM32L476xx
Peripherals
LCD
STM32 microcontrollers
General-Purpose Input/Output (GPIO)
Modules GPIOA, GPIOB, …GPIOH each comprise 16 GPIO pins
# of GPIO ports/pins varies among microcontroller parts
STM32L476VGTx has 82 GPIO pins (only 2 in GPIOH)
Pin options (each pin is configurable via GPIO registers):
Output: push-pull or open-drain+pull-up/pull-down
Selectable output speed
Input: floating, pull-up/pull-down
Analog: input or output
Alternate functions: up to 16 per pin
Data to/from peripherals (Timers, I2C/SPI, USART, USB, etc.)
Digital data input/output via GPIO registers
Input data reg. (IDR) – parallel (16-bit) data from pins
Output data reg. (ODR) – parallel (16-bit) data to pins
Bit set/reset registers (BSRR) for bitwise access to pins
STM32 GPIO pin structure
Analog IO
Alt. Function
LDR
PIN
IDR
BSRR
ODR
STR
Alt. Function
Addressing I/O registers (in C)
(from stm32l476xx.h header file)
#define PERIPH_BASE ((uint32_t)0x40000000) /* Peripheral base address in the alias region */
#define AHB2PERIPH_BASE (PERIPH_BASE + 0x08000000) /* AHB1 bus peripherals */
#define GPIOD_BASE (AHB2PERIPH_BASE + 0x0C00) /* GPIO Port D base address */
#define GPIOD ((GPIO_TypeDef *) GPIOD_BASE) /* GPIO Port D pointer */
/ * General Purpose I/O */
typedef struct /* Treat GPIO register set as a “record” data structure */
{
__IO uint32_t MODER; /* GPIO port mode register, Address offset: 0x00 */
__IO uint32_t OTYPER; /* GPIO port output type register, Address offset: 0x04 */
__IO uint32_t OSPEEDR; /* GPIO port output speed register, Address offset: 0x08 */
__IO uint32_t PUPDR; /* GPIO port pull-up/pull-down register, Address offset: 0x0C */
__IO uint32_t IDR; /* GPIO port input data register, Address offset: 0x10 */
__IO uint32_t ODR; /* GPIO port output data register, Address offset: 0x14 */
__IO uint32_t BSRR; /* GPIO port bit set/reset register, Address offset: 0x18 */
__IO uint32_t LCKR; /* GPIO port configuration lock register, Address offset: 0x1C */
__IO uint32_t AFR[2]; /* GPIO alternate function registers, Address offset: 0x20-0x24 */
} GPIO_TypeDef;
GPIOx->AFR[0] GPIOx->AFR[1]
(low pins 0..7) (high pins 8..15)
GPIO pin option registers
Input
GPIOx_PUPDR – pull-up/down
00 – no pull-up/pull-down
01 – pull-up
10 – pull-down
GPIOx_OTYPER – output type
0 = push/pull, 1 = open drain
(both on) (P-MOS off)
GPIOx_OSPEEDR – output speed
00 – 2 MHz low speed
01 – 25 MHz medium speed
10 – 50 MHz fast speed
11 – 100 MHz high speed (on 30 pf)
Discovery Board connections
User LEDs (positive logic)
LED4 (red) – PB2
LED5 (green) – PE8
Joystick (5 positions – positive logic)
PA0 +3v
c7 c6 c5 c4 c3 c2 c1 c0 value of R1
0 0 0 0 1 1 1 1 0x0F constant
c7 c6 c5 c4 1 1 1 1 result of the ORR
c7 c6 c5 c4 c3 c2 c1 c0 value of R1
1 1 1 1 0 0 0 0 BIC #x0F = AND #0xFFFFFFF0
c7 c6 c5 c4 0 0 0 0 result of the BIC
Assembly:
LDR R0,=GPIOD_ODR
LDRH R1,[R0] ; read port D
EOR R1,R1,#0x80 ; toggle state of pin PD7
STRH R1,[R0] ; update port D
b7 b6 b5 b4 b3 b2 b1 b0 value of R1
1 0 0 0 0 0 0 0 0x80 constant
~b7 b6 b5 b4 b3 b2 b1 b0 result of the EOR
Assembly:
LDR R0,=GPIOD ; GPIOD base address
MOV R1,#0x0400 ; select PD10 in BSSR Low
MOVT R1,#0x0080 ; select PD7 in BSSR High
STR R1,[R0,#BSSR] ; PD10=0 and PD7=1
Software drivers for GPIO pins
Initialize pin mode and options
Force output pin to 0
Force output pin to 1
Toggle state of output pin
Get state of input pin