|
|
@ -38,16 +38,26 @@ static const char* TAG = "sccb"; |
|
|
|
#define ACK_VAL 0x0 /*!< I2C ack value */ |
|
|
|
#define ACK_VAL 0x0 /*!< I2C ack value */ |
|
|
|
#define NACK_VAL 0x1 /*!< I2C nack value */ |
|
|
|
#define NACK_VAL 0x1 /*!< I2C nack value */ |
|
|
|
#if CONFIG_SCCB_HARDWARE_I2C_PORT1 |
|
|
|
#if CONFIG_SCCB_HARDWARE_I2C_PORT1 |
|
|
|
const int SCCB_I2C_PORT = 1; |
|
|
|
const int SCCB_I2C_PORT_DEFAULT = 1; |
|
|
|
#else |
|
|
|
#else |
|
|
|
const int SCCB_I2C_PORT = 0; |
|
|
|
const int SCCB_I2C_PORT_DEFAULT = 0; |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int sccb_i2c_port; |
|
|
|
|
|
|
|
static bool sccb_owns_i2c_port; |
|
|
|
|
|
|
|
|
|
|
|
int SCCB_Init(int pin_sda, int pin_scl) |
|
|
|
int SCCB_Init(int pin_sda, int pin_scl) |
|
|
|
{ |
|
|
|
{ |
|
|
|
ESP_LOGI(TAG, "pin_sda %d pin_scl %d", pin_sda, pin_scl); |
|
|
|
ESP_LOGI(TAG, "pin_sda %d pin_scl %d", pin_sda, pin_scl); |
|
|
|
i2c_config_t conf; |
|
|
|
i2c_config_t conf; |
|
|
|
|
|
|
|
esp_err_t ret; |
|
|
|
|
|
|
|
|
|
|
|
memset(&conf, 0, sizeof(i2c_config_t)); |
|
|
|
memset(&conf, 0, sizeof(i2c_config_t)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sccb_i2c_port = SCCB_I2C_PORT_DEFAULT; |
|
|
|
|
|
|
|
sccb_owns_i2c_port = true; |
|
|
|
|
|
|
|
ESP_LOGI(TAG, "sccb_i2c_port=%d\n", sccb_i2c_port); |
|
|
|
|
|
|
|
|
|
|
|
conf.mode = I2C_MODE_MASTER; |
|
|
|
conf.mode = I2C_MODE_MASTER; |
|
|
|
conf.sda_io_num = pin_sda; |
|
|
|
conf.sda_io_num = pin_sda; |
|
|
|
conf.sda_pullup_en = GPIO_PULLUP_ENABLE; |
|
|
|
conf.sda_pullup_en = GPIO_PULLUP_ENABLE; |
|
|
@ -55,30 +65,37 @@ int SCCB_Init(int pin_sda, int pin_scl) |
|
|
|
conf.scl_pullup_en = GPIO_PULLUP_ENABLE; |
|
|
|
conf.scl_pullup_en = GPIO_PULLUP_ENABLE; |
|
|
|
conf.master.clk_speed = SCCB_FREQ; |
|
|
|
conf.master.clk_speed = SCCB_FREQ; |
|
|
|
|
|
|
|
|
|
|
|
i2c_param_config(SCCB_I2C_PORT, &conf); |
|
|
|
if ((ret = i2c_param_config(sccb_i2c_port, &conf)) != ESP_OK) { |
|
|
|
i2c_driver_install(SCCB_I2C_PORT, conf.mode, 0, 0, 0); |
|
|
|
return ret; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return i2c_driver_install(sccb_i2c_port, conf.mode, 0, 0, 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int SCCB_Use_Port(int i2c_num) { // sccb use an already initialized I2C port
|
|
|
|
|
|
|
|
if (sccb_owns_i2c_port) { |
|
|
|
|
|
|
|
SCCB_Deinit(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (i2c_num < 0 || i2c_num > I2C_NUM_MAX) { |
|
|
|
|
|
|
|
return ESP_ERR_INVALID_ARG; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sccb_i2c_port = i2c_num; |
|
|
|
|
|
|
|
return ESP_OK; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int SCCB_Deinit(void) |
|
|
|
int SCCB_Deinit(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return i2c_driver_delete(SCCB_I2C_PORT); |
|
|
|
if (!sccb_owns_i2c_port) { |
|
|
|
|
|
|
|
return ESP_OK; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
sccb_owns_i2c_port = false; |
|
|
|
|
|
|
|
return i2c_driver_delete(sccb_i2c_port); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
uint8_t SCCB_Probe(void) |
|
|
|
uint8_t SCCB_Probe(void) |
|
|
|
{ |
|
|
|
{ |
|
|
|
uint8_t slave_addr = 0x0; |
|
|
|
uint8_t slave_addr = 0x0; |
|
|
|
// for (size_t i = 1; i < 0x80; i++) {
|
|
|
|
|
|
|
|
// i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
|
|
|
|
|
|
|
// i2c_master_start(cmd);
|
|
|
|
|
|
|
|
// i2c_master_write_byte(cmd, ( i << 1 ) | WRITE_BIT, ACK_CHECK_EN);
|
|
|
|
|
|
|
|
// i2c_master_stop(cmd);
|
|
|
|
|
|
|
|
// esp_err_t ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
|
|
|
|
|
|
|
|
// i2c_cmd_link_delete(cmd);
|
|
|
|
|
|
|
|
// if( ret == ESP_OK) {
|
|
|
|
|
|
|
|
// ESP_LOGW(TAG, "Found I2C Device at 0x%02X", i);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
for (size_t i = 0; i < CAMERA_MODEL_MAX; i++) { |
|
|
|
for (size_t i = 0; i < CAMERA_MODEL_MAX; i++) { |
|
|
|
if (slave_addr == camera_sensor[i].sccb_addr) { |
|
|
|
if (slave_addr == camera_sensor[i].sccb_addr) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
@ -88,7 +105,7 @@ uint8_t SCCB_Probe(void) |
|
|
|
i2c_master_start(cmd); |
|
|
|
i2c_master_start(cmd); |
|
|
|
i2c_master_write_byte(cmd, ( slave_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, ( slave_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
esp_err_t ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
esp_err_t ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if( ret == ESP_OK) { |
|
|
|
if( ret == ESP_OK) { |
|
|
|
return slave_addr; |
|
|
|
return slave_addr; |
|
|
@ -106,7 +123,7 @@ uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg) |
|
|
|
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | WRITE_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg, ACK_CHECK_EN); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if(ret != ESP_OK) return -1; |
|
|
|
if(ret != ESP_OK) return -1; |
|
|
|
cmd = i2c_cmd_link_create(); |
|
|
|
cmd = i2c_cmd_link_create(); |
|
|
@ -114,7 +131,7 @@ uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg) |
|
|
|
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | READ_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | READ_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_read_byte(cmd, &data, NACK_VAL); |
|
|
|
i2c_master_read_byte(cmd, &data, NACK_VAL); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if(ret != ESP_OK) { |
|
|
|
if(ret != ESP_OK) { |
|
|
|
ESP_LOGE(TAG, "SCCB_Read Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret); |
|
|
|
ESP_LOGE(TAG, "SCCB_Read Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret); |
|
|
@ -131,7 +148,7 @@ uint8_t SCCB_Write(uint8_t slv_addr, uint8_t reg, uint8_t data) |
|
|
|
i2c_master_write_byte(cmd, reg, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, data, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, data, ACK_CHECK_EN); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if(ret != ESP_OK) { |
|
|
|
if(ret != ESP_OK) { |
|
|
|
ESP_LOGE(TAG, "SCCB_Write Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret); |
|
|
|
ESP_LOGE(TAG, "SCCB_Write Failed addr:0x%02x, reg:0x%02x, data:0x%02x, ret:%d", slv_addr, reg, data, ret); |
|
|
@ -151,7 +168,7 @@ uint8_t SCCB_Read16(uint8_t slv_addr, uint16_t reg) |
|
|
|
i2c_master_write_byte(cmd, reg_u8[0], ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg_u8[0], ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg_u8[1], ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg_u8[1], ACK_CHECK_EN); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if(ret != ESP_OK) return -1; |
|
|
|
if(ret != ESP_OK) return -1; |
|
|
|
cmd = i2c_cmd_link_create(); |
|
|
|
cmd = i2c_cmd_link_create(); |
|
|
@ -159,7 +176,7 @@ uint8_t SCCB_Read16(uint8_t slv_addr, uint16_t reg) |
|
|
|
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | READ_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, ( slv_addr << 1 ) | READ_BIT, ACK_CHECK_EN); |
|
|
|
i2c_master_read_byte(cmd, &data, NACK_VAL); |
|
|
|
i2c_master_read_byte(cmd, &data, NACK_VAL); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if(ret != ESP_OK) { |
|
|
|
if(ret != ESP_OK) { |
|
|
|
ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, data); |
|
|
|
ESP_LOGE(TAG, "W [%04x]=%02x fail\n", reg, data); |
|
|
@ -180,7 +197,7 @@ uint8_t SCCB_Write16(uint8_t slv_addr, uint16_t reg, uint8_t data) |
|
|
|
i2c_master_write_byte(cmd, reg_u8[1], ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, reg_u8[1], ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, data, ACK_CHECK_EN); |
|
|
|
i2c_master_write_byte(cmd, data, ACK_CHECK_EN); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
ret = i2c_master_cmd_begin(SCCB_I2C_PORT, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
ret = i2c_master_cmd_begin(sccb_i2c_port, cmd, 1000 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
if(ret != ESP_OK) { |
|
|
|
if(ret != ESP_OK) { |
|
|
|
ESP_LOGE(TAG, "W [%04x]=%02x %d fail\n", reg, data, i++); |
|
|
|
ESP_LOGE(TAG, "W [%04x]=%02x %d fail\n", reg, data, i++); |
|
|
|