提交 | 用户 | 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 |
|
|
18 |
stKMSysCfg KMSysCfg ;
|
|
19 |
stKMem KMem;
|
|
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);
|
|
29 |
|
|
30 |
const char VersionStr[] __attribute__((at(0X8001000)))
|
|
31 |
= "3.00";
|
|
32 |
|
|
33 |
const stKMSysCfg KMStoreSysCfg /*__attribute__((at(STORECFGBASE)))*/ =
|
|
34 |
{
|
|
35 |
0x55aa,
|
|
36 |
0x0000,
|
|
37 |
0x00000000,
|
|
38 |
{
|
|
39 |
{
|
|
40 |
PortType_KLink, //PorttType
|
|
41 |
1, //ByteSize
|
|
42 |
0, //Parity
|
|
43 |
0, //StopBits
|
|
44 |
0, //EofChar
|
|
45 |
0, //SofChar
|
|
46 |
2304, //Buadrate * 100;
|
|
47 |
},
|
|
48 |
{
|
|
49 |
PortType_KBus, //PorttType
|
|
50 |
1, //ByteSize
|
|
51 |
0, //Parity
|
|
52 |
0, //StopBits
|
|
53 |
0, //EofChar
|
|
54 |
0, //SofChar
|
|
55 |
2304, //Buadrate * 100;
|
|
56 |
}
|
|
57 |
},
|
|
58 |
{{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}},
|
|
59 |
{{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}},
|
|
60 |
0x0003,
|
|
61 |
0x0004,
|
|
62 |
0x0005,
|
|
63 |
0x0006,
|
|
64 |
0x0007,
|
|
65 |
0x0008,
|
|
66 |
0x0009,
|
|
67 |
0x000a,
|
|
68 |
0x000b,
|
|
69 |
0x000c,
|
|
70 |
0x000d,
|
|
71 |
0x000e,
|
|
72 |
0x000f,
|
|
73 |
0x0010,
|
|
74 |
{0},
|
|
75 |
0x0011,
|
|
76 |
0x5aa5,
|
|
77 |
};
|
|
78 |
|
|
79 |
const stKMSysCfg KMStoreSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/;
|
|
80 |
|
|
81 |
int ReadFlashMem(void * pBuf, void * pAddrFlash, int nSize)
|
|
82 |
{
|
|
83 |
// memcpy(pBuf,pAddrFlash,nSize);
|
|
84 |
for (int i=0;i<nSize/4;i++)
|
|
85 |
{
|
|
86 |
((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i];
|
|
87 |
}
|
|
88 |
for (int i=nSize/4*2;i<nSize/2;i++)
|
|
89 |
{
|
|
90 |
((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i];
|
|
91 |
}
|
|
92 |
return nSize;
|
|
93 |
}
|
|
94 |
int EraseFlashMem(void * pAddrFlash, unsigned int Pages)
|
|
95 |
{
|
|
96 |
HAL_StatusTypeDef res;
|
|
97 |
res = HAL_FLASH_Unlock();
|
|
98 |
uint32_t ErrNo;
|
|
99 |
FLASH_EraseInitTypeDef erase1;
|
|
100 |
erase1.NbPages=Pages;
|
|
101 |
erase1.PageAddress=(unsigned int)pAddrFlash;
|
|
102 |
erase1.TypeErase=FLASH_TYPEERASE_PAGES;
|
|
103 |
res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
|
|
104 |
res = HAL_FLASH_Lock();
|
|
105 |
return res;
|
|
106 |
}
|
|
107 |
int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nSize)
|
|
108 |
{
|
|
109 |
HAL_StatusTypeDef res;
|
|
110 |
res = HAL_FLASH_Unlock();
|
|
111 |
/*
|
|
112 |
for (int i=0;i<nSize/2;i++)
|
|
113 |
{
|
|
114 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
115 |
}
|
|
116 |
*/
|
|
117 |
///*
|
|
118 |
for (int i=0;i<nSize/4;i++)
|
|
119 |
{
|
|
120 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)pAddrFlash + i*4, ((uint32_t *)pBuf)[i]);
|
|
121 |
}
|
|
122 |
|
|
123 |
for (int i = nSize/4 * 2 ; i < nSize/2 ; i++)
|
|
124 |
{
|
|
125 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
126 |
}
|
|
127 |
//*/
|
|
128 |
res = HAL_FLASH_Lock();
|
|
129 |
|
|
130 |
return res;
|
|
131 |
}
|
|
132 |
int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nSize)
|
|
133 |
{
|
|
134 |
|
|
135 |
HAL_StatusTypeDef res;
|
|
136 |
res = HAL_FLASH_Unlock();
|
|
137 |
uint32_t ErrNo;
|
|
138 |
FLASH_EraseInitTypeDef erase1;
|
|
139 |
erase1.NbPages=1;
|
|
140 |
erase1.PageAddress=(unsigned int)pAddrFlash;
|
|
141 |
erase1.TypeErase=FLASH_TYPEERASE_PAGES;
|
|
142 |
res = HAL_FLASHEx_Erase(&erase1,&ErrNo);
|
|
143 |
|
|
144 |
for (int i=0;i<nSize/2;i++)
|
|
145 |
{
|
|
146 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
147 |
}
|
|
148 |
/*
|
|
149 |
for (int i=0;i<nSize/4;i++)
|
|
150 |
{
|
|
151 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, (uint32_t)pAddrFlash + i*4, ((uint32_t *)pBuf)[i]);
|
|
152 |
}
|
|
153 |
|
|
154 |
for (int i = nSize/4 * 2 ; i < nSize/2 ; i++)
|
|
155 |
{
|
|
156 |
res = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, (uint32_t)pAddrFlash + i*2, ((uint16_t *)pBuf)[i]);
|
|
157 |
}
|
|
158 |
*/
|
|
159 |
res = HAL_FLASH_Lock();
|
|
160 |
|
|
161 |
return res;
|
|
162 |
}
|
|
163 |
|
|
164 |
int LoadDefaultSysCfg(pKMSysCfg theKMSysCfg)
|
|
165 |
{
|
|
166 |
memcpy(theKMSysCfg,&KMStoreSysCfg,sizeof(stKMSysCfg));
|
|
167 |
return 0;
|
|
168 |
}
|
|
169 |
int ReadSysCfgFromFlash(pKMSysCfg theKMSysCfg)
|
|
170 |
{
|
|
171 |
pKMSysCfg pStoreKMSysCfg = (pKMSysCfg)(STORE_SYSREG_BASE);
|
|
172 |
// find latest Store Cfg
|
|
173 |
int nIndex=-1;
|
|
174 |
int nMaxSeq=-1;
|
|
175 |
for (int i=0;i<8;i++)
|
|
176 |
{
|
|
177 |
if (pStoreKMSysCfg->Sign1 == 0x55aa && pStoreKMSysCfg->EndSign1 == 0x5aa5)
|
|
178 |
{
|
|
179 |
if (pStoreKMSysCfg->Seq1 > nMaxSeq)
|
|
180 |
{
|
|
181 |
nIndex=i;nMaxSeq=pStoreKMSysCfg->Seq1;
|
|
182 |
}
|
|
183 |
}
|
|
184 |
}
|
|
185 |
if (nIndex>=0 && nIndex <8)
|
|
186 |
{
|
|
187 |
ReadFlashMem(theKMSysCfg,(void *)(pStoreKMSysCfg+nIndex),sizeof(stKMSysCfg));
|
|
188 |
}else {
|
|
189 |
LoadDefaultSysCfg(theKMSysCfg);
|
|
190 |
}
|
|
191 |
//memcpy(theKMSysCfg,(void* )STORECFGBASE,sizeof(KMSysCfg));
|
|
192 |
return 0;
|
|
193 |
}
|
|
194 |
|
|
195 |
int WriteSysCfgToFlash(pKMSysCfg theKMSysCfg)
|
|
196 |
{
|
|
197 |
theKMSysCfg->Seq1++;
|
|
198 |
theKMSysCfg->cfgvar16++;
|
|
199 |
// find the next empty space to write
|
|
200 |
int nIndex=-1;
|
|
201 |
int s2=128;
|
|
202 |
for (int i=0;i<8;i++)
|
|
203 |
{
|
|
204 |
int skip=0;
|
|
205 |
unsigned char * nAddr2=(unsigned char *)(STORE_SYSREG_BASE+i*s2);
|
|
206 |
for (int j=0;j<s2;j++)
|
|
207 |
{
|
|
208 |
if ((nAddr2)[j] != 0xff) {skip =1;break;}
|
|
209 |
}
|
|
210 |
if (skip==1) {continue;}
|
|
211 |
nIndex=i;
|
|
212 |
break;
|
|
213 |
}
|
|
214 |
if (nIndex >=0 && nIndex <8) {
|
|
215 |
WriteToFlashMemNoErase(theKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(KMSysCfg));
|
|
216 |
}
|
|
217 |
else {
|
|
218 |
EraseAndWriteToFlashMem(theKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(KMSysCfg));
|
|
219 |
}
|
|
220 |
return 0;
|
|
221 |
}
|
|
222 |
|
|
223 |
int is_pow_of_2(uint32_t x) {
|
|
224 |
return !(x & (x-1));
|
|
225 |
}
|
|
226 |
|
|
227 |
uint32_t next_pow_of_2(uint32_t x)
|
|
228 |
{
|
|
229 |
if ( is_pow_of_2(x) )
|
|
230 |
return x;
|
|
231 |
x |= x>>1;
|
|
232 |
x |= x>>2;
|
|
233 |
x |= x>>4;
|
|
234 |
x |= x>>8;
|
|
235 |
x |= x>>16;
|
|
236 |
return x+1;
|
|
237 |
}
|
|
238 |
|
|
239 |
//uint8_t * pFlash1;
|
|
240 |
/*
|
|
241 |
stStoreCfg * GetCurStoreCfgAddr(void )
|
|
242 |
{
|
|
243 |
int s = sizeof(stStoreCfg);
|
|
244 |
int s2=next_pow_of_2(s);
|
|
245 |
stStoreCfg * p1;
|
|
246 |
int nMaxSN=0;
|
|
247 |
int nMaxId=0;
|
|
248 |
for (int i=0; s2*i < STORECFGPAGESIZE ; i++)
|
|
249 |
{
|
|
250 |
p1= (stStoreCfg *)(STORECFGBASE + s2 * i );
|
|
251 |
if (p1->Sign1 != START_SIGN) continue;
|
|
252 |
if (p1->EndSign1 != END_SIGN) continue;
|
|
253 |
|
|
254 |
if (p1->Seq1 >= nMaxSN) {nMaxSN = p1->Seq1; nMaxId = i;}
|
|
255 |
}
|
|
256 |
// nMaxId=nMaxId+1;
|
|
257 |
return (stStoreCfg *)(STORECFGBASE + s2 * nMaxId);
|
|
258 |
}
|
|
259 |
|
|
260 |
stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg )
|
|
261 |
{
|
|
262 |
int s = sizeof(stStoreCfg);
|
|
263 |
int s2=next_pow_of_2(s);
|
|
264 |
uint32_t nAddr1 = (uint32_t) CurCfg;
|
|
265 |
uint32_t nAddr2 = nAddr1 + s2;
|
|
266 |
for (int i=1;i<33;i++)
|
|
267 |
{
|
|
268 |
int skip=0;
|
|
269 |
nAddr2 = nAddr1 + s2*i;
|
|
270 |
if ((nAddr2 + s) > STORECFGBASE + STORECFGPAGESIZE)
|
|
271 |
{
|
|
272 |
nAddr2=STORECFGBASE; break;
|
|
273 |
}
|
|
274 |
for (int j=0;j<s2;j++)
|
|
275 |
{
|
|
276 |
if (((unsigned char *)nAddr2)[j] != 0xff)
|
|
277 |
{skip =1;}
|
|
278 |
}
|
|
279 |
if (skip==1) {continue;}
|
|
280 |
break;
|
|
281 |
}
|
|
282 |
stStoreCfg * p1 = (stStoreCfg *)nAddr2;
|
|
283 |
return p1;
|
|
284 |
}
|
|
285 |
|
|
286 |
|
|
287 |
int SaveStoreCfg(stStoreCfg * CurCfg)
|
|
288 |
{
|
|
289 |
return 0;
|
|
290 |
}
|
|
291 |
*/
|
|
292 |
// stStoreCfg Cfg2;
|
|
293 |
|
|
294 |
int LoadFlashDatas()
|
|
295 |
{
|
|
296 |
for (int i=0;i<16;i++)
|
|
297 |
{
|
|
298 |
// FlashDatas[i]=((uint16_t *)pConfigFlashBase)[i];
|
|
299 |
}
|
|
300 |
return 0;
|
|
301 |
}
|
|
302 |
/*
|
|
303 |
int LoadAndUpdateStoreCfg()
|
|
304 |
{
|
|
305 |
stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr();
|
|
306 |
|
|
307 |
Cfg2.Sign1=START_SIGN;
|
|
308 |
Cfg2.Seq1=pFCfg[0].Seq1+1;
|
|
309 |
Cfg2.CRC1=0x7777;
|
|
310 |
Cfg2.PowerCount=pFCfg[0].PowerCount+1;
|
|
311 |
Cfg2.UpTime=pFCfg[0].UpTime+1;
|
|
312 |
Cfg2.UserData1=pFCfg[0].UserData1;
|
|
313 |
Cfg2.EndSign1=END_SIGN;
|
|
314 |
stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg);
|
|
315 |
|
|
316 |
|
|
317 |
HAL_StatusTypeDef res;
|
|
318 |
if (pFCfg2 <= pFCfg)
|
|
319 |
{
|
|
320 |
res = (HAL_StatusTypeDef)EraseAndWriteToFlashMem(&Cfg2,pFCfg2,sizeof(stStoreCfg));
|
|
321 |
|
|
322 |
}else
|
|
323 |
{
|
|
324 |
res = (HAL_StatusTypeDef)WriteToFlashMemNoErase(&Cfg2,pFCfg2,sizeof(stStoreCfg));
|
|
325 |
}
|
|
326 |
return res;
|
|
327 |
}
|
|
328 |
*/
|
|
329 |
int CheckSavedData(void * pStartAddr, int PageSize, int Pages, int DataSize)
|
|
330 |
{
|
|
331 |
return 0;
|
|
332 |
};
|
|
333 |
|
|
334 |
|
|
335 |
int nMaxRunStatIndex=-1;
|
|
336 |
unsigned int nMaxRunStatSeq=0;
|
|
337 |
int nNextRunStatSpace=0;
|
|
338 |
int LoadDefaultRunStat(pRunStat theRunStat)
|
|
339 |
{
|
|
340 |
return 0;
|
|
341 |
}
|
|
342 |
int LoadRunStat(pRunStat theRunStat)
|
|
343 |
{
|
|
344 |
uchar * pRunStatStore = (uchar *)STORE_RUNSTAT_BASE;
|
|
345 |
pRunStat pStoreRunStats = (pRunStat)pRunStatStore;
|
|
346 |
// int s = sizeof(stRunStat);
|
|
347 |
|
|
348 |
for (int i=0;i * sizeof(stRunStat) < (STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES) ;i++)
|
|
349 |
{
|
|
350 |
if (pStoreRunStats[i].Sign1 == START_SIGN )
|
|
351 |
{
|
|
352 |
if (pStoreRunStats[i].Seq1 > nMaxRunStatSeq)
|
|
353 |
{
|
|
354 |
nMaxRunStatSeq = pStoreRunStats[i].Seq1;
|
|
355 |
nMaxRunStatIndex=i;
|
|
356 |
nNextRunStatSpace=i+1;
|
|
357 |
}
|
|
358 |
}
|
|
359 |
}
|
|
360 |
if (nMaxRunStatIndex>=0) // && nMaxRunStatIndex <8)
|
|
361 |
{
|
|
362 |
ReadFlashMem(theRunStat,(void *)(pStoreRunStats+nMaxRunStatIndex),sizeof(stRunStat));
|
|
363 |
}else {
|
|
364 |
LoadDefaultRunStat(theRunStat);
|
|
365 |
}
|
|
366 |
// find Next Space
|
|
367 |
// if Same Page with MaxSeq Index, then not erase, skip and skip.
|
|
368 |
// if next Page of MaxSeq Index, then earse if not empty;
|
|
369 |
if ((nNextRunStatSpace + 1) * sizeof(stRunStat) > STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES) {
|
|
370 |
nNextRunStatSpace=0;
|
|
371 |
}
|
|
372 |
return 0;
|
|
373 |
}
|
|
374 |
|
|
375 |
int SaveRunStat(pRunStat theRunStat)
|
|
376 |
{
|
|
377 |
nMaxRunStatSeq++;
|
|
378 |
theRunStat->Sign1=START_SIGN;
|
|
379 |
theRunStat->Seq1 = nMaxRunStatSeq;
|
|
380 |
theRunStat->PowerCount=KMem.PwrOnCount;
|
|
381 |
theRunStat->UpTime=KMem.TotalRunTime;
|
|
382 |
theRunStat->CRC1=0x11;
|
|
383 |
theRunStat->EndSign1=END_SIGN;
|
|
384 |
|
|
385 |
//check empty
|
|
386 |
unsigned char *pFlash = (unsigned char *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat));
|
|
387 |
int Skip=0;
|
|
388 |
for (int j=0;j<sizeof(stRunStat);j++)
|
|
389 |
{
|
|
390 |
if (pFlash[j]!=0xff) {Skip =1 ; break;}
|
|
391 |
}
|
|
392 |
if (Skip ==0 )
|
|
393 |
{
|
|
394 |
WriteToFlashMemNoErase(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat));
|
|
395 |
}else
|
|
396 |
{
|
|
397 |
EraseAndWriteToFlashMem(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat));
|
|
398 |
}
|
|
399 |
nMaxRunStatIndex=nNextRunStatSpace;
|
|
400 |
nNextRunStatSpace++;
|
|
401 |
if ((nNextRunStatSpace+1) * sizeof(stRunStat) > STORE_RUNSTAT_PAGESIZE * STORE_RUNSTAT_PAGES)
|
|
402 |
{
|
|
403 |
nNextRunStatSpace=0;
|
|
404 |
}
|
|
405 |
return 0;
|
|
406 |
}
|
|
407 |
|
|
408 |
|
|
409 |
int nEventCount=0;
|
|
410 |
int nEventMinIndex;
|
|
411 |
int nEventMaxIndex;
|
|
412 |
unsigned int nEventMaxSeq=0;
|
|
413 |
int nEventNextSpace;
|
|
414 |
int nMaxCurTime=0;
|
|
415 |
int CheckEventLog()
|
|
416 |
{
|
|
417 |
unsigned int nMinEventSeq=999999999;
|
|
418 |
uchar * pEventStore = (uchar *)STORE_LOG_BASE;
|
|
419 |
pEventLog theEventLog = (pEventLog) pEventStore;
|
|
420 |
// int s = sizeof(stEventLog);
|
|
421 |
nEventCount=0;
|
|
422 |
|
|
423 |
for (int i=0;i * sizeof(stEventLog) < (STORE_LOG_PAGESIZE * STORE_LOG_PAGES) ;i++)
|
|
424 |
{
|
|
425 |
if (theEventLog[i].Sign1 == START_SIGN )
|
|
426 |
{
|
|
427 |
nEventCount++;
|
|
428 |
if (theEventLog[i].Seq1 > nEventMaxSeq)
|
|
429 |
{
|
|
430 |
nEventMaxSeq = theEventLog[i].Seq1;
|
|
431 |
nEventMaxIndex=i;
|
|
432 |
nMaxCurTime=theEventLog[i].nTime;
|
|
433 |
nEventNextSpace=i+1;
|
|
434 |
}
|
|
435 |
if (theEventLog[i].Seq1 < nMinEventSeq)
|
|
436 |
{
|
|
437 |
nMinEventSeq = theEventLog[i].Seq1;
|
|
438 |
nEventMinIndex = i;
|
|
439 |
}
|
|
440 |
}
|
|
441 |
}
|
|
442 |
// find Next Space
|
|
443 |
// if Same Page with MaxSeq Index, then not erase, skip and skip.
|
|
444 |
// if next Page of MaxSeq Index, then earse if not empty;
|
|
445 |
if ((nEventNextSpace + 1) * sizeof(stEventLog) > STORE_LOG_PAGESIZE * STORE_LOG_PAGES) {
|
|
446 |
nEventNextSpace=0;
|
|
447 |
}
|
|
448 |
|
|
449 |
return nEventCount;
|
|
450 |
}
|
|
451 |
|
|
452 |
int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2)
|
|
453 |
{
|
|
454 |
nEventMaxSeq++;
|
|
455 |
stEventLog thisEventLog={START_SIGN, nEventMaxSeq, nTime,nEvent,nParam1,nParam2};
|
|
456 |
//check empty
|
|
457 |
unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog));
|
|
458 |
int Skip=0;
|
|
459 |
for (int j=0;j<sizeof(stEventLog);j++)
|
|
460 |
{
|
|
461 |
if (pFlash[j]!=0xff) {Skip =1 ; break;}
|
|
462 |
}
|
|
463 |
if (Skip ==0 )
|
|
464 |
{
|
|
465 |
WriteToFlashMemNoErase(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog));
|
|
466 |
}else
|
|
467 |
{
|
|
468 |
EraseAndWriteToFlashMem(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog));
|
|
469 |
|
|
470 |
}
|
|
471 |
nEventMaxIndex=nEventNextSpace;
|
|
472 |
nEventNextSpace++;
|
|
473 |
if ((nEventNextSpace+1) * sizeof(stEventLog) > STORE_LOG_PAGESIZE * STORE_LOG_PAGES)
|
|
474 |
{
|
|
475 |
nEventNextSpace=0;
|
|
476 |
}
|
|
477 |
nEventCount++;
|
|
478 |
KMem.nEventCount=nEventCount;
|
|
479 |
return 0;
|
|
480 |
}
|
|
481 |
|
|
482 |
pEventLog GetEventLogAddr(int nIndex)
|
|
483 |
{
|
|
484 |
int nEventIndex=nEventMinIndex + nIndex;
|
|
485 |
|
|
486 |
if (nEventIndex * sizeof(stEventLog) >= (STORE_LOG_PAGESIZE * STORE_LOG_PAGES))
|
|
487 |
{
|
|
488 |
nEventIndex -= (STORE_LOG_PAGESIZE * STORE_LOG_PAGES)/sizeof(stEventLog);
|
|
489 |
}
|
|
490 |
unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventIndex*sizeof(stEventLog));
|
|
491 |
|
|
492 |
return (pEventLog)pFlash;
|
|
493 |
}
|
|
494 |
|
|
495 |
int ClearEventLog(void)
|
|
496 |
{
|
|
497 |
EraseFlashMem((void *)STORE_LOG_BASE,STORE_LOG_PAGES);
|
|
498 |
nEventMinIndex=0;
|
|
499 |
nEventMaxIndex=0;
|
|
500 |
nEventMaxSeq=0;
|
|
501 |
nEventCount=0;
|
|
502 |
nEventNextSpace=0;
|
|
503 |
return 0;
|
|
504 |
}
|
|
505 |
int KMachineInit(void)
|
|
506 |
{
|
|
507 |
// ClearEventLog();
|
|
508 |
CheckEventLog();
|
|
509 |
LoadRunStat(&KMRunStat);
|
|
510 |
KMem.CurTimeSec=nMaxCurTime;
|
|
511 |
KMem.TotalRunTime=KMRunStat.UpTime;
|
|
512 |
KMRunStat.PowerCount++;
|
|
513 |
KMem.PwrOnCount=KMRunStat.PowerCount;
|
|
514 |
SaveRunStat(&KMRunStat);
|
|
515 |
KMem.SDD[15]=nMaxRunStatIndex;
|
|
516 |
KMem.SDD[16]=nMaxRunStatSeq;
|
|
517 |
KMem.SDD[17]=nNextRunStatSpace;
|
|
518 |
|
|
519 |
|
|
520 |
AddEventLog(KMem.CurTimeSec,EventTypePowerUp,1,12345);
|
|
521 |
KMem.SDD[19]=nEventCount;
|
|
522 |
KMem.SDD[20]=nEventMinIndex;
|
|
523 |
KMem.SDD[21]=nEventMaxIndex;
|
|
524 |
KMem.SDD[22]=nEventMaxSeq;
|
|
525 |
KMem.SDD[23]=nEventNextSpace;
|
|
526 |
|
|
527 |
return 0;
|
|
528 |
}
|