12
12
#include " I2C.h"
13
13
#include " sdkconfig.h"
14
14
#include < esp_log.h>
15
+ #include " GeneralUtils.h"
15
16
16
17
static const char * LOG_TAG = " I2C" ;
17
18
@@ -27,6 +28,7 @@ I2C::I2C() {
27
28
m_cmd = 0 ;
28
29
m_sdaPin = DEFAULT_SDA_PIN;
29
30
m_sclPin = DEFAULT_CLK_PIN;
31
+ m_portNum = I2C_NUM_0;
30
32
} // I2C
31
33
32
34
@@ -41,7 +43,10 @@ void I2C::beginTransaction() {
41
43
ESP_LOGD (LOG_TAG, " beginTransaction()" );
42
44
}
43
45
m_cmd = ::i2c_cmd_link_create ();
44
- ESP_ERROR_CHECK (::i2c_master_start (m_cmd));
46
+ esp_err_t errRc = ::i2c_master_start (m_cmd);
47
+ if (errRc != ESP_OK) {
48
+ ESP_LOGE (LOG_TAG, " i2c_master_start: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
49
+ }
45
50
m_directionKnown = false ;
46
51
} // beginTransaction
47
52
@@ -57,8 +62,15 @@ void I2C::endTransaction() {
57
62
if (debug) {
58
63
ESP_LOGD (LOG_TAG, " endTransaction()" );
59
64
}
60
- ESP_ERROR_CHECK (::i2c_master_stop (m_cmd));
61
- ESP_ERROR_CHECK (::i2c_master_cmd_begin (I2C_NUM_0, m_cmd, 1000 /portTICK_PERIOD_MS));
65
+ esp_err_t errRc = ::i2c_master_stop (m_cmd);
66
+ if (errRc != ESP_OK) {
67
+ ESP_LOGE (LOG_TAG, " i2c_master_stop: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
68
+ }
69
+
70
+ errRc = ::i2c_master_cmd_begin (m_portNum, m_cmd, 1000 /portTICK_PERIOD_MS);
71
+ if (errRc != ESP_OK) {
72
+ ESP_LOGE (LOG_TAG, " i2c_master_stop: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
73
+ }
62
74
::i2c_cmd_link_delete (m_cmd);
63
75
m_directionKnown = false ;
64
76
} // endTransaction
@@ -83,21 +95,30 @@ uint8_t I2C::getAddress() const
83
95
* @param [in] sclPin The pin to use for SCL clock.
84
96
* @return N/A.
85
97
*/
86
- void I2C::init (uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin) {
87
- ESP_LOGD (LOG_TAG, " >> I2c::init. address=%d, sda=%d, scl=%d" , address, sdaPin, sclPin);
88
- this ->m_sdaPin = sdaPin;
89
- this ->m_sclPin = sclPin;
90
- this ->m_address = address;
98
+ void I2C::init (uint8_t address, gpio_num_t sdaPin, gpio_num_t sclPin, uint32_t clockSpeed, i2c_port_t portNum) {
99
+ ESP_LOGD (LOG_TAG, " >> I2c::init. address=%d, sda=%d, scl=%d, clockSpeed=%d, portNum=%d" , address, sdaPin, sclPin, clockSpeed, portNum);
100
+ assert (portNum < I2C_NUM_MAX);
101
+ m_portNum = portNum;
102
+ m_sdaPin = sdaPin;
103
+ m_sclPin = sclPin;
104
+ m_address = address;
105
+
91
106
i2c_config_t conf;
92
- conf.mode = I2C_MODE_MASTER;
107
+ conf.mode = I2C_MODE_MASTER;
93
108
conf.sda_io_num = sdaPin;
94
109
conf.scl_io_num = sclPin;
95
110
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
96
111
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
97
- conf.master .clk_speed = 100000 ;
98
- ESP_ERROR_CHECK (::i2c_param_config (I2C_NUM_0, &conf));
112
+ conf.master .clk_speed = 100000 ;
113
+ esp_err_t errRc = ::i2c_param_config (m_portNum, &conf);
114
+ if (errRc != ESP_OK) {
115
+ ESP_LOGE (LOG_TAG, " i2c_param_config: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
116
+ }
99
117
if (!driverInstalled) {
100
- ESP_ERROR_CHECK (::i2c_driver_install (I2C_NUM_0, I2C_MODE_MASTER, 0 , 0 , 0 ));
118
+ errRc = ::i2c_driver_install (m_portNum, I2C_MODE_MASTER, 0 , 0 , 0 );
119
+ if (errRc != ESP_OK) {
120
+ ESP_LOGE (LOG_TAG, " i2c_driver_install: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
121
+ }
101
122
driverInstalled = true ;
102
123
}
103
124
} // init
@@ -117,9 +138,15 @@ void I2C::read(uint8_t* bytes, size_t length, bool ack) {
117
138
}
118
139
if (m_directionKnown == false ) {
119
140
m_directionKnown = true ;
120
- ESP_ERROR_CHECK (::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_READ, !ack));
141
+ esp_err_t errRc = ::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_READ, !ack);
142
+ if (errRc != ESP_OK) {
143
+ ESP_LOGE (LOG_TAG, " i2c_master_write_byte: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
144
+ }
145
+ }
146
+ esp_err_t errRc = ::i2c_master_read (m_cmd, bytes, length, !ack);
147
+ if (errRc != ESP_OK) {
148
+ ESP_LOGE (LOG_TAG, " i2c_master_read: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
121
149
}
122
- ESP_ERROR_CHECK (::i2c_master_read (m_cmd, bytes, length, !ack));
123
150
} // read
124
151
125
152
@@ -136,7 +163,10 @@ void I2C::read(uint8_t *byte, bool ack) {
136
163
}
137
164
if (m_directionKnown == false ) {
138
165
m_directionKnown = true ;
139
- ESP_ERROR_CHECK (::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_READ, !ack));
166
+ esp_err_t errRc = ::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_READ, !ack);
167
+ if (errRc != ESP_OK) {
168
+ ESP_LOGE (LOG_TAG, " i2c_master_write_byte: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
169
+ }
140
170
}
141
171
ESP_ERROR_CHECK (::i2c_master_read_byte (m_cmd, byte, !ack));
142
172
} // readByte
@@ -200,7 +230,7 @@ bool I2C::slavePresent(uint8_t address) {
200
230
::i2c_master_write_byte (cmd, (address << 1 ) | I2C_MASTER_WRITE, 1 /* expect ack */ );
201
231
::i2c_master_stop (cmd);
202
232
203
- esp_err_t espRc = ::i2c_master_cmd_begin (I2C_NUM_0 , cmd, 100 /portTICK_PERIOD_MS);
233
+ esp_err_t espRc = ::i2c_master_cmd_begin (m_portNum , cmd, 100 /portTICK_PERIOD_MS);
204
234
i2c_cmd_link_delete (cmd);
205
235
return espRc == 0 ; // Return true if the slave is present and false otherwise.
206
236
} // slavePresent
@@ -214,7 +244,10 @@ void I2C::start() {
214
244
if (debug) {
215
245
ESP_LOGD (LOG_TAG, " start()" );
216
246
}
217
- ESP_ERROR_CHECK (::i2c_master_start (m_cmd));
247
+ esp_err_t errRc = ::i2c_master_start (m_cmd);
248
+ if (errRc != ESP_OK) {
249
+ ESP_LOGE (LOG_TAG, " i2c_master_start: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
250
+ }
218
251
} // start
219
252
220
253
@@ -226,7 +259,10 @@ void I2C::stop() {
226
259
if (debug) {
227
260
ESP_LOGD (LOG_TAG, " stop()" );
228
261
}
229
- ESP_ERROR_CHECK (::i2c_master_stop (m_cmd));
262
+ esp_err_t errRc = ::i2c_master_stop (m_cmd);
263
+ if (errRc != ESP_OK) {
264
+ ESP_LOGE (LOG_TAG, " i2c_master_stop: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
265
+ }
230
266
m_directionKnown = false ;
231
267
} // stop
232
268
@@ -244,9 +280,15 @@ void I2C::write(uint8_t byte, bool ack) {
244
280
}
245
281
if (m_directionKnown == false ) {
246
282
m_directionKnown = true ;
247
- ESP_ERROR_CHECK (::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_WRITE, !ack));
283
+ esp_err_t errRc = ::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_WRITE, !ack);
284
+ if (errRc != ESP_OK) {
285
+ ESP_LOGE (LOG_TAG, " i2c_master_write_byte: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
286
+ }
287
+ }
288
+ esp_err_t errRc = ::i2c_master_write_byte (m_cmd, byte, !ack);
289
+ if (errRc != ESP_OK) {
290
+ ESP_LOGE (LOG_TAG, " i2c_master_write_byte: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
248
291
}
249
- ESP_ERROR_CHECK (::i2c_master_write_byte (m_cmd, byte, !ack));
250
292
} // write
251
293
252
294
@@ -264,9 +306,15 @@ void I2C::write(uint8_t *bytes, size_t length, bool ack) {
264
306
}
265
307
if (m_directionKnown == false ) {
266
308
m_directionKnown = true ;
267
- ESP_ERROR_CHECK (::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_WRITE, !ack));
309
+ esp_err_t errRc = ::i2c_master_write_byte (m_cmd, (m_address << 1 ) | I2C_MASTER_WRITE, !ack);
310
+ if (errRc != ESP_OK) {
311
+ ESP_LOGE (LOG_TAG, " i2c_master_write_byte: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
312
+ }
313
+ }
314
+ esp_err_t errRc = ::i2c_master_write (m_cmd, bytes, length, !ack);
315
+ if (errRc != ESP_OK) {
316
+ ESP_LOGE (LOG_TAG, " i2c_master_write: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
268
317
}
269
- ESP_ERROR_CHECK (::i2c_master_write (m_cmd, bytes, length, !ack));
270
318
} // write
271
319
272
320
0 commit comments