let's play stm32

34
Let’s Play STM32 PJay Chen <[email protected]> Hung-Da Lin <[email protected]>

Upload: jay-chen

Post on 24-May-2015

1.049 views

Category:

Technology


8 download

DESCRIPTION

a course Slide for Taipei Tech

TRANSCRIPT

Page 1: Let's Play STM32

Let’s Play STM32

PJay Chen <[email protected]>

Hung-Da Lin <[email protected]>

Page 2: Let's Play STM32

ARM Cortex-M4

Page 3: Let's Play STM32

ARM Processors

• Cortex-A (Application)High-end embedded OS, MMU. Ex: Smart Phone

• Cortex-R(Real-time)Ex: BlueRay players, Hard Disk Drive controllers…

• Cortex-M(Microcontroller)Embedded System, replace 8051

http://www.arm.com/index.php

Page 4: Let's Play STM32

ARM Cortex-M Chip

Page 5: Let's Play STM32

ARM Cortex-M

• ARMv7M Architecture

• No Cache, No MMU

• Support DIV instruction

• Interrupts automatically save/restore state

• Fixed memory map

• Thumb-2 processing coreHigh code density

Page 6: Let's Play STM32

ARM Cortex-M

Page 7: Let's Play STM32

Documentation

• The Definitive Guide To The ARM Cortex-M3

• ARM Cortex-M3 Introduction

• Jserv 暑期課程week3

Page 8: Let's Play STM32

STM32f4xx

Page 9: Let's Play STM32

Documentation

• STM32F429_Datasheet

Device features, Overview

Device block diagram

Memory map

Pinouts and pin description

• STM32F4xx_Reference Manual

How to use the STM32f4xx memory and peripherals

• 零死角玩轉STM32, but it’s use the Cortex-M3(STM32f10x)

• 成大資工wiki

• STM32開機流程

Page 10: Let's Play STM32

Memory Mapping

• Fixed

•共有4G的定址範圍

•存取特定位址則可對I/O設定以及記憶體

•板子預設程式起始位址為0x8000000

Page 11: Let's Play STM32

STM32F4xx Standard Peripherals Library

•透過ST提供的API來設置暫存器

•如同初學C語言時用printf(),不用知道是如何印出字串

•相對於直接配置暫存器的好處

開發速度較快

程式可讀性較佳

•有興趣可以看Library是如何配置暫存器

• STM32F429I-Discovery_FW_V1.0.1\Libraries\STM32F4xx_StdPeriph_Driver\

Page 12: Let's Play STM32

CMSIS

• CMSIS(Cortex Microcontroller Software Interface Standard)vendor-independent hardware abstraction layer便於移植程式至相同CPU(Cortex-M)的Chip

Page 13: Let's Play STM32

CMSIS

•參考範例

零死角玩轉STM32

Page 14: Let's Play STM32

CMSIS• system_stm32f4xx.c• 系統頻率配置可由STM32F4xx_Clock_Configuration.xls設置產生

Page 15: Let's Play STM32

CMSIS

• startup_stm32f4xx.s

• 規劃stack,以及vector table位址(含Reset_Handler)

• Reset後進入Reset_Handler

• 將data由flash搬到SRAM(Harvard architecture)

• 以及初始化bss區data成0

• 呼叫system_stm32f4xx.c的SystemInit(),進行時脈初始化

• bl main; 跳至Entry point,如main()

Linker script startup code

Page 16: Let's Play STM32

Device Block Diagram

STM32F407_datasheet

Page 17: Let's Play STM32

Device Block Diagram

STM32F407_datasheet

Page 18: Let's Play STM32

Clock Tree

• Clock gating

STM32F4xx_Reference Manual

Page 19: Let's Play STM32

How to Use Peripherals

GPIO Configuration

Alternate Function Configuration

Enable PeriphClockRCC_AHB1PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

Enable Alternate function PeriphClockRCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

Configure AF to specific GPIOGPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

Page 20: Let's Play STM32

GPIO

• GPIO(General-purpose I/O)

•可由軟體配置成不同功能(周邊電路)的輸入或輸出

•分成GPIOA~GPIOE組,各組有0~15個pin角

Page 21: Let's Play STM32

GPIO – Input Configuration

•配置為輸入pinGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

• Floating, Pull-up/pull-down以pull-up or pull-down 電阻來預設空接時輸入為高電位或低電位GPIO_InitStructure.GPIO_PuPd =

GPIO_PuPd_NOPULL;orGPIO_PuPd_UP ;orGPIO_PuPd_DOWN;

Page 22: Let's Play STM32

GPIO – Output Configuration•配置為輸出pin

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

• Push-PullGPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

• Open-DrainGPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

Page 23: Let's Play STM32

GPIO – Analog configuration

•配置為類比輸入/輸出pinGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;

•由input data register讀到的值恆為0

•用於ADC, DAC

Page 24: Let's Play STM32

GPIO – Alternate function configuration

•配置為AFGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

•用於SPI, USART, I2C, SDIO, TIM

Page 25: Let's Play STM32

Exercise !https://github.com/PJayChen/STM32f4xx_Bootloader/blob/master

/src/hw_conf.c#L12

Page 26: Let's Play STM32

An example, How to use USART

• Step 1, Check the AF mapping from the Datasheet1

23

Page 27: Let's Play STM32

An example, How to use USART

• Step 2, Find the USART1 is connect to which bus

Page 28: Let's Play STM32

An example, How to use USART

• Step 3, Let’s coding, Configure the system clocks

/* USART1 clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

/* GPIOA clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

Page 29: Let's Play STM32

An example, How to use USART

• Step 4, Configure the GPIO

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

Page 30: Let's Play STM32

An example, How to use USART

• Step 5, Connect USART with GPIO pins

GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); //TX

GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); //RX

Page 31: Let's Play STM32

An example, How to use USART

• Step 6, Configure the USART

//Structure With Data For USART ConfigurationUSART_InitTypeDef USART_InitStructure;//USART ParametersUSART_InitStructure.USART_BaudRate = 9600;USART_InitStructure.USART_WordLength = USART_WordLength_8b;USART_InitStructure.USART_StopBits = USART_StopBits_1;USART_InitStructure.USART_Parity = USART_Parity_No ;USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx ;//Configuring And Enabling USART1USART_Init(USART1, &USART_InitStructure);

USART_Cmd(USART1, ENABLE);

Page 32: Let's Play STM32

An example, How to use USART

• Step 7, Rx/Tx

Tx//Wait till the flag resets

while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);

//Send the data

USART_SendData(USART2, data);

Rx//Wait for character

while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET) ;

//Collect the character

Data = USART_ReceiveData(USART2); //the data type is uint8_t

Page 33: Let's Play STM32

FreeRTOS

Page 34: Let's Play STM32

Documentation

•成大資工分組報告

• The Architecture of Open Source Application: FreeRTOS

• Study of an operating system: FreeRTOS