13
13
// limitations under the License.
14
14
15
15
#include "esp32-hal-i2c.h"
16
+ #include "esp32-hal.h"
16
17
#include "freertos/FreeRTOS.h"
17
18
#include "freertos/task.h"
18
19
#include "freertos/semphr.h"
19
20
#include "rom/ets_sys.h"
20
21
#include "soc/i2c_reg.h"
22
+ #include "soc/i2c_struct.h"
21
23
#include "soc/dport_reg.h"
22
24
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)))
23
27
#define I2C_SCL_IDX (p ) ((p==0)?I2CEXT0_SCL_OUT_IDX:((p==1)?I2CEXT1_SCL_OUT_IDX:0))
24
28
#define I2C_SDA_IDX (p ) ((p==0)?I2CEXT0_SDA_OUT_IDX:((p==1)?I2CEXT1_SDA_OUT_IDX:0))
25
29
@@ -46,52 +50,56 @@ static i2c_t _i2c_bus_array[2] = {
46
50
{(volatile i2c_dev_t * )(DR_REG_I2C1_EXT_BASE ), NULL , 1 }
47
51
};
48
52
49
- void i2cAttachSCL (i2c_t * i2c , int8_t scl )
53
+ i2c_err_t i2cAttachSCL (i2c_t * i2c , int8_t scl )
50
54
{
51
55
if (i2c == NULL ){
52
- return ;
56
+ return I2C_ERROR_DEV ;
53
57
}
54
58
I2C_MUTEX_LOCK ();
55
59
pinMode (scl , OUTPUT );
56
60
pinMatrixOutAttach (scl , I2C_SCL_IDX (i2c -> num ), false, false);
57
61
pinMatrixInAttach (scl , I2C_SCL_IDX (i2c -> num ), false);
58
62
I2C_MUTEX_UNLOCK ();
63
+ return I2C_ERROR_OK ;
59
64
}
60
65
61
- void i2cDetachSCL (i2c_t * i2c , int8_t scl )
66
+ i2c_err_t i2cDetachSCL (i2c_t * i2c , int8_t scl )
62
67
{
63
68
if (i2c == NULL ){
64
- return ;
69
+ return I2C_ERROR_DEV ;
65
70
}
66
71
I2C_MUTEX_LOCK ();
67
72
pinMatrixOutDetach (scl , false, false);
68
73
pinMatrixInDetach (I2C_SCL_IDX (i2c -> num ), false, false);
69
74
pinMode (scl , INPUT );
70
75
I2C_MUTEX_UNLOCK ();
76
+ return I2C_ERROR_OK ;
71
77
}
72
78
73
- void i2cAttachSDA (i2c_t * i2c , int8_t sda )
79
+ i2c_err_t i2cAttachSDA (i2c_t * i2c , int8_t sda )
74
80
{
75
81
if (i2c == NULL ){
76
- return ;
82
+ return I2C_ERROR_DEV ;
77
83
}
78
84
I2C_MUTEX_LOCK ();
79
85
pinMode (sda , OUTPUT_OPEN_DRAIN );
80
86
pinMatrixOutAttach (sda , I2C_SDA_IDX (i2c -> num ), false, false);
81
87
pinMatrixInAttach (sda , I2C_SDA_IDX (i2c -> num ), false);
82
88
I2C_MUTEX_UNLOCK ();
89
+ return I2C_ERROR_OK ;
83
90
}
84
91
85
- void i2cDetachSDA (i2c_t * i2c , int8_t sda )
92
+ i2c_err_t i2cDetachSDA (i2c_t * i2c , int8_t sda )
86
93
{
87
94
if (i2c == NULL ){
88
- return ;
95
+ return I2C_ERROR_DEV ;
89
96
}
90
97
I2C_MUTEX_LOCK ();
91
98
pinMatrixOutDetach (sda , false, false);
92
99
pinMatrixInDetach (I2C_SDA_IDX (i2c -> num ), false, false);
93
100
pinMode (sda , INPUT );
94
101
I2C_MUTEX_UNLOCK ();
102
+ return I2C_ERROR_OK ;
95
103
}
96
104
97
105
/*
@@ -120,15 +128,15 @@ void i2cResetFiFo(i2c_t * i2c)
120
128
i2c -> dev -> fifo_conf .rx_fifo_rst = 0 ;
121
129
}
122
130
123
- int i2cWrite (i2c_t * i2c , uint16_t address , bool addr_10bit , uint8_t * data , uint8_t len , bool sendStop )
131
+ i2c_err_t i2cWrite (i2c_t * i2c , uint16_t address , bool addr_10bit , uint8_t * data , uint8_t len , bool sendStop )
124
132
{
125
133
int i ;
126
134
uint8_t index = 0 ;
127
135
uint8_t dataLen = len + (addr_10bit ?2 :1 );
128
136
address = (address << 1 );
129
137
130
138
if (i2c == NULL ){
131
- return 4 ;
139
+ return I2C_ERROR_DEV ;
132
140
}
133
141
134
142
I2C_MUTEX_LOCK ();
@@ -178,21 +186,21 @@ int i2cWrite(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uin
178
186
if (i2c -> dev -> int_raw .arbitration_lost ) {
179
187
//log_e("Bus Fail! Addr: %x", address >> 1);
180
188
I2C_MUTEX_UNLOCK ();
181
- return 4 ;
189
+ return I2C_ERROR_BUS ;
182
190
}
183
191
184
192
//Bus timeout
185
193
if (i2c -> dev -> int_raw .time_out ) {
186
194
//log_e("Bus Timeout! Addr: %x", address >> 1);
187
195
I2C_MUTEX_UNLOCK ();
188
- return 3 ;
196
+ return I2C_ERROR_TIMEOUT ;
189
197
}
190
198
191
199
//Transmission did not finish and ACK_ERR is set
192
200
if (i2c -> dev -> int_raw .ack_err ) {
193
201
//log_e("Ack Error! Addr: %x", address >> 1);
194
202
I2C_MUTEX_UNLOCK ();
195
- return 1 ;
203
+ return I2C_ERROR_ACK ;
196
204
}
197
205
198
206
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
204
212
205
213
}
206
214
I2C_MUTEX_UNLOCK ();
207
- return 0 ;
215
+ return I2C_ERROR_OK ;
208
216
}
209
217
210
- int i2cRead (i2c_t * i2c , uint16_t address , bool addr_10bit , uint8_t * data , uint8_t len , bool sendStop )
218
+ i2c_err_t i2cRead (i2c_t * i2c , uint16_t address , bool addr_10bit , uint8_t * data , uint8_t len , bool sendStop )
211
219
{
212
220
address = (address << 1 ) | 1 ;
213
221
uint8_t addrLen = (addr_10bit ?2 :1 );
@@ -216,7 +224,7 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
216
224
uint8_t willRead ;
217
225
218
226
if (i2c == NULL ){
219
- return 4 ;
227
+ return I2C_ERROR_DEV ;
220
228
}
221
229
222
230
I2C_MUTEX_LOCK ();
@@ -263,21 +271,21 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
263
271
if (i2c -> dev -> int_raw .arbitration_lost ) {
264
272
//log_e("Bus Fail! Addr: %x", address >> 1);
265
273
I2C_MUTEX_UNLOCK ();
266
- return -4 ;
274
+ return I2C_ERROR_BUS ;
267
275
}
268
276
269
277
//Bus timeout
270
278
if (i2c -> dev -> int_raw .time_out ) {
271
279
//log_e("Bus Timeout! Addr: %x", address >> 1);
272
280
I2C_MUTEX_UNLOCK ();
273
- return -3 ;
281
+ return I2C_ERROR_TIMEOUT ;
274
282
}
275
283
276
284
//Transmission did not finish and ACK_ERR is set
277
285
if (i2c -> dev -> int_raw .ack_err ) {
278
286
//log_e("Ack Error! Addr: %x", address >> 1);
279
287
I2C_MUTEX_UNLOCK ();
280
- return -1 ;
288
+ return I2C_ERROR_ACK ;
281
289
}
282
290
if (i2c -> dev -> ctr .trans_start || i2c -> dev -> status_reg .bus_busy || !(i2c -> dev -> int_raw .trans_complete ) || !(i2c -> dev -> command [cmdIdx - 1 ].done )) {
283
291
continue ;
@@ -294,15 +302,15 @@ int i2cRead(i2c_t * i2c, uint16_t address, bool addr_10bit, uint8_t * data, uint
294
302
len -= willRead ;
295
303
}
296
304
I2C_MUTEX_UNLOCK ();
297
- return 0 ;
305
+ return I2C_ERROR_OK ;
298
306
}
299
307
300
- void i2cSetFrequency (i2c_t * i2c , uint32_t clk_speed )
308
+ i2c_err_t i2cSetFrequency (i2c_t * i2c , uint32_t clk_speed )
301
309
{
302
310
uint32_t period = (APB_CLK_FREQ /clk_speed ) / 2 ;
303
311
304
312
if (i2c == NULL ){
305
- return ;
313
+ return I2C_ERROR_DEV ;
306
314
}
307
315
308
316
I2C_MUTEX_LOCK ();
@@ -318,6 +326,7 @@ void i2cSetFrequency(i2c_t * i2c, uint32_t clk_speed)
318
326
i2c -> dev -> sda_hold .time = 25 ;
319
327
i2c -> dev -> sda_sample .time = 25 ;
320
328
I2C_MUTEX_UNLOCK ();
329
+ return I2C_ERROR_OK ;
321
330
}
322
331
323
332
uint32_t i2cGetFrequency (i2c_t * i2c )
0 commit comments