提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file : KMachine.c |
|
4 |
* @brief : KMachine program body |
|
5 |
****************************************************************************** |
|
6 |
*/ |
|
7 |
|
|
8 |
#include "KMachine.h" |
5dd1b7
|
9 |
#include "KBus.h" |
483170
|
10 |
#include "string.h" |
Q |
11 |
#include "Globaldef.h" |
5dd1b7
|
12 |
#include "stm32f0xx.h" |
Q |
13 |
#include "stm32f0xx_ll_flash.h" |
842bb6
|
14 |
#include "PLCFunctions.h" |
5dd1b7
|
15 |
|
Q |
16 |
//#include "stm32f0xx_hal.h" |
483170
|
17 |
|
Q |
18 |
//#define OB_BASE ((uint32_t)0x1FFFF800U) /*!< FLASH Option Bytes base address */ |
|
19 |
//#define FLASHSIZE_BASE ((uint32_t)0x1FFFF7CCU) /*!< FLASH Size register base address */ |
|
20 |
//#define UID_BASE ((uint32_t)0x1FFFF7ACU) /*!< Unique device ID register base address */ |
5dd1b7
|
21 |
|
Q |
22 |
//#define FLASH_BASE |
|
23 |
//#define FLASH_PAGE_SIZE 0x00000400U |
|
24 |
//#define FLASH_BANK1_END ((uint32_t)0x0800FFFFU) /*!< FLASH END address of bank1 */ |
|
25 |
#define ApplicationAddress 0x08001000 //应用程序首地址定义 |
|
26 |
#if defined(STM32F030x8) |
842bb6
|
27 |
#define NEW_APP_INFOBLOCK_ADDR 0x08008000 // 存储的新应用程序信息块的地址 |
Q |
28 |
#define NEW_APP_ADDR ((uint32_t)0x08009000U) // 存储的新应用程序的地址 |
5dd1b7
|
29 |
#endif /* STM32F030x6 || STM32F030x8 || STM32F031x6 || STM32F051x8 || STM32F042x6 || STM32F048xx || STM32F058xx || STM32F070x6 */ |
Q |
30 |
|
|
31 |
#if defined(STM32F030xC) |
842bb6
|
32 |
#define NEW_APP_INFOBLOCK_ADDR 0x08020000 // 存储的新应用程序信息块的地址 |
Q |
33 |
#define NEW_APP_ADDR ((uint32_t)0x08021000U) // 存储的新应用程序的地址 |
5dd1b7
|
34 |
#endif /* STM32F071xB || STM32F072xB || STM32F078xx || STM32F091xC || STM32F098xx || STM32F030xC */ |
483170
|
35 |
|
Q |
36 |
|
|
37 |
stStoredKMSysCfg storedKMSysCfg ; |
|
38 |
stKMem KMem; |
|
39 |
stRunStat KMRunStat; |
842bb6
|
40 |
|
Q |
41 |
|
|
42 |
extern void SetErrLed(uchar bOn); |
483170
|
43 |
|
Q |
44 |
//uint8_t * pFlash1 = (uint8_t *)(STORECFGBASE); |
|
45 |
|
|
46 |
//void * pConfigFlashBase = (uint8_t *)(STORECFGBASE); |
|
47 |
|
|
48 |
//uint16_t FlashDatas[16]; |
|
49 |
|
|
50 |
//uint32_t * pUID = (uint32_t *)(UID_BASE); |
842bb6
|
51 |
|
8b51c7
|
52 |
/* |
Q |
53 |
const char VersionStr[] __attribute__((at(FLASH_BASE + 0X2000))) //__attribute__((at(0X8001000))) |
483170
|
54 |
= "3.00"; |
8b51c7
|
55 |
*/ |
483170
|
56 |
const stStoredKMSysCfg KMDefaultSysCfg /*__attribute__((at(STORECFGBASE)))*/ = |
Q |
57 |
{ |
|
58 |
START_SIGN, |
|
59 |
0x0000, |
|
60 |
{ |
|
61 |
CFG_VER, |
|
62 |
0x0000, //workmode |
|
63 |
0x0000, //switchfunc |
842bb6
|
64 |
4, //nCfgBlockCount; |
483170
|
65 |
{ //comportparam[2] |
Q |
66 |
{ |
|
67 |
PortType_KLink, //PorttType |
|
68 |
1, //Station |
842bb6
|
69 |
1152, //Buadrate = * 100; |
Q |
70 |
1, //ByteSize |
483170
|
71 |
0, //Parity |
Q |
72 |
0, //StopBits |
842bb6
|
73 |
1, //endType |
483170
|
74 |
0, //EofChar |
Q |
75 |
0, //SofChar |
842bb6
|
76 |
9, //endtime |
483170
|
77 |
0, //recvbuf |
Q |
78 |
0, //bufsize |
|
79 |
}, |
|
80 |
{ |
|
81 |
PortType_KBus, //PorttType |
|
82 |
0, //Station |
|
83 |
2304, //Buadrate = * 100; |
842bb6
|
84 |
1, //ByteSize |
483170
|
85 |
0, //Parity |
Q |
86 |
0, //StopBits |
842bb6
|
87 |
1, //endType |
483170
|
88 |
0, //EofChar |
Q |
89 |
0, //SofChar |
842bb6
|
90 |
1, //endtime |
483170
|
91 |
0, //recvbuf |
Q |
92 |
0, //bufsize |
|
93 |
} |
|
94 |
}, |
|
95 |
{{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}, //inputfilterparam |
|
96 |
{{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}}, //outputholdparam |
|
97 |
{ //default port mapping |
|
98 |
0x0010, |
|
99 |
0x0020, |
|
100 |
0x0030, |
|
101 |
0x0040, |
|
102 |
0x0050, |
842bb6
|
103 |
0x0060, |
Q |
104 |
0x0070, |
|
105 |
0x0080, |
483170
|
106 |
}, |
842bb6
|
107 |
0,0,0,0, |
Q |
108 |
{ |
|
109 |
{0,sizeof(stKMSysCfg)}, |
|
110 |
{1,100}, |
|
111 |
{2,100}, |
|
112 |
{3,32}, |
|
113 |
{4,32}, |
|
114 |
}, |
|
115 |
0x0008, //padding s |
483170
|
116 |
0x0009, |
Q |
117 |
0x000a, |
|
118 |
}, |
|
119 |
0x0011, //CRC16 |
|
120 |
END_SIGN, |
|
121 |
}; |
842bb6
|
122 |
|
Q |
123 |
int nEventCount=0; |
|
124 |
int nEventMinIndex; |
|
125 |
int nEventMaxIndex; |
|
126 |
unsigned int nEventMaxSeq=0; |
|
127 |
int nEventNextSpace; |
|
128 |
int nMaxCurTime=0; |
|
129 |
volatile int PowerState = 0; |
|
130 |
|
|
131 |
volatile int PowerDownEvent=0; |
|
132 |
volatile int OldPowerDownEvent=0; |
|
133 |
volatile int OldPowerDownEventTime=0; |
|
134 |
int nMaxRunStatIndex=-1; |
|
135 |
unsigned int nMaxRunStatSeq=0; |
|
136 |
int nNextRunStatSpace=0; |
|
137 |
|
|
138 |
int KMRegisterPort(ushort nType,stPortDef * theParam) |
|
139 |
{ |
|
140 |
int curPortId = KMem.nTotalPorts; |
|
141 |
|
|
142 |
KMem.pPorts[curPortId] = theParam; |
|
143 |
|
|
144 |
KMem.nTotalPorts++; |
|
145 |
return curPortId; |
|
146 |
} |
|
147 |
|
|
148 |
int KMPortReqFunc(int nPortIndex,int nReqId, int nParam1, int nParam2, void ** pData, unsigned short * nlen1) |
|
149 |
{ |
|
150 |
if (KMem.pPorts[nPortIndex]->ReqCommFunc) |
|
151 |
return KMem.pPorts[nPortIndex]->ReqCommFunc(KMem.pPorts[nPortIndex]->pInstance, nReqId, nParam1, nParam2, pData, nlen1); |
|
152 |
else return -1; |
|
153 |
} |
|
154 |
|
|
155 |
int KMRunService(int nSvrId, int nParam1, int nParam2, void **pData, unsigned short *nlen1) |
|
156 |
{ |
|
157 |
int res; |
|
158 |
switch(nSvrId) |
|
159 |
{ |
|
160 |
case ReqNone: |
|
161 |
break; |
|
162 |
|
|
163 |
case ReqInit: |
|
164 |
break; |
|
165 |
case ReqReset: |
|
166 |
__set_PRIMASK(1); //关闭全局中断 |
|
167 |
NVIC_SystemReset(); |
|
168 |
break; |
|
169 |
case ReqStop: |
|
170 |
break; |
|
171 |
case ReqRun: |
|
172 |
break; |
|
173 |
case ReqBlinkLED: |
|
174 |
break; |
|
175 |
case ReqStartDiag: |
|
176 |
break; |
|
177 |
case ReqStopDiag: |
|
178 |
break; |
|
179 |
case ReqUpdateFirm: |
|
180 |
|
|
181 |
res = WriteNewApp(nParam1,*pData,*nlen1); |
|
182 |
|
|
183 |
break; |
|
184 |
case ReqUpdateFirmInfo: |
|
185 |
res = WriteNewAppInfo(nParam1,*pData,*nlen1); |
|
186 |
break; |
|
187 |
|
|
188 |
default: |
|
189 |
res = -1; |
|
190 |
break; |
|
191 |
|
|
192 |
} |
|
193 |
return res; |
|
194 |
} |
|
195 |
|
|
196 |
int KMachineInit(void) |
|
197 |
{ |
|
198 |
// ClearEventLog(); |
|
199 |
KMem.LastScanTime=0; |
|
200 |
KMem.ScanTimeuS=0; |
|
201 |
KMem.MinScanTimeuS=99999; |
|
202 |
KMem.MaxScanTimeuS=0; |
|
203 |
|
|
204 |
// KMem.SDD[14]=(unsigned int)&KMStoreSysCfg; |
|
205 |
// KMem.SDD[15]=(unsigned int)&KMStoreSysCfg1; |
|
206 |
KMem.SDD[12]=((uint32_t *)UID_BASE)[0]; |
|
207 |
// KMem.SDD[13]=((uint32_t *)UID_BASE)[1]; |
|
208 |
// KMem.SDD[14]=((uint32_t *)UID_BASE)[2]; |
|
209 |
KMem.SDD[13]=PendSvCount; |
|
210 |
KMem.SDD[14]=RCC->CSR; |
|
211 |
// KMem.SDD[15]=*(uint32_t *)FLASHSIZE_BASE; |
|
212 |
// KMem.SDD[16]=(unsigned int)&KMSysCfg; |
|
213 |
|
|
214 |
KMem.nTotalPorts = 0; |
|
215 |
CheckEventLog(); |
|
216 |
LoadRunStat(&KMRunStat); |
|
217 |
KMem.CurTimeSec=nMaxCurTime; |
|
218 |
KMem.TotalRunTime=KMRunStat.UpTime; |
|
219 |
KMRunStat.PowerCount++; |
|
220 |
KMem.PwrOnCount=KMRunStat.PowerCount; |
|
221 |
SaveRunStat(&KMRunStat); |
|
222 |
KMem.SDD[15]=nMaxRunStatIndex; |
|
223 |
KMem.SDD[16]=nMaxRunStatSeq; |
|
224 |
KMem.SDD[17]=nNextRunStatSpace; |
|
225 |
|
|
226 |
|
|
227 |
AddEventLog(KMem.CurTimeSec,EventTypePowerUp,1,12345); |
|
228 |
KMem.SDD[19]=nEventCount; |
|
229 |
KMem.SDD[20]=nEventMinIndex; |
|
230 |
KMem.SDD[21]=nEventMaxIndex; |
|
231 |
KMem.SDD[22]=nEventMaxSeq; |
|
232 |
KMem.SDD[23]=nEventNextSpace; |
|
233 |
|
|
234 |
return 0; |
|
235 |
} |
|
236 |
|
|
237 |
int KMachineLoopProc(void) |
|
238 |
{ |
|
239 |
|
|
240 |
return 0; |
|
241 |
} |
|
242 |
|
483170
|
243 |
|
Q |
244 |
//const stKMSysCfg KMDefaultSysCfg2[7] /*__attribute__((at(STORECFGBASE+sizeof(stKMSysCfg))))*/; |
|
245 |
|
|
246 |
int ReadFlashMem(void * pBuf, void * pAddrFlash, int nByteSize) |
|
247 |
{ |
7d8ba5
|
248 |
memcpy(pBuf,pAddrFlash,nByteSize); |
Q |
249 |
// for (int i=0;i<nByteSize/4;i++) |
|
250 |
// { |
|
251 |
// ((uint32_t *)pBuf)[i] = ((uint32_t *)pAddrFlash)[i]; |
|
252 |
// } |
|
253 |
// for (int i=nByteSize/4*2;i<nByteSize/2;i++) |
|
254 |
// { |
|
255 |
// ((uint16_t *)pBuf)[i] = ((uint16_t *)pAddrFlash)[i]; |
|
256 |
// } |
|
257 |
|
483170
|
258 |
return nByteSize; |
Q |
259 |
} |
842bb6
|
260 |
|
483170
|
261 |
int EraseFlashMem(void * pAddrFlash, unsigned int Pages) |
Q |
262 |
{ |
5dd1b7
|
263 |
ErrorStatus res; |
Q |
264 |
res = LL_Flash_Unlock(); |
|
265 |
// uint32_t ErrNo; |
|
266 |
res = LL_Flash_PageErase(pAddrFlash,Pages); |
|
267 |
LL_FLASH_Lock(FLASH); |
842bb6
|
268 |
return res; |
483170
|
269 |
} |
842bb6
|
270 |
|
483170
|
271 |
int WriteToFlashMemNoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize) |
Q |
272 |
{ |
5dd1b7
|
273 |
ErrorStatus res; |
842bb6
|
274 |
SetErrLed(1); |
5dd1b7
|
275 |
res = LL_Flash_Unlock(); |
Q |
276 |
// __disable_irq(); |
483170
|
277 |
///* |
5dd1b7
|
278 |
for (int i=0;i<(nByteSize+1)/2;i++) |
483170
|
279 |
{ |
5dd1b7
|
280 |
unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); |
Q |
281 |
res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); |
|
282 |
if (res == ERROR) break; |
483170
|
283 |
} |
5dd1b7
|
284 |
// */ |
Q |
285 |
// __enable_irq(); |
|
286 |
LL_FLASH_Lock(FLASH); |
|
287 |
if (res == ERROR) return 1; |
|
288 |
return 0; |
483170
|
289 |
} |
842bb6
|
290 |
|
483170
|
291 |
int EraseAndWriteToFlashMem(void * pBuf, void * pAddrFlash, unsigned int nByteSize) |
Q |
292 |
{ |
|
293 |
|
842bb6
|
294 |
SetErrLed(1); |
5dd1b7
|
295 |
ErrorStatus res; |
Q |
296 |
res = LL_Flash_Unlock(); |
|
297 |
// __disable_irq(); |
|
298 |
int NbPages = (nByteSize-1) / FLASH_PAGE_SIZE + 1; |
|
299 |
// FLASH_EraseInitTypeDef erase1; |
|
300 |
// erase1.NbPages=(nByteSize-1) / FLASH_PAGE_SIZE + 1;; |
|
301 |
// erase1.PageAddress=(unsigned int)pAddrFlash; |
|
302 |
// erase1.TypeErase=FLASH_TYPEERASE_PAGES; |
|
303 |
res = LL_Flash_PageErase(pAddrFlash,NbPages); |
|
304 |
for (int i=0;i<(nByteSize+1)/2;i++) |
483170
|
305 |
{ |
5dd1b7
|
306 |
unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); |
Q |
307 |
res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); |
|
308 |
if (res == ERROR) break; |
483170
|
309 |
} |
5dd1b7
|
310 |
|
Q |
311 |
// __enable_irq(); |
|
312 |
LL_FLASH_Lock(FLASH); |
|
313 |
if (res == ERROR) return 1; |
|
314 |
return 0; |
483170
|
315 |
} |
Q |
316 |
|
842bb6
|
317 |
/* 烧录Flash,并自动擦除,页起始和跨页时自动擦除 页面中部分不擦除 */ |
Q |
318 |
int WriteToFlashAutoErase(void * pBuf, void * pAddrFlash, unsigned int nByteSize) |
|
319 |
{ |
|
320 |
SetErrLed(1); |
|
321 |
ErrorStatus res; |
|
322 |
res = LL_Flash_Unlock(); |
|
323 |
// __disable_irq(); |
|
324 |
|
|
325 |
int StartPage = (int)pAddrFlash / FLASH_PAGE_SIZE; |
|
326 |
int EndPage = ((int)pAddrFlash + nByteSize) / FLASH_PAGE_SIZE; |
|
327 |
int StartOffset = (int)pAddrFlash & (FLASH_PAGE_SIZE-1); |
|
328 |
|
|
329 |
int NbPages = EndPage -StartPage + 1; |
|
330 |
|
|
331 |
if (StartOffset == 0) { // 从最开始 , 全部擦除,然后再存储. |
|
332 |
res = LL_Flash_PageErase(pAddrFlash,NbPages); |
|
333 |
for (int i=0;i<(nByteSize+1)/2;i++) |
|
334 |
{ |
|
335 |
unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); |
|
336 |
res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); |
|
337 |
if (res == ERROR) break; |
|
338 |
} |
|
339 |
}else if (NbPages > 1){ // 跨页存储 |
|
340 |
// 先前面的部分 |
|
341 |
int i; |
|
342 |
for (i=0;i<(nByteSize+1)/2 && (((int)pAddrFlash + i*2) &(FLASH_PAGE_SIZE -1))!=0 ;i++) |
|
343 |
{ |
|
344 |
unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); |
|
345 |
res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); |
|
346 |
if (res == ERROR) break; |
|
347 |
} |
|
348 |
// 擦除后面的部分. |
|
349 |
res = LL_Flash_PageErase((void *)((int)pAddrFlash + i*2),NbPages - 1); |
|
350 |
// 继续存储 |
|
351 |
for ( ;i<(nByteSize+1)/2;i++) |
|
352 |
{ |
|
353 |
unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); |
|
354 |
res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); |
|
355 |
if (res == ERROR) break; |
|
356 |
} |
|
357 |
}else { |
|
358 |
// 正常写入,不需要擦除 |
|
359 |
for (int i=0;i<(nByteSize+1)/2;i++) |
|
360 |
{ |
|
361 |
unsigned short value = ((uint8_t *)pBuf)[i*2] + (((uint8_t *)pBuf)[i*2 +1] << 8); |
|
362 |
res = LL_FLASH_Program(ProgaraType_DATA16, (uint32_t)pAddrFlash + i*2, value); |
|
363 |
if (res == ERROR) break; |
|
364 |
} |
|
365 |
} |
|
366 |
// __enable_irq(); |
|
367 |
LL_FLASH_Lock(FLASH); |
|
368 |
if (res == ERROR) return 1; |
|
369 |
return 0; |
|
370 |
} |
|
371 |
|
483170
|
372 |
int ReadFactoryData(void * pDatabuf, int nByteCount) |
Q |
373 |
{ |
|
374 |
memcpy(pDatabuf,(stFactoryData *)FACTORY_DATA_BASE,nByteCount); |
|
375 |
return 0; |
|
376 |
} |
|
377 |
int WriteFactoryData(void * pDataBuf, int nByteCount) |
|
378 |
{ |
|
379 |
stFactoryData * p1 = (stFactoryData*) pDataBuf; |
|
380 |
stFactoryData * p2 = (stFactoryData *)FACTORY_DATA_BASE; |
|
381 |
p1->Seq1= p2->Seq1+1; |
|
382 |
|
|
383 |
EraseAndWriteToFlashMem(pDataBuf, (stFactoryData *)FACTORY_DATA_BASE,nByteCount); |
|
384 |
return 0; |
|
385 |
} |
842bb6
|
386 |
#if (ENABLE_PLC) |
Q |
387 |
int ReadPLCProgram(int nBank, int nProgByteAddr, void *pBuf, int nByteSize) |
483170
|
388 |
{ |
842bb6
|
389 |
stStoredBinProgs * pStoredProg; |
483170
|
390 |
if (nBank==0) { |
842bb6
|
391 |
pStoredProg = (void *)(STORE_PRG_BASE); |
483170
|
392 |
}else if (nBank ==1) { |
842bb6
|
393 |
pStoredProg = (void *)(ALT_PRG_BASE); |
Q |
394 |
}else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { |
|
395 |
pStoredProg = (void *)(STORE_PRG_BASE); |
483170
|
396 |
} else { |
842bb6
|
397 |
pStoredProg = (void *)(ALT_PRG_BASE); |
483170
|
398 |
} |
842bb6
|
399 |
void * progByteAddr; |
Q |
400 |
progByteAddr =(unsigned char *)&pStoredProg->BinInstrcns + nProgByteAddr; |
|
401 |
|
|
402 |
ReadFlashMem(pBuf, (void *)(progByteAddr), nByteSize); |
483170
|
403 |
return 0; |
Q |
404 |
} |
842bb6
|
405 |
|
Q |
406 |
int StartPLCProgram(int nBank, int nByteSize, int nCRC) |
|
407 |
{ |
|
408 |
void * progHdrAddr; |
|
409 |
int nRes = 0; |
|
410 |
if (nBank == 0) { |
|
411 |
progHdrAddr=(void *)(STORE_PRG_BASE); |
|
412 |
}else if (nBank==1) { |
|
413 |
progHdrAddr=(void *)(ALT_PRG_BASE); |
|
414 |
} else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { |
|
415 |
progHdrAddr=(void *)(ALT_PRG_BASE); |
|
416 |
}else{ |
|
417 |
progHdrAddr=(void *)(STORE_PRG_BASE); |
|
418 |
} |
|
419 |
|
|
420 |
stStoredHdr theHdr; |
|
421 |
theHdr.nBlockSign = 0xAA55; |
|
422 |
theHdr.nBlockType = 0; |
|
423 |
theHdr.nSeq = 1; |
|
424 |
theHdr.nSize = nByteSize; |
|
425 |
theHdr.nCRC2 = nCRC; |
|
426 |
|
|
427 |
WriteToFlashAutoErase(&theHdr,(void *)progHdrAddr,sizeof(stStoredHdr)); |
|
428 |
|
|
429 |
return nRes; |
|
430 |
} |
|
431 |
int WritePLCProgram(int nBank, int nProgAddress, void * pBuf, int nByteSize) |
483170
|
432 |
{ |
Q |
433 |
// Program Save Address;// |
|
434 |
// Program 2 Save Address; // |
842bb6
|
435 |
stStoredBinProgs * pStoredProg; |
Q |
436 |
|
483170
|
437 |
if (nBank == 0) { |
842bb6
|
438 |
pStoredProg=(stStoredBinProgs *)(STORE_PRG_BASE); |
483170
|
439 |
}else if (nBank==1) { |
842bb6
|
440 |
pStoredProg=(stStoredBinProgs *)(ALT_PRG_BASE); |
Q |
441 |
} else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { |
|
442 |
pStoredProg=(stStoredBinProgs *)(ALT_PRG_BASE); |
483170
|
443 |
}else{ |
842bb6
|
444 |
pStoredProg=(stStoredBinProgs *)(STORE_PRG_BASE); |
483170
|
445 |
} |
842bb6
|
446 |
void * progByteAddr; |
Q |
447 |
progByteAddr =(unsigned char *)&pStoredProg->BinInstrcns + nProgAddress; |
|
448 |
WriteToFlashAutoErase(pBuf,progByteAddr,nByteSize); |
|
449 |
|
483170
|
450 |
return 0; |
Q |
451 |
} |
|
452 |
|
842bb6
|
453 |
int FinishiPLCProgram(int nBank, int nProgSteps,int nCRC ) |
Q |
454 |
{ |
|
455 |
int nRes = 0; |
|
456 |
|
|
457 |
if (storedKMSysCfg.theKMSysCfg.nProgBank == 0 ) { |
|
458 |
storedKMSysCfg.theKMSysCfg.nProgBank = 1; |
|
459 |
}else { |
|
460 |
storedKMSysCfg.theKMSysCfg.nProgBank = 0; |
|
461 |
} |
|
462 |
storedKMSysCfg.theKMSysCfg.nProgSize = nProgSteps; |
|
463 |
|
|
464 |
WriteSysCfgToFlash(&storedKMSysCfg); |
|
465 |
|
|
466 |
KMRunStat.nBinProgSize=nProgSteps; |
|
467 |
KMRunStat.nBinProgBank=storedKMSysCfg.theKMSysCfg.nProgBank; |
|
468 |
|
|
469 |
SaveRunStat(&KMRunStat); |
|
470 |
|
|
471 |
return nRes; |
|
472 |
} |
|
473 |
int ReadPLCAnno(int nBank, int nProgByteAddr, void *pBuf, int nByteSize) |
|
474 |
{ |
|
475 |
stStoredBinProgs * pStoredProg; |
|
476 |
if (nBank==0) { |
|
477 |
pStoredProg = (void *)(STORE_PRG_BASE); |
|
478 |
}else if (nBank ==1) { |
|
479 |
pStoredProg = (void *)(ALT_PRG_BASE); |
|
480 |
}else if (storedKMSysCfg.theKMSysCfg.nProgBank==0) { |
|
481 |
pStoredProg = (void *)(STORE_PRG_BASE); |
|
482 |
} else { |
|
483 |
pStoredProg = (void *)(ALT_PRG_BASE); |
|
484 |
} |
|
485 |
void * progByteAddr; |
|
486 |
progByteAddr =(unsigned char *)&pStoredProg->BinInstrcns + nProgByteAddr; |
|
487 |
|
|
488 |
ReadFlashMem(pBuf, (void *)(progByteAddr), nByteSize); |
|
489 |
return 0; |
|
490 |
} |
|
491 |
|
|
492 |
int StartPLCAnno(int nBank, int nByteSize, int nCRC) |
|
493 |
{ |
|
494 |
void * StoredAnnoHdrAddr; |
|
495 |
int nRes = 0; |
|
496 |
|
|
497 |
StoredAnnoHdrAddr=(void *)(STORE_PLC_ANNO_BASE); |
|
498 |
|
|
499 |
|
|
500 |
stStoredHdr theHdr; |
|
501 |
theHdr.nBlockSign = 0xAA55; |
|
502 |
theHdr.nBlockType = 3; |
|
503 |
theHdr.nSeq = 1; |
|
504 |
theHdr.nSize = nByteSize; |
|
505 |
theHdr.nCRC2 = nCRC; |
|
506 |
|
|
507 |
WriteToFlashAutoErase(&theHdr,(void *)StoredAnnoHdrAddr,sizeof(stStoredHdr)); |
|
508 |
|
|
509 |
return nRes; |
|
510 |
} |
|
511 |
int WritePLCAnno(int nBank, int nByteAddress, void * pBuf, int nByteSize) |
|
512 |
{ |
|
513 |
// Program Save Address;// |
|
514 |
// Program 2 Save Address; // |
|
515 |
stStoredAnno * pStoredAnno; |
|
516 |
pStoredAnno=(stStoredAnno *)(STORE_PLC_ANNO_BASE); |
|
517 |
|
|
518 |
void * nByteAddr; |
|
519 |
nByteAddr =pStoredAnno->Annos + nByteAddress; |
|
520 |
WriteToFlashAutoErase(pBuf,nByteAddr,nByteSize); |
|
521 |
|
|
522 |
return 0; |
|
523 |
} |
|
524 |
int FinishiPLCAnno(int nBank, int nByteSize,int nCRC ) |
|
525 |
{ |
|
526 |
int nRes = 0; |
|
527 |
|
|
528 |
storedKMSysCfg.theKMSysCfg.nAnnoSize = nByteSize; |
|
529 |
WriteSysCfgToFlash(&storedKMSysCfg); |
|
530 |
/* |
|
531 |
KMRunStat.nBinProgSize=nProgSteps; |
|
532 |
KMRunStat.nBinProgBank=storedKMSysCfg.theKMSysCfg.nProgBank; |
|
533 |
|
|
534 |
SaveRunStat(&KMRunStat); |
|
535 |
*/ |
|
536 |
return nRes; |
|
537 |
} |
|
538 |
|
|
539 |
#endif //ENABLE_PLC |
483170
|
540 |
int LoadDefaultSysCfg(pStoredKMSysCfg theStoredKMSysCfg) |
Q |
541 |
{ |
|
542 |
memcpy(theStoredKMSysCfg,&KMDefaultSysCfg,sizeof(stStoredKMSysCfg)); |
|
543 |
return 0; |
|
544 |
} |
|
545 |
int ReadSysCfgFromFlash(pStoredKMSysCfg theStoredKMSysCfg) |
|
546 |
{ |
|
547 |
pStoredKMSysCfg pSKMSysCfg = (pStoredKMSysCfg)(STORE_SYSREG_BASE); |
|
548 |
// find latest Store Cfg |
|
549 |
int s2=128; |
|
550 |
int nIndex=-1; |
|
551 |
int nMaxSeq=0; |
|
552 |
for (int i=0;i<8;i++) |
|
553 |
{ |
|
554 |
pSKMSysCfg = (pStoredKMSysCfg)(STORE_SYSREG_BASE + s2*i); |
|
555 |
if (pSKMSysCfg->Sign1 == START_SIGN && pSKMSysCfg->EndSign1 == END_SIGN) |
|
556 |
{ |
|
557 |
if (pSKMSysCfg->Seq1 > nMaxSeq) |
|
558 |
{ |
|
559 |
nIndex=i;nMaxSeq=pSKMSysCfg->Seq1; |
|
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
if (nIndex>=0 && nIndex <8) |
|
564 |
{ |
|
565 |
ReadFlashMem(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex * s2),sizeof(stStoredKMSysCfg)); |
|
566 |
//ReadFlashMem(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE ),sizeof(stStoredKMSysCfg)); |
|
567 |
//memcpy(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE ),sizeof(stStoredKMSysCfg)); |
|
568 |
}else { |
|
569 |
LoadDefaultSysCfg(theStoredKMSysCfg); |
|
570 |
} |
|
571 |
//memcpy(theKMSysCfg,(void* )STORECFGBASE,sizeof(KMSysCfg)); |
|
572 |
return 0; |
|
573 |
} |
|
574 |
|
|
575 |
int WriteSysCfgToFlash(pStoredKMSysCfg theStoredKMSysCfg) |
|
576 |
{ |
|
577 |
theStoredKMSysCfg->Seq1++; |
|
578 |
theStoredKMSysCfg->Sign1 = START_SIGN; |
|
579 |
theStoredKMSysCfg->EndSign1 = END_SIGN; |
|
580 |
// EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(stStoredKMSysCfg)); |
|
581 |
// return 0; |
|
582 |
// theKMSysCfg->cfgvar16++; |
|
583 |
// find the next empty space to write |
|
584 |
int nIndex=-1; |
|
585 |
int s2=128; |
|
586 |
for (int i=0;i<8;i++) |
|
587 |
{ |
|
588 |
int skip=0; |
|
589 |
unsigned char * nAddr2=(unsigned char *)(STORE_SYSREG_BASE + i * s2); |
|
590 |
for (int j=0;j<s2;j++) |
|
591 |
{ |
|
592 |
if ((nAddr2)[j] != 0xff) {skip =1;break;} |
|
593 |
} |
|
594 |
if (skip==1) {continue;} |
|
595 |
nIndex=i; |
|
596 |
break; |
|
597 |
} |
|
598 |
if (nIndex >=0 && nIndex <8) { |
|
599 |
WriteToFlashMemNoErase(theStoredKMSysCfg,(void *)(STORE_SYSREG_BASE + nIndex*s2),sizeof(stStoredKMSysCfg)); |
|
600 |
} |
|
601 |
else { |
|
602 |
EraseAndWriteToFlashMem(theStoredKMSysCfg,(void *)STORE_SYSREG_BASE,sizeof(stStoredKMSysCfg)); |
|
603 |
} |
|
604 |
return 0; |
|
605 |
} |
|
606 |
|
|
607 |
int is_pow_of_2(uint32_t x) { |
|
608 |
return !(x & (x-1)); |
|
609 |
} |
|
610 |
|
|
611 |
uint32_t next_pow_of_2(uint32_t x) |
|
612 |
{ |
|
613 |
if ( is_pow_of_2(x) ) |
|
614 |
return x; |
|
615 |
x |= x>>1; |
|
616 |
x |= x>>2; |
|
617 |
x |= x>>4; |
|
618 |
x |= x>>8; |
|
619 |
x |= x>>16; |
|
620 |
return x+1; |
|
621 |
} |
|
622 |
|
|
623 |
//uint8_t * pFlash1; |
|
624 |
/* |
|
625 |
stStoreCfg * GetCurStoreCfgAddr(void ) |
|
626 |
{ |
|
627 |
int s = sizeof(stStoreCfg); |
|
628 |
int s2=next_pow_of_2(s); |
|
629 |
stStoreCfg * p1; |
|
630 |
int nMaxSN=0; |
|
631 |
int nMaxId=0; |
|
632 |
for (int i=0; s2*i < STORECFGPAGESIZE ; i++) |
|
633 |
{ |
|
634 |
p1= (stStoreCfg *)(STORECFGBASE + s2 * i ); |
|
635 |
if (p1->Sign1 != START_SIGN) continue; |
|
636 |
if (p1->EndSign1 != END_SIGN) continue; |
|
637 |
|
|
638 |
if (p1->Seq1 >= nMaxSN) {nMaxSN = p1->Seq1; nMaxId = i;} |
|
639 |
} |
|
640 |
// nMaxId=nMaxId+1; |
|
641 |
return (stStoreCfg *)(STORECFGBASE + s2 * nMaxId); |
|
642 |
} |
|
643 |
|
|
644 |
stStoreCfg * GetNextStoreCfgAddr(stStoreCfg * CurCfg ) |
|
645 |
{ |
|
646 |
int s = sizeof(stStoreCfg); |
|
647 |
int s2=next_pow_of_2(s); |
|
648 |
uint32_t nAddr1 = (uint32_t) CurCfg; |
|
649 |
uint32_t nAddr2 = nAddr1 + s2; |
|
650 |
for (int i=1;i<33;i++) |
|
651 |
{ |
|
652 |
int skip=0; |
|
653 |
nAddr2 = nAddr1 + s2*i; |
|
654 |
if ((nAddr2 + s) > STORECFGBASE + STORECFGPAGESIZE) |
|
655 |
{ |
|
656 |
nAddr2=STORECFGBASE; break; |
|
657 |
} |
|
658 |
for (int j=0;j<s2;j++) |
|
659 |
{ |
|
660 |
if (((unsigned char *)nAddr2)[j] != 0xff) |
|
661 |
{skip =1;} |
|
662 |
} |
|
663 |
if (skip==1) {continue;} |
|
664 |
break; |
|
665 |
} |
|
666 |
stStoreCfg * p1 = (stStoreCfg *)nAddr2; |
|
667 |
return p1; |
|
668 |
} |
|
669 |
|
|
670 |
|
|
671 |
int SaveStoreCfg(stStoreCfg * CurCfg) |
|
672 |
{ |
|
673 |
return 0; |
|
674 |
} |
|
675 |
*/ |
|
676 |
// stStoreCfg Cfg2; |
|
677 |
|
|
678 |
int LoadFlashDatas() |
|
679 |
{ |
|
680 |
for (int i=0;i<16;i++) |
|
681 |
{ |
|
682 |
// FlashDatas[i]=((uint16_t *)pConfigFlashBase)[i]; |
|
683 |
} |
|
684 |
return 0; |
|
685 |
} |
|
686 |
/* |
|
687 |
int LoadAndUpdateStoreCfg() |
|
688 |
{ |
|
689 |
stStoreCfg * pFCfg = (stStoreCfg *) GetCurStoreCfgAddr(); |
|
690 |
|
|
691 |
Cfg2.Sign1=START_SIGN; |
|
692 |
Cfg2.Seq1=pFCfg[0].Seq1+1; |
|
693 |
Cfg2.CRC1=0x7777; |
|
694 |
Cfg2.PowerCount=pFCfg[0].PowerCount+1; |
|
695 |
Cfg2.UpTime=pFCfg[0].UpTime+1; |
|
696 |
Cfg2.UserData1=pFCfg[0].UserData1; |
|
697 |
Cfg2.EndSign1=END_SIGN; |
|
698 |
stStoreCfg * pFCfg2 = GetNextStoreCfgAddr(pFCfg); |
|
699 |
|
|
700 |
|
|
701 |
HAL_StatusTypeDef res; |
|
702 |
if (pFCfg2 <= pFCfg) |
|
703 |
{ |
|
704 |
res = (HAL_StatusTypeDef)EraseAndWriteToFlashMem(&Cfg2,pFCfg2,sizeof(stStoreCfg)); |
|
705 |
|
|
706 |
}else |
|
707 |
{ |
|
708 |
res = (HAL_StatusTypeDef)WriteToFlashMemNoErase(&Cfg2,pFCfg2,sizeof(stStoreCfg)); |
|
709 |
} |
|
710 |
return res; |
|
711 |
} |
|
712 |
*/ |
|
713 |
int CheckSavedData(void * pStartAddr, int PageSize, int Pages, int DataSize) |
|
714 |
{ |
|
715 |
return 0; |
|
716 |
}; |
|
717 |
|
|
718 |
int LoadDefaultRunStat(pRunStat theRunStat) |
|
719 |
{ |
|
720 |
theRunStat->PowerCount=1; |
|
721 |
// theRunStat->UpTime=0; |
|
722 |
// theRunStat->UserData1=0; |
|
723 |
// theRunStat->WorkMode=0; |
|
724 |
// theRunStat->WorkMode2=0; |
|
725 |
// theRunStat->nBinProgBank=0; |
842bb6
|
726 |
// theRunStat->nBinInstrcnSize=0; |
483170
|
727 |
return 0; |
Q |
728 |
} |
|
729 |
int LoadRunStat(pRunStat theRunStat) |
|
730 |
{ |
|
731 |
uchar * pRunStatStore = (uchar *)STORE_RUNSTAT_BASE; |
|
732 |
pRunStat pStoreRunStats = (pRunStat)pRunStatStore; |
|
733 |
// int s = sizeof(stRunStat); |
|
734 |
|
5dd1b7
|
735 |
for (int i=0;i * sizeof(stRunStat) < (FLASH_PAGE_SIZE * STORE_RUNSTAT_PAGES) ;i++) |
483170
|
736 |
{ |
Q |
737 |
if (pStoreRunStats[i].Sign1 == START_SIGN ) |
|
738 |
{ |
|
739 |
if (pStoreRunStats[i].Seq1 > nMaxRunStatSeq) |
|
740 |
{ |
|
741 |
nMaxRunStatSeq = pStoreRunStats[i].Seq1; |
|
742 |
nMaxRunStatIndex=i; |
|
743 |
nNextRunStatSpace=i+1; |
|
744 |
} |
|
745 |
} |
|
746 |
} |
|
747 |
if (nMaxRunStatIndex>=0) // && nMaxRunStatIndex <8) |
|
748 |
{ |
|
749 |
ReadFlashMem(theRunStat,(void *)(pStoreRunStats+nMaxRunStatIndex),sizeof(stRunStat)); |
|
750 |
}else { |
|
751 |
LoadDefaultRunStat(theRunStat); |
|
752 |
} |
|
753 |
// find Next Space |
|
754 |
// if Same Page with MaxSeq Index, then not erase, skip and skip. |
|
755 |
// if next Page of MaxSeq Index, then earse if not empty; |
5dd1b7
|
756 |
if ((nNextRunStatSpace + 1) * sizeof(stRunStat) > FLASH_PAGE_SIZE * STORE_RUNSTAT_PAGES) { |
483170
|
757 |
nNextRunStatSpace=0; |
Q |
758 |
} |
|
759 |
return 0; |
|
760 |
} |
|
761 |
|
|
762 |
int SaveRunStat(pRunStat theRunStat) |
|
763 |
{ |
|
764 |
nMaxRunStatSeq++; |
|
765 |
theRunStat->Sign1=START_SIGN; |
|
766 |
theRunStat->Seq1 = nMaxRunStatSeq; |
|
767 |
theRunStat->PowerCount=KMem.PwrOnCount; |
|
768 |
theRunStat->UpTime=KMem.TotalRunTime; |
|
769 |
theRunStat->CRC1=0x11; |
|
770 |
theRunStat->EndSign1=END_SIGN; |
|
771 |
|
|
772 |
//check empty |
|
773 |
unsigned char *pFlash = (unsigned char *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)); |
|
774 |
int Skip=0; |
|
775 |
for (int j=0;j<sizeof(stRunStat);j++) |
|
776 |
{ |
|
777 |
if (pFlash[j]!=0xff) {Skip =1 ; break;} |
|
778 |
} |
|
779 |
if (Skip ==0 ) |
|
780 |
{ |
|
781 |
WriteToFlashMemNoErase(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat)); |
|
782 |
}else |
|
783 |
{ |
|
784 |
EraseAndWriteToFlashMem(theRunStat,(void *)(STORE_RUNSTAT_BASE + nNextRunStatSpace*sizeof(stRunStat)),sizeof(stRunStat)); |
|
785 |
} |
|
786 |
nMaxRunStatIndex=nNextRunStatSpace; |
|
787 |
nNextRunStatSpace++; |
5dd1b7
|
788 |
if ((nNextRunStatSpace+1) * sizeof(stRunStat) > FLASH_PAGE_SIZE * STORE_RUNSTAT_PAGES) |
483170
|
789 |
{ |
Q |
790 |
nNextRunStatSpace=0; |
|
791 |
} |
|
792 |
return 0; |
|
793 |
} |
|
794 |
|
842bb6
|
795 |
int WriteNewApp(int nProgByteOffset, void *pBuf, int nByteSize) |
5dd1b7
|
796 |
{ |
842bb6
|
797 |
|
5dd1b7
|
798 |
int res = -1; |
842bb6
|
799 |
int FlashSize = *(ushort *)FLASHSIZE_BASE; |
Q |
800 |
int NewAppAddress ; |
|
801 |
if (FlashSize == 64) { |
|
802 |
NewAppAddress = 0x08009000U; |
|
803 |
}else if (FlashSize == 256) { |
|
804 |
NewAppAddress = 0x08021000U; |
|
805 |
} |
|
806 |
if ((nProgByteOffset&(FLASH_PAGE_SIZE-1)) ==0){ |
5dd1b7
|
807 |
// EraseFlashMem((void *)(NewAppAddress + nProgByteAddr),1); |
842bb6
|
808 |
|
Q |
809 |
res = EraseAndWriteToFlashMem(pBuf,(void *)(NewAppAddress + nProgByteOffset),nByteSize); |
5dd1b7
|
810 |
}else { |
Q |
811 |
// if (nByteSize>64) return 0; |
842bb6
|
812 |
res = WriteToFlashMemNoErase(pBuf,(void *)(NewAppAddress + nProgByteOffset),nByteSize); |
5dd1b7
|
813 |
} |
Q |
814 |
return res; |
|
815 |
} |
|
816 |
|
|
817 |
int WriteNewAppInfo(int nProgByteAddr, void *pBuf, int nByteSize) |
|
818 |
{ |
842bb6
|
819 |
int FlashSize = *(ushort *)FLASHSIZE_BASE; |
Q |
820 |
int NewAppInfoBlockAddress ; |
|
821 |
if (FlashSize == 64) { |
|
822 |
NewAppInfoBlockAddress = 0x08008000; |
|
823 |
}else if (FlashSize == 256) { |
|
824 |
NewAppInfoBlockAddress = 0x08020000; |
|
825 |
} |
|
826 |
|
5dd1b7
|
827 |
int res = EraseAndWriteToFlashMem(pBuf,(void *)(NewAppInfoBlockAddress + nProgByteAddr),nByteSize); |
Q |
828 |
return res; |
|
829 |
} |
483170
|
830 |
|
Q |
831 |
int CheckEventLog() |
|
832 |
{ |
|
833 |
unsigned int nMinEventSeq=999999999; |
|
834 |
uchar * pEventStore = (uchar *)STORE_LOG_BASE; |
|
835 |
pEventLog theEventLog = (pEventLog) pEventStore; |
|
836 |
// int s = sizeof(stEventLog); |
|
837 |
nEventCount=0; |
|
838 |
|
5dd1b7
|
839 |
for (int i=0;i * sizeof(stEventLog) < (FLASH_PAGE_SIZE * STORE_LOG_PAGES) ;i++) |
483170
|
840 |
{ |
Q |
841 |
if (theEventLog[i].Sign1 == START_SIGN ) |
|
842 |
{ |
|
843 |
nEventCount++; |
|
844 |
if (theEventLog[i].Seq1 > nEventMaxSeq) |
|
845 |
{ |
|
846 |
nEventMaxSeq = theEventLog[i].Seq1; |
|
847 |
nEventMaxIndex=i; |
|
848 |
nMaxCurTime=theEventLog[i].nTime; |
|
849 |
nEventNextSpace=i+1; |
|
850 |
} |
|
851 |
if (theEventLog[i].Seq1 < nMinEventSeq) |
|
852 |
{ |
|
853 |
nMinEventSeq = theEventLog[i].Seq1; |
|
854 |
nEventMinIndex = i; |
|
855 |
} |
|
856 |
} |
|
857 |
} |
|
858 |
// find Next Space |
|
859 |
// if Same Page with MaxSeq Index, then not erase, skip and skip. |
|
860 |
// if next Page of MaxSeq Index, then earse if not empty; |
5dd1b7
|
861 |
if ((nEventNextSpace + 1) * sizeof(stEventLog) > FLASH_PAGE_SIZE * STORE_LOG_PAGES) { |
483170
|
862 |
nEventNextSpace=0; |
Q |
863 |
} |
|
864 |
|
|
865 |
return nEventCount; |
|
866 |
} |
|
867 |
|
|
868 |
int AddEventLog(uint32_t nTime, USHORT nEvent, USHORT nParam1, UINT nParam2) |
|
869 |
{ |
|
870 |
nEventMaxSeq++; |
|
871 |
stEventLog thisEventLog={START_SIGN, nEventMaxSeq, nTime,nEvent,nParam1,nParam2}; |
|
872 |
//check empty |
|
873 |
unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)); |
|
874 |
int Skip=0; |
|
875 |
for (int j=0;j<sizeof(stEventLog);j++) |
|
876 |
{ |
|
877 |
if (pFlash[j]!=0xff) {Skip =1 ; break;} |
|
878 |
} |
|
879 |
if (Skip ==0 ) |
|
880 |
{ |
|
881 |
WriteToFlashMemNoErase(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog)); |
|
882 |
}else |
|
883 |
{ |
|
884 |
EraseAndWriteToFlashMem(&thisEventLog,(void *)(STORE_LOG_BASE + nEventNextSpace*sizeof(stEventLog)),sizeof(stEventLog)); |
|
885 |
|
|
886 |
} |
|
887 |
nEventMaxIndex=nEventNextSpace; |
|
888 |
nEventNextSpace++; |
5dd1b7
|
889 |
if ((nEventNextSpace+1) * sizeof(stEventLog) > FLASH_PAGE_SIZE * STORE_LOG_PAGES) |
483170
|
890 |
{ |
Q |
891 |
nEventNextSpace=0; |
|
892 |
} |
|
893 |
nEventCount++; |
|
894 |
KMem.nEventCount=nEventCount; |
|
895 |
return 0; |
|
896 |
} |
|
897 |
|
|
898 |
pEventLog GetEventLogAddr(int nIndex) |
|
899 |
{ |
|
900 |
int nEventIndex=nEventMinIndex + nIndex; |
|
901 |
|
5dd1b7
|
902 |
if (nEventIndex * sizeof(stEventLog) >= (FLASH_PAGE_SIZE * STORE_LOG_PAGES)) |
483170
|
903 |
{ |
5dd1b7
|
904 |
nEventIndex -= (FLASH_PAGE_SIZE * STORE_LOG_PAGES)/sizeof(stEventLog); |
483170
|
905 |
} |
Q |
906 |
unsigned char *pFlash = (unsigned char *)(STORE_LOG_BASE + nEventIndex*sizeof(stEventLog)); |
|
907 |
|
|
908 |
return (pEventLog)pFlash; |
|
909 |
} |
|
910 |
|
|
911 |
int ClearEventLog(void) |
|
912 |
{ |
|
913 |
EraseFlashMem((void *)STORE_LOG_BASE,STORE_LOG_PAGES); |
|
914 |
nEventMinIndex=0; |
|
915 |
nEventMaxIndex=0; |
|
916 |
nEventMaxSeq=0; |
|
917 |
nEventCount=0; |
|
918 |
nEventNextSpace=0; |
|
919 |
return 0; |
|
920 |
} |
|
921 |
|
842bb6
|
922 |
inline void SetAddrBit(unsigned short * pW, unsigned char bitPos) |
Q |
923 |
{ |
|
924 |
(*pW)|=1<<(bitPos&0xf); |
483170
|
925 |
} |
Q |
926 |
|
842bb6
|
927 |
inline void ResetBit(unsigned short * pW, unsigned char bitPos) |
483170
|
928 |
{ |
842bb6
|
929 |
(*pW)&=~(1<<(bitPos&0xf)); |
483170
|
930 |
} |
Q |
931 |
|
842bb6
|
932 |
static inline void SetBitValue(unsigned short * pW, unsigned char bitPos, unsigned char Value) |
483170
|
933 |
{ |
842bb6
|
934 |
if (Value) { SetAddrBit(pW, bitPos);} |
Q |
935 |
else {ResetBit(pW, bitPos);} |
483170
|
936 |
} |
Q |
937 |
|
842bb6
|
938 |
static inline unsigned char GetBitValue(unsigned short W, unsigned char bitPos) |
483170
|
939 |
{ |
842bb6
|
940 |
if (W&(1<<(bitPos&0xf))) return 1; |
483170
|
941 |
else return 0; |
Q |
942 |
} |
|
943 |
|
|
944 |
|
|
945 |
unsigned char GetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr) |
|
946 |
{ |
|
947 |
unsigned char thisValue=0; |
|
948 |
unsigned short nWordAddr=(nCoilAddr&0xff0)>>4; |
842bb6
|
949 |
unsigned char nBitPos=nCoilAddr&0xf; |
483170
|
950 |
switch(nCoilType) |
Q |
951 |
{ |
|
952 |
case KLCoilTypeX: |
|
953 |
if (nCoilAddr >= KLCoilXCount) return 0; |
842bb6
|
954 |
thisValue = GetBitValue(KMem.WX[nWordAddr], nBitPos); |
483170
|
955 |
break; |
Q |
956 |
case KLCoilTypeY: |
|
957 |
if (nCoilAddr >= KLCoilYCount) return 0; |
842bb6
|
958 |
thisValue = GetBitValue(KMem.WY[nWordAddr], nBitPos); |
483170
|
959 |
break; |
Q |
960 |
case KLCoilTypeR: |
842bb6
|
961 |
|
Q |
962 |
if (nCoilAddr < KLCoilRCount) { |
|
963 |
thisValue = GetBitValue(KMem.WR[nWordAddr], nBitPos); |
|
964 |
}else if (nCoilAddr > 9000) { |
|
965 |
if (nCoilAddr == 9010) thisValue = 1; |
|
966 |
if (nCoilAddr == 9011) thisValue = 0; |
|
967 |
if (nCoilAddr == 9013) thisValue = GetBitValue(KMem.WSR[nWordAddr], 13); |
|
968 |
} |
|
969 |
// return thisValue; |
483170
|
970 |
break; |
Q |
971 |
case KLCoilTypeLX: |
|
972 |
if (nCoilAddr >= KLCoilLXCount) return 0; |
842bb6
|
973 |
thisValue = GetBitValue(KMem.WLX[nWordAddr], nBitPos); |
483170
|
974 |
break; |
Q |
975 |
case KLCoilTypeLY: |
|
976 |
if (nCoilAddr >= KLCoilLYCount) return 0; |
842bb6
|
977 |
thisValue = GetBitValue(KMem.WLY[nWordAddr], nBitPos); |
483170
|
978 |
break; |
842bb6
|
979 |
#if (ENABLE_PLC) |
483170
|
980 |
case KLCoilTypeT: |
Q |
981 |
if (nCoilAddr >= KLCoilTCount) return 0; |
842bb6
|
982 |
thisValue = GetBitValue(PLCMem.WT[nWordAddr], nBitPos); |
483170
|
983 |
break; |
Q |
984 |
case KLCoilTypeC: |
|
985 |
if (nCoilAddr >= KLCoilCCount) return 0; |
842bb6
|
986 |
thisValue = GetBitValue(PLCMem.WC[nWordAddr], nBitPos); |
483170
|
987 |
break; |
842bb6
|
988 |
#endif |
483170
|
989 |
case KLCoilTypeLR: |
Q |
990 |
if (nCoilAddr >= KLCoilLRCount) return 0; |
842bb6
|
991 |
thisValue = GetBitValue(KMem.WLR[nWordAddr], nBitPos); |
483170
|
992 |
break; |
Q |
993 |
case KLCoilTypeSR: |
|
994 |
if (nCoilAddr >= KLCoilSRCount) return 0; |
842bb6
|
995 |
thisValue = GetBitValue(KMem.WSR[nWordAddr], nBitPos); |
483170
|
996 |
break; |
Q |
997 |
default: |
|
998 |
break; |
|
999 |
} |
|
1000 |
return thisValue; |
|
1001 |
} |
|
1002 |
int SetCoilValue(unsigned char nCoilType, unsigned short nCoilAddr, unsigned char nCoilValue) |
|
1003 |
{ |
|
1004 |
unsigned short nWordAddr=(nCoilAddr&0xff0)>>4; |
|
1005 |
unsigned char nBitAddr=nCoilAddr&0xf; |
|
1006 |
switch(nCoilType) |
|
1007 |
{ |
|
1008 |
case KLCoilTypeX: |
|
1009 |
if (nCoilAddr >= KLCoilXCount) return 0; |
|
1010 |
SetBitValue(&KMem.WX[nWordAddr], nBitAddr, nCoilValue); |
|
1011 |
break; |
|
1012 |
case KLCoilTypeY: |
|
1013 |
if (nCoilAddr >= KLCoilYCount) return 0; |
|
1014 |
SetBitValue(&KMem.WY[nWordAddr], nBitAddr, nCoilValue); |
|
1015 |
break; |
|
1016 |
case KLCoilTypeR: |
|
1017 |
if (nCoilAddr >= KLCoilRCount) return 0; |
|
1018 |
SetBitValue(&KMem.WR[nWordAddr], nBitAddr, nCoilValue); |
|
1019 |
break; |
|
1020 |
case KLCoilTypeLX: |
|
1021 |
if (nCoilAddr >= KLCoilLXCount) return 0; |
|
1022 |
SetBitValue(&KMem.WLX[nWordAddr], nBitAddr, nCoilValue); |
|
1023 |
break; |
|
1024 |
case KLCoilTypeLY: |
|
1025 |
if (nCoilAddr >= KLCoilLYCount) return 0; |
|
1026 |
SetBitValue(&KMem.WLY[nWordAddr], nBitAddr, nCoilValue); |
|
1027 |
break; |
842bb6
|
1028 |
#if (ENABLE_PLC) |
483170
|
1029 |
case KLCoilTypeT: |
Q |
1030 |
if (nCoilAddr >= KLCoilTCount) return 0; |
842bb6
|
1031 |
SetBitValue(&PLCMem.WT[nWordAddr], nBitAddr, nCoilValue); |
483170
|
1032 |
break; |
Q |
1033 |
case KLCoilTypeC: |
|
1034 |
if (nCoilAddr >= KLCoilCCount) return 0; |
842bb6
|
1035 |
SetBitValue(&PLCMem.WC[nWordAddr], nBitAddr, nCoilValue); |
483170
|
1036 |
break; |
842bb6
|
1037 |
#endif |
483170
|
1038 |
case KLCoilTypeLR: |
Q |
1039 |
if (nCoilAddr >= KLCoilLRCount) return 0; |
|
1040 |
SetBitValue(&KMem.WLR[nWordAddr], nBitAddr, nCoilValue); |
|
1041 |
break; |
|
1042 |
case KLCoilTypeSR: |
|
1043 |
if (nCoilAddr >= KLCoilSRCount) return 0; |
|
1044 |
SetBitValue(&KMem.WSR[nWordAddr], nBitAddr, nCoilValue); |
|
1045 |
break; |
|
1046 |
default: |
|
1047 |
break; |
|
1048 |
} |
|
1049 |
return 0; |
|
1050 |
} |
|
1051 |
|
|
1052 |
int GetVarData(int nDataType, int nDataAddr) |
|
1053 |
{ |
|
1054 |
// TODO: ?????????. |
|
1055 |
int thisValue = 0; |
|
1056 |
|
|
1057 |
switch (nDataType) |
|
1058 |
{ |
|
1059 |
case KLDataTypeDEC: |
|
1060 |
case KLDataTypeHEX: |
|
1061 |
thisValue = nDataAddr; |
|
1062 |
break; |
|
1063 |
case KLDataTypeWX: |
|
1064 |
if (nDataAddr >= KLDataWXCount) return 0; |
|
1065 |
thisValue = KMem.WX[nDataAddr]; |
|
1066 |
break; |
|
1067 |
case KLDataTypeWY: |
|
1068 |
if (nDataAddr >= KLDataWYCount) return 0; |
|
1069 |
thisValue = KMem.WY[nDataAddr]; |
|
1070 |
break; |
|
1071 |
case KLDataTypeWR: |
|
1072 |
if (nDataAddr >= KLDataWRCount) return 0; |
|
1073 |
thisValue = KMem.WR[nDataAddr]; |
|
1074 |
break; |
|
1075 |
case KLDataTypeWLX: |
|
1076 |
if (nDataAddr >= KLDataWLCount) return 0; |
|
1077 |
thisValue = KMem.WLX[nDataAddr]; |
|
1078 |
break; |
|
1079 |
case KLDataTypeWLY: |
|
1080 |
if (nDataAddr >= KLDataWLCount) return 0; |
|
1081 |
thisValue = KMem.WLY[nDataAddr]; |
|
1082 |
break; |
|
1083 |
case KLDataTypeDT: |
|
1084 |
if (nDataAddr >= KLDataDTCount) return 0; |
|
1085 |
thisValue = (signed short)KMem.DT[nDataAddr]; |
|
1086 |
break; |
|
1087 |
case KLDataTypeSDT: |
|
1088 |
if (nDataAddr >= KLDataSDTCount) return 0; |
|
1089 |
thisValue = KMem.SDT[nDataAddr]; |
|
1090 |
break; |
|
1091 |
case KLDataTypeWSR: |
|
1092 |
if (nDataAddr >= KLCoilLRCount) return 0; |
|
1093 |
thisValue = KMem.WSR[nDataAddr]; |
|
1094 |
break; |
842bb6
|
1095 |
#if (ENABLE_PLC) |
483170
|
1096 |
case KLDataTypeSV: |
Q |
1097 |
if (nDataAddr >= KLDataSVCount) return 0; |
842bb6
|
1098 |
thisValue = PLCMem.SV[nDataAddr]; |
483170
|
1099 |
break; |
Q |
1100 |
case KLDataTypeEV: |
|
1101 |
if (nDataAddr >= KLDataEVCount) return 0; |
842bb6
|
1102 |
thisValue = PLCMem.EV[nDataAddr]; |
483170
|
1103 |
break; |
842bb6
|
1104 |
#endif |
483170
|
1105 |
case KLDataTypeLD: |
Q |
1106 |
if (nDataAddr >= KLDataLDCount) return 0; |
|
1107 |
thisValue = KMem.DT[nDataAddr]; |
|
1108 |
break; |
|
1109 |
case KLDataSysCfg: |
|
1110 |
if (nDataAddr >= KLCoilSRCount) return 0; |
|
1111 |
thisValue = KMem.SDT[nDataAddr]; |
|
1112 |
break; |
|
1113 |
case KLDataTypeFlash: |
|
1114 |
if (nDataAddr >= KLCoilSRCount) return 0; |
|
1115 |
thisValue = KMem.SDT[nDataAddr]; |
|
1116 |
break; |
|
1117 |
case KLDataTypeTest: |
|
1118 |
if (nDataAddr >= KLCoilSRCount) return 0; |
|
1119 |
thisValue = KMem.SDT[nDataAddr]; |
|
1120 |
break; |
|
1121 |
} |
|
1122 |
return thisValue; |
|
1123 |
} |
|
1124 |
|
|
1125 |
|
|
1126 |
int SetVarData(int nDataType, int nDataAddr, int nDataValue) |
|
1127 |
{ |
|
1128 |
// TODO: ?????????. |
|
1129 |
switch (nDataType) |
|
1130 |
{ |
|
1131 |
// case KLDataTypeDEC: |
|
1132 |
// case KLDataTypeHEX: |
|
1133 |
// break; |
|
1134 |
case KLDataTypeWX: |
|
1135 |
if (nDataAddr >= KLDataWXCount) return 0; |
|
1136 |
KMem.WX[nDataAddr] = nDataValue; |
|
1137 |
break; |
|
1138 |
case KLDataTypeWY: |
|
1139 |
if (nDataAddr >= KLDataWYCount) return 0; |
|
1140 |
KMem.WY[nDataAddr] = nDataValue; |
|
1141 |
break; |
|
1142 |
case KLDataTypeWR: |
|
1143 |
if (nDataAddr >= KLDataWRCount) return 0; |
|
1144 |
KMem.WR[nDataAddr] = nDataValue; |
|
1145 |
break; |
|
1146 |
case KLDataTypeWLX: |
|
1147 |
if (nDataAddr >= KLDataWLCount) return 0; |
|
1148 |
KMem.WLX[nDataAddr] = nDataValue; |
|
1149 |
break; |
|
1150 |
case KLDataTypeWLY: |
|
1151 |
if (nDataAddr >= KLDataWLCount) return 0; |
|
1152 |
KMem.WLY[nDataAddr] = nDataValue; |
|
1153 |
break; |
|
1154 |
case KLDataTypeDT: |
|
1155 |
if (nDataAddr >= KLDataDTCount) return 0; |
|
1156 |
KMem.DT[nDataAddr] = nDataValue; |
|
1157 |
break; |
|
1158 |
case KLDataTypeSDT: |
|
1159 |
if (nDataAddr >= KLDataSDTCount) return 0; |
|
1160 |
KMem.SDT[nDataAddr] = nDataValue; |
|
1161 |
break; |
|
1162 |
case KLDataTypeWSR: |
|
1163 |
if (nDataAddr >= KLCoilLRCount) return 0; |
|
1164 |
KMem.WSR[nDataAddr] = nDataValue; |
|
1165 |
break; |
842bb6
|
1166 |
#if (ENABLE_PLC) |
483170
|
1167 |
case KLDataTypeSV: |
Q |
1168 |
if (nDataAddr >= KLDataSVCount) return 0; |
842bb6
|
1169 |
PLCMem.SV[nDataAddr] = nDataValue; |
483170
|
1170 |
break; |
Q |
1171 |
case KLDataTypeEV: |
|
1172 |
if (nDataAddr >= KLDataEVCount) return 0; |
842bb6
|
1173 |
PLCMem.EV[nDataAddr] = nDataValue; |
483170
|
1174 |
break; |
842bb6
|
1175 |
#endif |
483170
|
1176 |
case KLDataTypeLD: |
Q |
1177 |
if (nDataAddr >= KLDataLDCount) return 0; |
|
1178 |
KMem.DT[nDataAddr] = nDataValue; |
|
1179 |
break; |
|
1180 |
case KLDataSysCfg: |
|
1181 |
if (nDataAddr >= KLCoilSRCount) return 0; |
|
1182 |
KMem.SDT[nDataAddr] = nDataValue; |
|
1183 |
break; |
|
1184 |
case KLDataTypeFlash: |
|
1185 |
if (nDataAddr >= KLCoilSRCount) return 0; |
|
1186 |
KMem.SDT[nDataAddr] = nDataValue; |
|
1187 |
break; |
|
1188 |
case KLDataTypeTest: |
|
1189 |
if (nDataAddr >= KLCoilSRCount) return 0; |
|
1190 |
KMem.SDT[nDataAddr] = nDataValue; |
|
1191 |
break; |
|
1192 |
} |
|
1193 |
|
|
1194 |
return 0; |
|
1195 |
} |
|
1196 |
|
5dd1b7
|
1197 |
|
Q |
1198 |
int KMachineSvFunc (int nChn, int nSvType, int nParam,void * pBuf, int nLen1) |
|
1199 |
{ |
|
1200 |
int iRet =0; |
|
1201 |
switch (nSvType){ |
|
1202 |
case ReqNone: |
|
1203 |
break; |
|
1204 |
case ReqInit: |
|
1205 |
break; |
|
1206 |
case ReqReset: |
|
1207 |
break; |
|
1208 |
case ReqStop: |
|
1209 |
break; |
|
1210 |
case ReqRun: |
|
1211 |
break; |
842bb6
|
1212 |
case ReqBlinkLED: |
Q |
1213 |
KMRunStat.bLEDFlick=nParam; |
5dd1b7
|
1214 |
break; |
Q |
1215 |
case ReqStartDiag: |
|
1216 |
break; |
|
1217 |
case ReqStopDiag: |
|
1218 |
break; |
|
1219 |
case ReqTransFirmware: |
|
1220 |
break; |
|
1221 |
case ReqTransCfg: |
|
1222 |
break; |
|
1223 |
case ReqTransProg: |
|
1224 |
break; |
|
1225 |
case ReqTransData: |
|
1226 |
break; |
|
1227 |
case ReqTransBlink: |
|
1228 |
break; |
|
1229 |
case ReqTransChild: |
|
1230 |
break; |
|
1231 |
case ReqTransInfo: |
|
1232 |
break; |
|
1233 |
case ReqTransOutBandData: |
|
1234 |
break; |
|
1235 |
case ReqRead1Bit: |
|
1236 |
break; |
|
1237 |
case ReqWrite1Bit: |
|
1238 |
break; |
|
1239 |
case ReqReadBits: |
|
1240 |
break; |
|
1241 |
case ReqWriteBits: |
|
1242 |
break; |
|
1243 |
case ReqReadData: |
|
1244 |
break; |
|
1245 |
case ReqWriteData: |
|
1246 |
break; |
|
1247 |
case ReqRemoteTran: |
|
1248 |
break; |
|
1249 |
|
|
1250 |
default: |
|
1251 |
iRet = -1; |
|
1252 |
break; |
|
1253 |
} |
|
1254 |
return iRet; |
|
1255 |
} |