luat_i2c_rtt.c 4.1 KB

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