TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
Learn How to Develop Embedded System for ARM @ 2014.12.22 JuluOSDev
1. Learn How to Develop
embedded system for ARM
GPIO of STM32F4-Discovery for example
StarNight @ 2014.12.22 JuluOSDev
2. Who am I?
潘建宏 / Jian-Hong Pan (StarNight)
About Me : http://about.me/StarNight
出沒在~
GitHub : starnight
PTT : zack2004
plurk : StarNight
Facebook : Jian-Hong Pan
目前繼續在種花店當個打雜園丁 ~
3. Outline
1. History
2. Before we start ...
3. Reference previous code
4. Makefile
5. Start tracing
6. Header files
7. Source file
8. GPIO of STM32F4
9. Do the porting with original STM32F4 library
4. History
1. I am a newer who want to program and flash
ARM chip.
2. I have attended some groups that share
ARM embedded system related. However,
ones should have some basic knowledge or
technique (including APIs) to follow
speakers.
3. For advanced usage, I spent time to enrich
related basic knowledge.
4. To reduce the gap, I want to share the
processes / courses that I have taken.
5. Before we start ...
1. The toolchain: GNU Tools for ARM
Embedded Processors
2. STLink: https://github.com/texane/stlink
You may use the pre-compiled packages mentioned
above for your OS.
3. A development board, STM32F4-Discovery
for example.
Reference:
F9 Microkernel code reading - part 1 P.23 ~
STM32F4+F9 新手無痛開發入門指南
6. Reference previous code
Hellow world of embeded system:
Blinking LED
https://github.
com/Malkavian/tuts/tree/master/stm/blinky
7. Libraries
1. STM32F4-Discovery Library:
a. http://www.st.
com/web/catalog/tools/FM116/SC959/SS1532/
PF252419
2. STM32F4 DSP and standard peripherals
library:
a. http://www.st.
com/web/catalog/tools/FM147/CL1794/SC961/
SS1743/PF257901
8. Readme file of blinky
https://github.com/Malkavian/tuts/tree/
master/stm/blinky
1. Files:
a. https://github.com/Malkavian/tuts/tree/master/
stm/blinky#files
2. Missing files
a. https://github.com/Malkavian/tuts/tree/master/
stm/blinky#missing-files
9. What does blinky do?
Start
Setup LEDs
Turn LEDs
on/off in order
Flash all LEDs
Initial the GPIO of the
output pins connected
to LEDs.
main
function
Make LEDs on
STM32F4-Discovery
blink and flash.
10. Makefile of blinky
1. PROJ_NAME:
a. This project's name which will be the binary file's
name.
2. STM_DIR:
a. The STM firmware library package directoy in this
computer.
3. STM_SRC:
a. The STM firmware library package sources directoy
in this computer.
11. Makefile of blinky (Cont.)
4. vpath:
a. The virtual path of other source files, except the
current directory.
5. SRCS:
a. The source files in current directory, the source files
in vpath and the source files with user defined path.
Also, they are the files going to be compiled.
6. INC_DIRS:
a. The located pathes of STM header files.
7. TOOLS_DIR:
a. The path of GNU ARM toolchain.
12. Questions:
Where is the definition of GPIO API?
Does the main function is the start
point of the MCU after be powered
on?
15. Included header in main.c
/* stm32f4_discovery.h is located in
* Utilities/STM32F4-Discovery
* and defines the GPIO Pins where the leds are
connected.
* Including this header also includes stm32f4xx.h and
* stm32f4xx_conf.h, which includes stm32f4xx_gpio.h
*/
#include "stm32f4_discovery.h"
…
int main(void) {
...
16. stm32f4_discovery.h
1. Included in main.c
2. Located at $(STM_DIR)/Utilities/STM32F4-
Discovery/stm32f4_discovery.h
3. Includes stm32f4xx.h
4. Defines the devices, terminals and actions
belong to STM32F4-Discovery board.
17. stm32f4xx.h
1. Included in ./stm32f4_discovery.h
2. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Include/stm32f4xx.h
3. Also located at STM32F4xx_DSP_StdPeriph_Lib_V1.
3.0
/Libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f
4xx.h
4. Includes stm32f4xx_conf.h if defined
USE_STDPERIPH_DRIVER, core_cm4.h and
system_stm32f4xx.h
5. Defines the STM32F4xx general APIs
18. CMSIS
The ARM® Cortex® Microcontroller Software Interface
Standard (CMSIS) is a vendor-independent hardware
abstraction layer for the Cortex-M processor series and
specifies debugger interfaces.
Creation of software is a major cost factor in the embedded
industry. By standardizing the software interfaces across all
Cortex-M silicon vendor products, especially when creating
new projects or migrating existing software to a new device,
means significant cost reductions.
Reference: http://www.arm.
com/products/processors/cortex-m/cortex-microcontroller-
software-interface-standard.php
20. stm32f4xx_conf.h
1. Included in stm32f4xx.h
2. Located at ./stm32f4xx_conf.h
3. Includes the stm32f4xx related peripheral
headers
4. Enable and choose the peripheral headers
which going to be used
21. core_cm4.h
1. Included in stm32f4xx.h
2. Located at $(STM_DIR)
/Libraries/CMSIS/Include/core_cm4.h
3. Defines the CMSIS Cortex-M4 Core
Peripheral Access Layer
22. system_stm32fxx.h
1. Included in stm32f4xx.h
2. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Include/sy
stem_stm32f4xx.h
3. Also located at
STM32F4xx_DSP_StdPeriph_Lib_V1.3.0
/Libraries/CMSIS/Device/ST/STM32F4xx/Incl
ude/system_stm32f4xx.h
4. Defines the CMSIS Cortex-M4 Device
System APIs
23. stm32f4xx_gpio.h
1. Include in stm32f4xx_conf.h
2. Located at $(STM_DIR)
/Libraries/STM32F4xx_StdPeriph_Driver/inc/
stm32f4xx_gpio.h
3. Includes stm32f4xx.h
4. Defines the STM32F4xx APIs of gerneral
purpose input/output peripheral
24. stm32f4xx_rcc.h
1. Include in stm32f4xx_conf.h
2. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Include/st
m32f4xx_rcc.h
3. Includes stm32f4xx.h
4. Defines the STM32F4xx APIs of reset and
clock control peripheral
26. system_stm32f4xx.c
1. Located at ./system_stm32f4xx.c
2. Also located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/sy
stem_stm32f4xx.c
3. Also located at STM32F4xx_DSP_StdPeriph_Lib_V1.
3.0
/Libraries/CMSIS/Device/ST/STM32F4xx/Source/Templ
ates/system_stm32f4xx.c
4. Initialization code writen by ST which contains the
system clock configuration for STM32F4xx devices.
28. Is the main function the
start point of the MCU
after it is powered on?
29. Linker file: stm32_flash.ld
1. ENTRY(Reset_Handler)
2. Reset_Handler is defined in
startup_stm32f4xx.s
3. Branch to the main function when call the
application's entry point.
/* Call the application's entry point.*/
bl main
30. startup_stm32f4xx.s
1. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/Tr
ueSTUDIO/startup_stm32f4xx.s
2. Also loacted a STM32F4xx_DSP_StdPeriph_Lib_V1.3.0
/Libraries/CMSIS/Device/ST/STM32F4xx/Source/Templ
ates/TrueSTUDIO/startup_stm32f40_41xxx.s
3. The assembly code in this file is the first one to be
executed
31. After Reset, the MCU does
1. Copy the data segment
initializers from flash to
SRAM
2. Zero fill the bss segment
3. Call the clock system
intitialization function
4. Call static constructors
5. Call the application's
entry point (main
function)
Reset
Flash to SRAM
Zero bss segment
Initial clock system
Static constructor
Application’s entry point
32. Example of GPIO in main.c
1. int main(void) (The application's entry point.)
1. Call setup_leds function to intial GPIO
pins.
2. A forever while loop to have fancy lighting.
a. Call led_round function.
b. Call flash_all_leds function.
33. Example of GPIO in main.c (Cont.)
1. static void setup_leds(void)
a. Initial the GPIO of output pins for controlling the
LEDs.
2. static void led_round(void)
a. On-Off the output pins to make the LEDs be turned
on and off in order.
3. static void flash_all_leds(void)
a. On-Off the output pins to make all LEDs flashing.
4. static void delay(__IO unit32_t nCount)
a. hard code delay
b. __IO defined in $(STM_DIR)
/Libraries/CMSIS/Include/core_cm4.h
34. GPIO of STM32F4
1. GPIO function diagram
2. Input configuration
3. Output configuration
Referenced from:
RM0090 Reference manual STM32F405xx/07xx,
STM32F415xx/17xx, STM32F42xxx and STM32F43xxx
advanced ARM ® -based 32-bit MCUs, P. 266, 273 ~ 275
37. GPIO Input configuration
1. The output buffer is disabled.
2. The Schmitt trigger input is activated.
3. The pull-up and pull-down resistors are
activated depending on the value in the
GPIOx_PUPDR register.
4. The data present on the I/O pin are sampled
into the input data register every AHB1 clock
cycle.
5. A read access to the input data register
provides the I/O State.
39. GPIO Output configuration
1. The output buffer is enabled:
a. Open drain mode: A “0” in the Output register
activates the N-MOS whereas a “1” in the Output
register leaves the port in Hi-Z (the P-MOS is never
activated)
b. Push-pull mode: A “0” in the Output register
activates the N-MOS whereas a “1” in the Output
register activates the P-MOS
2. The Schmitt trigger input is activated.
40. GPIO Output configuration (Cont.)
3. The weak pull-up and pull-down resistors are
activated or not depending on the value in
the GPIOx_PUPDR register.
4. The data present on the I/O pin are sampled
into the input data register every AHB1 clock
cycle.
5. A read access to the input data register gets
the I/O state.
6. A read access to the output data register
gets the last written value.
42. GPIO port mode register
(GPIOx_MODER) (x = A..I/J/K)
Bits 2y:2y+1 MODERy[1:0]:
Port x configuration bits (y = 0..15)
These bits are written by software to configure the I/O
direction mode.
00: Input (reset state)
01: General purpose output mode
10: Alternate function mode
11: Analog mode
43. GPIO port output type register
(GPIOx_OTYPER) (x = A..I/J/K)
Bits 31:16 Reserved, must be kept at reset value.
Bits 15:0 OTy:
Port x configuration bits (y = 0..15)
These bits are written by software to configure the
output type of the I/O port.
0: Output push-pull (reset state)
1: Output open-drain
44. GPIO port output speed register
(GPIOx_OSPEEDR) (x = A..I/J/K)
Bits 2y:2y+1 OSPEEDRy[1:0]:
Port x configuration bits (y = 0..15)
These bits are written by software to configure the I/O
output speed.
00: Low speed
01: Medium speed
10: Fast speed
11: High speed
45. GPIO port pull-up/pull-down register
(GPIOx_PUPDR) (x = A..I/J/K)
Bits 2y:2y+1 PUPDRy[1:0]:
Port x configuration bits (y = 0..15)
These bits are written by software to configure the I/O
pull-up or pull-down
00: No pull-up, pull-down
01: Pull-up
10: Pull-down
11: Reserved
46. GPIO port input data register
(GPIOx_IDR) (x = A..I/J/K)
Bits 31:16 Reserved, must be kept at reset value.
Bits 15:0 IDRy:
Port input data (y = 0..15)
These bits are read-only and can be accessed in word
mode only. They contain the input value of the
corresponding I/O port.
47. GPIO port output data register
(GPIOx_ODR) (x = A..I/J/K)
Bits 31:16 Reserved, must be kept at reset value.
Bits 15:0 ODRy:
Port output data (y = 0..15)
These bits can be read and written by software.
Note: For atomic bit set/reset, the ODR bits can be
individually set and reset by writing to the GPIOx_BSRR
register (x = A..I/J/K).
48. GPIO port bit set/reset register
(GPIOx_BSRR) (x = A..I/J/K)
Bits 31:16 BRy: Port x reset bit y (y = 0..15)
These bits are write-only and can be accessed in word, half-word or byte
mode. A read to these bits returns the value 0x0000.
0: No action on the corresponding ODRx bit
1: Resets the corresponding ODRx bit
Note: If both BSx and BRx are set, BSx has priority.
Bits 15:0 BSy: Port x set bit y (y= 0..15)
These bits are write-only and can be accessed in word, half-word or byte
mode. A read to these bits returns the value 0x0000.
0: No action on the corresponding ODRx bit
1: Sets the corresponding ODRx bit
49. GPIO port configuration lock register
(GPIOx_LCKR) (x = A..I/J/K)
Bits 31:17 Reserved, must be kept at reset value.
Bit 16 LCKK[16]: Lock key
This bit can be read any time. It can only be modified using the lock key
write sequence.
0: Port configuration lock key not active
1: Port configuration lock key active. The GPIOx_LCKR register is locked
until an MCU reset occurs.
Bits 15:0 LCKy: Port x lock bit y (y= 0..15)
These bits are read/write but can only be written when the LCKK bit is 0.
0: Port configuration not locked
1: Port configuration locked
50. GPIO alternate function low register
(GPIOx_AFRL) (x = A..I/J/K)
Bits 31:0 AFRLy:
Alternate function selection for port x bit y (y = 0..7) →
for low 8 pins of the port.
These bits are written by software to configure alternate
function I/Os
AFRLy selection: 0000 ~ 1111 → AF0 ~ AF15
51. GPIO alternate function high register
(GPIOx_AFRH) (x = A..I/J/K)
Bits 31:0 AFRHy:
Alternate function selection for port x bit y (y = 8..15) →
for high 8 pins of the port.
These bits are written by software to configure alternate
function I/Os
AFRHy selection: 0000 ~ 1111 → AF0 ~ AF15
52. For pins 8 to 15, the GPIOx_AFRH
[31:0] register selects the dedicated
alternate function
Alternate function on STM32F407xx
For pins 0 to 7, the GPIOx_AFRL
[31:0] register selects the dedicated
alternate function
Referenced from: RM0090 Reference manual STM32F405xx/07xx,
STM32F415xx/17xx, STM32F42xxx and STM32F43xxx advanced ARM ® -
based 32-bit MCUs, Figure 26. Selecting an alternate function on
STM32F405xx/07xx and STM32F415xx/17xx, P. 270
53. Let’s Trace main.c Deeply
Defined macroes
Initial GPIO as Output
Set / Reset Output
https://github.
com/Malkavian/tuts/blob/master/stm/blinky/main.c
55. I want to practice.
The library is for STM32F4-Dicovery
board only. However, there will be a
lot of differece between STM32F4-
Discovery and the board designed by
oneself.
Why do port with original
standard STM43F4 library
56. Referenced from: STM32F4DISCOVERY Peripherals, Rev: B.2(PCB.SCH),
1/9/2012, Sheet 6 of 6
Output LEDs on Schematic of STM32F4-Discovery
57. Referenced from: STM32F4DISCOVERY Peripherals, Rev: B.2(PCB.SCH),
1/9/2012, Sheet 6 of 6
User Button on Schematic of STM32F4-Discovery
58. Clicked
What does practice do?
Start
Setup LEDs &
User Button
Flash all LEDs
Turn on/off
LEDs in order
Check
User
Button
Not
Clicked
59. 1. Codes:
a. https://github.
com/starnight/STM32F4/tree/master/GPIO
2. Reference:
a. STM32F4-Discovery schematics:
http://www.st.
com/web/catalog/tools/FM116/SC959/SS1532/PF25
2419
b. devthrash/STM32F4-examples:
https://github.com/devthrash/STM32F4-
examples/blob/master/GPIO/main.c
Programming and Using STD Library
60. Reference
1. Blink for stm32f4-discovery board on Linux with Makefile
http://liviube.wordpress.com/2013/04/22/blink-for-stm32f4-discovery-board-
on-linux-with-makefile/
2. STM32F4 GPIO tutorial
http://eliaselectronics.com/stm32f4-tutorials/stm32f4-gpio-tutorial/
3. blinky
https://github.com/Malkavian/tuts/blob/master/stm/blinky
4. Setting up the ARM toolchain
http://eliaselectronics.com/stm32f4-tutorials/setting-up-the-stm32f4-arm-
development-toolchain/
61. Reference (Cont.)
5. STM32F4 Library
http://www.emcu.it/STM32F4xx/STM32F4-Library/STM32F4-Library.html
6. STM32F4-Discovery Library
http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF252419
7. STM32F4 GPIO Configuration
http://patrickleyman.be/blog/stm32f4-gpio-
configuration/devthrash/STM32F4-examples
https://github.com/devthrash/STM32F4-examples
8. Using I/O ports on the STM32 F4 Discovery
http://www.rapitasystems.com/blog/using-io-ports-on-the-stm32-f4-
discovery