📄 eeprom.lst
字号:
265 )
266 {
267 1 uchar i=0;
268 1 uint j=0;
269 1 /* Filter the parameters */
270 1 if(num_of_byte>32) num_of_byte=32;
271 1 if(address>0x3fff) address=0;
272 1
273 1 /* make sure that the device is ready */
274 1 /* NOTE:No time out strategy here. */
275 1 //while(!AT25_IsReady()); //jerkoh
276 1
277 1 for(j=0;j<60000;j++)
278 1 {
279 2 if(AT25_IsReady()==1)
280 2 {
281 3 break;
282 3 }
283 2 }
284 1 if(j==60000)
285 1 {
286 2 return;
287 2 }
288 1
289 1
290 1 /* make sure that the device is write enabled */
291 1 X_CS=0;
292 1 _nop_();
293 1 SPI_WriteByte(WREN);
294 1 X_CS=1;
295 1 _nop_();
296 1 _nop_();
297 1
298 1 /* write op_code,address and data into the device */
299 1 X_CS=0;
300 1 _nop_();
301 1 SPI_WriteByte(WRITE);//op_code
302 1 SPI_WriteByte((uchar)((address & 0x0F00)>>8)); //write address A11~A0
303 1 SPI_WriteByte((uchar)(address & 0x00FF));
C51 COMPILER V8.16 EEPROM 12/10/2009 11:17:10 PAGE 6
304 1
305 1
306 1 for(i=0;i<num_of_byte;i++)
307 1 {
308 2
309 2 SPI_WriteByte(*source);
310 2 source++;
311 2 }
312 1 X_CS=1;
313 1
314 1 }
315
316
317
318 /*---------------------------------------------------------
319 Init_AT25XXX()
320
321 Function: Initialize the AT25XXXA.
322 Input: None.
323 Output: None.
324 NOTE: This function initialize the AT25XXXA with no
325 data protected and #WP disable.
326 -----------------------------------------------------------*/
327 void Init_AT25XXX(uchar SetSR)
328 {
329 1 AT25_SetStatusReg(SetSR);
330 1 }
331
332
333
334
335
336 /***************************************************************************************
337 **函数功能:flash_c8051f310
338 **入参: addr
339 **返回值: 无
340 **作者: jerkoh
341 **日期: 2009-03-16
342 **说明: c8051f310擦读写
343 ***************************************************************************************/
344
345 #ifdef CFSL
void FLASH_PageErase (UINT addr)
{
bit EA_SAVE = EA; // preserve EA
UCHAR xdata * data pwrite; // FLASH write pointer
EA = 0; // disable interrupts
// change clock speed to slow, then restore later
VDM0CN = 0x80; // enable VDD monitor
RSTSRC = 0x02; // enable VDD monitor as a reset source
pwrite = (UCHAR xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x03; // PSWE = 1; PSEE = 1
VDM0CN = 0x80; // enable VDD monitor
RSTSRC = 0x02; // enable VDD monitor as a reset source
*pwrite = 0; // initiate page erase
PSCTL &= ~0x03; // PSWE = 0; PSEE = 0
EA = EA_SAVE; // restore interrupts
C51 COMPILER V8.16 EEPROM 12/10/2009 11:17:10 PAGE 7
}
#endif
368
369 #ifdef CFSL
void FLASH_ByteWrite (UINT addr, UCHAR byte)
{
bit EA_SAVE = EA; // preserve EA
UCHAR xdata * data pwrite; // FLASH write pointer
EA = 0; // disable interrupts
// change clock speed to slow, then restore later
VDM0CN = 0x80; // enable VDD monitor
RSTSRC = 0x02; // enable VDD monitor as a reset source
pwrite = (UCHAR xdata *) addr;
FLKEY = 0xA5; // Key Sequence 1
FLKEY = 0xF1; // Key Sequence 2
PSCTL |= 0x01; // PSWE = 1
VDM0CN = 0x80; // enable VDD monitor
RSTSRC = 0x02; // enable VDD monitor as a reset source
*pwrite = byte; // write the byte
PSCTL &= ~0x01; // PSWE = 0
EA = EA_SAVE; // restore interrupts
}
#endif
392
393 #ifdef CFSL
UCHAR FLASH_PageRead(UINT addr)
{
bit EA_SAVE = EA; // preserve EA
UCHAR dat;
char code * data pread; // FLASH read pointer
EA = 0;
pread=(char code *)addr;
dat=*pread;
EA = EA_SAVE; // restore interrupts
return dat;
}
#endif
409 /***************************************************************************************
410 **函数功能:flash_save
411 **入参: bufaddr s_len
412 **返回值: 无
413 **作者: jerkoh
414 **日期: 2009-03-16
415 **说明: 无
416 ***************************************************************************************/
417 void flash_save(unsigned char *source_buf,unsigned int desk_addr,unsigned char s_len)
418 {
419 1 unsigned char j;
420 1
421 1
422 1
423 1 if(s_len<=32) //make sure it's not over 512bytes
424 1 {
425 2
426 2 for(j=0;j<3;j++)
427 2 {
C51 COMPILER V8.16 EEPROM 12/10/2009 11:17:10 PAGE 8
428 3
429 3 AT25_WritePage(desk_addr,s_len,source_buf);
430 3 desk_addr+=64; //保存3份数据 隔开空间32字节
431 3 }
432 2
433 2
434 2 }
435 1 }
436 /***************************************************************************************
437 **函数功能:flash_boot
438 **入参: 无
439 **返回值: 无
440 **作者: jerkoh
441 **日期: 2009-06-30
442 **说明: 提取参数,如果检验失败 设备默认值
443 ***************************************************************************************/
444 void flash_boot()
445 {
446 1 UCHAR j;
447 1 READER_PCS xdata *pt;
448 1 UINT nf_addr;
449 1
450 1 nf_addr=F_ADDR;
451 1
452 1 for(j=0;j<3;j++)
453 1 {
454 2
455 2 AT25_ReadArray(F_ADDR,32,combuf);
456 2 pt=(READER_PCS xdata *)(combuf);
457 2
458 2
459 2 if((cal_crc(combuf,sizeof(READER_PCS)-2))==(pt->pcs_crc16))
460 2 {
461 3 break; //crc16正确 退出
462 3 }
463 2
464 2 else
465 2 {
466 3 nf_addr+=64; //错误的话查下一块 不连续 隔开32字节
467 3 }
468 2 }
469 1
470 1 // memcpy(combuf,defdata,sizeof(defdata)); //提取默认值
471 1
472 1 //flash_save(combuf,F_ADDR,32); //保存数据至flash,自动3份;
473 1
474 1 if(j==0)
475 1 {
476 2 // set_cs();
477 2 //if(DEBUG) Printf_String("\r\n[msg:]flash ok"); //块1正确直接判断正确
478 2 }
479 1
480 1 else if ((j==1)||(j==2))
481 1 {
482 2 //if(DEBUG) Printf_String("\r\n[msg:]flashok 1-2"); //如果块2或3正确,3块统一
483 2 flash_save(combuf,F_ADDR,32);
484 2 }
485 1 else
486 1 {
487 2 //if(DEBUG) Printf_String("\r\n[msg:]flash error"); //都不正确,写默认值
488 2 memcpy(combuf,defdata,sizeof(defdata)); //提取默认值
489 2
C51 COMPILER V8.16 EEPROM 12/10/2009 11:17:10 PAGE 9
490 2 flash_save(combuf,F_ADDR,32); //保存数据至flash,自动3份;
491 2 //set_cs();
492 2
493 2 }
494 1 set_cs();
495 1 }
496
497 /***************************************************************************************
498 **函数功能:set_cs
499 **入参: 无
500 **返回值: 无
501 **作者: jerkoh
502 **日期: 2009-06-30
503 **说明: 设置系统参数
504 ***************************************************************************************/
505 void set_cs()
506 {
507 1
508 1 READER_PCS xdata *ptr;
509 1
510 1 ptr=(READER_PCS xdata *)(combuf);
511 1
512 1 wiefmt=wie[ptr->pcs_wieformat];
513 1 power =pow[ptr->pcs_power];
514 1 INACTIVITY_TIME=cardouttime[ptr->pcs_samecardtime];
515 1 RELAY_TIME_IN =relay_time[ptr->pcs_relaytime];
516 1 RELAY_TIME_OUT =relay_time[ptr->pcs_relaytime];
517 1
518 1 memcpy(MyID,ptr->pcs_readerid,sizeof(MyID));
519 1 }
520
521
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 1031 ----
CONSTANT SIZE = 32 ----
XDATA SIZE = ---- 35
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -