Ppt#06 GPIO Assembly Programming
Ppt#06 GPIO Assembly Programming
SUBJECT-EMBEDDED SYSTEMS
BATCH-22CS (6TH SEMESTER THIRD YEAR)
GPIO Registers
On-board LED
Toggling LED
2
System Architecture
The main system consists of:
Two masters:
ARM Cortex-M0 core
General-purpose DMA
Four slaves:
Internal SRAM
Internal Flash memory
AHB1 with AHB to APB bridge which connects all the APB
peripherals
AHB2 dedicated to GPIO ports
These are interconnected using a multilayer AHB bus architecture as
shown in Figure 1:
3
System Architecture
4
System Architecture
System bus
This bus connects the system bus of the ARM Cortex-M0 core (peripherals
bus) to a BusMatrix which manages the arbitration between the core and
the DMA.
DMA bus
This bus connects the AHB master interface of the DMA to the BusMatrix
which manages the access of CPU and DMA to SRAM, Flash memory and
peripherals.
BusMatrix
The BusMatrix manages the access arbitration between the core system
bus and the DMA master bus.
AHB to APB bridge (APB)
The AHB to APB bridge provides full synchronous connections between the
AHB and the APB bus.
After each device reset, all peripheral clocks are disabled (except for the
SRAM and Flash). Before using a peripheral you have to enable its clock in
the RCC_AHBENR, RCC_APB2ENR or RCC_APB1ENR register.
5
STM32F0x0 peripheral register boundary addresses
6
General Purpose I/Os (GPIO)
Each general-purpose I/O (GPIO) port has
Four 32-bit configuration registers
GPIOx_MODER
GPIOx_OTYPER
GPIOx_OSPEEDR
GPIOx_PUPDR
Two 32-bit data registers
GPIOx_IDR
GPIOx_ODR
A 32-bit set/reset register
GPIOx_BSRR
Ports A and B also have a 32-bit locking register
GPIOx_LCKR
Two 32-bit alternate function selection registers
GPIOx_AFRH
GPIOx_AFRL.
7
General Purpose I/Os (GPIO)
8
General Purpose I/Os (GPIO) Register
GPIO port mode register (GPIOx_MODER) (x =A..D, F)
Address offset:0x00
Reset values:
0x2800 0000 for port A and
0x0000 0000 for other ports
Bits 2y+1:2y MODERy[1:0]: Port x configuration bits (y = 0..15) These bits are
written by software to configure the I/O mode.
00: Input mode (reset state)
01: General purpose output mode
10: Alternate function mode
11: Analog mode
9
General Purpose I/Os (GPIO) Register
GPIO port input data register (GPIOx_IDR) (x =A..D, F)
Address offset:0x10
Reset values:
0x 0000 XXXX (where X is undefined)
10
General Purpose I/Os (GPIO) Register
GPIO port output data register (GPIOx_ODR) (x =A..D, F)
Address offset:0x14
Reset values:
0x 0000 0000
11
General Purpose I/Os (GPIO) Register
GPIO alternate function Low register (GPIOx_AFRL) (x =A..D)
Address offset:0x20
Reset values:
0x 0000 0000
Bits 31:0 AFSELy[3:0]: Alternate function selection for port x pin y (y = 0..7)
These bits are written by software to configure alternate function I/Os
12
General Purpose I/Os (GPIO) Register
GPIO alternate function High register (GPIOx_AFRH) (x =A..D)
Address offset:0x24
Reset values:
0x 0000 0000
Bits 31:0 AFSELy[3:0]: Alternate function selection for port x pin y (y = 8..15)
These bits are written by software to configure alternate function I/Os
AFSELy selection: for both AFRL and AFRH Register
13
RCC Register Map
The following table gives the RCC register map and the reset values.
14
GPIO Assembly Programming
Example#01: Write an assembly program to turn ON on-board-LED of
stm32f030R8 cortex-M0 ARM (connected to PC9(pin#9 of GPIOC )).
Steps
1. In DATA area declare the variables for BASE address of RCC, and
GPIOs using AHB1 and AHB2 base address from STM32F0x0
peripheral register boundary addresses.
2. In CODE area Enable the clock for GPIOC using RCC_AHBENR Register
(19th bit of the register). Use RCC base address with 0x14 offset value
(RCC_BASE+0x14) .
3. Set the mode in MODER register of PC9(GPIOC,pin#9) as output (01
for output). Use GPIOC base address with 0x00 offset value
(GPIOC_BASE+0x00) .
4. Write HIGH to PC9 in output data register(ODR). Use GPIOC base
address with 0x14 offset value (GPIOC_BASE+0x14) .
15
GPIO Assembly Programming
Example#01: Write an assembly program to turn ON on-board-LED of
stm32f030R8 cortex-M0 ARM (connected to pin#9 of GPIOC ).
Home Work:
1. Write an assembly program to turn ON all bits of PORTB.
17
GPIO Assembly Programming
Example#02: Write an assembly program for stm32f030R8 cortex-M0
ARM to toggle PC8 with a delay of 1sec.
Steps
1. In DATA area declare the variables for BASE address of RCC, and
GPIOs using AHB1 and AHB2 base address from STM32F0x0
peripheral register boundary addresses.
2. In CODE area Enable the clock for GPIOC using RCC_AHBENR Register
(19th bit of the register). Use RCC base address with 0x14 offset value
(RCC_BASE+0x14) .
3. Set the mode in MODER register of PC8(GPIOC,pin#8) as output (01
for output). Use GPIOC base address with 0x00 offset value
(GPIOC_BASE+0x00) .
4. Write HIGH to PC8 in output data register(ODR). Use GPIOC base
address with 0x14 offset value (GPIOC_BASE+0x14) .
5. Call a delay function,
6. write LOW to PC8 in ODR.
7. Call a delay function
8. Repeat step 4 to 7
18
GPIO Assembly Programming
area mydata, DATA
AHB1_BASE EQU 0x40020000
AHB2_BASE EQU 0x48000000
RCC_BASE EQU (AHB1_BASE+0x1000)
GPIOC_BASE EQU (AHB2_BASE+0x800)
23
GPIO Assembly Programming
Example#04: Write an assembly program for stm32f030R8 cortex-M0
ARM for common-anode 7-segment Display connected to GPIOA(PA7-
PA0). Segment patterns for decimal digits are given in following table.
Segment patterns for the 10 decimal digits for a common anode 7-seg LED Display
Decimal PA7 PA6 PA5 PA4 PA3 PA2 PA1 PA0 Hex value
value . g f e d c b a
0 1 1 0 0 0 0 0 0 0xC0
1 1 1 1 1 1 0 0 1 0xF9
2 1 0 1 0 0 1 0 0 0xA4
3 1 0 1 1 0 0 0 0 0xB0
4 1 0 0 1 1 0 0 1 0x99
5 1 0 0 1 0 0 1 0 0x92
6 1 0 0 0 0 0 1 0 0x82
7 1 1 1 1 1 0 0 0 0xF8
8 1 0 0 0 0 0 0 0 0x80
9 1 0 0 1 0 0 0 0 0x90
24
GPIO Assembly Programming
area mydata, DATA
AHB1_BASE EQU 0x40020000
AHB2_BASE EQU 0x48000000
RCC_BASE EQU (AHB1_BASE+0x1000)
GPIOA_BASE EQU (AHB2_BASE+0x00)
GPIOB_BASE EQU (AHB2_BASE+0x400)
GPIOC_BASE EQU (AHB2_BASE+0x800)
HOME WORK:
3.Write an assembly program for stm32f030R8 cortex-M0 ARM for common-
cathode 7-segment Display.
30
Thanks
31