learn how to develop embedded system for arm @ 2014.12.22 juluosdev
Post on 12-Jul-2015
1.725 Views
Preview:
TRANSCRIPT
Learn How to Develop embedded system for ARM
GPIO of STM32F4-Discovery for example
StarNight @ 2014.12.22 JuluOSDev
Who am I?
潘建宏 / Jian-Hong Pan (StarNight)About Me : http://about.me/StarNight
出沒在~
GitHub : starnightPTT : zack2004plurk : StarNightFacebook : Jian-Hong Pan
目前繼續在種花店當個打雜園丁 ~
Outline
1. History2. Before we start ...3. Reference previous code4. Makefile5. Start tracing6. Header files7. Source file8. GPIO of STM32F49. Do the porting with original STM32F4 library
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.
Before we start ...
1. The toolchain: GNU Tools for ARM Embedded Processors
2. STLink: https://github.com/texane/stlinkYou 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 新手無痛開發入門指南
Reference previous code
Hellow world of embeded system:
Blinking LEDhttps://github.
com/Malkavian/tuts/tree/master/stm/blinky
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
Readme file of blinky
https://github.com/Malkavian/tuts/tree/master/stm/blinky1. Files:
a. https://github.com/Malkavian/tuts/tree/master/stm/blinky#files
2. Missing filesa. https://github.com/Malkavian/tuts/tree/master/
stm/blinky#missing-files
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.
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.
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.
Questions:Where is the definition of GPIO API?
Does the main function is the start point of the MCU after be powered
on?
Where is the definition of GPIO API?
Referenced from: STM32F405xx/STM32F407xx Datasheet, Figure 5. STM32F40x block diagram, P.18
GPIO of STM32F40x block diagram
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) {...
stm32f4_discovery.h
1. Included in main.c2. Located at $(STM_DIR)/Utilities/STM32F4-
Discovery/stm32f4_discovery.h3. Includes stm32f4xx.h4. Defines the devices, terminals and actions
belong to STM32F4-Discovery board.
stm32f4xx.h
1. Included in ./stm32f4_discovery.h2. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Include/stm32f4xx.h3. Also located at STM32F4xx_DSP_StdPeriph_Lib_V1.
3.0/Libraries/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h
4. Includes stm32f4xx_conf.h if defined USE_STDPERIPH_DRIVER, core_cm4.h and system_stm32f4xx.h
5. Defines the STM32F4xx general APIs
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
CMSIS consists
1. CMSIS-CORE2. CMSIS-Driver3. CMSIS-DSP4. CMSIS-RTOS API5. CMSIS-Pack6. CMSIS-SVD7. CMSIS-DAPReference: http://www.arm.com/products/processors/cortex-m/cortex-microcontroller-software-interface-standard.php
stm32f4xx_conf.h
1. Included in stm32f4xx.h2. Located at ./stm32f4xx_conf.h3. Includes the stm32f4xx related peripheral
headers4. Enable and choose the peripheral headers
which going to be used
core_cm4.h
1. Included in stm32f4xx.h2. Located at $(STM_DIR)
/Libraries/CMSIS/Include/core_cm4.h3. Defines the CMSIS Cortex-M4 Core
Peripheral Access Layer
system_stm32fxx.h
1. Included in stm32f4xx.h2. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Include/system_stm32f4xx.h
3. Also located at STM32F4xx_DSP_StdPeriph_Lib_V1.3.0/Libraries/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h
4. Defines the CMSIS Cortex-M4 Device System APIs
stm32f4xx_gpio.h
1. Include in stm32f4xx_conf.h2. Located at $(STM_DIR)
/Libraries/STM32F4xx_StdPeriph_Driver/inc/stm32f4xx_gpio.h
3. Includes stm32f4xx.h4. Defines the STM32F4xx APIs of gerneral
purpose input/output peripheral
stm32f4xx_rcc.h
1. Include in stm32f4xx_conf.h2. Located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Include/stm32f4xx_rcc.h
3. Includes stm32f4xx.h4. Defines the STM32F4xx APIs of reset and
clock control peripheral
main.c
1. Located at ./main.c2. The main source file of the project.
system_stm32f4xx.c
1. Located at ./system_stm32f4xx.c2. Also located at $(STM_DIR)
/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
3. Also located at STM32F4xx_DSP_StdPeriph_Lib_V1.3.0/Libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c
4. Initialization code writen by ST which contains the system clock configuration for STM32F4xx devices.
stm32f4xx_rcc.c, stm32f4xx_gpio.c
1. Located at $(STM_DIR)/Libraries/STM32F4xx_StdPeriph_Driver/src/…
2. Implement the peripheral functions
Is the main function the start point of the MCU after it is powered on?
Linker file: stm32_flash.ld
1. ENTRY(Reset_Handler)2. Reset_Handler is defined in
startup_stm32f4xx.s3. Branch to the main function when call the
application's entry point./* Call the application's entry point.*/bl main
startup_stm32f4xx.s
1. Located at $(STM_DIR)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f4xx.s
2. Also loacted a STM32F4xx_DSP_StdPeriph_Lib_V1.3.0/Libraries/CMSIS/Device/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f40_41xxx.s
3. The assembly code in this file is the first one to be executed
After Reset, the MCU does
1. Copy the data segment initializers from flash to SRAM
2. Zero fill the bss segment3. Call the clock system
intitialization function4. Call static constructors5. Call the application's
entry point (main function)
Reset
Flash to SRAM
Zero bss segment
Initial clock system
Static constructor
Application’s entry point
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.
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 delayb. __IO defined in $(STM_DIR)
/Libraries/CMSIS/Include/core_cm4.h
GPIO of STM32F4
1. GPIO function diagram2. Input configuration3. Output configuration
Referenced from:RM0090 Reference manual STM32F405xx/07xx, STM32F415xx/17xx, STM32F42xxx and STM32F43xxx advanced ARM ® -based 32-bit MCUs, P. 266, 273 ~ 275
1
2 3
45 AHB1 clock
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.
1
23
4AHB1 clock5
6
1.a/b
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.
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.
Data registers● GPIOx_IDR● GPIOx_ODR● GPIOx_BSRR
(x = A..I/J/K)
GPIO registers
Configuration registers● GPIOx_MODER● GPIOx_OTYPER● GPIOx_OSPEEDR● GPIOx_PUPDR● GPIOx_LCKR● GPIOx_AFRL/AFRH
Referenced from:RM0090 Reference manual STM32F405xx/07xx, STM32F415xx/17xx, STM32F42xxx and STM32F43xxx advanced ARM ® -based 32-bit MCUs, P. 278 ~ 283
成大資工Wiki GPIO 暫存器 Overview
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 mode10: Alternate function mode11: Analog mode
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
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 speed01: Medium speed10: Fast speed11: High speed
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-down00: No pull-up, pull-down01: Pull-up10: Pull-down11: Reserved
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.
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).
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 bit1: Resets the corresponding ODRx bitNote: 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 bit1: Sets the corresponding ODRx bit
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 keyThis bit can be read any time. It can only be modified using the lock key write sequence.0: Port configuration lock key not active1: 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 locked1: Port configuration locked
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/OsAFRLy selection: 0000 ~ 1111 → AF0 ~ AF15
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/OsAFRHy selection: 0000 ~ 1111 → AF0 ~ AF15
For pins 8 to 15, the GPIOx_AFRH[31:0] register selects the dedicated
alternate function
Alternate function on STM32F407xxFor 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
Let’s Trace main.c DeeplyDefined macroes
Initial GPIO as Output
Set / Reset Output
https://github.com/Malkavian/tuts/blob/master/stm/blinky/main.c
Practice:
Do the porting with original STM32F4 library
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
Referenced from: STM32F4DISCOVERY Peripherals, Rev: B.2(PCB.SCH), 1/9/2012, Sheet 6 of 6
Output LEDs on Schematic of STM32F4-Discovery
Referenced from: STM32F4DISCOVERY Peripherals, Rev: B.2(PCB.SCH), 1/9/2012, Sheet 6 of 6
User Button on Schematic of STM32F4-Discovery
Clicked
What does practice do?
Start
Setup LEDs & User Button
Flash all LEDs
Turn on/off LEDs in order
Check User
Button
Not Clicked
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/PF252419
b. devthrash/STM32F4-examples: https://github.com/devthrash/STM32F4-examples/blob/master/GPIO/main.c
Programming and Using STD Library
Reference1. 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 tutorialhttp://eliaselectronics.com/stm32f4-tutorials/stm32f4-gpio-tutorial/
3. blinkyhttps://github.com/Malkavian/tuts/blob/master/stm/blinky
4. Setting up the ARM toolchainhttp://eliaselectronics.com/stm32f4-tutorials/setting-up-the-stm32f4-arm-development-toolchain/
Reference (Cont.)5. STM32F4 Library
http://www.emcu.it/STM32F4xx/STM32F4-Library/STM32F4-Library.html6. STM32F4-Discovery Library
http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/PF2524197. STM32F4 GPIO Configuration
http://patrickleyman.be/blog/stm32f4-gpio-configuration/devthrash/STM32F4-exampleshttps://github.com/devthrash/STM32F4-examples
8. Using I/O ports on the STM32 F4 Discoveryhttp://www.rapitasystems.com/blog/using-io-ports-on-the-stm32-f4-discovery
Refernce (Cont.)9. STM32开发板例程讲解之二:GPIO的描述和配置,GPIO_IOTG例程精讲
_ch…http://www.360doc.com/content/11/0314/14/5963783_101015283.shtml
10. Week #7 (Oct 30) :: Peripherals and hardware interfacehttp://wiki.csie.ncku.edu.tw/embedded/2012w7
11. General-purpose Input/Output (GPIO)http://wiki.csie.ncku.edu.tw/embedded/GPIO
12. STM32之GPIO输入输出
http://blog.csdn.net/wjjontheway/article/details/930218913. STM32F4 DSP and standard peripherals library
http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/PF257901
Refernce (Cont.)14. CMSIS - Cortex Microcontroller Software Interface Standard
http://www.arm.com/products/processors/cortex-m/cortex-microcontroller-software-interface-standard.php
15. STM32 Programming (中文)http://wiki.csie.ncku.edu.tw/embedded/Lab19/stm32-prog.pdf
16. STM32F407 硬體週邊介紹
http://www.slideshare.net/ssusereb6ca4/stm32f417. Let's Play STM32
http://www.slideshare.net/jaychen94009/arm-cortex-m418. Tutorial: STM32 - Integrated Debugging in Eclipse using GNU toolchain
http://erika.tuxfamily.org/wiki/index.php?title=Tutorial:_STM32_-_Integrated_Debugging_in_Eclipse_using_GNU_toolchain&oldid=5474
Thank you&
Let’s practice ~
top related