Skip to content

Commit f9f8957

Browse files
committed
return proper errors
1 parent 9ec4389 commit f9f8957

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

‎cores/esp32/esp32-hal-i2c.c‎

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,17 @@
1313
// limitations under the License.
1414

1515
#include"esp32-hal-i2c.h"
16+
#include"esp32-hal.h"
1617
#include"freertos/FreeRTOS.h"
1718
#include"freertos/task.h"
1819
#include"freertos/semphr.h"
1920
#include"rom/ets_sys.h"
2021
#include"soc/i2c_reg.h"
22+
#include"soc/i2c_struct.h"
2123
#include"soc/dport_reg.h"
2224

25+
//#define I2C_DEV(i) (volatile i2c_dev_t *)((i)?DR_REG_I2C1_EXT_BASE:DR_REG_I2C_EXT_BASE)
26+
//#define I2C_DEV(i) ((i2c_dev_t *)(REG_I2C_BASE(i)))
2327
#defineI2C_SCL_IDX(p) ((p==0)?I2CEXT0_SCL_OUT_IDX:((p==1)?I2CEXT1_SCL_OUT_IDX:0))
2428
#defineI2C_SDA_IDX(p) ((p==0)?I2CEXT0_SDA_OUT_IDX:((p==1)?I2CEXT1_SDA_OUT_IDX:0))
2529

@@ -46,52 +50,56 @@ static i2c_t _i2c_bus_array[2] ={
4650
{(volatilei2c_dev_t*)(DR_REG_I2C1_EXT_BASE), NULL, 1}
4751
};
4852

49-
voidi2cAttachSCL(i2c_t*i2c, int8_tscl)
53+
i2c_err_ti2cAttachSCL(i2c_t*i2c, int8_tscl)
5054
{
5155
if(i2c==NULL){
52-
return;
56+
returnI2C_ERROR_DEV;
5357
}
5458
I2C_MUTEX_LOCK();
5559
pinMode(scl, OUTPUT);
5660
pinMatrixOutAttach(scl, I2C_SCL_IDX(i2c->num), false, false);
5761
pinMatrixInAttach(scl, I2C_SCL_IDX(i2c->num), false);
5862
I2C_MUTEX_UNLOCK();
63+
returnI2C_ERROR_OK;
5964
}
6065

61-
voidi2cDetachSCL(i2c_t*i2c, int8_tscl)
66+
i2c_err_ti2cDetachSCL(i2c_t*i2c, int8_tscl)
6267
{
6368
if(i2c==NULL){
64-
return;
69+
returnI2C_ERROR_DEV;
6570
}
6671
I2C_MUTEX_LOCK();
6772
pinMatrixOutDetach(scl, false, false);
6873
pinMatrixInDetach(I2C_SCL_IDX(i2c->num), false, false);
6974
pinMode(scl, INPUT);
7075
I2C_MUTEX_UNLOCK();
76+
returnI2C_ERROR_OK;
7177
}
7278

73-
voidi2cAttachSDA(i2c_t*i2c, int8_tsda)
79+
i2c_err_ti2cAttachSDA(i2c_t*i2c, int8_tsda)
7480
{
7581
if(i2c==NULL){
76-
return;
82+
returnI2C_ERROR_DEV;
7783
}
7884
I2C_MUTEX_LOCK();
7985
pinMode(sda, OUTPUT_OPEN_DRAIN);
8086
pinMatrixOutAttach(sda, I2C_SDA_IDX(i2c->num), false, false);
8187
pinMatrixInAttach(sda, I2C_SDA_IDX(i2c->num), false);
8288
I2C_MUTEX_UNLOCK();
89+
returnI2C_ERROR_OK;
8390
}
8491

85-
voidi2cDetachSDA(i2c_t*i2c, int8_tsda)
92+
i2c_err_ti2cDetachSDA(i2c_t*i2c, int8_tsda)
8693
{
8794
if(i2c==NULL){
88-
return;
95+
returnI2C_ERROR_DEV;
8996
}
9097
I2C_MUTEX_LOCK();
9198
pinMatrixOutDetach(sda, false, false);
9299
pinMatrixInDetach(I2C_SDA_IDX(i2c->num), false, false);
93100
pinMode(sda, INPUT);
94101
I2C_MUTEX_UNLOCK();
102+
returnI2C_ERROR_OK;
95103
}
96104

97105
/*
@@ -120,15 +128,15 @@ void i2cResetFiFo(i2c_t * i2c)
120128
i2c->dev->fifo_conf.rx_fifo_rst=0;
121129
}
122130

123-
inti2cWrite(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop)
131+
i2c_err_ti2cWrite(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop)
124132
{
125133
inti;
126134
uint8_tindex=0;
127135
uint8_tdataLen=len+ (addr_10bit?2:1);
128136
address= (address << 1);
129137

130138
if(i2c==NULL){
131-
return4;
139+
returnI2C_ERROR_DEV;
132140
}
133141

134142
I2C_MUTEX_LOCK();
@@ -178,21 +186,21 @@ int i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uin
178186
if(i2c->dev->int_raw.arbitration_lost){
179187
//log_e("Bus Fail! Addr: %x", address >> 1);
180188
I2C_MUTEX_UNLOCK();
181-
return4;
189+
returnI2C_ERROR_BUS;
182190
}
183191

184192
//Bus timeout
185193
if(i2c->dev->int_raw.time_out){
186194
//log_e("Bus Timeout! Addr: %x", address >> 1);
187195
I2C_MUTEX_UNLOCK();
188-
return3;
196+
returnI2C_ERROR_TIMEOUT;
189197
}
190198

191199
//Transmission did not finish and ACK_ERR is set
192200
if(i2c->dev->int_raw.ack_err){
193201
//log_e("Ack Error! Addr: %x", address >> 1);
194202
I2C_MUTEX_UNLOCK();
195-
return1;
203+
returnI2C_ERROR_ACK;
196204
}
197205

198206
if(i2c->dev->ctr.trans_start||i2c->dev->status_reg.bus_busy|| !(i2c->dev->int_raw.trans_complete) || !(i2c->dev->command[2].done)){
@@ -204,10 +212,10 @@ int i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uin
204212

205213
}
206214
I2C_MUTEX_UNLOCK();
207-
return0;
215+
returnI2C_ERROR_OK;
208216
}
209217

210-
inti2cRead(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop)
218+
i2c_err_ti2cRead(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop)
211219
{
212220
address= (address << 1) | 1;
213221
uint8_taddrLen= (addr_10bit?2:1);
@@ -216,7 +224,7 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
216224
uint8_twillRead;
217225

218226
if(i2c==NULL){
219-
return4;
227+
returnI2C_ERROR_DEV;
220228
}
221229

222230
I2C_MUTEX_LOCK();
@@ -263,21 +271,21 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
263271
if(i2c->dev->int_raw.arbitration_lost){
264272
//log_e("Bus Fail! Addr: %x", address >> 1);
265273
I2C_MUTEX_UNLOCK();
266-
return-4;
274+
returnI2C_ERROR_BUS;
267275
}
268276

269277
//Bus timeout
270278
if(i2c->dev->int_raw.time_out){
271279
//log_e("Bus Timeout! Addr: %x", address >> 1);
272280
I2C_MUTEX_UNLOCK();
273-
return-3;
281+
returnI2C_ERROR_TIMEOUT;
274282
}
275283

276284
//Transmission did not finish and ACK_ERR is set
277285
if(i2c->dev->int_raw.ack_err){
278286
//log_e("Ack Error! Addr: %x", address >> 1);
279287
I2C_MUTEX_UNLOCK();
280-
return-1;
288+
returnI2C_ERROR_ACK;
281289
}
282290
if(i2c->dev->ctr.trans_start||i2c->dev->status_reg.bus_busy|| !(i2c->dev->int_raw.trans_complete) || !(i2c->dev->command[cmdIdx-1].done)){
283291
continue;
@@ -294,15 +302,15 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
294302
len-=willRead;
295303
}
296304
I2C_MUTEX_UNLOCK();
297-
return0;
305+
returnI2C_ERROR_OK;
298306
}
299307

300-
voidi2cSetFrequency(i2c_t*i2c, uint32_tclk_speed)
308+
i2c_err_ti2cSetFrequency(i2c_t*i2c, uint32_tclk_speed)
301309
{
302310
uint32_tperiod= (APB_CLK_FREQ/clk_speed) / 2;
303311

304312
if(i2c==NULL){
305-
return;
313+
returnI2C_ERROR_DEV;
306314
}
307315

308316
I2C_MUTEX_LOCK();
@@ -318,6 +326,7 @@ void i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
318326
i2c->dev->sda_hold.time=25;
319327
i2c->dev->sda_sample.time=25;
320328
I2C_MUTEX_UNLOCK();
329+
returnI2C_ERROR_OK;
321330
}
322331

323332
uint32_ti2cGetFrequency(i2c_t*i2c)

‎cores/esp32/esp32-hal-i2c.h‎

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,32 @@
1919
extern"C"{
2020
#endif
2121

22-
#include"esp32-hal.h"
23-
#include"soc/i2c_struct.h"
22+
#include<stdint.h>
23+
#include<stdbool.h>
24+
25+
typedefenum{
26+
I2C_ERROR_OK,
27+
I2C_ERROR_DEV,
28+
I2C_ERROR_ACK,
29+
I2C_ERROR_TIMEOUT,
30+
I2C_ERROR_BUS
31+
} i2c_err_t;
2432

2533
structi2c_struct_t;
2634
typedefstructi2c_struct_ti2c_t;
2735

2836
i2c_t*i2cInit(uint8_ti2c_num, uint16_tslave_addr, booladdr_10bit_en);
2937

30-
voidi2cSetFrequency(i2c_t*i2c, uint32_tclk_speed);
38+
i2c_err_ti2cSetFrequency(i2c_t*i2c, uint32_tclk_speed);
3139
uint32_ti2cGetFrequency(i2c_t*i2c);
3240

33-
voidi2cAttachSCL(i2c_t*i2c, int8_tscl);
34-
voidi2cDetachSCL(i2c_t*i2c, int8_tscl);
35-
voidi2cAttachSDA(i2c_t*i2c, int8_tsda);
36-
voidi2cDetachSDA(i2c_t*i2c, int8_tsda);
41+
i2c_err_ti2cAttachSCL(i2c_t*i2c, int8_tscl);
42+
i2c_err_ti2cDetachSCL(i2c_t*i2c, int8_tscl);
43+
i2c_err_ti2cAttachSDA(i2c_t*i2c, int8_tsda);
44+
i2c_err_ti2cDetachSDA(i2c_t*i2c, int8_tsda);
3745

38-
inti2cWrite(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop);
39-
inti2cRead(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop);
46+
i2c_err_ti2cWrite(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop);
47+
i2c_err_ti2cRead(i2c_t*i2c, uint16_taddress, booladdr_10bit, uint8_t*data, uint8_tlen, boolsendStop);
4048

4149

4250
#ifdef__cplusplus

0 commit comments

Comments
(0)