24LCxx.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * File : 24LCxx.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-09-21 JoyChen First version, support 24LC024H eeprom device
  13. */
  14. #include <rtthread.h>
  15. #include "i2c.h"
  16. #define EE_Address 0xA0
  17. #define EE24LC024H
  18. /*
  19. Note: If eeprom size lager then EE_MEM_SIZE byte, you must define EE_ADDR_SIZE == I2C_MEM_2Bytes
  20. */
  21. #ifdef EE24LC024H
  22. #define EE_ADDR_SIZE I2C_MEM_1Byte
  23. #define EE_MEM_SIZE 256
  24. #define EE_PageSize 16
  25. #endif
  26. static struct rt_device ee_dev;
  27. uint32_t EE_ReadBuffer(void *pBuffer, rt_off_t ReadAddr, rt_size_t NumByteToRead)
  28. {
  29. return I2C_IORW(I2C1, (uint8_t *)pBuffer, (uint16_t)NumByteToRead, (uint16_t)ReadAddr, EE_Address | 0x01, I2C_MEM_1Byte );
  30. }
  31. uint32_t EE_WritePage(void *pBuffer, uint16_t WriteAddr)
  32. {
  33. I2C_IORW(I2C1, (uint8_t *)pBuffer, EE_PageSize , WriteAddr, EE_Address , EE_ADDR_SIZE );
  34. /*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error )
  35. rt_kprintf("EE ACK failed\n");*/
  36. rt_thread_delay(50);
  37. return 0;
  38. }
  39. uint32_t EE_WriteByte(void *pBuffer, uint16_t WriteAddr)
  40. {
  41. I2C_IORW(I2C1, (uint8_t *)pBuffer, 1 , WriteAddr, EE_Address, EE_ADDR_SIZE );
  42. /*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error )
  43. rt_kprintf("EE ACK failed\n");*/
  44. rt_thread_delay(50);
  45. return 0;
  46. }
  47. Status EE_WriteBuffer(const void *pBuffer, rt_off_t WriteAddr, rt_size_t NumByteToWrite)
  48. {
  49. uint8_t NumOfPage = 0, NumOfSingle = 0;
  50. uint16_t Addr = 0,count = 0;
  51. uint8_t *ptr = (uint8_t *)pBuffer;
  52. Addr = (uint16_t)(WriteAddr&0xFFFF);
  53. count = (uint16_t)(NumByteToWrite&0xFFFF);
  54. if ((WriteAddr + NumByteToWrite) > EE_MEM_SIZE)
  55. return Error;
  56. while (count >= EE_PageSize)
  57. {
  58. EE_WritePage(ptr, Addr);
  59. Addr += EE_PageSize;
  60. count -= EE_PageSize;
  61. ptr += EE_PageSize;
  62. }
  63. while (count)
  64. {
  65. EE_WriteByte(ptr++, Addr++);
  66. count--;
  67. }
  68. return Success;
  69. }
  70. static rt_err_t ee24LCxx_init(rt_device_t dev)
  71. {
  72. return RT_EOK;
  73. }
  74. static rt_size_t ee24LCxx_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size)
  75. {
  76. if (EE_ReadBuffer(buf, pos, size) == Success)
  77. return size;
  78. else
  79. return -1;
  80. }
  81. static rt_size_t ee24LCxx_write(rt_device_t dev, rt_off_t pos, const void *buf, rt_size_t size)
  82. {
  83. if (EE_WriteBuffer(buf, pos, size) == Success)
  84. return size;
  85. else
  86. return -1;
  87. }
  88. static rt_err_t ee24LCxx_open(rt_device_t dev, rt_uint16_t oflag)
  89. {
  90. return RT_EOK;
  91. }
  92. static rt_err_t ee24LCxx_close(rt_device_t dev)
  93. {
  94. return RT_EOK;
  95. }
  96. static rt_err_t ee24LCxx_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  97. {
  98. return RT_EOK;
  99. }
  100. void ee24LCxx_hw_init(void)
  101. {
  102. uint32_t delay, i;
  103. I2C1_INIT();
  104. for (i =0; i < 4; i++)
  105. {
  106. delay = 0xFFFFF;
  107. while (delay--);
  108. }
  109. ee_dev.init = ee24LCxx_init;
  110. ee_dev.open = ee24LCxx_open;
  111. ee_dev.close = ee24LCxx_close;
  112. ee_dev.read = ee24LCxx_read;
  113. ee_dev.write = ee24LCxx_write;
  114. ee_dev.control = ee24LCxx_control;
  115. ee_dev.type = RT_Device_Class_Unknown;
  116. rt_device_register(&ee_dev, "eeprom", RT_DEVICE_FLAG_RDWR);
  117. }
  118. void dump_ee(void)
  119. {
  120. rt_device_t dev;
  121. char buf[EE_MEM_SIZE];
  122. int i, j;
  123. dev = rt_device_find("eeprom");
  124. rt_device_read(dev, 0, buf, EE_MEM_SIZE );
  125. for (i = 0; i < 16; i++)
  126. {
  127. for (j = 0; j < 16; j++)
  128. {
  129. rt_kprintf("0x%02X ", buf[ i*16+ j]);
  130. }
  131. rt_kprintf("\n");
  132. }
  133. }
  134. void ee_reset(void)
  135. {
  136. char buf[EE_MEM_SIZE], read[EE_MEM_SIZE];
  137. int i;
  138. rt_device_t dev = rt_device_find("eeprom");
  139. for (i = 0; i < EE_MEM_SIZE; i++)
  140. {
  141. buf[i] = 0xFF;
  142. read[i] = 0;
  143. }
  144. if (rt_device_write(dev, 0, buf, EE_MEM_SIZE ) == EE_MEM_SIZE)
  145. rt_kprintf("Write Success\n");
  146. rt_device_read(dev, 0, read, EE_MEM_SIZE );
  147. for (i = 0; i < EE_MEM_SIZE; i++)
  148. {
  149. if (buf[i] != read[i])
  150. rt_kprintf("EE Failed %X != %X at %d\n", buf[i], read[i], i);
  151. }
  152. }
  153. #ifdef RT_USING_FINSH
  154. #include <finsh.h>
  155. FINSH_FUNCTION_EXPORT(ee_reset, test system);
  156. FINSH_FUNCTION_EXPORT(dump_ee, test system);
  157. #endif