user.c
3.28 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "user.h"
//保存固定参数
#define FLASH_CONST_DATA_SAVE_ADDRESS ADDR_FLASH_SECTOR_10
#define CONST_DATA_SAVE_LEN 16
SystemInfoUnion sysInfoUnion;
//系统信息保存到FLASH
//输入:起始地址、起始校验结果
//返回:数据保存后的结束地址
//校验结果更新到CheckSum
unsigned long SystemInfo_WriteFlash(unsigned long startAddress,unsigned long *CheckSum)
{
unsigned long i;
unsigned long address;
unsigned long *pData;
FLASH_Unlock();
address = 0;
pData = (unsigned long*)(&sysInfoUnion);
for(i = 0; i < sizeof(SystemInfoUnion)/4; address+=4,i++)
{
FLASH_ProgramWord(startAddress+address, pData[i]);
*CheckSum ^= pData[i];
}
FLASH_Lock();
return startAddress + address;
}
//从FLASH读取系统信息
unsigned long SystemInfo_ReadFlash(unsigned long startAddress)
{
unsigned long i;
unsigned long *pData;
unsigned long *pFlash;
pFlash = (unsigned long*)(startAddress);
pData = (unsigned long*)(&sysInfoUnion);
for(i = 0; i < sizeof(SystemInfoUnion)/4; i++)
{
pData[i] = pFlash[i];
}
return startAddress + sizeof(SystemInfoUnion);
}
//清除系统参数
void PowerOff_ClearConstFlash()
{
__set_PRIMASK(1); /* 关中断 */
FLASH_Unlock();
FLASH_EraseSector(bsp_GetSector(FLASH_CONST_DATA_SAVE_ADDRESS),VoltageRange_3);
FLASH_Lock();
__set_PRIMASK(0); /* 开中断 */
}
void PowerOff_HeadInfo_WriteInfo(unsigned long startAddress,unsigned long len,unsigned long checkSum)
{
FLASH_Unlock();
FLASH_ProgramWord(startAddress+0, len);
FLASH_ProgramWord(startAddress+4, checkSum);
FLASH_Lock();
}
unsigned char PowerOn_ReadFlash_ConstData()//读取系统参数
{
unsigned long i;
unsigned long CheckSum = 0;
unsigned long address;
unsigned long *pFlash;
pFlash = (unsigned long*)FLASH_CONST_DATA_SAVE_ADDRESS;
if(pFlash[0] > 0x4000)
{
return 0;
}
for(i = 0; i < pFlash[0]/4; i++)
{
CheckSum ^= pFlash[2+i];
}
if(CheckSum != pFlash[1])
{
return 0;
}
address = FLASH_CONST_DATA_SAVE_ADDRESS + 8;
address = SystemInfo_ReadFlash(address);
return 1;
}
void PowerOn_WriteFlash_ConstData()//写系统FLASH
{
unsigned long CheckSum = 0;
unsigned long address;
address = FLASH_CONST_DATA_SAVE_ADDRESS + 8;
address = SystemInfo_WriteFlash(address,&CheckSum);
address -= FLASH_CONST_DATA_SAVE_ADDRESS;
address -= 8;
PowerOff_HeadInfo_WriteInfo(FLASH_CONST_DATA_SAVE_ADDRESS,address,CheckSum);
}
void proccessSetupInfo(u8 *RecvBuffer,u16 BufferLength)
{
if(strncmp("$para", (const char*)RecvBuffer, 5) == 0)//地标动作命令
{
Setup_Process();
}
}
void ProccessSetupInfo(u8 Res)
{
static char Buffer[120];
static u16 iii = 0;
if(iii > 100)
{
iii = 0;
}
if(Res == 0x24) // $
{
iii = 0;
}
Buffer[iii++] = Res;
if(Res == 0x23) // #
{
proccessSetupInfo((u8 *)Buffer,iii);
iii = 0;
}
}
void initFactoryParam()
{
initSetup(sysInfoUnion.systemInfo.SystemParameter,FACTORYPARAMNUM);//设置参数
PowerOn_ReadFlash_ConstData();
while(1)
{
static int countTime = 0;
u8 buff;
if(ReadUart(COM1,&buff,1))
{
ProccessSetupInfo(buff);
}
bsp_DelayMS(1);
if(countTime++ > 5000)
{
break;
}
}
}