sccb.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * This file is part of the OpenMV project.
  3. * Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
  4. * This work is licensed under the MIT license, see the file LICENSE for details.
  5. *
  6. * SCCB (I2C like) driver.
  7. *
  8. */
  9. #include <stdbool.h>
  10. #include <string.h>
  11. #include <freertos/FreeRTOS.h>
  12. #include <freertos/task.h>
  13. #include "sccb.h"
  14. #include "sensor.h"
  15. #include <stdio.h>
  16. #include "sdkconfig.h"
  17. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  18. #include "esp32-hal-log.h"
  19. #else
  20. #include "esp_log.h"
  21. static const char* TAG = "sccb";
  22. #endif
  23. #define LITTLETOBIG(x) ((x<<8)|(x>>8))
  24. #include "driver/i2c.h"
  25. // support IDF 5.x
  26. #ifndef portTICK_RATE_MS
  27. #define portTICK_RATE_MS portTICK_PERIOD_MS
  28. #endif
  29. #define SCCB_FREQ CONFIG_SCCB_CLK_FREQ /*!< I2C master frequency*/
  30. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  31. #define READ_BIT I2C_MASTER_READ /*!< I2C master read */
  32. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  33. #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
  34. #define ACK_VAL 0x0 /*!< I2C ack value */
  35. #define NACK_VAL 0x1 /*!< I2C nack value */
  36. #if CONFIG_SCCB_HARDWARE_I2C_PORT1
  37. const int SCCB_I2C_PORT = 1;
  38. #else
  39. const int SCCB_I2C_PORT = 0;
  40. #endif
  41. int SCCB_Init(int pin_sda, int pin_scl)
  42. {
  43. ESP_LOGI(TAG, "pin_sda %d pin_scl %d", pin_sda, pin_scl);
  44. i2c_config_t conf;
  45. memset(&conf, 0, sizeof(i2c_config_t));
  46. conf.mode = I2C_MODE_MASTER;
  47. conf.sda_io_num = pin_sda;
  48. conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
  49. conf.scl_io_num = pin_scl;
  50. conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
  51. conf.master.clk_speed = SCCB_FREQ;
  52. i2c_param_config(SCCB_I2C_PORT, &conf);
  53. i2c_driver_install(SCCB_I2C_PORT, conf.mode, 0, 0, 0);
  54. return 0;
  55. }
  56. int SCCB_Deinit(void)
  57. {
  58. return i2c_driver_delete(SCCB_I2C_PORT);
  59. }
  60. uint8_t SCCB_Probe(void)
  61. {
  62. uint8_t slave_addr = 0x0;
  63. // for (size_t i = 0; i < 10; i++) {
  64. // i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  65. // i2c_master_start(cmd);
  66. // i2c_master_write_byte(cmd, ( 0x30 << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  67. // i2c_master_stop(cmd);
  68. // esp_err_t ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  69. // i2c_cmd_link_delete(cmd);
  70. // if( ret == ESP_OK) {
  71. // ESP_LOGW(TAG, "Found I2C Device at 0x%02X", i);
  72. // }
  73. // }
  74. for (size_t i = 0; i < CAMERA_MODEL_MAX; i++) {
  75. if (slave_addr == camera_sensor[i].sccb_addr) {
  76. continue;
  77. }
  78. slave_addr = camera_sensor[i].sccb_addr;
  79. ESP_LOGD(TAG, "i2c addr = 0x%X", slave_addr);
  80. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  81. i2c_master_start(cmd);
  82. i2c_master_write_byte(cmd, ( slave_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  83. i2c_master_stop(cmd);
  84. esp_err_t ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  85. i2c_cmd_link_delete(cmd);
  86. if( ret == ESP_OK) {
  87. return slave_addr;
  88. }
  89. vTaskDelay(10 / portTICK_PERIOD_MS);
  90. }
  91. return 0;
  92. }
  93. uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg)
  94. {
  95. uint8_t data=0;
  96. esp_err_t ret = ESP_FAIL;
  97. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  98. i2c_master_start(cmd);
  99. i2c_master_write_byte(cmd, ( slv_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  100. i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
  101. i2c_master_stop(cmd);
  102. ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  103. i2c_cmd_link_delete(cmd);
  104. if(ret != ESP_OK) return -1;
  105. cmd = i2c_cmd_link_create();
  106. i2c_master_start(cmd);
  107. i2c_master_write_byte(cmd, ( slv_addr << 1 ) | READ_BIT, ACK_CHECK_EN);
  108. i2c_master_read_byte(cmd, &data, NACK_VAL);
  109. i2c_master_stop(cmd);
  110. ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  111. i2c_cmd_link_delete(cmd);
  112. if(ret != ESP_OK) {
  113. ESP_LOGE(TAG, "SCCB_Read Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret);
  114. }
  115. return data;
  116. }
  117. uint8_t SCCB_Write(uint8_t slv_addr, uint8_t reg, uint8_t data)
  118. {
  119. esp_err_t ret = ESP_FAIL;
  120. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  121. i2c_master_start(cmd);
  122. i2c_master_write_byte(cmd, ( slv_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  123. i2c_master_write_byte(cmd, reg, ACK_CHECK_EN);
  124. i2c_master_write_byte(cmd, data, ACK_CHECK_EN);
  125. i2c_master_stop(cmd);
  126. ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  127. i2c_cmd_link_delete(cmd);
  128. if(ret != ESP_OK) {
  129. ESP_LOGE(TAG, "SCCB_Write Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret);
  130. }
  131. return ret == ESP_OK ? 0 : -1;
  132. }
  133. uint8_t SCCB_Read16(uint8_t slv_addr, uint16_t reg)
  134. {
  135. uint8_t data=0;
  136. esp_err_t ret = ESP_FAIL;
  137. uint16_t reg_htons = LITTLETOBIG(reg);
  138. uint8_t *reg_u8 = (uint8_t *)&reg_htons;
  139. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  140. i2c_master_start(cmd);
  141. i2c_master_write_byte(cmd, ( slv_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  142. i2c_master_write_byte(cmd, reg_u8[0], ACK_CHECK_EN);
  143. i2c_master_write_byte(cmd, reg_u8[1], ACK_CHECK_EN);
  144. i2c_master_stop(cmd);
  145. ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  146. i2c_cmd_link_delete(cmd);
  147. if(ret != ESP_OK) return -1;
  148. cmd = i2c_cmd_link_create();
  149. i2c_master_start(cmd);
  150. i2c_master_write_byte(cmd, ( slv_addr << 1 ) | READ_BIT, ACK_CHECK_EN);
  151. i2c_master_read_byte(cmd, &data, NACK_VAL);
  152. i2c_master_stop(cmd);
  153. ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  154. i2c_cmd_link_delete(cmd);
  155. if(ret != ESP_OK) {
  156. ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, data);
  157. }
  158. return data;
  159. }
  160. uint8_t SCCB_Write16(uint8_t slv_addr, uint16_t reg, uint8_t data)
  161. {
  162. static uint16_t i = 0;
  163. esp_err_t ret = ESP_FAIL;
  164. uint16_t reg_htons = LITTLETOBIG(reg);
  165. uint8_t *reg_u8 = (uint8_t *)&reg_htons;
  166. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  167. i2c_master_start(cmd);
  168. i2c_master_write_byte(cmd, ( slv_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN);
  169. i2c_master_write_byte(cmd, reg_u8[0], ACK_CHECK_EN);
  170. i2c_master_write_byte(cmd, reg_u8[1], ACK_CHECK_EN);
  171. i2c_master_write_byte(cmd, data, ACK_CHECK_EN);
  172. i2c_master_stop(cmd);
  173. ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
  174. i2c_cmd_link_delete(cmd);
  175. if(ret != ESP_OK) {
  176. ESP_LOGE(TAG, "W [%04x]=%02x %d fail\n", reg, data, i++);
  177. }
  178. return ret == ESP_OK ? 0 : -1;
  179. }