sccb-ng.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 with the new esp-idf I2C API.
  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-ng";
  22. #endif
  23. #define LITTLETOBIG(x) ((x << 8) | (x >> 8))
  24. #include "esp_private/i2c_platform.h"
  25. #include "driver/i2c_master.h"
  26. #include "driver/i2c_types.h"
  27. // support IDF 5.x
  28. #ifndef portTICK_RATE_MS
  29. #define portTICK_RATE_MS portTICK_PERIOD_MS
  30. #endif
  31. #define TIMEOUT_MS 1000 /*!< I2C timeout duration */
  32. #define SCCB_FREQ CONFIG_SCCB_CLK_FREQ /*!< I2C master frequency */
  33. #if CONFIG_SCCB_HARDWARE_I2C_PORT1
  34. const int SCCB_I2C_PORT_DEFAULT = 1;
  35. #else
  36. const int SCCB_I2C_PORT_DEFAULT = 0;
  37. #endif
  38. #define MAX_DEVICES UINT8_MAX-1
  39. /*
  40. The legacy I2C driver used addresses to differentiate between devices, whereas the new driver uses
  41. i2c_master_dev_handle_t structs which are registed to the bus.
  42. To avoid re-writing all camera dependant code, we simply translate the devices address to the corresponding
  43. device_handle. This keeps all interfaces to the drivers identical.
  44. To perform this conversion the following local struct is used.
  45. */
  46. typedef struct
  47. {
  48. i2c_master_dev_handle_t dev_handle;
  49. uint16_t address;
  50. } device_t;
  51. static device_t devices[MAX_DEVICES];
  52. static uint8_t device_count = 0;
  53. static int sccb_i2c_port;
  54. static bool sccb_owns_i2c_port;
  55. i2c_master_dev_handle_t *get_handle_from_address(uint8_t slv_addr)
  56. {
  57. for (uint8_t i = 0; i < device_count; i++)
  58. {
  59. if (slv_addr == devices[i].address)
  60. {
  61. return &(devices[i].dev_handle);
  62. }
  63. }
  64. ESP_LOGE(TAG, "Device with address %02x not found", slv_addr);
  65. return NULL;
  66. }
  67. int SCCB_Install_Device(uint8_t slv_addr)
  68. {
  69. esp_err_t ret;
  70. i2c_master_bus_handle_t bus_handle;
  71. if (device_count > MAX_DEVICES)
  72. {
  73. ESP_LOGE(TAG, "cannot add more than %d devices", MAX_DEVICES);
  74. return ESP_FAIL;
  75. }
  76. ret = i2c_master_get_bus_handle(sccb_i2c_port, &bus_handle);
  77. if (ret != ESP_OK)
  78. {
  79. ESP_LOGE(TAG, "failed to get SCCB I2C Bus handle for port %d", sccb_i2c_port);
  80. return ret;
  81. }
  82. i2c_device_config_t dev_cfg = {
  83. .dev_addr_length = I2C_ADDR_BIT_LEN_7,
  84. .device_address = slv_addr, // not yet set
  85. .scl_speed_hz = SCCB_FREQ,
  86. };
  87. ret = i2c_master_bus_add_device(bus_handle, &dev_cfg, &(devices[device_count].dev_handle));
  88. if (ret != ESP_OK)
  89. {
  90. ESP_LOGE(TAG, "failed to install SCCB I2C device: %s", esp_err_to_name(ret));
  91. return -1;
  92. }
  93. devices[device_count].address = slv_addr;
  94. device_count++;
  95. return 0;
  96. }
  97. int SCCB_Init(int pin_sda, int pin_scl)
  98. {
  99. ESP_LOGI(TAG, "pin_sda %d pin_scl %d", pin_sda, pin_scl);
  100. // i2c_config_t conf;
  101. esp_err_t ret;
  102. sccb_i2c_port = SCCB_I2C_PORT_DEFAULT;
  103. sccb_owns_i2c_port = true;
  104. ESP_LOGI(TAG, "sccb_i2c_port=%d", sccb_i2c_port);
  105. i2c_master_bus_config_t i2c_mst_config = {
  106. .clk_source = I2C_CLK_SRC_DEFAULT,
  107. .i2c_port = SCCB_I2C_PORT_DEFAULT,
  108. .scl_io_num = pin_scl,
  109. .sda_io_num = pin_sda,
  110. .glitch_ignore_cnt = 7,
  111. .flags.enable_internal_pullup = 1};
  112. i2c_master_bus_handle_t bus_handle;
  113. ret = i2c_new_master_bus(&i2c_mst_config, &bus_handle);
  114. if (ret != ESP_OK)
  115. {
  116. ESP_LOGE(TAG, "failed to install SCCB I2C master bus on port %d: %s", sccb_i2c_port, esp_err_to_name(ret));
  117. return ret;
  118. }
  119. return ESP_OK;
  120. }
  121. int SCCB_Use_Port(int i2c_num)
  122. { // sccb use an already initialized I2C port
  123. if (sccb_owns_i2c_port)
  124. {
  125. SCCB_Deinit();
  126. }
  127. if (i2c_num < 0 || i2c_num > I2C_NUM_MAX)
  128. {
  129. return ESP_ERR_INVALID_ARG;
  130. }
  131. sccb_i2c_port = i2c_num;
  132. return ESP_OK;
  133. }
  134. int SCCB_Deinit(void)
  135. {
  136. esp_err_t ret;
  137. for (uint8_t i = 0; i < device_count; i++)
  138. {
  139. ret = i2c_master_bus_rm_device(devices[i].dev_handle);
  140. if (ret != ESP_OK)
  141. {
  142. ESP_LOGE(TAG, "failed to remove SCCB I2C Device");
  143. return ret;
  144. }
  145. devices[i].dev_handle = NULL;
  146. devices[i].address = 0;
  147. }
  148. device_count = 0;
  149. if (!sccb_owns_i2c_port)
  150. {
  151. return ESP_OK;
  152. }
  153. sccb_owns_i2c_port = false;
  154. i2c_master_bus_handle_t bus_handle;
  155. ret = i2c_master_get_bus_handle(sccb_i2c_port, &bus_handle);
  156. if (ret != ESP_OK)
  157. {
  158. ESP_LOGE(TAG, "failed to get SCCB I2C Bus handle for port %d", sccb_i2c_port);
  159. return ret;
  160. }
  161. ret = i2c_del_master_bus(bus_handle);
  162. if (ret != ESP_OK)
  163. {
  164. ESP_LOGE(TAG, "failed to get delete SCCB I2C Master Bus at port %d", sccb_i2c_port);
  165. return ret;
  166. }
  167. return ESP_OK;
  168. }
  169. uint8_t SCCB_Probe(void)
  170. {
  171. uint8_t slave_addr = 0x0;
  172. esp_err_t ret;
  173. i2c_master_bus_handle_t bus_handle;
  174. ret = i2c_master_get_bus_handle(sccb_i2c_port, &bus_handle);
  175. if (ret != ESP_OK)
  176. {
  177. ESP_LOGE(TAG, "failed to get SCCB I2C Bus handle for port %d", sccb_i2c_port);
  178. return ret;
  179. }
  180. for (size_t i = 0; i < CAMERA_MODEL_MAX; i++)
  181. {
  182. if (slave_addr == camera_sensor[i].sccb_addr)
  183. {
  184. continue;
  185. }
  186. slave_addr = camera_sensor[i].sccb_addr;
  187. ret = i2c_master_probe(bus_handle, slave_addr, TIMEOUT_MS);
  188. if (ret == ESP_OK)
  189. {
  190. if (SCCB_Install_Device(slave_addr) != 0)
  191. {
  192. return 0;
  193. }
  194. return slave_addr;
  195. }
  196. }
  197. return 0;
  198. }
  199. uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg)
  200. {
  201. i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));
  202. uint8_t tx_buffer[1];
  203. uint8_t rx_buffer[1];
  204. tx_buffer[0] = reg;
  205. esp_err_t ret = i2c_master_transmit_receive(dev_handle, tx_buffer, 1, rx_buffer, 1, TIMEOUT_MS);
  206. if (ret != ESP_OK)
  207. {
  208. ESP_LOGE(TAG, "SCCB_Read Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, rx_buffer[0], ret);
  209. }
  210. return rx_buffer[0];
  211. }
  212. int SCCB_Write(uint8_t slv_addr, uint8_t reg, uint8_t data)
  213. {
  214. i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));
  215. uint8_t tx_buffer[2];
  216. tx_buffer[0] = reg;
  217. tx_buffer[1] = data;
  218. esp_err_t ret = i2c_master_transmit(dev_handle, tx_buffer, 2, TIMEOUT_MS);
  219. if (ret != ESP_OK)
  220. {
  221. ESP_LOGE(TAG, "SCCB_Write Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret);
  222. }
  223. return ret == ESP_OK ? 0 : -1;
  224. }
  225. uint8_t SCCB_Read16(uint8_t slv_addr, uint16_t reg)
  226. {
  227. i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));
  228. uint8_t rx_buffer[1];
  229. uint16_t reg_htons = LITTLETOBIG(reg);
  230. uint8_t *reg_u8 = (uint8_t *)&reg_htons;
  231. esp_err_t ret = i2c_master_transmit_receive(dev_handle, reg_u8, 2, rx_buffer, 1, TIMEOUT_MS);
  232. if (ret != ESP_OK)
  233. {
  234. ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, rx_buffer[0]);
  235. }
  236. return rx_buffer[0];
  237. }
  238. int SCCB_Write16(uint8_t slv_addr, uint16_t reg, uint8_t data)
  239. {
  240. i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));
  241. uint8_t tx_buffer[3];
  242. tx_buffer[0] = reg >> 8;
  243. tx_buffer[1] = reg & 0x00ff;
  244. tx_buffer[2] = data;
  245. esp_err_t ret = i2c_master_transmit(dev_handle, tx_buffer, 3, TIMEOUT_MS);
  246. if (ret != ESP_OK)
  247. {
  248. ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, data);
  249. }
  250. return ret == ESP_OK ? 0 : -1;
  251. }
  252. uint16_t SCCB_Read_Addr16_Val16(uint8_t slv_addr, uint16_t reg)
  253. {
  254. i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));
  255. uint8_t rx_buffer[2];
  256. uint16_t reg_htons = LITTLETOBIG(reg);
  257. uint8_t *reg_u8 = (uint8_t *)&reg_htons;
  258. esp_err_t ret = i2c_master_transmit_receive(dev_handle, reg_u8, 2, rx_buffer, 2, TIMEOUT_MS);
  259. uint16_t data = ((uint16_t)rx_buffer[0] << 8) | (uint16_t)rx_buffer[1];
  260. if (ret != ESP_OK)
  261. {
  262. ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, data);
  263. }
  264. return data;
  265. }
  266. int SCCB_Write_Addr16_Val16(uint8_t slv_addr, uint16_t reg, uint16_t data)
  267. {
  268. i2c_master_dev_handle_t dev_handle = *(get_handle_from_address(slv_addr));
  269. uint8_t tx_buffer[4];
  270. tx_buffer[0] = reg >> 8;
  271. tx_buffer[1] = reg & 0x00ff;
  272. tx_buffer[2] = data >> 8;
  273. tx_buffer[3] = data & 0x00ff;
  274. esp_err_t ret = i2c_master_transmit(dev_handle, tx_buffer, 4, TIMEOUT_MS);
  275. if (ret != ESP_OK)
  276. {
  277. ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, data);
  278. }
  279. return ret == ESP_OK ? 0 : -1;
  280. }