i2c_dev.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. */
  24. #include <rtdevice.h>
  25. static rt_err_t i2c_bus_device_init(rt_device_t dev)
  26. {
  27. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  28. RT_ASSERT(bus != RT_NULL);
  29. return RT_EOK;
  30. }
  31. static rt_size_t i2c_bus_device_read(rt_device_t dev,
  32. rt_off_t pos,
  33. void *buffer,
  34. rt_size_t count)
  35. {
  36. rt_uint16_t addr;
  37. rt_uint16_t flags;
  38. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  39. RT_ASSERT(bus != RT_NULL);
  40. RT_ASSERT(buffer != RT_NULL);
  41. i2c_dbg("I2C bus dev [%s] reading %u bytes.\n", dev->parent.name, count);
  42. addr = pos & 0xffff;
  43. flags = (pos >> 16) & 0xffff;
  44. return rt_i2c_master_recv(bus, addr, flags, buffer, count);
  45. }
  46. static rt_size_t i2c_bus_device_write(rt_device_t dev,
  47. rt_off_t pos,
  48. const void *buffer,
  49. rt_size_t count)
  50. {
  51. rt_uint16_t addr;
  52. rt_uint16_t flags;
  53. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  54. RT_ASSERT(bus != RT_NULL);
  55. RT_ASSERT(buffer != RT_NULL);
  56. i2c_dbg("I2C bus dev writing %u bytes.\n", dev->parent.name, count);
  57. addr = pos & 0xffff;
  58. flags = (pos >> 16) & 0xffff;
  59. return rt_i2c_master_send(bus, addr, flags, buffer, count);
  60. }
  61. static rt_err_t i2c_bus_device_control(rt_device_t dev,
  62. rt_uint8_t cmd,
  63. void *args)
  64. {
  65. rt_err_t ret;
  66. struct rt_i2c_priv_data *priv_data;
  67. struct rt_i2c_bus_device *bus = (struct rt_i2c_bus_device *)dev->user_data;
  68. RT_ASSERT(bus != RT_NULL);
  69. switch (cmd)
  70. {
  71. /* set 10-bit addr mode */
  72. case RT_I2C_DEV_CTRL_10BIT:
  73. bus->flags |= RT_I2C_ADDR_10BIT;
  74. break;
  75. case RT_I2C_DEV_CTRL_ADDR:
  76. bus->addr = *(rt_uint16_t *)args;
  77. break;
  78. case RT_I2C_DEV_CTRL_TIMEOUT:
  79. bus->timeout = *(rt_uint32_t *)args;
  80. break;
  81. case RT_I2C_DEV_CTRL_RW:
  82. priv_data = (struct rt_i2c_priv_data *)args;
  83. ret = rt_i2c_transfer(bus, priv_data->msgs, priv_data->number);
  84. if (ret < 0)
  85. {
  86. return -RT_EIO;
  87. }
  88. break;
  89. default:
  90. break;
  91. }
  92. return RT_EOK;
  93. }
  94. rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
  95. const char *name)
  96. {
  97. struct rt_device *device;
  98. RT_ASSERT(bus != RT_NULL);
  99. device = &bus->parent;
  100. device->user_data = bus;
  101. /* set device type */
  102. device->type = RT_Device_Class_I2CBUS;
  103. /* initialize device interface */
  104. device->init = i2c_bus_device_init;
  105. device->open = RT_NULL;
  106. device->close = RT_NULL;
  107. device->read = i2c_bus_device_read;
  108. device->write = i2c_bus_device_write;
  109. device->control = i2c_bus_device_control;
  110. /* register to device manager */
  111. rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  112. return RT_EOK;
  113. }