|
STM32 IAP之bootloader 協(xié)議采用YMODEM:
分bootloader工程和IAP工程代碼。
* 本例程在 奮斗版STM32開(kāi)發(fā)板V2,V2.1,V3,V5,MINI上調(diào)試通過(guò)
*
* 文件名: main.c
* 內(nèi)容簡(jiǎn)述:
*
* 演示通過(guò)串口1顯示ADC1的11通道的測(cè)量結(jié)果
* 輔助軟件:PC機(jī)上需要運(yùn)行串口調(diào)試助手軟件。
基于MDK版本: 3.8
基于官方外設(shè)庫(kù)版本: 3.5
*
* 文件歷史:
* 版本號(hào) 日期 作者 說(shuō)明
*
*/
1設(shè)計(jì)要求
利用ADC的第11通道對(duì)開(kāi)發(fā)板輸入的電壓值作AD轉(zhuǎn)換,采用連續(xù)轉(zhuǎn)換模式,轉(zhuǎn)換結(jié)果通過(guò)DMA通
道1讀取。ADC轉(zhuǎn)換的結(jié)果,每間隔1秒鐘向串口發(fā)送一次。
2 硬件電路設(shè)計(jì)
在開(kāi)發(fā)板上通用I/O口PC.01與XS8的14腳相連,將PC.01映射到ADC第11通道,即可實(shí)現(xiàn)利用
ADC_IN11
對(duì)輸入電壓作AD轉(zhuǎn)換。
3軟件程序設(shè)計(jì)
根據(jù)設(shè)計(jì)任務(wù)要求,軟件程序主要包括:
(1) 配置GPIO口,將PC.01配置為ADC的第11采樣通道;將配置GPIO中PA.09和PA.10根
引腳為串口輸入輸出。
(2) 設(shè)置ADC,將ADC_IN11設(shè)置為連續(xù)轉(zhuǎn)換模式;
(3) 配置DMA通道1用于ADC_IN14傳輸轉(zhuǎn)換的結(jié)果;
(4) 配置串口及相關(guān)發(fā)送功能;
(5) 每隔1S向串口輸出AD轉(zhuǎn)換結(jié)果。
4 運(yùn)行過(guò)程
(1) 使用Keil uVision3 通過(guò)JLINK仿真器連接開(kāi)發(fā)板,使用串口線,連接實(shí)驗(yàn)板
上的UART1(XS5)和PC機(jī)的串口,打開(kāi)實(shí)驗(yàn)例程目錄下的STM32-FD-ADC.Uv2例程,編譯鏈接工程;
(2) 在PC機(jī)上運(yùn)行windows自帶的超級(jí)終端串口通信程序(波特率115200、1位停止位、無(wú)校驗(yàn)
位、無(wú)硬件流
控制);或者使用其它串口通信程序;
(3) 點(diǎn)擊MDK 的Debug菜單,點(diǎn)擊Start/Stop Debug Session;
(4) 可以看到串口輸出數(shù)值不斷變化,正常顯示結(jié)果如下所示。
usart1 print AD_value --------------------------
The current AD value = 2096
The current AD value = 2096
The current AD value = 2048
The current AD value = 2087
The current AD value = 2112
The current AD value = 2112
The current AD value = 2003
The current AD value = 1998
The current AD value = 1999
The current AD value = 2092
The current AD value = 2048
The current AD value = 2051
The current AD value = 2056
The current AD value = 2048
The current AD value = 2048
The current AD value = 2096
The current AD value = 2001
....
....
- #include "stm32f10x.h"
- #include "stm32f10x_adc.h"
- #include "stm32f10x_dma.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_usart.h"
- #include "stm32f10x_flash.h"
- #include "misc.h"
- #include <stdarg.h>
- #define ADC1_DR_Address ((u32)0x4001244C)
- #define FLASH_IAP_ADDR 0x08000000 //iap程序起始地址(存放在FLASH)
- #define LED1 GPIO_Pin_5
- #define LED2 GPIO_Pin_6
- #define LED3 GPIO_Pin_3
- static unsigned long ticks;
- unsigned char Clock1s;
- vu16 ADC_ConvertedValue;
- typedef void (*iapfun)(void);
- iapfun jump2iap;
- void RCC_Configuration(void);
- void ADC_Configuration(void);
- void Usart1_Init(void);
- void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...);
- void LED_Configuration(void);
- void KEY_Configuration(void);
- void iap_jump(u32 iapxaddr);
- /****************************************************************************
- * 名 稱:int main(void)
- * 功 能:主函數(shù)
- * 入口參數(shù):無(wú)
- * 出口參數(shù):無(wú)
- * 說(shuō) 明:
- * 調(diào)用方法:
- ****************************************************************************/
- int main(void)
- {
- static _Bool flag_ledtoogle=0;
- RCC_Configuration(); //設(shè)置內(nèi)部時(shí)鐘及外設(shè)時(shí)鐘使能
- LED_Configuration();
- KEY_Configuration();
- Usart1_Init(); //串口1初始化
- ADC_Configuration(); //ADC初始化
- USART_OUT(USART1,"\r\n USART1 print AD_value -------------------------- \r\n");
- while(1)
- {
- if (ticks++ >= 900000)
- { //間隔時(shí)間顯示轉(zhuǎn)換結(jié)果
- ticks = 0;
- Clock1s = 1;
- flag_ledtoogle^=1;
- }
- if(flag_ledtoogle)
- {
- GPIO_ResetBits(GPIOB, LED1); //熄滅LED0-3
- }
- else
- {
- GPIO_SetBits(GPIOB, LED1); //熄滅LED0-3
- }
- if (Clock1s)
- {
- Clock1s = 0;
-
- USART_OUT(USART1,"The current AD value = %d \r\n", ADC_ConvertedValue);
- }
-
- if (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0x00)
- {
- iap_jump(FLASH_IAP_ADDR);
- }
- }
- }
- //=============================================================================================
- //函數(shù)名稱: void iap_jump(u32 iapxaddr)
- //輸入?yún)?shù): appxaddr:用戶代碼起始地址.
- //輸出參數(shù):
- //返回值 :
- //功能描述: 跳轉(zhuǎn)到IAP
- //注意事項(xiàng): void
- //=============================================================================================
- void iap_jump(u32 iapxaddr)
- {
- if(((*(vu32*)iapxaddr)&0x2FFE0000)==0x20000000) //檢查棧頂?shù)刂肥欠窈戏?0x20000000是sram的起始地址,也是程序的棧頂?shù)刂?br />
- {
- // printf("ok\r\n");
- //Delay_Ms(10);
- jump2iap=(iapfun)*(vu32*)(iapxaddr+4); //用戶代碼區(qū)第二個(gè)字為程序開(kāi)始地址(復(fù)位地址)
- // MSR_MSP(*(vu32*)iapxaddr); //初始化APP堆棧指針(用戶代碼區(qū)的第一個(gè)字用于存放棧頂?shù)刂?
- __set_MSP(*(__IO uint32_t*) iapxaddr);
- jump2iap(); //跳轉(zhuǎn)到APP.
- }
- else
- {
- // printf("iap program loss,please check\r\n");
- }
- }
- /*******************************************************************************
- * @函數(shù)名稱 KEY_Configuration
- * @函數(shù)說(shuō)明 按鍵初始化
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void KEY_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- //配置按鍵
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /*******************************************************************************
- * @函數(shù)名稱 LED_Configuration
- * @函數(shù)說(shuō)明 配置使用LED
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void LED_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //使能LED所在GPIO的時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
- //初始化LED的GPIO
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = LED1;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, LED1); //熄滅LED0-3
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD , ENABLE);
- //初始化LED的GPIO
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = LED2 | LED3;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- GPIO_SetBits(GPIOD, LED2 | LED3); //熄滅LED0-3
- }
- /****************************************************************************
- * 名 稱:void ADC_Configuration(void)
- * 功 能:ADC 配置函數(shù)
- * 入口參數(shù):無(wú)
- * 出口參數(shù):無(wú)
- * 說(shuō) 明:
- * 調(diào)用方法:
- ****************************************************************************/
- void ADC_Configuration(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- //設(shè)置AD模擬輸入端口為輸入 1路AD 規(guī)則通道
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /* Enable DMA clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- /* Enable ADC1 and GPIOC clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
- /* DMA channel1 configuration ----------------------------------------------*/
- //使能DMA
- DMA_DeInit(DMA1_Channel1);
- DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //DMA通道1的地址
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //DMA傳送地址
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //傳送方向
- DMA_InitStructure.DMA_BufferSize = 1; //傳送內(nèi)存大小,1個(gè)16位
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //傳送內(nèi)存地址遞增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //ADC1轉(zhuǎn)換的數(shù)據(jù)是16位
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //傳送的目的地址是16位寬度
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循環(huán)
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
-
- /* 允許DMA1通道1傳輸結(jié)束中斷 */
- //DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, ENABLE);
- //使能DMA通道1
- DMA_Cmd(DMA1_Channel1, ENABLE);
-
-
- //ADC配置
- /* ADC轉(zhuǎn)換時(shí)間: ─ STM32F103xx增強(qiáng)型產(chǎn)品:時(shí)鐘為56MHz時(shí)為1μs(時(shí)鐘為72MHz為1.17μs)
- ADC采樣范圍0-3.3V */
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //設(shè)置ADC的時(shí)鐘為72MHZ/6=12M
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作在獨(dú)立模式
- ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模數(shù)轉(zhuǎn)換工作在掃描模式(多通道)還是單次(單通道)模式
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模數(shù)轉(zhuǎn)換工作在連續(xù)模式,還是單次模式
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//轉(zhuǎn)換由軟件而不是外部觸發(fā)啟動(dòng)
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC數(shù)據(jù)右對(duì)齊
- ADC_InitStructure.ADC_NbrOfChannel = 1; //規(guī)定了順序進(jìn)行規(guī)則轉(zhuǎn)換的ADC通道的數(shù)目。這個(gè)數(shù)目的取值范圍是1到16
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /* ADC1 regular channels configuration [規(guī)則模式通道配置]*/
- //ADC1 規(guī)則通道配置
- ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5); //通道11采樣時(shí)間 55.5周期
-
- //使能ADC1 DMA
- ADC_DMACmd(ADC1, ENABLE);
- //使能ADC1
- ADC_Cmd(ADC1, ENABLE);
-
- // 初始化ADC1校準(zhǔn)寄存器
- ADC_ResetCalibration(ADC1);
- //檢測(cè)ADC1校準(zhǔn)寄存器初始化是否完成
- while(ADC_GetResetCalibrationStatus(ADC1));
-
- //開(kāi)始校準(zhǔn)ADC1
- ADC_StartCalibration(ADC1);
- //檢測(cè)是否完成校準(zhǔn)
- while(ADC_GetCalibrationStatus(ADC1));
-
- //ADC1轉(zhuǎn)換啟動(dòng)
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- /****************************************************************************
- * 名 稱:void RCC_Configuration(void)
- * 功 能:系統(tǒng)時(shí)鐘配置為72MHZ, 外設(shè)時(shí)鐘配置
- * 入口參數(shù):無(wú)
- * 出口參數(shù):無(wú)
- * 說(shuō) 明:
- * 調(diào)用方法:無(wú)
- ****************************************************************************/
- void RCC_Configuration(void){
- SystemInit();
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
- | RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE , ENABLE);
- }
- /****************************************************************************
- * 名 稱:void Usart1_Init(void)
- * 功 能:串口1初始化函數(shù)
- * 入口參數(shù):無(wú)
- * 出口參數(shù):無(wú)
- * 說(shuō) 明:
- * 調(diào)用方法:無(wú)
- ****************************************************************************/
- void Usart1_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 , ENABLE); //使能串口1時(shí)鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //LCD背光控制
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOD, GPIO_Pin_13); //LCD背光關(guān)閉
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //復(fù)用開(kāi)漏輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- USART_InitStructure.USART_BaudRate = 9600; //速率115200bps
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數(shù)據(jù)位8位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位
- USART_InitStructure.USART_Parity = USART_Parity_No; //無(wú)校驗(yàn)位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無(wú)硬件流控
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
- /* Configure USART1 */
- USART_Init(USART1, &USART_InitStructure); //配置串口參數(shù)函數(shù)
- /* Enable the USART1 */
- USART_Cmd(USART1, ENABLE);
-
- }
- /******************************************************
- 整形數(shù)據(jù)轉(zhuǎn)字符串函數(shù)
- char *itoa(int value, char *string, int radix)
- radix=10 標(biāo)示是10進(jìn)制 非十進(jìn)制,轉(zhuǎn)換結(jié)果為0;
- 例:d=-379;
- 執(zhí)行 itoa(d, buf, 10); 后
-
- buf="-379"
- **********************************************************/
- char *itoa(int value, char *string, int radix)
- {
- int i, d;
- int flag = 0;
- char *ptr = string;
- /* This implementation only works for decimal numbers. */
- if (radix != 10)
- {
- *ptr = 0;
- return string;
- }
- if (!value)
- {
- *ptr++ = 0x30;
- *ptr = 0;
- return string;
- }
- /* if this is a negative value insert the minus sign. */
- if (value < 0)
- {
- *ptr++ = '-';
- /* Make the value positive. */
- value *= -1;
- }
- for (i = 10000; i > 0; i /= 10)
- {
- d = value / i;
- if (d || flag)
- {
- *ptr++ = (char)(d + 0x30);
- value -= (d * i);
- flag = 1;
- }
- }
- /* Null terminate the string. */
- *ptr = 0;
- return string;
- } /* NCL_Itoa */
- /****************************************************************************
- * 名 稱:void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...)
- * 功 能:格式化串口輸出函數(shù)
- * 入口參數(shù):USARTx: 指定串口
- Data: 發(fā)送數(shù)組
- ...: 不定參數(shù)
- * 出口參數(shù):無(wú)
- * 說(shuō) 明:格式化串口輸出函數(shù)
- "\r" 回車(chē)符 USART_OUT(USART1, "abcdefg\r")
- "\n" 換行符 USART_OUT(USART1, "abcdefg\r\n")
- "%s" 字符串 USART_OUT(USART1, "字符串是:%s","abcdefg")
- "%d" 十進(jìn)制 USART_OUT(USART1, "a=%d",10)
- * 調(diào)用方法:無(wú)
- ****************************************************************************/
- void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...){
- const char *s;
- int d;
-
- char buf[16];
- va_list ap;
- va_start(ap, Data);
- while(*Data!=0){ //判斷是否到達(dá)字符串結(jié)束符
- if(*Data==0x5c){ //'\'
- switch (*++Data){
- case 'r': //回車(chē)符
- USART_SendData(USARTx, 0x0d);
- Data++;
- break;
- case 'n': //換行符
- USART_SendData(USARTx, 0x0a);
- Data++;
- break;
-
- default:
- Data++;
- break;
- }
-
-
- }
- else if(*Data=='%'){ //
- switch (*++Data){
- case 's': //字符串
- s = va_arg(ap, const char *);
- for ( ; *s; s++) {
- USART_SendData(USARTx,*s);
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
- }
- Data++;
- break;
- case 'd': //十進(jìn)制
- d = va_arg(ap, int);
- itoa(d, buf, 10);
- for (s = buf; *s; s++) {
- USART_SendData(USARTx,*s);
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
- }
- Data++;
- break;
- default:
- Data++;
- break;
- }
- }
- else USART_SendData(USARTx, *Data++);
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
- }
- }
- /******************* (C) COPYRIGHT 2013 奮斗STM32 *****END OF FILE****/
復(fù)制代碼- //=======================================Copyright(c)===========================================
- // Auto Code Making Company
- // Auto Code Department
- // CopyRight Private
- //-------------------------------------------------FileMessage------------------------------------
- //FileName main.c
- //Author yanggang
- //Creation Time
-
- //Description
- //-----------------------------------------------當(dāng)前版本修訂----------------------------------
- //修改人
- //版 本
- //修改日期
- //Description
- //=============================================================================================
- /*******************************************************************************
- ** 文件名: mian.c
- ** 功能: USART初始化和RCC設(shè)置,然后從common.c中執(zhí)行主菜單
- ** 相關(guān)文件: stm32f10x.h
- *******************************************************************************/
- /* 包含頭文件 *****************************************************************/
- #include "common.h"
- /* 類(lèi)型聲明 ------------------------------------------------------------------*/
- /* 宏 ------------------------------------------------------------------------*/
- #define LED0 GPIO_Pin_0
- #define LED1 GPIO_Pin_1
- #define LED2 GPIO_Pin_2
- #define LED3 GPIO_Pin_3
- /* 變量 ----------------------------------------------------------------------*/
- extern pFunction Jump_To_Application;
- extern uint32_t JumpAddress;
- /* 函數(shù)聲明 ------------------------------------------------------------------*/
- void Delay(__IO uint32_t nCount);
- void LED_Configuration(void);
- static void IAP_Init(void);
- void KEY_Configuration(void);
- void GPIO_Configuration(void);
- void USART_Configuration(void);
- /* 函數(shù)功能 ------------------------------------------------------------------*/
- /*******************************************************************************
- * @函數(shù)名稱 main
- * @函數(shù)說(shuō)明 主函數(shù)
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- int main(void)
- {
- //Flash 解鎖
- FLASH_Unlock();
- LED_Configuration();
- //配置按鍵
- KEY_Configuration() ;
- IAP_Init();
- //按鍵是否按下
- if (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0x00)
- {
- //假如USER1按鍵按下
- //執(zhí)行IAP驅(qū)動(dòng)程序更新Flash程序
- SerialPutString("\r\n======================================================================");
- SerialPutString("\r\n= =");
- SerialPutString("\r\n= In-Application Programming Application (Version 1.0.0) =");
- SerialPutString("\r\n= =");
- SerialPutString("\r\n======================================================================");
- SerialPutString("\r\n\r\n");
- Main_Menu ();
- }
- //否則執(zhí)行用戶程序
- else
- {
- //判斷用戶是否已經(jīng)下載程序,因?yàn)檎G闆r下此地址是棧地址。
- //若沒(méi)有這一句的話,即使沒(méi)有下載程序也會(huì)進(jìn)入而導(dǎo)致跑飛。
- if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
- {
- SerialPutString("Execute user Program\r\n\n");
- //跳轉(zhuǎn)至用戶代碼
- JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
- Jump_To_Application = (pFunction) JumpAddress;
- //初始化用戶程序的堆棧指針
- __set_MSP(*(__IO uint32_t*) ApplicationAddress);
- Jump_To_Application();
- }
- else
- {
- SerialPutString("no user Program\r\n\n");
- }
- }
- while (1)
- {
- }
- }
- /*******************************************************************************
- * @函數(shù)名稱 LED_Configuration
- * @函數(shù)說(shuō)明 配置使用LED
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void LED_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //使能LED所在GPIO的時(shí)鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE , ENABLE);
- //初始化LED的GPIO
- GPIO_InitStructure.GPIO_Pin = LED0 | LED1 | LED2 | LED3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- GPIO_SetBits(GPIOE, LED2 | LED3); //熄滅LED0-3
-
- }
- /*******************************************************************************
- * @函數(shù)名稱 KEY_Configuration
- * @函數(shù)說(shuō)明 按鍵初始化
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void KEY_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
- //配置按鍵
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- /*******************************************************************************
- * @函數(shù)名稱 GPIO_Configuration
- * @函數(shù)說(shuō)明 配置使用USART1的相關(guān)IO管腳
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
- // 配置 USART1 Tx (PA.09) 作為功能引腳并上拉輸出模式
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //配置 USART1 Tx (PA.10) 作為功能引腳并是浮空輸入模式
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- /*******************************************************************************
- * @函數(shù)名稱 IAP_Init
- * @函數(shù)說(shuō)明 配置使用IAP
- * @輸入?yún)?shù) 無(wú)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void IAP_Init(void)
- {
- USART_InitTypeDef USART_InitStructure;
- /* USART1 配置 ------------------------------------------------------------
- USART1 配置如下:
- - 波特率 = 115200 baud
- - 字長(zhǎng) = 8 Bits
- - 一個(gè)停止位
- - 無(wú)校驗(yàn)
- - 無(wú)硬件流控制
- - 接受和發(fā)送使能
- --------------------------------------------------------------------------*/
- USART_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;
- GPIO_Configuration();
- USART_Init(USART1, &USART_InitStructure);
- // 使能 USART1
- USART_Cmd(USART1, ENABLE);
- }
- /*******************************************************************************
- * @函數(shù)名稱 Delay
- * @函數(shù)說(shuō)明 插入一段延時(shí)時(shí)間
- * @輸入?yún)?shù) nCount: 指定延時(shí)時(shí)間長(zhǎng)度
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void Delay(__IO uint32_t nCount)
- {
- for (; nCount != 0; nCount--);
- }
- #ifdef USE_FULL_ASSERT
- /*******************************************************************************
- * @函數(shù)名稱 assert_failed
- * @函數(shù)說(shuō)明 報(bào)告在檢查參數(shù)發(fā)生錯(cuò)誤時(shí)的源文件名和錯(cuò)誤行數(shù)
- * @輸入?yún)?shù) file: 源文件名
- line: 錯(cuò)誤所在行數(shù)
- * @輸出參數(shù) 無(wú)
- * @返回參數(shù) 無(wú)
- *******************************************************************************/
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* 用戶可以增加自己的代碼用于報(bào)告錯(cuò)誤的文件名和所在行數(shù),
- 例如:printf("錯(cuò)誤參數(shù)值: 文件名 %s 在 %d行\(zhòng)r\n", file, line) */
- //死循環(huán)
- while (1)
- {
- }
- }
- #endif
- /*******************************文件結(jié)束***************************************/
復(fù)制代碼
全部資料51hei下載地址:
STM32 IAP之bootloader 協(xié)議采用YMODEM.rar
(3.78 MB, 下載次數(shù): 148)
2018-9-6 02:47 上傳
點(diǎn)擊文件名下載附件
STM32 IAP之bootloader 協(xié)議采用YMODEM 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|