fm24clxx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * File : fm24clxx.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-07-14 aubr.cool 1st version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "fm24clxx.h"
  27. struct fm24clxx_device
  28. {
  29. struct rt_device parent;
  30. struct rt_i2c_bus_device *bus;
  31. };
  32. /* RT-Thread device interface */
  33. static rt_err_t fm24clxx_init(rt_device_t dev)
  34. {
  35. return RT_EOK;
  36. }
  37. static rt_err_t fm24clxx_open(rt_device_t dev, rt_uint16_t oflag)
  38. {
  39. return RT_EOK;
  40. }
  41. static rt_err_t fm24clxx_close(rt_device_t dev)
  42. {
  43. return RT_EOK;
  44. }
  45. static rt_err_t fm24clxx_control(rt_device_t dev, int cmd, void *args)
  46. {
  47. return RT_EOK;
  48. }
  49. static rt_size_t fm24clxx_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  50. {
  51. struct fm24clxx_device *fm24clxx;
  52. const struct fm24clxx_config *cfg;
  53. struct rt_i2c_msg msg[2];
  54. rt_uint8_t mem_addr[2] = {0,};
  55. rt_size_t ret = 0;
  56. RT_ASSERT(dev != 0);
  57. fm24clxx = (struct fm24clxx_device *) dev;
  58. RT_ASSERT(fm24clxx->parent.user_data != 0);
  59. cfg = (const struct fm24clxx_config *) fm24clxx->parent.user_data;
  60. if(pos > cfg->size)
  61. {
  62. return 0;
  63. }
  64. if(pos + size > cfg->size)
  65. {
  66. size = cfg->size - pos;
  67. }
  68. msg[0].addr = cfg->addr;
  69. msg[0].flags = cfg->flags | RT_I2C_WR;
  70. mem_addr[0] = (pos >> 8);
  71. mem_addr[1] = (rt_uint8_t) pos;
  72. msg[0].buf = (rt_uint8_t *) mem_addr;
  73. msg[0].len = 2;
  74. msg[1].addr = cfg->addr;
  75. msg[1].flags = cfg->flags | RT_I2C_RD;
  76. msg[1].buf = (rt_uint8_t *) buffer;
  77. msg[1].len = size;
  78. ret = rt_i2c_transfer(fm24clxx->bus, msg, 2);
  79. return (ret == 2) ? size : 0;
  80. }
  81. static rt_size_t fm24clxx_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  82. {
  83. struct fm24clxx_device *fm24clxx;
  84. const struct fm24clxx_config *cfg;
  85. struct rt_i2c_msg msg[2];
  86. rt_uint8_t mem_addr[2] = {0,};
  87. rt_size_t ret = 0;
  88. RT_ASSERT(dev != 0);
  89. fm24clxx = (struct fm24clxx_device *) dev;
  90. RT_ASSERT(fm24clxx->parent.user_data != 0);
  91. cfg = (const struct fm24clxx_config *) fm24clxx->parent.user_data;
  92. if(pos > cfg->size)
  93. {
  94. return 0;
  95. }
  96. if(pos + size > cfg->size)
  97. {
  98. size = cfg->size - pos;
  99. }
  100. msg[0].addr = cfg->addr;
  101. msg[0].flags = cfg->flags | RT_I2C_WR;
  102. mem_addr[0] = (pos >> 8);
  103. mem_addr[1] = (rt_uint8_t) pos;
  104. msg[0].buf = (rt_uint8_t *) mem_addr;
  105. msg[0].len = 2;
  106. msg[1].addr = cfg->addr;
  107. msg[1].flags = cfg->flags | RT_I2C_WR | RT_I2C_NO_START;
  108. msg[1].buf = (rt_uint8_t *) buffer;
  109. msg[1].len = size;
  110. ret = rt_i2c_transfer(fm24clxx->bus, msg, 2);
  111. return (ret == 2) ? size : 0;
  112. }
  113. rt_err_t fm24clxx_register(const char *fm_device_name, const char *i2c_bus, void *user_data)
  114. {
  115. static struct fm24clxx_device fm24clxx_drv;
  116. struct rt_i2c_bus_device *bus;
  117. bus = rt_i2c_bus_device_find(i2c_bus);
  118. if (bus == RT_NULL)
  119. {
  120. return RT_ENOSYS;
  121. }
  122. fm24clxx_drv.bus = bus;
  123. fm24clxx_drv.parent.type = RT_Device_Class_Block;
  124. fm24clxx_drv.parent.init = fm24clxx_init;
  125. fm24clxx_drv.parent.open = fm24clxx_open;
  126. fm24clxx_drv.parent.close = fm24clxx_close;
  127. fm24clxx_drv.parent.read = fm24clxx_read;
  128. fm24clxx_drv.parent.write = fm24clxx_write;
  129. fm24clxx_drv.parent.control = fm24clxx_control;
  130. fm24clxx_drv.parent.user_data = user_data;
  131. return rt_device_register(&fm24clxx_drv.parent, fm_device_name, RT_DEVICE_FLAG_RDWR);
  132. }