Let's Play STM32
-
Upload
jay-chen -
Category
Technology
-
view
1.041 -
download
8
description
Transcript of Let's Play STM32
ARM Cortex-M4
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
ARM Cortex-M Chip
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
ARM Cortex-M
Documentation
• The Definitive Guide To The ARM Cortex-M3
• ARM Cortex-M3 Introduction
• Jserv 暑期課程week3
STM32f4xx
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開機流程
Memory Mapping
• Fixed
•共有4G的定址範圍
•存取特定位址則可對I/O設定以及記憶體
•板子預設程式起始位址為0x8000000
STM32F4xx Standard Peripherals Library
•透過ST提供的API來設置暫存器
•如同初學C語言時用printf(),不用知道是如何印出字串
•相對於直接配置暫存器的好處
開發速度較快
程式可讀性較佳
•有興趣可以看Library是如何配置暫存器
• STM32F429I-Discovery_FW_V1.0.1\Libraries\STM32F4xx_StdPeriph_Driver\
CMSIS
• CMSIS(Cortex Microcontroller Software Interface Standard)vendor-independent hardware abstraction layer便於移植程式至相同CPU(Cortex-M)的Chip
CMSIS• system_stm32f4xx.c• 系統頻率配置可由STM32F4xx_Clock_Configuration.xls設置產生
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
Device Block Diagram
STM32F407_datasheet
Device Block Diagram
STM32F407_datasheet
Clock Tree
• Clock gating
STM32F4xx_Reference Manual
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);
GPIO
• GPIO(General-purpose I/O)
•可由軟體配置成不同功能(周邊電路)的輸入或輸出
•分成GPIOA~GPIOE組,各組有0~15個pin角
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;
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;
GPIO – Analog configuration
•配置為類比輸入/輸出pinGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
•由input data register讀到的值恆為0
•用於ADC, DAC
GPIO – Alternate function configuration
•配置為AFGPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
•用於SPI, USART, I2C, SDIO, TIM
Exercise !https://github.com/PJayChen/STM32f4xx_Bootloader/blob/master
/src/hw_conf.c#L12
An example, How to use USART
• Step 1, Check the AF mapping from the Datasheet1
23
An example, How to use USART
• Step 2, Find the USART1 is connect to which bus
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);
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);
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
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);
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
FreeRTOS
Documentation
•成大資工分組報告
• The Architecture of Open Source Application: FreeRTOS
• Study of an operating system: FreeRTOS