STM32 IAP之bootloader 協議采用YMODEM:
分bootloader工程和IAP工程代碼。
* 本例程在 奮斗版STM32開發板V2,V2.1,V3,V5,MINI上調試通過
*
* 文件名: main.c
* 內容簡述:
*
* 演示通過串口1顯示ADC1的11通道的測量結果
* 輔助軟件:PC機上需要運行串口調試助手軟件。
基于MDK版本: 3.8
基于官方外設庫版本: 3.5
*
* 文件歷史:
* 版本號 日期 作者 說明
*
*/
1設計要求
利用ADC的第11通道對開發板輸入的電壓值作AD轉換,采用連續轉換模式,轉換結果通過DMA通
道1讀取。ADC轉換的結果,每間隔1秒鐘向串口發送一次。
2 硬件電路設計
在開發板上通用I/O口PC.01與XS8的14腳相連,將PC.01映射到ADC第11通道,即可實現利用
ADC_IN11
對輸入電壓作AD轉換。
3軟件程序設計
根據設計任務要求,軟件程序主要包括:
(1) 配置GPIO口,將PC.01配置為ADC的第11采樣通道;將配置GPIO中PA.09和PA.10根
引腳為串口輸入輸出。
(2) 設置ADC,將ADC_IN11設置為連續轉換模式;
(3) 配置DMA通道1用于ADC_IN14傳輸轉換的結果;
(4) 配置串口及相關發送功能;
(5) 每隔1S向串口輸出AD轉換結果。
4 運行過程
(1) 使用Keil uVision3 通過JLINK仿真器連接開發板,使用串口線,連接實驗板
上的UART1(XS5)和PC機的串口,打開實驗例程目錄下的STM32-FD-ADC.Uv2例程,編譯鏈接工程;
(2) 在PC機上運行windows自帶的超級終端串口通信程序(波特率115200、1位停止位、無校驗
位、無硬件流
控制);或者使用其它串口通信程序;
(3) 點擊MDK 的Debug菜單,點擊Start/Stop Debug Session;
(4) 可以看到串口輸出數值不斷變化,正常顯示結果如下所示。
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)
- * 功 能:主函數
- * 入口參數:無
- * 出口參數:無
- * 說 明:
- * 調用方法:
- ****************************************************************************/
- int main(void)
- {
- static _Bool flag_ledtoogle=0;
- RCC_Configuration(); //設置內部時鐘及外設時鐘使能
- 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)
- { //間隔時間顯示轉換結果
- 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);
- }
- }
- }
- //=============================================================================================
- //函數名稱: void iap_jump(u32 iapxaddr)
- //輸入參數: appxaddr:用戶代碼起始地址.
- //輸出參數:
- //返回值 :
- //功能描述: 跳轉到IAP
- //注意事項: void
- //=============================================================================================
- void iap_jump(u32 iapxaddr)
- {
- if(((*(vu32*)iapxaddr)&0x2FFE0000)==0x20000000) //檢查棧頂地址是否合法.0x20000000是sram的起始地址,也是程序的棧頂地址
- {
- // printf("ok\r\n");
- //Delay_Ms(10);
- jump2iap=(iapfun)*(vu32*)(iapxaddr+4); //用戶代碼區第二個字為程序開始地址(復位地址)
- // MSR_MSP(*(vu32*)iapxaddr); //初始化APP堆棧指針(用戶代碼區的第一個字用于存放棧頂地址)
- __set_MSP(*(__IO uint32_t*) iapxaddr);
- jump2iap(); //跳轉到APP.
- }
- else
- {
- // printf("iap program loss,please check\r\n");
- }
- }
- /*******************************************************************************
- * @函數名稱 KEY_Configuration
- * @函數說明 按鍵初始化
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- 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);
- }
- /*******************************************************************************
- * @函數名稱 LED_Configuration
- * @函數說明 配置使用LED
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- void LED_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //使能LED所在GPIO的時鐘
- 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 配置函數
- * 入口參數:無
- * 出口參數:無
- * 說 明:
- * 調用方法:
- ****************************************************************************/
- void ADC_Configuration(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- //設置AD模擬輸入端口為輸入 1路AD 規則通道
- 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; //傳送內存大小,1個16位
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //傳送內存地址遞增
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //ADC1轉換的數據是16位
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //傳送的目的地址是16位寬度
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //循環
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &DMA_InitStructure);
-
- /* 允許DMA1通道1傳輸結束中斷 */
- //DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, ENABLE);
- //使能DMA通道1
- DMA_Cmd(DMA1_Channel1, ENABLE);
-
-
- //ADC配置
- /* ADC轉換時間: ─ STM32F103xx增強型產品:時鐘為56MHz時為1μs(時鐘為72MHz為1.17μs)
- ADC采樣范圍0-3.3V */
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //設置ADC的時鐘為72MHZ/6=12M
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作在獨立模式
- ADC_InitStructure.ADC_ScanConvMode = ENABLE; //模數轉換工作在掃描模式(多通道)還是單次(單通道)模式
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //模數轉換工作在連續模式,還是單次模式
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//轉換由軟件而不是外部觸發啟動
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ADC數據右對齊
- ADC_InitStructure.ADC_NbrOfChannel = 1; //規定了順序進行規則轉換的ADC通道的數目。這個數目的取值范圍是1到16
- ADC_Init(ADC1, &ADC_InitStructure);
-
- /* ADC1 regular channels configuration [規則模式通道配置]*/
- //ADC1 規則通道配置
- ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5); //通道11采樣時間 55.5周期
-
- //使能ADC1 DMA
- ADC_DMACmd(ADC1, ENABLE);
- //使能ADC1
- ADC_Cmd(ADC1, ENABLE);
-
- // 初始化ADC1校準寄存器
- ADC_ResetCalibration(ADC1);
- //檢測ADC1校準寄存器初始化是否完成
- while(ADC_GetResetCalibrationStatus(ADC1));
-
- //開始校準ADC1
- ADC_StartCalibration(ADC1);
- //檢測是否完成校準
- while(ADC_GetCalibrationStatus(ADC1));
-
- //ADC1轉換啟動
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);
- }
- /****************************************************************************
- * 名 稱:void RCC_Configuration(void)
- * 功 能:系統時鐘配置為72MHZ, 外設時鐘配置
- * 入口參數:無
- * 出口參數:無
- * 說 明:
- * 調用方法:無
- ****************************************************************************/
- 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初始化函數
- * 入口參數:無
- * 出口參數:無
- * 說 明:
- * 調用方法:無
- ****************************************************************************/
- void Usart1_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 , ENABLE); //使能串口1時鐘
- 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背光關閉
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復用推挽輸出
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //復用開漏輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure); //A端口
- USART_InitStructure.USART_BaudRate = 9600; //速率115200bps
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //數據位8位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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; //收發模式
- /* Configure USART1 */
- USART_Init(USART1, &USART_InitStructure); //配置串口參數函數
- /* Enable the USART1 */
- USART_Cmd(USART1, ENABLE);
-
- }
- /******************************************************
- 整形數據轉字符串函數
- char *itoa(int value, char *string, int radix)
- radix=10 標示是10進制 非十進制,轉換結果為0;
- 例:d=-379;
- 執行 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,...)
- * 功 能:格式化串口輸出函數
- * 入口參數:USARTx: 指定串口
- Data: 發送數組
- ...: 不定參數
- * 出口參數:無
- * 說 明:格式化串口輸出函數
- "\r" 回車符 USART_OUT(USART1, "abcdefg\r")
- "\n" 換行符 USART_OUT(USART1, "abcdefg\r\n")
- "%s" 字符串 USART_OUT(USART1, "字符串是:%s","abcdefg")
- "%d" 十進制 USART_OUT(USART1, "a=%d",10)
- * 調用方法:無
- ****************************************************************************/
- 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){ //判斷是否到達字符串結束符
- if(*Data==0x5c){ //'\'
- switch (*++Data){
- case 'r': //回車符
- 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': //十進制
- 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****/
復制代碼- //=======================================Copyright(c)===========================================
- // Auto Code Making Company
- // Auto Code Department
- // CopyRight Private
- //-------------------------------------------------FileMessage------------------------------------
- //FileName main.c
- //Author yanggang
- //Creation Time
-
- //Description
- //-----------------------------------------------當前版本修訂----------------------------------
- //修改人
- //版 本
- //修改日期
- //Description
- //=============================================================================================
- /*******************************************************************************
- ** 文件名: mian.c
- ** 功能: USART初始化和RCC設置,然后從common.c中執行主菜單
- ** 相關文件: stm32f10x.h
- *******************************************************************************/
- /* 包含頭文件 *****************************************************************/
- #include "common.h"
- /* 類型聲明 ------------------------------------------------------------------*/
- /* 宏 ------------------------------------------------------------------------*/
- #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;
- /* 函數聲明 ------------------------------------------------------------------*/
- 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);
- /* 函數功能 ------------------------------------------------------------------*/
- /*******************************************************************************
- * @函數名稱 main
- * @函數說明 主函數
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- int main(void)
- {
- //Flash 解鎖
- FLASH_Unlock();
- LED_Configuration();
- //配置按鍵
- KEY_Configuration() ;
- IAP_Init();
- //按鍵是否按下
- if (GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0x00)
- {
- //假如USER1按鍵按下
- //執行IAP驅動程序更新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 ();
- }
- //否則執行用戶程序
- else
- {
- //判斷用戶是否已經下載程序,因為正常情況下此地址是棧地址。
- //若沒有這一句的話,即使沒有下載程序也會進入而導致跑飛。
- if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
- {
- SerialPutString("Execute user Program\r\n\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)
- {
- }
- }
- /*******************************************************************************
- * @函數名稱 LED_Configuration
- * @函數說明 配置使用LED
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- void LED_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- //使能LED所在GPIO的時鐘
- 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
-
- }
- /*******************************************************************************
- * @函數名稱 KEY_Configuration
- * @函數說明 按鍵初始化
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- 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);
- }
- /*******************************************************************************
- * @函數名稱 GPIO_Configuration
- * @函數說明 配置使用USART1的相關IO管腳
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- 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);
- }
- /*******************************************************************************
- * @函數名稱 IAP_Init
- * @函數說明 配置使用IAP
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- void IAP_Init(void)
- {
- USART_InitTypeDef USART_InitStructure;
- /* USART1 配置 ------------------------------------------------------------
- USART1 配置如下:
- - 波特率 = 115200 baud
- - 字長 = 8 Bits
- - 一個停止位
- - 無校驗
- - 無硬件流控制
- - 接受和發送使能
- --------------------------------------------------------------------------*/
- 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);
- }
- /*******************************************************************************
- * @函數名稱 Delay
- * @函數說明 插入一段延時時間
- * @輸入參數 nCount: 指定延時時間長度
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- void Delay(__IO uint32_t nCount)
- {
- for (; nCount != 0; nCount--);
- }
- #ifdef USE_FULL_ASSERT
- /*******************************************************************************
- * @函數名稱 assert_failed
- * @函數說明 報告在檢查參數發生錯誤時的源文件名和錯誤行數
- * @輸入參數 file: 源文件名
- line: 錯誤所在行數
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* 用戶可以增加自己的代碼用于報告錯誤的文件名和所在行數,
- 例如:printf("錯誤參數值: 文件名 %s 在 %d行\r\n", file, line) */
- //死循環
- while (1)
- {
- }
- }
- #endif
- /*******************************文件結束***************************************/
復制代碼
全部資料51hei下載地址:
STM32 IAP之bootloader 協議采用YMODEM.rar
(3.78 MB, 下載次數: 148)
2018-9-6 02:47 上傳
點擊文件名下載附件
STM32 IAP之bootloader 協議采用YMODEM 下載積分: 黑幣 -5
|