项目中正在使用的 STM32系列单片机 的远程IAP使用方式,从此刷机不用再拆机!
单片机源程序如下:
- /*******************************************************************************
- ** 功能: 远程烧写程序的启动文件
- *******************************************************************************/
- #include "stm32boot.h"
- #include "EEPROM.h"
- /* 类型声明 -----------------------------------------------------------------*/
- typedef void (*pFunction)(void);
- /* 变量 ----------------------------------------------------------------------*/
- pFunction Jump_To_Application ;
- uint32_t JumpAddress;
- #define ApplicationAddress 0x8003000
- void NVIC_Configuration(void);
- /*******************************************************************************
- * @函数名称 main
- * @函数说明 主函数
- * @输入参数 无
- * @输出参数 无
- * @返回参数 无
- *******************************************************************************/
- extern __IO unsigned long time32_incr;
- unsigned long portGetTickCnt(void)
- {
- // time32_incr = SysTick->VAL;
- return time32_incr;
- }
- void Sleep(unsigned int time_ms)
- {
- unsigned long end = portGetTickCnt() + time_ms;
- while ((signed long)(portGetTickCnt() - end) <= 0)
- ;
- }
- void CloseIrq(void)
- {
- SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk;//关闭SYSTICK中断
- DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, DISABLE); //使能DMA通道4传输完成中断
- DMA_ITConfig(DMA1_Channel7, DMA_IT_TE, DISABLE);
- USART_ITConfig(USART2, USART_IT_IDLE, DISABLE);
- USART_DeInit(USART2);
- DMA_DeInit(DMA1_Channel7);
- DMA_DeInit(DMA1_Channel6);
- }
- int main(void)
- {
- int i=1200;
- u16 FLAG = 0;
- /* Enable PWR and BKP clocks */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
- /* Allow access to BKP Domain */
- PWR_BackupAccessCmd(ENABLE);
- FLASH_Unlock();
- UART2_Configuration();
- SysTick_Init();
- NVIC_Configuration();
- Sleep(20);
- printf("bootv2.0-20181210\r\n");
- while (i--)
- {
- Communication();
- Sleep(20);
- }
- eeprom_read_block(&FLAG, PROGRAMS_ADDR, 2);
-
- if (FLAG == 0xE0E0)
- {
- // DevAddr = 1;
- // NVIC_Configuration();
- // FLASH_Unlock() ;
- // //串口初始化
- // UART2_Configuration();0
- // //系统时基启动
- // // SysTick_Init();
- //
- // led_on();
- while (1)
- {
- Communication();
- }
- }
- //否则执行用户程序
- else
- {
- //判断用户是否已经下载程序,因为正常情况下此地址是栈地址。
- //若没有这一句的话,即使没有下载程序也会进入而导致跑飞。
- FLASH_Lock();
-
- if (((*(__IO uint32_t*)ApplicationAddress) & 0x2FFE0000 ) == 0x20000000)
- {
- JumpAddress = *(__IO uint32_t*) (ApplicationAddress + 4);
- Jump_To_Application = (pFunction) JumpAddress;
- //初始化用户程序的堆栈指针
- __set_MSP(*(__IO uint32_t*) ApplicationAddress);
- printf("jump to app\r\n");
- CloseIrq();
- Jump_To_Application();
- }
- else
- {
- u16 data = 0xE0E0;
- eeprom_write_block(&data, PROGRAMS_ADDR, 2);
- printf("no app wait program\r\n");
- SoftReset();
- }
- }
- while (1)
- {
- }
- }
- #define Set_NVIC(Channel,Preemption,Sub,Cmd) { NVIC_InitStructure.NVIC_IRQChannel = Channel; \
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = Preemption; \
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = Sub; \
- NVIC_InitStructure.NVIC_IRQChannelCmd = Cmd; \
- NVIC_Init(&NVIC_InitStructure); }
-
-
-
-
- //************************************************************************************************
- //**名 称:NVIC_Init
- //**说 明:中断优先级初始化
- //**形 参:无
- //**返 回 值:无
- //************************************************************************************************
- void NVIC_Configuration (void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //4个抢占优先级,4个从优先级
- /* Configure the SysTick handler priority */
- NVIC_SetPriority(SysTick_IRQn, 0);//SysTick中断优先级设置 //非实时任务优先级最低 3,0
- Set_NVIC(USART2_IRQn , 2, 0,ENABLE) ;
- }
- /*******************************文件结束***************************************/
复制代码
所有资料51hei提供下载:
STM32远程升级.7z
(1.08 MB, 下载次数: 33)
|