I am trying to implement I2C bus to read/write from a slave drive.
When the ESP32 starts after programming or reset, the first two cycles of bus scan work flawlessly, but third and beyond they do not detect anything.
If it was the slave holding the line: reset should not solve the problem.
Your insight would be greatly appreciated!
Thank you,
Filipe
Output:
D (1988) i2c_driver: >> i2cScanner
D (1988) intr_alloc: Connected src 49 to int 12 (cpu 0)
D (1988) i2c_driver: I2C initialized 0
D (2988) i2c_driver: 0 1 2 3 4 5 6 7 8 9 a b c d e f
D (3988) i2c_driver: �0: -- -- -- -- -- -- 09 -- -- -- -- -- --
D (3988) i2c_driver: 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (3988) i2c_driver: 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (3998) i2c_driver: 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (4008) i2c_driver: 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (4008) i2c_driver: 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (4018) i2c_driver:
D (5018) i2c_driver: >> i2cScanner
D (5018) i2c_driver: I2C initialized 0
D (6018) i2c_driver: 0 1 2 3 4 5 6 7 8 9 a b c d e f
D (7018) i2c_driver: �0: -- -- -- -- -- -- 09 -- -- -- -- -- --
D (7018) i2c_driver: 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (7018) i2c_driver: 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (7028) i2c_driver: 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (7088) i2c_driver: 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (7248) i2c_driver: 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (7398) i2c_driver:
D (8398) i2c_driver: >> i2cScanner
D (8398) i2c_driver: I2C initialized 0
D (9398) i2c_driver: 0 1 2 3 4 5 6 7 8 9 a b c d e f
D (10538) i2c_driver: 00: -- -- -- -- -- -- -- -- -- -- -- -- --
D (10698) i2c_driver: 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (10858) i2c_driver: 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (11018) i2c_driver: 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (11178) i2c_driver: 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (11338) i2c_driver: 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
D (11488) i2c_driver:
Code: Select all
static char tag[] = "i2c_driver";
esp_err_t initialize_i2c_control_master_I2C_NUM_0(){
esp_err_t espRc=0;
static bool i2c_inialized=false;
if (!i2c_inialized){
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = SDA_PIN;
conf.scl_io_num = SCL_PIN;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = I2C_FREQ;
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
espRc = i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
ESP_ERROR_CHECK (espRc);
if (espRc!=ESP_OK ){
ESP_LOGD(tag, "Unable to initialize i2c driver");
}
i2c_inialized = true;
}
ESP_LOGD(tag, "I2C initialized %i", espRc);
vTaskDelay(1000 / portTICK_PERIOD_MS);
return 0;
}
esp_err_t destroy_i2c_control_master(){
return i2c_driver_delete(I2C_NUM_0);
}
void task_i2cscanner() {
ESP_LOGD(tag, ">> i2cScanner");
int i;
esp_err_t espRc;
i2c_cmd_handle_t cmd;
char line[100];
char aux[20];
espRc = initialize_i2c_control_master_I2C_NUM_0();
ESP_LOGD(tag, " 0 1 2 3 4 5 6 7 8 9 a b c d e f");
vTaskDelay(1000 / portTICK_PERIOD_MS);
sprintf(line, "00: ");
for (i=3; i< 0x70; i++) {
cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */);
i2c_master_read_byte(cmd, (uint8_t*)line, false);
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
if (i%16 == 0) {
ESP_LOGD(tag, "%s", line);
sprintf(line, "%.2x:", i);
}
if (espRc == 0) {
sprintf(aux, " %.2x", i);
strcat(line, aux);
} else {
strcat(line, " --");
}
//ESP_LOGD(tag, "i=%d, rc=%d (0x%x)", i, espRc, espRc);
i2c_cmd_link_delete(cmd);
}
ESP_LOGD(tag,"\n");
return;
}
void app_main(){
while (1){
task_i2cscanner();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}