i2c_dev.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * File : i2c_dev.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. * 2012-04-25 weety first version
  23. * 2014-08-03 bernard fix some compiling warning
  24. */
  25. #include <rtdevice.h>
  26. static rt_size_t i2c_bus_device_read(rt_device_t dev,
  27. rt_off_t pos,
  28. void *buffer,
  29. rt_size_t count)
  30. {
  31. rt_uint16_t addr;
  32. rt_uint16_t flags;
  33. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  34. RT_ASSERT(bus != RT_NULL);
  35. RT_ASSERT(buffer != RT_NULL);
  36. i2c_dbg("I2C bus dev [%s] reading %u bytes.\n", dev->parent.name, count);
  37. addr = pos & 0xffff;
  38. flags = (pos >> 16) & 0xffff;
  39. return rt_i2c_master_recv(bus, addr, flags, buffer, count);
  40. }
  41. static rt_size_t i2c_bus_device_write(rt_device_t dev,
  42. rt_off_t pos,
  43. const void *buffer,
  44. rt_size_t count)
  45. {
  46. rt_uint16_t addr;
  47. rt_uint16_t flags;
  48. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  49. RT_ASSERT(bus != RT_NULL);
  50. RT_ASSERT(buffer != RT_NULL);
  51. i2c_dbg("I2C bus dev [%s] writing %u bytes.\n", dev->parent.name, count);
  52. addr = pos & 0xffff;
  53. flags = (pos >> 16) & 0xffff;
  54. return rt_i2c_master_send(bus, addr, flags, buffer, count);
  55. }
  56. static rt_err_t i2c_bus_device_control(rt_device_t dev,
  57. int cmd,
  58. void *args)
  59. {
  60. rt_err_t ret;
  61. struct rt_i2c_priv_data *priv_data;
  62. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  63. RT_ASSERT(bus != RT_NULL);
  64. switch (cmd)
  65. {
  66. /* set 10-bit addr mode */
  67. case RT_I2C_DEV_CTRL_10BIT:
  68. bus->flags |= RT_I2C_ADDR_10BIT;
  69. break;
  70. case RT_I2C_DEV_CTRL_ADDR:
  71. bus->addr = *(rt_uint16_t *)args;
  72. break;
  73. case RT_I2C_DEV_CTRL_TIMEOUT:
  74. bus->timeout = *(rt_uint32_t *)args;
  75. break;
  76. case RT_I2C_DEV_CTRL_RW:
  77. priv_data = (struct rt_i2c_priv_data *)args;
  78. ret = rt_i2c_transfer(bus, priv_data->msgs, priv_data->number);
  79. if (ret < 0)
  80. {
  81. return -RT_EIO;
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. return RT_EOK;
  88. }
  89. #ifdef RT_USING_DEVICE_OPS
  90. const static struct rt_device_ops i2c_ops =
  91. {
  92. RT_NULL,
  93. RT_NULL,
  94. RT_NULL,
  95. i2c_bus_device_read,
  96. i2c_bus_device_write,
  97. i2c_bus_device_control
  98. };
  99. #endif
  100. rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
  101. const char *name)
  102. {
  103. struct rt_device *device;
  104. RT_ASSERT(bus != RT_NULL);
  105. device = &bus->parent;
  106. device->user_data = bus;
  107. /* set device type */
  108. device->type = RT_Device_Class_I2CBUS;
  109. /* initialize device interface */
  110. #ifdef RT_USING_DEVICE_OPS
  111. device->ops = &i2c_ops;
  112. #else
  113. device->init = RT_NULL;
  114. device->open = RT_NULL;
  115. device->close = RT_NULL;
  116. device->read = i2c_bus_device_read;
  117. device->write = i2c_bus_device_write;
  118. device->control = i2c_bus_device_control;
  119. #endif
  120. /* register to device manager */
  121. rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  122. return RT_EOK;
  123. }