main.c
2.44 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
98
99
100
101
102
/**************************************************************************
* STM32F407ZET6芯片有512K的FLASH,有192K的RAM(128K SRAM和64K CCM)
*
* Bootloader功能介绍:
* 在芯片上电之后,首先运行的是Bootloader,Bootloader引导程序根据Flash上Flag段
* 的数据选择将哪一部分的固件加载到运行区间。
*
* BootLoader预留空间为16K
****************************************************************************/
#include "delay.h"
#include "usart.h"
#include "iap.h"
#include "flash.h"
#include "timer.h"
#include "fsmc.h"
int main(void)
{
uint32_t handle;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组4
Delay_init();
USART1_Config(115200); //debug
UART5_Config(115200); //IAP
TIM3_Config(2048,2048); //Fram control
FSMC_SRAM_Init(); //EXT SRAM
printf("\r\n**********BootLoader**********\r\n");
memset(UART_RX_BUF,0,USART_REC_LEN);
Detection_Update(); //本地升级
handle = STMFLASH_ReadWord(FLAG_BASE_ADDR);
switch(handle) //选择加载那一部分的固件
{
case BOOTLOADER_RUN: //运行段
printf("\r\n**********Runing Firmware **********\r\n");
Tick_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);
Tick_Delay_ms(10);
#endif
handle = APP_LOAD_ADDR;
STMFLASH_Write(APP_RUN_ADDR,(uint32_t *)handle,128*1024);
Tick_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);
Tick_Delay_ms(10);
handle = BOOTLOADER_RUN;
STMFLASH_Write(FLAG_BASE_ADDR,(uint32_t *)&handle,4);
Tick_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");
Tick_Delay_ms(5000);
}
}