main.c
2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**************************************************************************
* STM32F407ZET6芯片有512K的FLASH,有192K的RAM(128K SRAM和64K CCM)
*
* Bootloader功能介绍:
* 在芯片上电之后,首先运行的是Bootloader,Bootloader引导程序根据Flash上Flag段
* 的数据选择将哪一部分的固件加载到运行区间。
*
* BootLoader预留空间为16K
****************************************************************************/
#include "delay.h"
#include "string.h"
#include "usart1.h"
#include "uart5.h"
#include "iap.h"
#include "flash.h"
int main(void)
{
uint32_t handle;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
delay_init();
uart1_init();
uart5_init();
Detection_Update(); //本地升级
handle = STMFLASH_ReadWord(FLAG_BASE_ADDR);
//检测是否OTA
//检测是否本地
//执行
switch(handle) //选择加载那一部分的固件
{
case BOOTLOADER_RUN: //运行段
printf("\r\n**********Runing Firmware **********\r\n");
delay_ms(20);
Run_Flash_App(APP_RUN_ADDR);
break;
case BOOTLOADER_LOAD: //升级段
printf("\r\n**********Loading Firmware **********\r\n");
#if 0
handle = APP_RUN_ADDR;
STMFLASH_Write(APP_BACK_ADDR,(uint32_t *)handle,128*1024);
delay_ms(10);
#endif
handle = APP_LOAD_ADDR;
STMFLASH_Write(APP_RUN_ADDR,(uint32_t *)handle,192*1024);
delay_ms(10);
handle = BOOTLOADER_RUN;
STMFLASH_Write(FLAG_BASE_ADDR,(uint32_t *)&handle,4);
Run_Flash_App(APP_RUN_ADDR);
break;
#if 0
case BOOTLOADER_BACK: //回滚段
printf("\r\n**********Backup Firmware **********\r\n");
handle = APP_BACK_ADDR;
STMFLASH_Write(APP_RUN_ADDR,(uint32_t *)handle,128*1024);
delay_ms(10);
handle = BOOTLOADER_RUN;
STMFLASH_Write(FLAG_BASE_ADDR,(uint32_t *)&handle,4);
delay_ms(10);
Run_Flash_App(APP_RUN_ADDR);
break;
#endif
case BOOTLOADER_DEFAULT: //利于出厂(默认加载这一段)
printf("\r\n**********Default Firmware **********\r\n");
handle = BOOTLOADER_RUN;
STMFLASH_Write(FLAG_BASE_ADDR,(uint32_t *)&handle,4);
Run_Flash_App(APP_RUN_ADDR);
break;
default :
printf("\r\n**********Don^t Having Firmware **********\r\n");
break;
}
while(1) //预留通过网口升级的接口(CH395Q)
{
printf("\r\n********IDLE********\r\n");
delay_ms(5000);
}
}