Skip to content

Commit e21b822

Browse files
committed
IDF I2C Corrections
1 parent b74f464 commit e21b822

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/Platforms/API_I2C_EspressifIDF.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifdef ESP32_CMAKE
33

44
#include <assert.h>
5-
#include <string.h> // memcpy
5+
#include <string.h> // memcpy
66

77
#include "Platforms/API_I2C.h"
88
#include "driver/i2c_master.h"
@@ -37,18 +37,18 @@ error_t i2c_bus_create(struct I2CConfig *config) {
3737
i2c_mst_config.flags.enable_internal_pullup = true;
3838

3939
i2c_master_bus_handle_t bus_handle;
40-
if (i2c_new_master_bus(&i2c_mst_config, &bus_handle)!=ESP_OK){
40+
if (i2c_new_master_bus(&i2c_mst_config, &bus_handle) != ESP_OK) {
4141
AD_LOGE("i2c_new_master_bus");
4242
return ESP_FAIL;
4343
}
4444

45-
if (i2c_master_probe(bus_handle, pins.address > 0, -1)!=RESULT_OK){
45+
if (i2c_master_probe(bus_handle, pins.address, -1) != RESULT_OK) {
4646
AD_LOGE("Address check failed: scanning addresses:");
47-
for (int j=0;j<127;j++){
47+
for (int j = 0; j < 127; j++) {
4848
auto rc = i2c_master_probe(bus_handle, j, -1);
4949
AD_LOGE("- address: 0x%x -> %d", j, rc);
5050
}
51-
return ESP_FAIL;
51+
return ESP_FAIL;
5252
}
5353

5454
// store dev_handle
@@ -65,7 +65,7 @@ void i2c_bus_delete(i2c_bus_handle_t bus) {
6565

6666
error_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
6767
int reglen, uint8_t *data, int datalen) {
68-
AD_LOGI("i2c_bus_write_bytes address: 0x%x", addr);
68+
AD_LOGD("i2c_bus_write_bytes address: 0x%x", addr);
6969
i2c_master_bus_handle_t bus_handle = (i2c_master_bus_handle_t)bus;
7070

7171
I2CConfig *cfg = get_config(bus);
@@ -74,11 +74,11 @@ error_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
7474

7575
i2c_device_config_t dev_cfg = {};
7676
dev_cfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
77-
dev_cfg.device_address = cfg->address > 1;
77+
dev_cfg.device_address = cfg->address;
7878
dev_cfg.scl_speed_hz = cfg->frequency;
7979

8080
i2c_master_dev_handle_t dev_handle;
81-
if (!i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle)==ESP_OK){
81+
if (!i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle) == ESP_OK) {
8282
AD_LOGE("i2c_new_master_bus");
8383
return ESP_FAIL;
8484
}
@@ -90,13 +90,16 @@ error_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
9090
memcpy(total_data + reglen, data, datalen);
9191

9292
esp_err_t ret = ESP_OK;
93-
ret |= i2c_master_transmit(dev_handle, total_data, total_len, -1) == ESP_OK;
94-
// ret |= i2c_master_transmit(dev_handle, reg, reglen, -1) == ESP_OK;
95-
// ret |= i2c_master_transmit(dev_handle, data, datalen, -1) == ESP_OK;
96-
ret |= i2c_master_bus_wait_all_done(bus_handle, -1) == ESP_OK;
93+
ret |= i2c_master_transmit(dev_handle, total_data, total_len, -1);
94+
if (ret == ESP_OK) {
95+
ret = i2c_master_bus_wait_all_done(bus_handle, -1);
96+
if (ret != ESP_OK) AD_LOGE("i2c_master_bus_wait_all_done");
97+
} else {
98+
AD_LOGE("i2c_master_transmit");
99+
}
97100

98101
if (i2c_master_bus_rm_device(dev_handle) != ESP_OK) {
99-
AD_LOGI("i2c_master_bus_rm_device");
102+
AD_LOGE("i2c_master_bus_rm_device");
100103
}
101104

102105
if (ret != ESP_OK) {
@@ -108,7 +111,7 @@ error_t i2c_bus_write_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
108111
error_t i2c_bus_read_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
109112
int reglen, uint8_t *read_buffer, int read_size) {
110113
// get port
111-
AD_LOGI("i2c_bus_read_bytes address: 0x%x", addr);
114+
AD_LOGD("i2c_bus_read_bytes address: 0x%x", addr);
112115
i2c_master_bus_handle_t bus_handle = (i2c_master_bus_handle_t)bus;
113116

114117
I2CConfig *cfg = get_config(bus);
@@ -117,11 +120,11 @@ error_t i2c_bus_read_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
117120

118121
i2c_device_config_t dev_cfg = {};
119122
dev_cfg.dev_addr_length = I2C_ADDR_BIT_LEN_7;
120-
dev_cfg.device_address = cfg->address > 1;
123+
dev_cfg.device_address = cfg->address;
121124
dev_cfg.scl_speed_hz = cfg->frequency;
122125

123126
i2c_master_dev_handle_t dev_handle;
124-
if (!i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle)==ESP_OK){
127+
if (!i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle) == ESP_OK) {
125128
AD_LOGE("i2c_master_bus_add_device");
126129
return ESP_FAIL;
127130
}
@@ -132,9 +135,9 @@ error_t i2c_bus_read_bytes(i2c_bus_handle_t bus, int addr, uint8_t *reg,
132135
memcpy(write_buffer + 1, reg, reglen);
133136

134137
esp_err_t ret = ESP_OK;
135-
ret |= i2c_master_transmit_receive(dev_handle, write_buffer, write_size, read_buffer,
136-
read_size, -1) == ESP_OK;
137-
ret |= i2c_master_bus_wait_all_done(bus_handle, -1) == ESP_OK;
138+
ret = i2c_master_transmit_receive(dev_handle, write_buffer, write_size,
139+
read_buffer, read_size, -1);
140+
ret = i2c_master_bus_wait_all_done(bus_handle, -1);
138141
if (ret != ESP_OK) {
139142
AD_LOGE("i2c_bus_read_bytes");
140143
}

0 commit comments

Comments
 (0)