找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7197|回复: 13
收起左侧

stm32f103单片机的串口IAP升级程序

  [复制链接]
ID:212189 发表于 2019-4-4 22:06 | 显示全部楼层 |阅读模式
使用stm32f103r8t6 实现IAP升级代码精简但是很实用
可以用来制作远程升级
iap.jpg
单片机源程序如下:
  1. /*******************************************************************************
  2. ** 文件名:                 mian.c
  3. ** 版本:                  1.0
  4. ** 工作环境:         RealView MDK-ARM 4.14
  5. ** 作者:                 wuguoyana
  6. ** 生成日期:         2011-04-28
  7. ** 功能:                USART初始化和RCC设置,然后从common.c中执行主菜单
  8. ** 相关文件:        stm32f10x.h

  9. *******************************************************************************/
  10. /* 包含头文件 *****************************************************************/
  11. #include "common.h"
  12. #define USART1_IRQChannel            ((u8)0x25)  /* USART1 global Interrupt */
  13. /* 类型声明 ------------------------------------------------------------------*/
  14. /* 宏 ------------------------------------------------------------------------*/
  15. #define LED2   GPIO_Pin_6
  16. #define LED3   GPIO_Pin_7
  17. #define LED4   GPIO_Pin_8
  18. #define LED5   GPIO_Pin_9

  19. #define TRUE        0xff
  20. #define FALSE        0x00

  21. /* 变量 ----------------------------------------------------------------------*/
  22. extern pFunction Jump_To_Application;
  23. extern uint32_t JumpAddress;

  24. /* 函数声明 ------------------------------------------------------------------*/
  25. void Delay(__IO uint32_t nCount);
  26. void LED_Configuration(void);
  27. static void IAP_Init(void);
  28. void KEY_Configuration(void);
  29. void GPIO_Configuration(void);
  30. void USART_Configuration(void);
  31. unsigned char RCC_Configuration(void);
  32. void NVIC_Configuration(void);
  33. /* 函数功能 ------------------------------------------------------------------*/

  34. /*******************************************************************************
  35.   * @函数名称        main
  36.   * @函数说明   主函数
  37.   * @输入参数   无
  38.   * @输出参数   无
  39.   * @返回参数   无
  40. *******************************************************************************/
  41. int main(void)
  42. {
  43.         //Flash 解锁
  44.         FLASH_Unlock();
  45.         NVIC_Configuration();
  46.     LED_Configuration();
  47.     //配置按键
  48.     KEY_Configuration() ;
  49.     IAP_Init();
  50.     //按键是否按下
  51.     //if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)  == 0x00)
  52.     if (1)
  53.     {
  54.         //假如按键按下
  55.         //执行IAP驱动程序更新Flash程序

  56.         SerialPutString("\r\n======================================================================");
  57.         SerialPutString("\r\n=                                                                    =");
  58.         SerialPutString("\r\n=     In-Application Programming Application  (Version 1.0.0)        =");
  59.         SerialPutString("\r\n=                                                                    =");
  60.         SerialPutString("\r\n======================================================================");
  61.         SerialPutString("\r\n\r\n");
  62.         Main_Menu ();
  63.     }
  64.     //否则执行用户程序
  65.     else
  66.     {
  67.         //判断用户是否已经下载程序,因为正常情况下此地址是栈地址。
  68.         //若没有这一句的话,即使没有下载程序也会进入而导致跑飞。
  69.         if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
  70.         {
  71.             SerialPutString("Execute user Program\r\n\n");
  72.             //跳转至用户代码
  73.             JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
  74.             Jump_To_Application = (pFunction) JumpAddress;

  75.             //初始化用户程序的堆栈指针
  76.             __set_MSP(*(__IO uint32_t*) ApplicationAddress);
  77.             Jump_To_Application();
  78.         }
  79.         else
  80.         {
  81.             SerialPutString("no user Program\r\n\n");
  82.         }
  83.     }

  84.     while (1)
  85.     {

  86.     }
  87. }


  88. /*******************************************************************************
  89.   * @函数名称        LED_Configuration
  90.   * @函数说明   配置使用LED
  91.   * @输入参数   无
  92.   * @输出参数   无
  93.   * @返回参数   无
  94. *******************************************************************************/
  95. void LED_Configuration(void)
  96. {
  97.     GPIO_InitTypeDef GPIO_InitStructure;
  98.     //使能LED所在GPIO的时钟
  99.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
  100.     //初始化LED的GPIO
  101.     GPIO_InitStructure.GPIO_Pin = LED4 | LED5;
  102.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  103.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  104.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  105.     GPIO_SetBits(GPIOB,LED4 | LED5);  //熄灭LED2-5
  106. }

  107. void NVIC_Configuration(void)
  108. {
  109.   NVIC_InitTypeDef NVIC_InitStructure;

  110.   /* Set the Vector Table base location at 0x08000000 */
  111.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x3000);   

  112.   /* Configure one bit for preemption priority */
  113.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  114.   
  115.   /* Enable the EXTI8 Interrupt */
  116.   NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  117.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  118.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  119.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  120.   NVIC_Init(&NVIC_InitStructure);
  121. }

  122. /*******************************************************************************
  123.   * @函数名称        KEY_Configuration
  124.   * @函数说明   按键初始化
  125.   * @输入参数   无
  126.   * @输出参数   无
  127.   * @返回参数   无
  128. *******************************************************************************/
  129. void KEY_Configuration(void)
  130. {
  131.     GPIO_InitTypeDef GPIO_InitStructure;
  132.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  133.     //配置按键
  134.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  135.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  136.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  137. }

  138. /*******************************************************************************
  139.   * @函数名称        GPIO_Configuration
  140.   * @函数说明   配置使用USART1的相关IO管脚
  141.   * @输入参数   无
  142.   * @输出参数   无
  143.   * @返回参数   无
  144. *******************************************************************************/
  145. void GPIO_Configuration(void)
  146. {
  147.     GPIO_InitTypeDef GPIO_InitStructure;
  148.         
  149.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  150.     // 配置 USART1 Tx (PA.09) 作为功能引脚并上拉输出模式
  151.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  152.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  153.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  154.     GPIO_Init(GPIOA, &GPIO_InitStructure);

  155.     //配置 USART1 Tx (PA.10) 作为功能引脚并是浮空输入模式
  156.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  157.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  158.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  159. }

  160. /*******************************************************************************
  161.   * @函数名称        IAP_Init
  162.   * @函数说明   配置使用IAP
  163.   * @输入参数   无
  164.   * @输出参数   无
  165.   * @返回参数   无
  166. *******************************************************************************/
  167. void IAP_Init(void)
  168. {
  169.     USART_InitTypeDef USART_InitStructure;
  170.         GPIO_Configuration();

  171.     /* USART1 配置 ------------------------------------------------------------
  172.          USART1 配置如下:
  173.           - 波特率      = 115200 baud
  174.           - 字长        = 8 Bits
  175.           - 一个停止位
  176.           - 无校验
  177.           - 无硬件流控制
  178.           - 接受和发送使能
  179.     --------------------------------------------------------------------------*/
  180.     USART_InitStructure.USART_BaudRate = 115200;
  181.     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  182.     USART_InitStructure.USART_StopBits = USART_StopBits_1;
  183.     USART_InitStructure.USART_Parity = USART_Parity_No;
  184.     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  185.     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;


  186.     USART_Init(USART1, &USART_InitStructure);
  187.     // 使能 USART1
  188.     USART_Cmd(USART1, ENABLE);
  189.         
  190. }

  191. /*******************************************************************************
  192.   * @函数名称        Delay
  193.   * @函数说明   插入一段延时时间
  194.   * @输入参数   nCount: 指定延时时间长度
  195.   * @输出参数   无
  196.   * @返回参数   无
  197. *******************************************************************************/
  198. void Delay(__IO uint32_t nCount)
  199. {
  200.     for (; nCount != 0; nCount--);
  201. }

  202. #ifdef  USE_FULL_ASSERT

  203. /*******************************************************************************
  204.   * @函数名称        assert_failed
  205.   * @函数说明   报告在检查参数发生错误时的源文件名和错误行数
  206.   * @输入参数   file: 源文件名
  207.                                   line: 错误所在行数
  208.   * @输出参数   无
  209.   * @返回参数   无
  210. *******************************************************************************/
  211. void assert_failed(uint8_t* file, uint32_t line)
  212. {
  213.     /* 用户可以增加自己的代码用于报告错误的文件名和所在行数,
  214.        例如:printf("错误参数值: 文件名 %s 在 %d行\r\n", file, line) */

  215.     //死循环
  216.     while (1)
  217.     {
  218.     }
  219. }
  220. #endif



  221. /*******************************文件结束***************************************/
复制代码

所有资料51hei提供下载:
STM32 IAP源码和测试代码.7z (248.23 KB, 下载次数: 233)
回复

使用道具 举报

ID:110257 发表于 2019-4-5 06:48 | 显示全部楼层
IAP 空间最少能做到多少?
回复

使用道具 举报

ID:285194 发表于 2019-4-11 14:42 | 显示全部楼层
你好楼主,有个问题想请问一下,你给的例程我能够运行成功,但是换了我自己本身的程序后,能够烧写进去,但是不能够运行,好像卡死一样,这是我的一些基本程序
1.png
2.png
3.png
回复

使用道具 举报

ID:285194 发表于 2019-4-11 14:43 | 显示全部楼层
你那边的程序我也已经改成 0x8010000的了
回复

使用道具 举报

ID:285194 发表于 2019-4-12 10:30 | 显示全部楼层
已经改好了,我是部分和楼主不符合的相关.c和.h文件替换掉,再在APP函数前面加了个__set_PRIMASK(0);全局中断开启,终于调通了?希望楼主看到信息能够回复一下,是不是你的文件里面,某些固件库有改动某些信息,谢谢楼主的教程。
回复

使用道具 举报

ID:56855 发表于 2019-5-23 21:27 | 显示全部楼层
还不错
回复

使用道具 举报

ID:445069 发表于 2019-6-11 09:57 | 显示全部楼层
stm32f103串口IAP升级程序,这个必须顶!
回复

使用道具 举报

ID:371270 发表于 2019-9-9 15:00 | 显示全部楼层
好东西但是我编译出来一堆错误www
回复

使用道具 举报

ID:68585 发表于 2019-9-16 14:26 | 显示全部楼层
这个很好,做个标记
回复

使用道具 举报

ID:334749 发表于 2020-7-3 16:01 | 显示全部楼层
Main_Menu ();函数定义那里去啦?
回复

使用道具 举报

ID:390701 发表于 2021-8-6 16:18 | 显示全部楼层
nice,可以运行
回复

使用道具 举报

ID:390701 发表于 2021-8-6 16:19 | 显示全部楼层
老农要进城 发表于 2020-7-3 16:01
Main_Menu ();函数定义那里去啦?

if(1)打印输出下面
回复

使用道具 举报

ID:584195 发表于 2021-8-6 18:12 | 显示全部楼层
学习了,希望以后可以用得上!
回复

使用道具 举报

ID:390701 发表于 2021-8-9 09:45 | 显示全部楼层
但是为什么只能更新一次,第二次后更新无效呢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表