24LCxx.c 3.8 KB

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