Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Handling Improvement and Dynamic Memory Allocation #1192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 57 additions & 37 deletions i2c/scanner/i2cscanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}