i2c_core.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * File : i2c_core.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. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  26. const char *bus_name)
  27. {
  28. rt_err_t res = RT_EOK;
  29. rt_mutex_init(&bus->lock, "i2c_bus_lock", RT_IPC_FLAG_FIFO);
  30. if (bus->timeout == 0) bus->timeout = RT_TICK_PER_SECOND;
  31. res = rt_i2c_bus_device_device_init(bus, bus_name);
  32. i2c_dbg("I2C bus [%s] registered\n", bus_name);
  33. return res;
  34. }
  35. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name)
  36. {
  37. struct rt_i2c_bus_device *bus;
  38. rt_device_t dev = rt_device_find(bus_name);
  39. if (dev == RT_NULL || dev->type != RT_Device_Class_I2CBUS)
  40. {
  41. i2c_dbg("I2C bus %s not exist\n", bus_name);
  42. return RT_NULL;
  43. }
  44. bus = (struct rt_i2c_bus_device *)dev->user_data;
  45. return bus;
  46. }
  47. rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  48. struct rt_i2c_msg msgs[],
  49. rt_uint32_t num)
  50. {
  51. rt_size_t ret;
  52. if (bus->ops->master_xfer)
  53. {
  54. #ifdef RT_I2C_DEBUG
  55. for (ret = 0; ret < num; ret++)
  56. {
  57. i2c_dbg("msgs[%d] %c, addr=0x%02x, len=%d%s\n", ret,
  58. (msgs[ret].flags & RT_I2C_RD) ? 'R' : 'W',
  59. msgs[ret].addr, msgs[ret].len);
  60. }
  61. #endif
  62. rt_mutex_take(&bus->lock, RT_WAITING_FOREVER);
  63. ret = bus->ops->master_xfer(bus, msgs, num);
  64. rt_mutex_release(&bus->lock);
  65. return ret;
  66. }
  67. else
  68. {
  69. i2c_dbg("I2C bus operation not supported\n");
  70. return 0;
  71. }
  72. }
  73. rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  74. rt_uint16_t addr,
  75. rt_uint16_t flags,
  76. const rt_uint8_t *buf,
  77. rt_uint32_t count)
  78. {
  79. rt_size_t ret;
  80. struct rt_i2c_msg msg;
  81. msg.addr = addr;
  82. msg.flags = flags & RT_I2C_ADDR_10BIT;
  83. msg.len = count;
  84. msg.buf = (rt_uint8_t *)buf;
  85. ret = rt_i2c_transfer(bus, &msg, 1);
  86. return (ret > 0) ? count : ret;
  87. }
  88. rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  89. rt_uint16_t addr,
  90. rt_uint16_t flags,
  91. rt_uint8_t *buf,
  92. rt_uint32_t count)
  93. {
  94. rt_size_t ret;
  95. struct rt_i2c_msg msg;
  96. RT_ASSERT(bus != RT_NULL);
  97. msg.addr = addr;
  98. msg.flags = flags & RT_I2C_ADDR_10BIT;
  99. msg.flags |= RT_I2C_RD;
  100. msg.len = count;
  101. msg.buf = buf;
  102. ret = rt_i2c_transfer(bus, &msg, 1);
  103. return (ret > 0) ? count : ret;
  104. }
  105. int rt_i2c_core_init(void)
  106. {
  107. return 0;
  108. }
  109. INIT_COMPONENT_EXPORT(rt_i2c_core_init);