提交 | 用户 | age
|
bfc108
|
1 |
/**
|
Q |
2 |
******************************************************************************
|
|
3 |
* @file : KMachine.c
|
|
4 |
* @brief : KMachine program body
|
|
5 |
******************************************************************************
|
|
6 |
*/
|
|
7 |
|
|
8 |
#include "KMachine.h"
|
|
9 |
#include "string.h"
|
|
10 |
#include "Globaldef.h"
|
|
11 |
#include "stm32f0xx_hal.h"
|
|
12 |
|
|
13 |
//#define OB_BASE ((uint32_t)0x1FFFF800U) /*!< FLASH Option Bytes base address */
|
|
14 |
//#define FLASHSIZE_BASE ((uint32_t)0x1FFFF7CCU) /*!< FLASH Size register base address */
|
|
15 |
//#define UID_BASE ((uint32_t)0x1FFFF7ACU) /*!< Unique device ID register base address */
|
|
16 |
|
|
17 |
|
85d591
|
18 |
stStoredKMSysCfg storedKMSysCfg ;
|
bfc108
|
19 |
stKMem KMem;
|
Q |
20 |
stRunStat KMRunStat;
|
|
21 |
|
|
22 |
//uint8_t * pFlash1 = (uint8_t *)(STORECFGBASE);
|
|
23 |
|
|
24 |
//void * pConfigFlashBase = (uint8_t *)(STORECFGBASE);
|
|
25 |
|
|
26 |
//uint16_t FlashDatas[16];
|
|
27 |
|
|
28 |
//uint32_t * pUID = (uint32_t *)(UID_BASE);
|
85d591
|
29 |
const stKMInfoBlock KMInfoBlock =
|
Q |
30 |
{
|
|
31 |
BOARD_TYPE, //nDeviceType
|
|
32 |
0x0100, //ProgVer
|
|
33 |
0x0100, //KLinkVer
|
|
34 |
0x0100, //nCapacity
|
|
35 |
16, //nDInput;
|
|
36 |
16, //nDOutput
|
|
37 |
0, //nAInput
|
|
38 |
0, //nAOutput
|
|
39 |
0, //nHInput
|
|
40 |
0, //nHOutput
|
|
41 |
0, //nExt1;
|
|
42 |
0, //nExt2;
|
|
43 |
};
|
|
44 |
const char VersionStr[] __attribute__((at(FLASH_BASE + 0X1000))) //__attribute__((at(0X8001000)))
|
bfc108
|
45 |
= "3.00";
|
Q |
46 |
|
85d591
|
47 |
const stStoredKMSysCfg KMDefaultSysCfg /*__attribute__((at(STORECFGBASE)))*/ =
|
bfc108
|
48 |
{
|
85d591
|
49 |
START_SIGN,
|
bfc108
|
50 |
0x0000,
|
85d591
|
51 |
|
Q |
52 |
CFG_VER,
|
|
53 |
0x0000,
|
|
54 |
0x0000,
|
|
55 |
{0,0,0,0,0,0},
|
bfc108
|
56 |
{
|
Q |
57 |
{
|
85d591
|
58 |
1,
|
Q |
59 |
0,
|
|
60 |
2304, //Buadrate * 100;
|
bfc108
|
61 |
PortType_KLink, //PorttType
|
Q |
62 |
1, //ByteSize
|
|
63 |
0, //Parity
|
|
64 |
0, //StopBits
|
|
65 |
0, //EofChar
|
|
66 |
0, //SofChar
|
|
67 |
},
|
|
68 |
{
|
85d591
|
69 |
1,
|
Q |
70 |
0,
|
|
71 |
2304, //Buadrate * 100;
|
bfc108
|
72 |
PortType_KBus, //PorttType
|
Q |
73 |
1, //ByteSize
|
|
74 |
0, //Parity
|
|
75 |
0, //StopBits
|
|
76 |
0, //EofChar
|
|
77 |
0, //SofChar
|
|
78 |
}
|
|
79 |
},
|
|
80 |
{{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}},
|
|
81 |
{{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1},{0,1}},
|
|
82 |
0x0003,
|
|
83 |
0x0004,
|
|
84 |
0x0005,
|
|
85 |
0x0006,
|
|
86 |
0x0007,
|
|
87 |
0x0008,
|
|
88 |
0x0009,
|
|
89 |
0x000a,
|
|
90 |
0x000b,
|
|
91 |
0x000c,
|
|
92 |
0x000d,
|
|
93 |
0x000e,
|
|
94 |
0x000f,
|
|
95 |
0x0010,
|
|
96 |
{0},
|
|
97 |
0x0011,
|
85d591
|
98 |
END_SIGN,
|
bfc108
|
99 |
};
|
Q |
100 |
|
85d591
|
101 |
const stKMSysCfg KMDefaultSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/;
|
bfc108
|
102 |
|
85d591
|
103 |
int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize)
|
bfc108
|
104 |
{
|
Q |
105 |
// memcpy(pBuf,pAddrFlash,nSize);
|
85d591
|
106 |
for (int i=0;i<nByteSize/4;i++)
|
bfc108
|
107 |
{
|
Q |
108 |
((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i];
|
|
109 |
}
|
85d591
|
110 |
for (int i=nByteSize/4*2;i<nByteSize/2;i++)
|
bfc108
|
111 |
{
|
Q |
112 |
((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i];
|
|
113 |
}
|
85d591
|
114 |
return nByteSize;
|
bfc108
|
115 |
}
|
Q |
116 |
int EraseFlashMem(void * pAddrFlash, unsigned int Pages)
|
|
117 |
{
|
|
118 |
HAL_StatusTypeDef res;
|
|
119 |
res = HAL_FLASH_Unlock();
|
|
120 |
uint32_t ErrNo;
|
|
121 |
FLASH_EraseInitTypeDef erase1;
|
|
122 |
erase1.NbPages=Pages;
|
|
123 |
erase1.PageAddress=(unsigned int)pAddrFlash;
|
|
124 |
erase1.TypeErase=FLASH_TYPEERASE_PAGES;
|
|
125 |
res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
|
|
126 |
res = HAL_FLASH_Lock();
|
|
127 |
return res;
|
|
128 |
}
|
85d591
|
129 |
int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize)
|
bfc108
|
130 |
{
|
Q |
131 |
HAL_StatusTypeDef res;
|
|
132 |
res = HAL_FLASH_Unlock();
|
|
133 |
/*
|
|
134 |
for (int i=0;i<nSize/2;i++)
|
|
135 |
{
|
|
136 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
137 |
}
|
|
138 |
*/
|
|
139 |
///*
|
85d591
|
140 |
for (int i=0;i<nByteSize/4;i++)
|
bfc108
|
141 |
{
|
Q |
142 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)pAddrFlash + i*4, ((uint32_t *)pBuf)[i]);
|
|
143 |
}
|
|
144 |
|
85d591
|
145 |
for (int i = nByteSize/4 * 2 ; i < nByteSize/2 ; i++)
|
bfc108
|
146 |
{
|
Q |
147 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
148 |
}
|
|
149 |
//*/
|
|
150 |
res = HAL_FLASH_Lock();
|
|
151 |
|
|
152 |
return res;
|
|
153 |
}
|
85d591
|
154 |
int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize)
|
bfc108
|
155 |
{
|
Q |
156 |
|
|
157 |
HAL_StatusTypeDef res;
|
|
158 |
res = HAL_FLASH_Unlock();
|
|
159 |
uint32_t ErrNo;
|
|
160 |
FLASH_EraseInitTypeDef erase1;
|
|
161 |
erase1.NbPages=1;
|
|
162 |
erase1.PageAddress=(unsigned int)pAddrFlash;
|
|
163 |
erase1.TypeErase=FLASH_TYPEERASE_PAGES;
|
|
164 |
res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
|
|
165 |
|
85d591
|
166 |
for (int i=0;i<nByteSize/2;i++)
|
bfc108
|
167 |
{
|
Q |
168 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
169 |
}
|
|
170 |
/*
|
|
171 |
for (int i=0;i<nSize/4;i++)
|
|
172 |
{
|
|
173 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)pAddrFlash + i*4, ((uint32_t *)pBuf)[i]);
|
|
174 |
}
|
|
175 |
|
|
176 |
for (int i = nSize/4 * 2 ; i < nSize/2 ; i++)
|
|
177 |
{
|
|
178 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
179 |
}
|
|
180 |
*/
|
|
181 |
res = HAL_FLASH_Lock();
|
|
182 |
|
|
183 |
return res;
|
|
184 |
}
|
|
185 |
|
85d591
|
186 |
int ReadFactoryData(void * pDatabuf, int nByteCount)
|
bfc108
|
187 |
{
|
85d591
|
188 |
memcpy(pDatabuf,(stFactoryData *)FACTORY_DATA_BASE,nByteCount);
|
bfc108
|
189 |
return 0;
|
Q |
190 |
}
|
85d591
|
191 |
int WriteFactoryData(void * pDataBuf, int nByteCount)
|
bfc108
|
192 |
{
|
85d591
|
193 |
EraseAndWriteToFlashMem(pDataBuf, (stFactoryData *)FACTORY_DATA_BASE,nByteCount);
|
Q |
194 |
return 0;
|
|
195 |
}
|
|
196 |
|
|
197 |
int ReadProgram(int nProgByteAddr, void *pBuf, int nByteSize, int nBank)
|
|
198 |
{
|
|
199 |
if (nBank==0) {
|
|
200 |
ReadFlashMem(pBuf, (void *)(STORE_PRG_BASE+nProgByteAddr), nByteSize);
|
|
201 |
}else if (nBank ==1) {
|
|
202 |
ReadFlashMem(pBuf, (void *)(ALT_PRG_BASE+nProgByteAddr), nByteSize);
|
|
203 |
}else if (KMRunStat.nBinProgBank==0) {
|
|
204 |
ReadFlashMem(pBuf, (void *)(STORE_PRG_BASE+nProgByteAddr), nByteSize);
|
|
205 |
} else {
|
|
206 |
ReadFlashMem(pBuf, (void *)(ALT_PRG_BASE+nProgByteAddr), nByteSize);
|
|
207 |
}
|
|
208 |
return 0;
|
|
209 |
}
|
|
210 |
int WriteProgram(int nProgAddress, void * pBuf, int nByteSize, int nBank)
|
|
211 |
{
|
|
212 |
// Program Save Address;//
|
|
213 |
// Program 2 Save Address; //
|
|
214 |
void * progByteAddr;
|
|
215 |
if (nBank == 0) {
|
|
216 |
progByteAddr=(void *)(STORE_PRG_BASE+nProgAddress);
|
|
217 |
}else if (nBank==1) {
|
|
218 |
progByteAddr=(void *)(ALT_PRG_BASE+nProgAddress);
|
|
219 |
} else if (KMRunStat.nBinProgBank==0) {
|
|
220 |
progByteAddr=(void *)(ALT_PRG_BASE+nProgAddress);
|
|
221 |
}else{
|
|
222 |
progByteAddr=(void *)(STORE_PRG_BASE+nProgAddress);
|
|
223 |
}
|
|
224 |
if (nProgAddress ==0) {
|
|
225 |
EraseAndWriteToFlashMem(pBuf, progByteAddr, nByteSize);
|
|
226 |
}else{
|
|
227 |
WriteToFlashMemNoErase(pBuf, progByteAddr, nByteSize);
|
|
228 |
}
|
|
229 |
return 0;
|
|
230 |
}
|
|
231 |
|
|
232 |
int LoadDefaultSysCfg(pStoredKMSysCfg theStoredKMSysCfg)
|
|
233 |
{
|
|
234 |
memcpy(theStoredKMSysCfg,&KMDefaultSysCfg,sizeof(stKMSysCfg));
|
|
235 |
return 0;
|
|
236 |
}
|
|
237 |
int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg)
|
|
238 |
{
|
|
239 |
pStoredKMSysCfg pStoreKMSysCfg = (pStoredKMSysCfg)(STORE_SYSREG_BASE);
|
bfc108
|
240 |
// find latest Store Cfg
|
Q |
241 |
int nIndex=-1;
|
|
242 |
int nMaxSeq=-1;
|
|
243 |
for (int i=0;i<8;i++)
|
|
244 |
{
|
85d591
|
245 |
if (pStoreKMSysCfg->Sign1 == START_SIGN && pStoreKMSysCfg->EndSign1 == END_SIGN)
|
bfc108
|
246 |
{
|
Q |
247 |
if (pStoreKMSysCfg->Seq1 > nMaxSeq)
|
|
248 |
{
|
|
249 |
nIndex=i;nMaxSeq=pStoreKMSysCfg->Seq1;
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|
|
253 |
if (nIndex>=0 && nIndex <8)
|
|
254 |
{
|
85d591
|
255 |
ReadFlashMem(theStoredKMSysCfg,(void *)(&pStoreKMSysCfg[nIndex]),sizeof(stStoredKMSysCfg));
|
bfc108
|
256 |
}else {
|
85d591
|
257 |
LoadDefaultSysCfg(theStoredKMSysCfg);
|
bfc108
|
258 |
}
|
Q |
259 |
//memcpy(theKMSysCfg,(void* )STORECFGBASE,sizeof(KMSysCfg));
|
|
260 |
return 0;
|
|
261 |
}
|
|
262 |
|
85d591
|
263 |
int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg)
|
bfc108
|
264 |
{
|
85d591
|
265 |
theStoredKMSysCfg->Seq1++;
|
Q |
266 |
// theKMSysCfg->cfgvar16++;
|
bfc108
|
267 |
// find the next empty space to write
|
Q |
268 |
int nIndex=-1;
|
|
269 |
int s2=128;
|
|
270 |
for (int i=0;i<8;i++)
|
|
271 |
{
|
|
272 |
int skip=0;
|
|
273 |
unsigned char * nAddr2=(unsigned char *)(STORE_SYSREG_BASE+i*s2);
|
|
274 |
for (int j=0;j<s2;j++)
|
|
275 |
{
|
|
276 |
if ((nAddr2)[j] != 0xff) {skip =1;break;}
|
|
277 |
}
|
|
278 |
if (skip==1) {continue;}
|
|
279 |
nIndex=i;
|
|
280 |
break;
|
|
281 |
}
|
|
282 |
if (nIndex >=0 && nIndex <8) {
|
85d591
|
283 |
WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(theStoredKMSysCfg));
|
bfc108
|
284 |
}
|
Q |
285 |
else {
|
85d591
|
286 |
EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(theStoredKMSysCfg));
|
bfc108
|
287 |
}
|
Q |
288 |
return 0;
|
|
289 |
}
|
|
290 |
|
|
291 |
int is_pow_of_2(uint32_t x) {
|
|
292 |
return !(x & (x-1));
|
|
293 |
}
|
|
294 |
|
|
295 |
uint32_t next_pow_of_2(uint32_t x)
|
|
296 |
{
|
|
297 |
if ( is_pow_of_2(x) )
|
|
298 |
return x;
|
|
299 |
x |= x>>1;
|
|
300 |
x |= x>>2;
|
|
301 |
x |= x>>4;
|
|
302 |
x |= x>>8;
|
|
303 |
x |= x>>16;
|
|
304 |
return x+1;
|
|
305 |
}
|
|
306 |
|
|
307 |
//uint8_t * pFlash1;
|
|
308 |
/*
|
|
309 |
stStoreCfg * GetCurStoreCfgAddr(void )
|
|
310 |
{
|
|
311 |
int s = sizeof(stStoreCfg);
|
|
312 |
int s2=next_pow_of_2(s);
|
|
313 |
stStoreCfg * p1;
|
|
314 |
int nMaxSN=0;
|
|
315 |
int nMaxId=0;
|
|
316 |
for (int i=0; s2*i < STORECFGPAGESIZE ; i++)
|
|
317 |
{
|
|
318 |
p1= (stStoreCfg *)(STORECFGBASE + s2 * i );
|
|
319 |
if (p1->Sign1 != START_SIGN) continue;
|
|
320 |
if (p1->EndSign1 != END_SIGN) continue;
|
|
321 |
|
|
322 |
if (p1->Seq1 >= nMaxSN) {nMaxSN = p1->Seq1; nMaxId = i;}
|
|
323 |
}
|
|
324 |
// nMaxId=nMaxId+1;
|
|
325 |
return (stStoreCfg *)(STORECFGBASE + s2 * nMaxId);
|
|
326 |
}
|
|
327 |
|
|
328 |
stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg )
|
|
329 |
{
|
|
330 |
int s = sizeof(stStoreCfg);
|
|
331 |
int s2=next_pow_of_2(s);
|
|
332 |
uint32_t nAddr1 = (uint32_t) CurCfg;
|
|
333 |
uint32_t nAddr2 = nAddr1 + s2;
|
|
334 |
for (int i=1;i<33;i++)
|
|
335 |
{
|
|
336 |
int skip=0;
|
|
337 |
nAddr2 = nAddr1 + s2*i;
|
|
338 |
if ((nAddr2 + s) > STORECFGBASE + STORECFGPAGESIZE)
|
|
339 |
{
|
|
340 |
nAddr2=STORECFGBASE; break;
|
|
341 |
}
|
|
342 |
for (int j=0;j<s2;j++)
|
|
343 |
{
|
|
344 |
if (((unsigned char *)nAddr2)[j] != 0xff)
|
|
345 |
{skip =1;}
|
|
346 |
}
|
|
347 |
if (skip==1) {continue;}
|
|
348 |
break;
|
|
349 |
}
|
|
350 |
stStoreCfg * p1 = (stStoreCfg *)nAddr2;
|
|
351 |
return p1;
|
|
352 |
}
|
|
353 |
|
|
354 |
|
|
355 |
int SaveStoreCfg(stStoreCfg * CurCfg)
|
|
356 |
{
|
|
357 |
return 0;
|
|
358 |
}
|
|
359 |
*/
|
|
360 |
// stStoreCfg Cfg2;
|
|
361 |
|
|
362 |
int LoadFlashDatas()
|
|
363 |
{
|
|
364 |
for (int i=0;i<16;i++)
|
|
365 |
{
|
|
366 |
// FlashDatas[i]=((uint16_t *)pConfigFlashBase)[i];
|
|
367 |
}
|
|
368 |
return 0;
|
|
369 |
}
|
|
370 |
/*
|
|
371 |
int LoadAndUpdateStoreCfg()
|
|
372 |
{
|
|
373 |
stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
|
|
374 |
|
|
375 |
Cfg2.Sign1=START_SIGN;
|
|
376 |
Cfg2.Seq1=pFCfg[0].Seq1+1;
|
|
377 |
Cfg2.CRC1=0x7777;
|
|
378 |
Cfg2.PowerCount=pFCfg[0].PowerCount+1;
|
|
379 |
Cfg2.UpTime=pFCfg[0].UpTime+1;
|
|
380 |
Cfg2.UserData1=pFCfg[0].UserData1;
|
|
381 |
Cfg2.EndSign1=END_SIGN;
|
|
382 |
stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);
|
|
383 |
|
|
384 |
|
|
385 |
HAL_StatusTypeDef res;
|
|
386 |
if (pFCfg2 <= pFCfg)
|
|
387 |
{
|
|
388 |
res = (HAL_StatusTypeDef)EraseAndWriteToFlashMem(&Cfg2,pFCfg2,sizeof(stStoreCfg));
|
|
389 |
|
|
390 |
}else
|
|
391 |
{
|
|
392 |
res = (HAL_StatusTypeDef)WriteToFlashMemNoErase(&Cfg2,pFCfg2,sizeof(stStoreCfg));
|
|
393 |
}
|
|
394 |
return res;
|
|
395 |
}
|
|
396 |
*/
|
|
397 |
int CheckSavedData(void * pStartAddr, int PageSize, int Pages, int DataSize)
|
|
398 |
{
|
|
399 |
return 0;
|
|
400 |
};
|
|
401 |
|
|
402 |
|
|
403 |
int nMaxRunStatIndex=-1;
|
|
404 |
unsigned int nMaxRunStatSeq=0;
|
|
405 |
int nNextRunStatSpace=0;
|
|
406 |
int LoadDefaultRunStat(pRunStat theRunStat)
|
|
407 |
{
|
|
408 |
return 0;
|
|
409 |
}
|
|
410 |
int LoadRunStat(pRunStat theRunStat)
|
|
411 |
{
|
|
412 |
uchar * pRunStatStore = (uchar *)STORE_RUNSTAT_BASE;
|
|
413 |
pRunStat pStoreRunStats = (pRunStat)pRunStatStore;
|
|
414 |
// int s = sizeof(stRunStat);
|
|
415 |
|
|
416 |
for (int i=0;i * sizeof(stRunStat) < (STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES) ;i++)
|
|
417 |
{
|
|
418 |
if (pStoreRunStats[i].Sign1 == START_SIGN )
|
|
419 |
{
|
|
420 |
if (pStoreRunStats[i].Seq1 > nMaxRunStatSeq)
|
|
421 |
{
|
|
422 |
nMaxRunStatSeq = pStoreRunStats[i].Seq1;
|
|
423 |
nMaxRunStatIndex=i;
|
|
424 |
nNextRunStatSpace=i+1;
|
|
425 |
}
|
|
426 |
}
|
|
427 |
}
|
|
428 |
if (nMaxRunStatIndex>=0) // && nMaxRunStatIndex <8)
|
|
429 |
{
|
|
430 |
ReadFlashMem(theRunStat,(void *)(pStoreRunStats+nMaxRunStatIndex),sizeof(stRunStat));
|
|
431 |
}else {
|
|
432 |
LoadDefaultRunStat(theRunStat);
|
|
433 |
}
|
|
434 |
// find Next Space
|
|
435 |
// if Same Page with MaxSeq Index, then not erase, skip and skip.
|
|
436 |
// if next Page of MaxSeq Index, then earse if not empty;
|
|
437 |
if ((nNextRunStatSpace + 1) * sizeof(stRunStat) > STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES) {
|
|
438 |
nNextRunStatSpace=0;
|
|
439 |
}
|
|
440 |
return 0;
|
|
441 |
}
|
|
442 |
|
|
443 |
int SaveRunStat(pRunStat theRunStat)
|
|
444 |
{
|
|
445 |
nMaxRunStatSeq++;
|
|
446 |
theRunStat->Sign1=START_SIGN;
|
|
447 |
theRunStat->Seq1 = nMaxRunStatSeq;
|
|
448 |
theRunStat->PowerCount=KMem.PwrOnCount;
|
|
449 |
theRunStat->UpTime=KMem.TotalRunTime;
|
|
450 |
theRunStat->CRC1=0x11;
|
|
451 |
theRunStat->EndSign1=END_SIGN;
|
|
452 |
|
|
453 |
//check empty
|
|
454 |
unsigned char *pFlash = (unsigned char *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat));
|
|
455 |
int Skip=0;
|
|
456 |
for (int j=0;j<sizeof(stRunStat);j++)
|
|
457 |
{
|
|
458 |
if (pFlash[j]!=0xff) {Skip =1 ; break;}
|
|
459 |
}
|
|
460 |
if (Skip ==0 )
|
|
461 |
{
|
|
462 |
WriteToFlashMemNoErase(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat));
|
|
463 |
}else
|
|
464 |
{
|
|
465 |
EraseAndWriteToFlashMem(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat));
|
|
466 |
}
|
|
467 |
nMaxRunStatIndex=nNextRunStatSpace;
|
|
468 |
nNextRunStatSpace++;
|
|
469 |
if ((nNextRunStatSpace+1) * sizeof(stRunStat) > STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES)
|
|
470 |
{
|
|
471 |
nNextRunStatSpace=0;
|
|
472 |
}
|
|
473 |
return 0;
|
|
474 |
}
|
|
475 |
|
|
476 |
|
|
477 |
int nEventCount=0;
|
|
478 |
int nEventMinIndex;
|
|
479 |
int nEventMaxIndex;
|
|
480 |
unsigned int nEventMaxSeq=0;
|
|
481 |
int nEventNextSpace;
|
|
482 |
int nMaxCurTime=0;
|
85d591
|
483 |
volatile int PowerDownEvent=0;
|
Q |
484 |
volatile int OldPowerDownEvent=0;
|
|
485 |
volatile int OldPowerDownEventTime=0;
|
|
486 |
|
bfc108
|
487 |
int CheckEventLog()
|
Q |
488 |
{
|
|
489 |
unsigned int nMinEventSeq=999999999;
|
|
490 |
uchar * pEventStore = (uchar *)STORE_LOG_BASE;
|
|
491 |
pEventLog theEventLog = (pEventLog) pEventStore;
|
|
492 |
// int s = sizeof(stEventLog);
|
|
493 |
nEventCount=0;
|
|
494 |
|
|
495 |
for (int i=0;i * sizeof(stEventLog) < (STORE_LOG_PAGESIZE * STORE_LOG_PAGES) ;i++)
|
|
496 |
{
|
|
497 |
if (theEventLog[i].Sign1 == START_SIGN )
|
|
498 |
{
|
|
499 |
nEventCount++;
|
|
500 |
if (theEventLog[i].Seq1 > nEventMaxSeq)
|
|
501 |
{
|
|
502 |
nEventMaxSeq = theEventLog[i].Seq1;
|
|
503 |
nEventMaxIndex=i;
|
|
504 |
nMaxCurTime=theEventLog[i].nTime;
|
|
505 |
nEventNextSpace=i+1;
|
|
506 |
}
|
|
507 |
if (theEventLog[i].Seq1 < nMinEventSeq)
|
|
508 |
{
|
|
509 |
nMinEventSeq = theEventLog[i].Seq1;
|
|
510 |
nEventMinIndex = i;
|
|
511 |
}
|
|
512 |
}
|
|
513 |
}
|
|
514 |
// find Next Space
|
|
515 |
// if Same Page with MaxSeq Index, then not erase, skip and skip.
|
|
516 |
// if next Page of MaxSeq Index, then earse if not empty;
|
|
517 |
if ((nEventNextSpace + 1) * sizeof(stEventLog) > STORE_LOG_PAGESIZE * STORE_LOG_PAGES) {
|
|
518 |
nEventNextSpace=0;
|
|
519 |
}
|
|
520 |
|
|
521 |
return nEventCount;
|
|
522 |
}
|
|
523 |
|
|
524 |
int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2)
|
|
525 |
{
|
|
526 |
nEventMaxSeq++;
|
|
527 |
stEventLog thisEventLog={START_SIGN, nEventMaxSeq, nTime,nEvent,nParam1,nParam2};
|
|
528 |
//check empty
|
|
529 |
unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog));
|
|
530 |
int Skip=0;
|
|
531 |
for (int j=0;j<sizeof(stEventLog);j++)
|
|
532 |
{
|
|
533 |
if (pFlash[j]!=0xff) {Skip =1 ; break;}
|
|
534 |
}
|
|
535 |
if (Skip ==0 )
|
|
536 |
{
|
|
537 |
WriteToFlashMemNoErase(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog));
|
|
538 |
}else
|
|
539 |
{
|
|
540 |
EraseAndWriteToFlashMem(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog));
|
|
541 |
|
|
542 |
}
|
|
543 |
nEventMaxIndex=nEventNextSpace;
|
|
544 |
nEventNextSpace++;
|
|
545 |
if ((nEventNextSpace+1) * sizeof(stEventLog) > STORE_LOG_PAGESIZE * STORE_LOG_PAGES)
|
|
546 |
{
|
|
547 |
nEventNextSpace=0;
|
|
548 |
}
|
|
549 |
nEventCount++;
|
|
550 |
KMem.nEventCount=nEventCount;
|
|
551 |
return 0;
|
|
552 |
}
|
|
553 |
|
|
554 |
pEventLog GetEventLogAddr(int nIndex)
|
|
555 |
{
|
|
556 |
int nEventIndex=nEventMinIndex + nIndex;
|
|
557 |
|
|
558 |
if (nEventIndex * sizeof(stEventLog) >= (STORE_LOG_PAGESIZE * STORE_LOG_PAGES))
|
|
559 |
{
|
|
560 |
nEventIndex -= (STORE_LOG_PAGESIZE * STORE_LOG_PAGES)/sizeof(stEventLog);
|
|
561 |
}
|
|
562 |
unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventIndex*sizeof(stEventLog));
|
|
563 |
|
|
564 |
return (pEventLog)pFlash;
|
|
565 |
}
|
|
566 |
|
|
567 |
int ClearEventLog(void)
|
|
568 |
{
|
|
569 |
EraseFlashMem((void *)STORE_LOG_BASE,STORE_LOG_PAGES);
|
|
570 |
nEventMinIndex=0;
|
|
571 |
nEventMaxIndex=0;
|
|
572 |
nEventMaxSeq=0;
|
|
573 |
nEventCount=0;
|
|
574 |
nEventNextSpace=0;
|
|
575 |
return 0;
|
|
576 |
}
|
|
577 |
int KMachineInit(void)
|
|
578 |
{
|
|
579 |
// ClearEventLog();
|
|
580 |
CheckEventLog();
|
|
581 |
LoadRunStat(&KMRunStat);
|
|
582 |
KMem.CurTimeSec=nMaxCurTime;
|
|
583 |
KMem.TotalRunTime=KMRunStat.UpTime;
|
|
584 |
KMRunStat.PowerCount++;
|
|
585 |
KMem.PwrOnCount=KMRunStat.PowerCount;
|
|
586 |
SaveRunStat(&KMRunStat);
|
|
587 |
KMem.SDD[15]=nMaxRunStatIndex;
|
|
588 |
KMem.SDD[16]=nMaxRunStatSeq;
|
|
589 |
KMem.SDD[17]=nNextRunStatSpace;
|
|
590 |
|
|
591 |
|
|
592 |
AddEventLog(KMem.CurTimeSec,EventTypePowerUp,1,12345);
|
|
593 |
KMem.SDD[19]=nEventCount;
|
|
594 |
KMem.SDD[20]=nEventMinIndex;
|
|
595 |
KMem.SDD[21]=nEventMaxIndex;
|
|
596 |
KMem.SDD[22]=nEventMaxSeq;
|
|
597 |
KMem.SDD[23]=nEventNextSpace;
|
|
598 |
|
|
599 |
return 0;
|
|
600 |
}
|
85d591
|
601 |
|
Q |
602 |
inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr)
|
|
603 |
{
|
|
604 |
(*pW)|=1<<(bitAddr&0xf);
|
|
605 |
}
|
|
606 |
|
|
607 |
inline void ResetBit(unsigned short * pW, unsigned char bitAddr)
|
|
608 |
{
|
|
609 |
(*pW)&=~(1<<(bitAddr&0xf));
|
|
610 |
}
|
|
611 |
|
|
612 |
static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value)
|
|
613 |
{
|
|
614 |
if (Value) { SetAddrBit(pW, bitAddr);}
|
|
615 |
else {ResetBit(pW, bitAddr);}
|
|
616 |
}
|
|
617 |
|
|
618 |
static inline unsigned char GetBitValue(unsigned short W, unsigned char bitAddr)
|
|
619 |
{
|
|
620 |
if (W&(1<<(bitAddr&0xf))) return 1;
|
|
621 |
else return 0;
|
|
622 |
}
|
|
623 |
|
|
624 |
|
|
625 |
unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr)
|
|
626 |
{
|
|
627 |
unsigned char thisValue=0;
|
|
628 |
unsigned short nWordAddr=(nCoilAddr&0xff0)>>4;
|
|
629 |
unsigned char nBitAddr=nCoilAddr&0xf;
|
|
630 |
switch(nCoilType)
|
|
631 |
{
|
|
632 |
case KLCoilTypeX:
|
|
633 |
if (nCoilAddr >= KLCoilXCount) return 0;
|
|
634 |
thisValue = GetBitValue(KMem.WX[nWordAddr], nBitAddr);
|
|
635 |
break;
|
|
636 |
case KLCoilTypeY:
|
|
637 |
if (nCoilAddr >= KLCoilYCount) return 0;
|
|
638 |
thisValue = GetBitValue(KMem.WY[nWordAddr], nBitAddr);
|
|
639 |
break;
|
|
640 |
case KLCoilTypeR:
|
|
641 |
if (nCoilAddr >= KLCoilRCount) return 0;
|
|
642 |
thisValue = GetBitValue(KMem.WR[nWordAddr], nBitAddr);
|
|
643 |
break;
|
|
644 |
case KLCoilTypeLX:
|
|
645 |
if (nCoilAddr >= KLCoilLXCount) return 0;
|
|
646 |
thisValue = GetBitValue(KMem.WLX[nWordAddr], nBitAddr);
|
|
647 |
break;
|
|
648 |
case KLCoilTypeLY:
|
|
649 |
if (nCoilAddr >= KLCoilLYCount) return 0;
|
|
650 |
thisValue = GetBitValue(KMem.WLY[nWordAddr], nBitAddr);
|
|
651 |
break;
|
|
652 |
case KLCoilTypeT:
|
|
653 |
if (nCoilAddr >= KLCoilTCount) return 0;
|
|
654 |
thisValue = GetBitValue(KMem.WT[nWordAddr], nBitAddr);
|
|
655 |
break;
|
|
656 |
case KLCoilTypeC:
|
|
657 |
if (nCoilAddr >= KLCoilCCount) return 0;
|
|
658 |
thisValue = GetBitValue(KMem.WC[nWordAddr], nBitAddr);
|
|
659 |
break;
|
|
660 |
case KLCoilTypeLR:
|
|
661 |
if (nCoilAddr >= KLCoilLRCount) return 0;
|
|
662 |
thisValue = GetBitValue(KMem.WLR[nWordAddr], nBitAddr);
|
|
663 |
break;
|
|
664 |
case KLCoilTypeSR:
|
|
665 |
if (nCoilAddr >= KLCoilSRCount) return 0;
|
|
666 |
thisValue = GetBitValue(KMem.WSR[nWordAddr], nBitAddr);
|
|
667 |
break;
|
|
668 |
default:
|
|
669 |
break;
|
|
670 |
}
|
|
671 |
return thisValue;
|
|
672 |
}
|
|
673 |
int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue)
|
|
674 |
{
|
|
675 |
unsigned short nWordAddr=(nCoilAddr&0xff0)>>4;
|
|
676 |
unsigned char nBitAddr=nCoilAddr&0xf;
|
|
677 |
switch(nCoilType)
|
|
678 |
{
|
|
679 |
case KLCoilTypeX:
|
|
680 |
if (nCoilAddr >= KLCoilXCount) return 0;
|
|
681 |
SetBitValue(&KMem.WX[nWordAddr], nBitAddr, nCoilValue);
|
|
682 |
break;
|
|
683 |
case KLCoilTypeY:
|
|
684 |
if (nCoilAddr >= KLCoilYCount) return 0;
|
|
685 |
SetBitValue(&KMem.WY[nWordAddr], nBitAddr, nCoilValue);
|
|
686 |
break;
|
|
687 |
case KLCoilTypeR:
|
|
688 |
if (nCoilAddr >= KLCoilRCount) return 0;
|
|
689 |
SetBitValue(&KMem.WR[nWordAddr], nBitAddr, nCoilValue);
|
|
690 |
break;
|
|
691 |
case KLCoilTypeLX:
|
|
692 |
if (nCoilAddr >= KLCoilLXCount) return 0;
|
|
693 |
SetBitValue(&KMem.WLX[nWordAddr], nBitAddr, nCoilValue);
|
|
694 |
break;
|
|
695 |
case KLCoilTypeLY:
|
|
696 |
if (nCoilAddr >= KLCoilLYCount) return 0;
|
|
697 |
SetBitValue(&KMem.WLY[nWordAddr], nBitAddr, nCoilValue);
|
|
698 |
break;
|
|
699 |
case KLCoilTypeT:
|
|
700 |
if (nCoilAddr >= KLCoilTCount) return 0;
|
|
701 |
SetBitValue(&KMem.WT[nWordAddr], nBitAddr, nCoilValue);
|
|
702 |
break;
|
|
703 |
case KLCoilTypeC:
|
|
704 |
if (nCoilAddr >= KLCoilCCount) return 0;
|
|
705 |
SetBitValue(&KMem.WC[nWordAddr], nBitAddr, nCoilValue);
|
|
706 |
break;
|
|
707 |
case KLCoilTypeLR:
|
|
708 |
if (nCoilAddr >= KLCoilLRCount) return 0;
|
|
709 |
SetBitValue(&KMem.WLR[nWordAddr], nBitAddr, nCoilValue);
|
|
710 |
break;
|
|
711 |
case KLCoilTypeSR:
|
|
712 |
if (nCoilAddr >= KLCoilSRCount) return 0;
|
|
713 |
SetBitValue(&KMem.WSR[nWordAddr], nBitAddr, nCoilValue);
|
|
714 |
break;
|
|
715 |
default:
|
|
716 |
break;
|
|
717 |
}
|
|
718 |
return 0;
|
|
719 |
}
|
|
720 |
|
|
721 |
int GetVarData(int nDataType, int nDataAddr)
|
|
722 |
{
|
|
723 |
// TODO: ?????????.
|
|
724 |
int thisValue = 0;
|
|
725 |
|
|
726 |
switch (nDataType)
|
|
727 |
{
|
|
728 |
case KLDataTypeDEC:
|
|
729 |
case KLDataTypeHEX:
|
|
730 |
thisValue = nDataAddr;
|
|
731 |
break;
|
|
732 |
case KLDataTypeWX:
|
|
733 |
if (nDataAddr >= KLDataWXCount) return 0;
|
|
734 |
thisValue = KMem.WX[nDataAddr];
|
|
735 |
break;
|
|
736 |
case KLDataTypeWY:
|
|
737 |
if (nDataAddr >= KLDataWYCount) return 0;
|
|
738 |
thisValue = KMem.WY[nDataAddr];
|
|
739 |
break;
|
|
740 |
case KLDataTypeWR:
|
|
741 |
if (nDataAddr >= KLDataWRCount) return 0;
|
|
742 |
thisValue = KMem.WR[nDataAddr];
|
|
743 |
break;
|
|
744 |
case KLDataTypeWLX:
|
|
745 |
if (nDataAddr >= KLDataWLCount) return 0;
|
|
746 |
thisValue = KMem.WLX[nDataAddr];
|
|
747 |
break;
|
|
748 |
case KLDataTypeWLY:
|
|
749 |
if (nDataAddr >= KLDataWLCount) return 0;
|
|
750 |
thisValue = KMem.WLY[nDataAddr];
|
|
751 |
break;
|
|
752 |
case KLDataTypeDT:
|
|
753 |
if (nDataAddr >= KLDataDTCount) return 0;
|
|
754 |
thisValue = (signed short)KMem.DT[nDataAddr];
|
|
755 |
break;
|
|
756 |
case KLDataTypeSDT:
|
|
757 |
if (nDataAddr >= KLDataSDTCount) return 0;
|
|
758 |
thisValue = KMem.SDT[nDataAddr];
|
|
759 |
break;
|
|
760 |
case KLDataTypeWSR:
|
|
761 |
if (nDataAddr >= KLCoilLRCount) return 0;
|
|
762 |
thisValue = KMem.WSR[nDataAddr];
|
|
763 |
break;
|
|
764 |
case KLDataTypeSV:
|
|
765 |
if (nDataAddr >= KLDataSVCount) return 0;
|
|
766 |
thisValue = KMem.SV[nDataAddr];
|
|
767 |
break;
|
|
768 |
case KLDataTypeEV:
|
|
769 |
if (nDataAddr >= KLDataEVCount) return 0;
|
|
770 |
thisValue = KMem.EV[nDataAddr];
|
|
771 |
break;
|
|
772 |
case KLDataTypeLD:
|
|
773 |
if (nDataAddr >= KLDataLDCount) return 0;
|
|
774 |
thisValue = KMem.DT[nDataAddr];
|
|
775 |
break;
|
|
776 |
case KLDataSysCfg:
|
|
777 |
if (nDataAddr >= KLCoilSRCount) return 0;
|
|
778 |
thisValue = KMem.SDT[nDataAddr];
|
|
779 |
break;
|
|
780 |
case KLDataTypeFlash:
|
|
781 |
if (nDataAddr >= KLCoilSRCount) return 0;
|
|
782 |
thisValue = KMem.SDT[nDataAddr];
|
|
783 |
break;
|
|
784 |
case KLDataTypeTest:
|
|
785 |
if (nDataAddr >= KLCoilSRCount) return 0;
|
|
786 |
thisValue = KMem.SDT[nDataAddr];
|
|
787 |
break;
|
|
788 |
}
|
|
789 |
return thisValue;
|
|
790 |
}
|
|
791 |
|
|
792 |
|
|
793 |
int SetVarData(int nDataType, int nDataAddr, int nDataValue)
|
|
794 |
{
|
|
795 |
// TODO: ?????????.
|
|
796 |
switch (nDataType)
|
|
797 |
{
|
|
798 |
// case KLDataTypeDEC:
|
|
799 |
// case KLDataTypeHEX:
|
|
800 |
// break;
|
|
801 |
case KLDataTypeWX:
|
|
802 |
if (nDataAddr >= KLDataWXCount) return 0;
|
|
803 |
KMem.WX[nDataAddr] = nDataValue;
|
|
804 |
break;
|
|
805 |
case KLDataTypeWY:
|
|
806 |
if (nDataAddr >= KLDataWYCount) return 0;
|
|
807 |
KMem.WY[nDataAddr] = nDataValue;
|
|
808 |
break;
|
|
809 |
case KLDataTypeWR:
|
|
810 |
if (nDataAddr >= KLDataWRCount) return 0;
|
|
811 |
KMem.WR[nDataAddr] = nDataValue;
|
|
812 |
break;
|
|
813 |
case KLDataTypeWLX:
|
|
814 |
if (nDataAddr >= KLDataWLCount) return 0;
|
|
815 |
KMem.WLX[nDataAddr] = nDataValue;
|
|
816 |
break;
|
|
817 |
case KLDataTypeWLY:
|
|
818 |
if (nDataAddr >= KLDataWLCount) return 0;
|
|
819 |
KMem.WLY[nDataAddr] = nDataValue;
|
|
820 |
break;
|
|
821 |
case KLDataTypeDT:
|
|
822 |
if (nDataAddr >= KLDataDTCount) return 0;
|
|
823 |
KMem.DT[nDataAddr] = nDataValue;
|
|
824 |
break;
|
|
825 |
case KLDataTypeSDT:
|
|
826 |
if (nDataAddr >= KLDataSDTCount) return 0;
|
|
827 |
KMem.SDT[nDataAddr] = nDataValue;
|
|
828 |
break;
|
|
829 |
case KLDataTypeWSR:
|
|
830 |
if (nDataAddr >= KLCoilLRCount) return 0;
|
|
831 |
KMem.WSR[nDataAddr] = nDataValue;
|
|
832 |
break;
|
|
833 |
case KLDataTypeSV:
|
|
834 |
if (nDataAddr >= KLDataSVCount) return 0;
|
|
835 |
KMem.SV[nDataAddr] = nDataValue;
|
|
836 |
break;
|
|
837 |
case KLDataTypeEV:
|
|
838 |
if (nDataAddr >= KLDataEVCount) return 0;
|
|
839 |
KMem.EV[nDataAddr] = nDataValue;
|
|
840 |
break;
|
|
841 |
case KLDataTypeLD:
|
|
842 |
if (nDataAddr >= KLDataLDCount) return 0;
|
|
843 |
KMem.DT[nDataAddr] = nDataValue;
|
|
844 |
break;
|
|
845 |
case KLDataSysCfg:
|
|
846 |
if (nDataAddr >= KLCoilSRCount) return 0;
|
|
847 |
KMem.SDT[nDataAddr] = nDataValue;
|
|
848 |
break;
|
|
849 |
case KLDataTypeFlash:
|
|
850 |
if (nDataAddr >= KLCoilSRCount) return 0;
|
|
851 |
KMem.SDT[nDataAddr] = nDataValue;
|
|
852 |
break;
|
|
853 |
case KLDataTypeTest:
|
|
854 |
if (nDataAddr >= KLCoilSRCount) return 0;
|
|
855 |
KMem.SDT[nDataAddr] = nDataValue;
|
|
856 |
break;
|
|
857 |
}
|
|
858 |
|
|
859 |
return 0;
|
|
860 |
}
|
|
861 |
|