luat_i2c_rtt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "luat_base.h"
  2. #include "luat_i2c.h"
  3. #include "luat_log.h"
  4. #include "rtthread.h"
  5. #include "rthw.h"
  6. #include "rtdevice.h"
  7. #define DBG_TAG "luat.i2c"
  8. #define DBG_LVL DBG_WARN
  9. #include <rtdbg.h>
  10. #ifdef RT_USING_I2C
  11. #define I2C_DEVICE_ID_MAX 3
  12. static struct rt_i2c_bus_device* i2c_devs[I2C_DEVICE_ID_MAX + 1];
  13. static int luat_i2c_rtt_init() {
  14. char name[9];
  15. name[0] = 'i';
  16. name[1] = '2';
  17. name[2] = 'c';
  18. name[4] = 0x00;
  19. // 搜索i2c0,i2c1,i2c2 ....
  20. for (size_t i = 0; i <= I2C_DEVICE_ID_MAX; i++)
  21. {
  22. name[3] = '0' + i;
  23. i2c_devs[i] = (struct rt_i2c_bus_device *)rt_device_find(name);
  24. LOG_I("search i2c name=%s ptr=0x%08X", name, i2c_devs[i]);
  25. }
  26. // 搜索i2c0soft,i2c1soft,i2c2soft ....
  27. name[4] = 's';
  28. name[5] = 'o';
  29. name[6] = 'f';
  30. name[7] = 't';
  31. name[8] = 0x00;
  32. for (size_t i = 0; i < I2C_DEVICE_ID_MAX; i++)
  33. {
  34. if (i2c_devs[i] != RT_NULL) continue;
  35. name[3] = '0' + i;
  36. i2c_devs[i] = (struct rt_i2c_bus_device *)rt_device_find(name);
  37. LOG_I("search i2c name=%s ptr=0x%08X", name, i2c_devs[i]);
  38. }
  39. // 看看有没有i2c
  40. if (i2c_devs[0] == RT_NULL) {
  41. i2c_devs[0] = (struct rt_i2c_bus_device *)rt_device_find("i2c");
  42. LOG_I("search i2c name=%s ptr=0x%08X", "i2c", i2c_devs[0]);
  43. }
  44. return 0;
  45. }
  46. INIT_COMPONENT_EXPORT(luat_i2c_rtt_init);
  47. int luat_i2c_exist(int id) {
  48. if (id < 0 || id > I2C_DEVICE_ID_MAX) {
  49. LOG_W("no such i2c device id=%ld", id);
  50. return 0;
  51. }
  52. LOG_I("i2c id=%d ptr=0x%08X", id, i2c_devs[id]);
  53. return i2c_devs[id] == RT_NULL ? 0 : 1;
  54. }
  55. static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_uint8_t reg, rt_uint8_t *data,rt_uint8_t len)
  56. {
  57. rt_uint8_t buf[3];
  58. buf[0] = reg; //cmd
  59. buf[1] = data[0];
  60. buf[2] = data[1];
  61. if (rt_i2c_master_send(bus, addr, 0, buf, 3) == 3)
  62. return RT_EOK;
  63. else
  64. return -RT_ERROR;
  65. }
  66. static rt_err_t read_regs(struct rt_i2c_bus_device *bus, rt_uint16_t addr, rt_uint8_t *buf, rt_uint8_t len)
  67. {
  68. struct rt_i2c_msg msgs;
  69. msgs.addr = addr;
  70. msgs.flags = RT_I2C_RD;
  71. msgs.buf = buf;
  72. msgs.len = len;
  73. if (rt_i2c_transfer(bus, &msgs, 1) == 1)
  74. {
  75. return RT_EOK;
  76. }
  77. else
  78. {
  79. return -RT_ERROR;
  80. }
  81. }
  82. int luat_i2c_setup(int id, int speed, int slaveaddr) {
  83. if (!luat_i2c_exist(id)) return 1;
  84. // 无事可做
  85. rt_device_open(&i2c_devs[id]->parent, 0);
  86. return 0;
  87. }
  88. int luat_i2c_close(int id) {
  89. if (!luat_i2c_exist(id)) return 1;
  90. // 无事可做
  91. rt_device_close(&i2c_devs[id]->parent);
  92. return 0;
  93. }
  94. int luat_i2c_transfer(int id, int addr, int flags, void* buff, size_t len) {
  95. if (!luat_i2c_exist(id)) return -1;
  96. struct rt_i2c_msg msgs;
  97. msgs.addr = addr;
  98. msgs.flags = flags;
  99. msgs.buf = buff;
  100. msgs.len = len;
  101. LOG_I("i2c_transfer len=%d flags=%d", msgs.len, flags);
  102. if (rt_i2c_transfer(i2c_devs[id], &msgs, 1) == 1)
  103. {
  104. return RT_EOK;
  105. }
  106. else
  107. {
  108. return -RT_ERROR;
  109. }
  110. }
  111. int luat_i2c_send(int id, int addr, void* buff, size_t len) {
  112. return luat_i2c_transfer(id, addr, RT_I2C_WR, buff, len);
  113. }
  114. int luat_i2c_recv(int id, int addr, void* buff, size_t len) {
  115. return luat_i2c_transfer(id, addr, RT_I2C_RD, buff, len);
  116. }
  117. int luat_i2c_write_reg(int id, int addr, int reg, uint16_t value) {
  118. if (!luat_i2c_exist(id)) return 1;
  119. rt_uint8_t buf[3];
  120. buf[0] = reg; //cmd
  121. buf[1] = (value >> 8) & 0xFF;
  122. buf[2] = value & 0xFF;
  123. if (rt_i2c_master_send(i2c_devs[id], addr, 0, buf, 3) == 3)
  124. return RT_EOK;
  125. else
  126. return -RT_ERROR;
  127. }
  128. int luat_i2c_read_reg(int id, int addr, int reg, uint16_t* value) {
  129. if (!luat_i2c_exist(id)) return 1;
  130. // struct rt_i2c_msg msgs;
  131. // uint8_t a;
  132. // a = reg;
  133. // msgs.addr = addr;
  134. // msgs.flags = RT_I2C_RD;
  135. // msgs.buf = &a;
  136. // msgs.len = 1;
  137. rt_i2c_master_send(i2c_devs[id], addr, 0, (const rt_uint8_t*)&reg, 1);
  138. char buff[2] = {0};
  139. rt_i2c_master_recv(i2c_devs[id], addr, 0, buff, 2);
  140. *value = buff[0] << 8 + buff[1];
  141. return 0;
  142. }
  143. #endif