使用stm32f103r8t6 實現IAP升級代碼精簡但是很實用
可以用來制作遠程升級
iap.jpg (123.92 KB, 下載次數: 106)
下載附件
2019-4-4 22:06 上傳
單片機源程序如下:
- /*******************************************************************************
- ** 文件名: mian.c
- ** 版本: 1.0
- ** 工作環境: RealView MDK-ARM 4.14
- ** 作者: wuguoyana
- ** 生成日期: 2011-04-28
- ** 功能: USART初始化和RCC設置,然后從common.c中執行主菜單
- ** 相關文件: stm32f10x.h
- *******************************************************************************/
- /* 包含頭文件 *****************************************************************/
- #include "common.h"
- #define USART1_IRQChannel ((u8)0x25) /* USART1 global Interrupt */
- /* 類型聲明 ------------------------------------------------------------------*/
- /* 宏 ------------------------------------------------------------------------*/
- #define LED2 GPIO_Pin_6
- #define LED3 GPIO_Pin_7
- #define LED4 GPIO_Pin_8
- #define LED5 GPIO_Pin_9
- #define TRUE 0xff
- #define FALSE 0x00
- /* 變量 ----------------------------------------------------------------------*/
- 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);
- unsigned char RCC_Configuration(void);
- void NVIC_Configuration(void);
- /* 函數功能 ------------------------------------------------------------------*/
- /*******************************************************************************
- * @函數名稱 main
- * @函數說明 主函數
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- int main(void)
- {
- //Flash 解鎖
- FLASH_Unlock();
- NVIC_Configuration();
- LED_Configuration();
- //配置按鍵
- KEY_Configuration() ;
- IAP_Init();
- //按鍵是否按下
- //if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9) == 0x00)
- if (1)
- {
- //假如按鍵按下
- //執行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_GPIOB , ENABLE);
- //初始化LED的GPIO
- GPIO_InitStructure.GPIO_Pin = LED4 | LED5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,LED4 | LED5); //熄滅LED2-5
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Set the Vector Table base location at 0x08000000 */
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x3000);
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
-
- /* Enable the EXTI8 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /*******************************************************************************
- * @函數名稱 KEY_Configuration
- * @函數說明 按鍵初始化
- * @輸入參數 無
- * @輸出參數 無
- * @返回參數 無
- *******************************************************************************/
- void KEY_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- //配置按鍵
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_Init(GPIOB, &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_10MHz;
- 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;
- GPIO_Configuration();
- /* USART1 配置 ------------------------------------------------------------
- USART1 配置如下:
- - 波特率 = 115200 baud
- - 字長 = 8 Bits
- - 一個停止位
- - 無校驗
- - 無硬件流控制
- - 接受和發送使能
- --------------------------------------------------------------------------*/
- USART_InitStructure.USART_BaudRate = 115200;
- 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;
- 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源碼和測試代碼.7z
(248.23 KB, 下載次數: 241)
2019-4-5 02:38 上傳
點擊文件名下載附件
IAP升級程序 下載積分: 黑幣 -5
|