01
02
03
|
if
((*((uint32_t *)EXE_FLAG_ADDR))==0x12345678){
JumpToApplication(APP_START_ADDR);
}
|
01
02
03
04
05
06
07
|
if
((*((uint32_t *)EXE_FLAG_ADDR))==0xFFFFFFFF){
uint32_t ExeFlag = 0x12345678;
__set_PRIMASK(1);
//禁止全局中断
FLASH_Unlock();
ProgramDatatoFlash(EXE_FLAG_ADDR,(uint8_t*)(&ExeFlag),4);
FLASH_Lock();
}
|
01
02
03
04
05
06
07
08
09
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// USB2XXXTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include "../../USB2XXX/source/bootloader.h"
int
_tmain(
int
argc, _TCHAR* argv[])
{
int
PackSize = 1024*10;
int
TimeOut = 0;
int
ApplicationAddress = 0x08020000;
int
BootAddress = 0x08000000;
FW_INFO FwInfo;
bool
state;
int
ret;
//扫描查找设备
ret = BT_ScanDevice(
true
);
if
(ret <= 0){
printf
(
"No device connected!\n"
);
return
0;
}
//打开设备
state = BT_OpenDevice(0);
if
(!state){
printf
(
"Open device error!\n"
);
return
0;
}
//获取固件信息
BT_GetFirmwareInfo(0,&FwInfo);
printf
(
"Firmware Name:%s\n"
,FwInfo.FirmwareName);
printf
(
"Firmware Functions:%08X\n"
,FwInfo.Functions);
//判断当前固件是否为Bootloader固件
while
(!(FwInfo.Functions&FUNCTION_BOOTLOADER)){
//控制程序跳转到Bootloader
state = BT_ExcuteFirmware(0,BootAddress);
printf
(
"BT_ExcuteFirmware state = %d\n"
,state);
BT_CloseDevice(0);
do
{
Sleep(100);
ret = BT_ScanDevice(
true
);
//扫描查找设备
if
((ret <= 0)&&(TimeOut > 50)){
printf
(
"No device connected!\n"
);
return
0;
}
else
if
(ret > 0){
break
;
}
TimeOut++;
}
while
(ret<=0);
TimeOut = 0;
do
{
Sleep(100);
state = BT_OpenDevice(0);
//打开设备
if
((!state)&&(TimeOut > 50)){
printf
(
"Open device error!\n"
);
return
0;
}
else
if
(state){
break
;
}
TimeOut++;
}
while
(!state);
//获取固件信息
BT_GetFirmwareInfo(0,&FwInfo);
printf
(
"Firmware Name:%s\n"
,FwInfo.FirmwareName);
printf
(
"Firmware Functions:%08X\n"
,FwInfo.Functions);
}
//打开固件文件
FILE
*pFile=
fopen
(
"Project.bin"
,
"rb"
);
//获取文件的指针
fseek
(pFile,0,SEEK_END);
//把指针移动到文件的结尾 ,获取文件长度
int
FileLen=
ftell
(pFile);
//获取文件长度
static
char
*pBuf = (
char
*)
malloc
(FileLen);
//定义文件指针
if
(pBuf == NULL){
printf
(
"malloc error\n"
);
return
0;
}
rewind
(pFile);
//把指针移动到文件开头 因为我们一开始把指针移动到结尾,如果不移动回来 会出错
fread
(pBuf,1,FileLen,pFile);
//读文件
fclose
(pFile);
// 关闭文件
//擦除之前的固件
state = BT_EraseSectors(0,ApplicationAddress,ApplicationAddress+FileLen);
if
(!state){
printf
(
"BT_EraseSectors error!\n"
);
return
0;
}
else
{
printf
(
"BT_EraseSectors success\n"
);
}
//循环写入固件数据到芯片Flash
int
PackIndex = 0;
for
(PackIndex=0;PackIndex
state = BT_WriteData(0,ApplicationAddress+PackIndex*PackSize,(unsigned
char
*)(&pBuf[PackIndex*PackSize]),PackSize,0);
if
(!state){
printf
(
"BT_WriteData Error\n"
);
return
0;
}
}
if
(FileLen%PackSize){
state = BT_WriteData(0,ApplicationAddress+PackIndex*PackSize,(unsigned
char
*)(&pBuf[PackIndex*PackSize]),FileLen%PackSize,0);
if
(!state){
printf
(
"BT_WriteData Error\n"
);
return
0;
}
}
printf
(
"BT_WriteData Success\n"
);
//循环校验数据是否写成功
for
(PackIndex=0;PackIndex
state = BT_VerifyData(0,ApplicationAddress+PackIndex*PackSize,(unsigned
char
*)(&pBuf[PackIndex*PackSize]),PackSize);
if
(!state){
printf
(
"BT_VerifyData Error\n"
);
return
0;
}
}
if
(FileLen%PackSize){
state = BT_VerifyData(0,ApplicationAddress+PackIndex*PackSize,(unsigned
char
*)(&pBuf[PackIndex*PackSize]),FileLen%PackSize);
if
(!state){
printf
(
"BT_VerifyData Error\n"
);
return
0;
}
}
printf
(
"BT_VerifyData Success\n"
);
//执行固件
state = BT_ExcuteFirmware(0,ApplicationAddress);
printf
(
"BT_ExcuteFirmware state = %d\n"
,state);
//关闭设备
BT_CloseDevice(0);
return
0;
}
|