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