eeprom.c
上传用户:jankzhpno
上传日期:2022-08-03
资源大小:4763k
文件大小:4k
源码类别:

Windows CE

开发平台:

Visual C++

  1. /*
  2.  create by hzh, to support wr/rd parameters to/from eeprom
  3. */
  4. #include "def.h"
  5. #include "2440addr.h"
  6. #include "2440lib.h"
  7. #define SLV_ADDR_24C02 0xa0
  8. void Delay(int ms);
  9. static volatile int iic_ints;
  10. static void __irq IicInt(void)
  11. {
  12. ClearPending(BIT_IIC);
  13. iic_ints++;
  14. }
  15. static int iic_wr(U8 data, U8 cnt)
  16. {
  17. int i, c;
  18. rIICCON &= ~0x10; //Resume IIC operation to send
  19. if(cnt)
  20. rIICDS = data;
  21. i = 3; //3ms
  22. //while(!(rIICCON&0x10)&&i--) Delay(1);
  23. c = iic_ints;
  24. while(iic_ints==c&&i--) Delay(1);
  25. //rIICCON &= ~0x10;
  26. if(!i)
  27. return -1;
  28. return (rIICSTAT&1)?-1:0;
  29. }
  30. static int iic_rd(U8 *pBuf)
  31. {
  32. int i, c;
  33. rIICCON &= ~0x10; //Resumes IIC operation to receive
  34. i = 3; //3ms
  35. //while(!(rIICCON&0x10)&&i--) Delay(1);
  36. c = iic_ints;
  37. while(iic_ints==c&i--) Delay(1);
  38. if(i) {
  39. *pBuf = rIICDS;
  40. return 0;
  41. }
  42. return -1;
  43. }
  44. static int seek_iic_device(U8 slvAddr)
  45. {
  46. int i;
  47. i = 40; //400ms
  48. while(i--) {
  49. rIICDS   = slvAddr;
  50. rIICSTAT = 0xf0; //MasTx,Start
  51. if(!iic_wr(0, 0))
  52. break;
  53. Delay(10);
  54. }
  55. rIICSTAT = 0xd0; //Stop MasTx condition
  56. return i<40;
  57. }
  58. //8 bits register address only
  59. static int write_eeprom(U8 slvAddr, U8 addr, U8 data)
  60. {
  61. int i;
  62. U8 ch[3];
  63. ch[1] = addr;
  64. ch[2] = data;
  65. rIICDS   = slvAddr;
  66. rIICSTAT = 0xf0; //MasTx,Start
  67. for(i=0; i<3; i++)
  68. if(iic_wr(ch[i], i))
  69. break;
  70. rIICSTAT = 0xd0; //Stop MasTx condition
  71. if(i<3)
  72. return -1;
  73. i = 200; //200ms
  74. while(i--) {
  75. Delay(1);
  76. rIICDS     = slvAddr;
  77. rIICSTAT   = 0xf0;
  78. if(!iic_wr(0, 0))
  79. break;
  80. }
  81. rIICSTAT = 0xd0;
  82. return i?0:-1;
  83. }
  84. //8 bits register address only
  85. static int read_eeprom(U8 slvAddr, U8 addr, U8 *pBuf)
  86. {
  87. int i;
  88. U8 ch[2];
  89. ch[1] = addr;
  90. rIICDS   = slvAddr;
  91. rIICSTAT = 0xf0; //MasTx,Start
  92. for(i=0; i<2; i++)
  93. if(iic_wr(ch[i], i))
  94. break;
  95. if(i<2) {
  96. rIICSTAT = 0xd0; //Stop MasTx condition
  97. return -1;
  98. }
  99. rIICDS   = slvAddr;
  100. rIICSTAT = 0xb0; //Master Rx,Start
  101. if(!iic_wr(0, 0)) //wait ack
  102. i = iic_rd(pBuf);
  103. rIICSTAT = 0x90; //Stop MaxRx condition
  104. return i;
  105. }
  106. static void init_iic_op(void)
  107. {
  108. rCLKCON |= 1<<16;
  109. rGPEUP  |= 0xc000;                  //Pull-up disable
  110. rGPECON &= ~0xf0000000;
  111. rGPECON |= 0xa0000000;              //GPE15:IICSDA , GPE14:IICSCL 
  112. //Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
  113. // If PCLK 50.7MHz, IICCLK = 3.17MHz, Tx Clock = 0.198MHz
  114. //rIICCON = (1<<7) | (1<<6) | (1<<5) | (0); //IICCLK=PCLK/512=97K
  115. //just read one byte, don't enable ack!
  116. rIICCON = (0<<7) | (1<<6) | (1<<5) | (0); //IICCLK=PCLK/512=97K
  117. rIICSTAT = 0x10;                    //IIC bus data output enable(Rx/Tx)
  118. rIICADD  = 0x10;                    //2410 slave address = [7:1]
  119. pISR_IIC = (unsigned)IicInt;
  120. EnableIrq(BIT_IIC);
  121. }
  122. static void end_iic_op(void)
  123. {
  124. rCLKCON &= ~(1<<16);
  125. DisableIrq(BIT_IIC);
  126. }
  127. int write_24c02(U8 *pBuf)
  128. {
  129. int i;
  130. U8 dat[256];
  131. for(i=0; i<256; i++)
  132. dat[i] = 255-i;
  133. init_iic_op();
  134. for(i=0; i<256; i++) {
  135. //if(seek_iic_device(SLV_ADDR_24C02))
  136. // break;
  137. //if(write_eeprom(SLV_ADDR_24C02, i, pBuf[i]))
  138. if(write_eeprom(SLV_ADDR_24C02, i, dat[i]))
  139. break;
  140. }
  141. end_iic_op();
  142. Uart_Printf("Write EEPROM %s!n", (i<256)?"Fail":"Success");
  143. return i<256;
  144. }
  145. int read_24c02(U8 *pBuf)
  146. {
  147. int i;
  148. init_iic_op();
  149. for(i=0; i<256; i++) {
  150. if(read_eeprom(SLV_ADDR_24C02, i, pBuf+i))
  151. break;
  152. }
  153. end_iic_op();
  154. //Uart_Printf("Read EEPROM %s!n", (i<256)?"Fail":"Success");
  155. return i<128;
  156. }