From e49821221f3f46b924c76ac7b311e5183590400d Mon Sep 17 00:00:00 2001 From: Suraj7897 Date: Sun, 12 May 2024 22:28:09 +0530 Subject: [PATCH] Error Handling Improvement and Dynamic Memory Allocation --- i2c/scanner/i2cscanner.c | 94 ++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 37 deletions(-) diff --git a/i2c/scanner/i2cscanner.c b/i2c/scanner/i2cscanner.c index 267dbb07..0f76df7f 100644 --- a/i2c/scanner/i2cscanner.c +++ b/i2c/scanner/i2cscanner.c @@ -10,41 +10,61 @@ static char tag[] = "i2cscanner"; -void task_i2cscanner(void *ignore) { - ESP_LOGD(tag, ">> i2cScanner"); - 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 = 100000; - i2c_param_config(I2C_NUM_0, &conf); - - i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); - - int i; - esp_err_t espRc; - printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); - printf("00: "); - for (i=3; i< 0x78; i++) { - i2c_cmd_handle_t cmd = i2c_cmd_link_create(); - i2c_master_start(cmd); - i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, 1 /* expect ack */); - i2c_master_stop(cmd); - - espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS); - if (i%16 == 0) { - printf("\n%.2x:", i); - } - if (espRc == 0) { - printf(" %.2x", i); - } else { - printf(" --"); - } - //ESP_LOGD(tag, "i=%d, rc=%d (0x%x)", i, espRc, espRc); - i2c_cmd_link_delete(cmd); - } - printf("\n"); - vTaskDelete(NULL); +typedef void (*i2c_scan_callback_t)(uint8_t address, bool success); + +void i2c_scanner_task(void *parameters) { + ESP_LOGD(tag, ">> i2cScanner"); + + i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = SDA_PIN, + .scl_io_num = SCL_PIN, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master = { + .clk_speed = 100000, + }, + }; + i2c_param_config(I2C_NUM_0, &conf); + i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0); + + printf(" 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"); + printf("00: "); + + for (int i = 3; i < 0x78; i++) { + i2c_cmd_handle_t cmd = i2c_cmd_link_create(); + if (!cmd) { + ESP_LOGE(tag, "Failed to create I2C command"); + continue; + } + i2c_master_start(cmd); + i2c_master_write_byte(cmd, (i << 1) | I2C_MASTER_WRITE, true); + i2c_master_stop(cmd); + + esp_err_t espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10 / portTICK_PERIOD_MS); + bool success = (espRc == ESP_OK); + if (success) { + printf(" %.2x", i); + } else { + printf(" --"); + } + i2c_cmd_link_delete(cmd); + } + printf("\n"); + + vTaskDelete(NULL); } + +void i2c_scanner_init(i2c_scan_callback_t callback) { + xTaskCreate(i2c_scanner_task, "i2c_scanner_task", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL); +} + +void i2c_scan_callback(uint8_t address, bool success) { + if (success) { + ESP_LOGI(tag, "Device found at address 0x%x", address); + } else { + ESP_LOGI(tag, "No device found at address 0x%x", address); + } +} + +