24LCxx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 256 byte, you must define EE_ADDR_SIZE == I2C_MEM_2Bytes
  20. */
  21. #ifdef EE24LC024H
  22. #define EE_PageSize 8
  23. #define EE_ADDR_SIZE I2C_MEM_1Byte
  24. #define EE_MEM_SIZE 256
  25. #define EE_PageSize 16
  26. #endif
  27. static struct rt_device ee_dev;
  28. uint32_t EE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
  29. {
  30. return I2C_IORW(I2C1, pBuffer, NumByteToRead, ReadAddr, EE_Address | 0x01, I2C_MEM_1Byte );
  31. }
  32. uint32_t EE_WritePage(uint8_t* pBuffer, uint16_t WriteAddr)
  33. {
  34. I2C_IORW(I2C1, pBuffer, EE_PageSize , WriteAddr, EE_Address , EE_ADDR_SIZE );
  35. /*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error )
  36. rt_kprintf("EE ACK failed\n");*/
  37. rt_thread_delay(50);
  38. }
  39. uint32_t EE_WriteByte(uint8_t* pBuffer, uint16_t WriteAddr)
  40. {
  41. I2C_IORW(I2C1, 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. }
  46. Status EE_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
  47. {
  48. uint8_t NumOfPage = 0, NumOfSingle = 0;
  49. uint16_t Addr = 0,count = 0;
  50. uint8_t* ptr = pBuffer;
  51. Addr = WriteAddr;
  52. count = NumByteToWrite;
  53. if( (WriteAddr + NumByteToWrite ) > EE_MEM_SIZE )
  54. return Error;
  55. while( count >= EE_PageSize )
  56. {
  57. EE_WritePage(ptr, Addr);
  58. Addr += EE_PageSize;
  59. count -= EE_PageSize;
  60. ptr += EE_PageSize;
  61. }
  62. while( count )
  63. {
  64. EE_WriteByte( ptr++, Addr++ );
  65. count--;
  66. }
  67. return Success;
  68. }
  69. static rt_err_t ee24LCxx_init (rt_device_t dev)
  70. {
  71. return RT_EOK;
  72. }
  73. static rt_size_t ee24LCxx_read( rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size )
  74. {
  75. if( EE_ReadBuffer(buf, pos, size) == Success )
  76. return size;
  77. else
  78. return -1;
  79. }
  80. static rt_size_t ee24LCxx_write( rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size )
  81. {
  82. if( EE_WriteBuffer(buf, pos, size) == Success )
  83. return size;
  84. else
  85. return -1;
  86. }
  87. static rt_err_t ee24LCxx_open(rt_device_t dev, rt_uint16_t oflag)
  88. {
  89. return RT_EOK;
  90. }
  91. static rt_err_t ee24LCxx_close(rt_device_t dev)
  92. {
  93. return RT_EOK;
  94. }
  95. static rt_err_t ee24LCxx_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  96. {
  97. return RT_EOK;
  98. }
  99. void ee24LCxx_hw_init()
  100. {
  101. I2C1_INIT();
  102. ee_dev.init = ee24LCxx_init;
  103. ee_dev.open = ee24LCxx_open;
  104. ee_dev.close = ee24LCxx_close;
  105. ee_dev.read = ee24LCxx_read;
  106. ee_dev.write = ee24LCxx_write;
  107. ee_dev.control = ee24LCxx_control;
  108. ee_dev.type = RT_Device_Class_I2C;
  109. rt_device_register(&ee_dev, "eeprom", RT_DEVICE_FLAG_RDWR);
  110. }
  111. void ee_test()
  112. {
  113. char buf[256], read[256];
  114. int i,ret;
  115. rt_device_t dev;
  116. dev = rt_device_find("eeprom");
  117. for(i = 0; i < 256; i++ )
  118. {
  119. buf[i] = i;
  120. read[i] = 0;
  121. }
  122. if( rt_device_write(dev, 0, buf, 256 ) == 256 )
  123. rt_kprintf("Write Success\n");
  124. rt_device_read(dev, 0, read, 256 );
  125. for(i = 0; i < 256; i++ )
  126. {
  127. if( buf[i] != read[i] )
  128. rt_kprintf("EE Failed %X != %X at %d\n", buf[i], read[i], i);
  129. }
  130. rt_kprintf("Finsh\n");
  131. }
  132. #ifdef RT_USING_FINSH
  133. #include <finsh.h>
  134. FINSH_FUNCTION_EXPORT(ee_test, test system);
  135. #endif