learn how to develop embedded system for arm @ 2014.12.22 juluosdev

64
Learn How to Develop embedded system for ARM GPIO of STM32F4-Discovery for example StarNight @ 2014.12.22 JuluOSDev

Upload: chien-hung-pan

Post on 12-Jul-2015

1.725 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Learn How to Develop embedded system for ARM

GPIO of STM32F4-Discovery for example

StarNight @ 2014.12.22 JuluOSDev

Page 2: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Who am I?

潘建宏 / Jian-Hong Pan (StarNight)About Me : http://about.me/StarNight

出沒在~

GitHub : starnightPTT : zack2004plurk : StarNightFacebook : Jian-Hong Pan

目前繼續在種花店當個打雜園丁 ~

Page 3: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 4: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 5: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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 新手無痛開發入門指南

Page 6: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Reference previous code

Hellow world of embeded system:

Blinking LEDhttps://github.

com/Malkavian/tuts/tree/master/stm/blinky

Page 9: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 10: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 11: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 12: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Questions:Where is the definition of GPIO API?

Does the main function is the start point of the MCU after be powered

on?

Page 13: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Where is the definition of GPIO API?

Page 14: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Referenced from: STM32F405xx/STM32F407xx Datasheet, Figure 5. STM32F40x block diagram, P.18

GPIO of STM32F40x block diagram

Page 15: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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) {...

Page 16: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 17: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 18: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 20: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 21: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 22: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 23: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 24: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 25: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

main.c

1. Located at ./main.c2. The main source file of the project.

Page 26: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 27: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

stm32f4xx_rcc.c, stm32f4xx_gpio.c

1. Located at $(STM_DIR)/Libraries/STM32F4xx_StdPeriph_Driver/src/…

2. Implement the peripheral functions

Page 28: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Is the main function the start point of the MCU after it is powered on?

Page 29: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 30: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 31: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 32: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 33: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 34: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 35: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev
Page 36: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

1

2 3

45 AHB1 clock

Page 37: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 38: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

1

23

4AHB1 clock5

6

1.a/b

Page 39: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 40: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 41: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 42: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 43: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 44: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 45: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 46: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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.

Page 47: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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).

Page 48: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 49: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 50: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 51: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 52: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 53: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 54: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Practice:

Do the porting with original STM32F4 library

Page 55: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 56: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Referenced from: STM32F4DISCOVERY Peripherals, Rev: B.2(PCB.SCH), 1/9/2012, Sheet 6 of 6

Output LEDs on Schematic of STM32F4-Discovery

Page 57: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Referenced from: STM32F4DISCOVERY Peripherals, Rev: B.2(PCB.SCH), 1/9/2012, Sheet 6 of 6

User Button on Schematic of STM32F4-Discovery

Page 58: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Clicked

What does practice do?

Start

Setup LEDs & User Button

Flash all LEDs

Turn on/off LEDs in order

Check User

Button

Not Clicked

Page 62: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 63: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

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

Page 64: Learn how to develop embedded system for ARM @ 2014.12.22 JuluOSDev

Thank you&

Let’s practice ~