luat_i2c_idf5.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "luat_base.h"
  2. #include "luat_i2c.h"
  3. #include "driver/i2c.h"
  4. #include "idf5_io_def.h"
  5. #define LUAT_LOG_TAG "i2c"
  6. #include "luat_log.h"
  7. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  8. #define I2C_CHECK(i2cid) ((i2cid<0 || i2cid>=SOC_I2C_NUM) ? -1:0)
  9. static uint8_t i2c_init = 0;
  10. int luat_i2c_exist(int id){
  11. return (I2C_CHECK(id)==0);
  12. }
  13. int luat_i2c_setup(int id, int speed){
  14. if (I2C_CHECK(id)){
  15. return -1;
  16. }
  17. if (i2c_init & (1 << id)){
  18. return 0;
  19. }
  20. i2c_config_t conf = {0};
  21. conf.mode = I2C_MODE_MASTER;
  22. if (id == 0){
  23. conf.sda_io_num = I2C0_SDA_IO_NUM;
  24. conf.scl_io_num = I2C0_SCL_IO_NUM;
  25. }
  26. #if SOC_I2C_NUM >= 2
  27. else if(id == 1){
  28. conf.sda_io_num = I2C1_SDA_IO_NUM;
  29. conf.scl_io_num = I2C1_SCL_IO_NUM;
  30. }
  31. #endif
  32. else{
  33. return -1;
  34. }
  35. conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
  36. conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
  37. if (speed == 0)
  38. conf.master.clk_speed = 100 * 1000;
  39. else if (speed == 1)
  40. conf.master.clk_speed = 400 * 1000;
  41. i2c_param_config(id, &conf);
  42. i2c_driver_install(id, conf.mode, 0, 0, 0);
  43. i2c_init |= 1 << id;
  44. return 0;
  45. }
  46. int luat_i2c_close(int id){
  47. if (I2C_CHECK(id)){
  48. return -1;
  49. }
  50. if (i2c_init & (1 << id)){
  51. i2c_driver_delete(id);
  52. i2c_init ^= (1 << id);
  53. }
  54. return 0;
  55. }
  56. int luat_i2c_send(int id, int addr, void *buff, size_t len, uint8_t stop){
  57. if (I2C_CHECK(id)){
  58. return -1;
  59. }
  60. esp_err_t ret;
  61. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  62. ret = i2c_master_start(cmd);
  63. if (ret) goto error;
  64. ret = i2c_master_write_byte(cmd, addr << 1 | I2C_MASTER_WRITE, ACK_CHECK_EN);
  65. if (ret) goto error;
  66. ret = i2c_master_write(cmd, (const uint8_t *)buff, len, ACK_CHECK_EN);
  67. if (ret) goto error;
  68. if (stop){
  69. ret = i2c_master_stop(cmd);
  70. if (ret) goto error;
  71. }
  72. ret = i2c_master_cmd_begin(id, cmd, 1000 / portTICK_PERIOD_MS);
  73. if (ret) goto error;
  74. i2c_cmd_link_delete(cmd);
  75. return 0;
  76. error:
  77. i2c_cmd_link_delete(cmd);
  78. return -1;
  79. }
  80. int luat_i2c_recv(int id, int addr, void *buff, size_t len){
  81. if (I2C_CHECK(id)){
  82. return -1;
  83. }
  84. esp_err_t ret;
  85. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  86. ret = i2c_master_start(cmd);
  87. if (ret) goto error;
  88. ret = i2c_master_write_byte(cmd, addr << 1 | I2C_MASTER_READ, ACK_CHECK_EN);
  89. if (ret) goto error;
  90. ret = i2c_master_read(cmd, (uint8_t *)buff, len, I2C_MASTER_LAST_NACK);
  91. if (ret) goto error;
  92. ret = i2c_master_stop(cmd);
  93. if (ret) goto error;
  94. ret = i2c_master_cmd_begin(id, cmd, 1000 / portTICK_PERIOD_MS);
  95. if (ret) goto error;
  96. i2c_cmd_link_delete(cmd);
  97. return 0;
  98. error:
  99. i2c_cmd_link_delete(cmd);
  100. return -1;
  101. }