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#define I2C_SCL_IDX (p ) ((p==0)?I2CEXT0_SCL_OUT_IDX:((p==1)?I2CEXT1_SCL_OUT_IDX:0))
2428#define I2C_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 {(volatile i2c_dev_t * )(DR_REG_I2C1_EXT_BASE ), NULL , 1 }
4751};
4852
49- void i2cAttachSCL (i2c_t * i2c , int8_t scl )
53+ i2c_err_t i2cAttachSCL (i2c_t * i2c , int8_t scl )
5054{
5155 if (i2c == NULL ){
52- return ;
56+ return I2C_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+ return I2C_ERROR_OK ;
5964}
6065
61- void i2cDetachSCL (i2c_t * i2c , int8_t scl )
66+ i2c_err_t i2cDetachSCL (i2c_t * i2c , int8_t scl )
6267{
6368 if (i2c == NULL ){
64- return ;
69+ return I2C_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+ return I2C_ERROR_OK ;
7177}
7278
73- void i2cAttachSDA (i2c_t * i2c , int8_t sda )
79+ i2c_err_t i2cAttachSDA (i2c_t * i2c , int8_t sda )
7480{
7581 if (i2c == NULL ){
76- return ;
82+ return I2C_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+ return I2C_ERROR_OK ;
8390}
8491
85- void i2cDetachSDA (i2c_t * i2c , int8_t sda )
92+ i2c_err_t i2cDetachSDA (i2c_t * i2c , int8_t sda )
8693{
8794 if (i2c == NULL ){
88- return ;
95+ return I2C_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+ return I2C_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- 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 )
124132{
125133 int i ;
126134 uint8_t index = 0 ;
127135 uint8_t dataLen = len + (addr_10bit ?2 :1 );
128136 address = (address << 1 );
129137
130138 if (i2c == NULL ){
131- return 4 ;
139+ return I2C_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- return 4 ;
189+ return I2C_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- return 3 ;
196+ return I2C_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- return 1 ;
203+ return I2C_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- return 0 ;
215+ return I2C_ERROR_OK ;
208216}
209217
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 )
211219{
212220 address = (address << 1 ) | 1 ;
213221 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
216224 uint8_t willRead ;
217225
218226 if (i2c == NULL ){
219- return 4 ;
227+ return I2C_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+ return I2C_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+ return I2C_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+ return I2C_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- return 0 ;
305+ return I2C_ERROR_OK ;
298306}
299307
300- void i2cSetFrequency (i2c_t * i2c , uint32_t clk_speed )
308+ i2c_err_t i2cSetFrequency (i2c_t * i2c , uint32_t clk_speed )
301309{
302310 uint32_t period = (APB_CLK_FREQ /clk_speed ) / 2 ;
303311
304312 if (i2c == NULL ){
305- return ;
313+ return I2C_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+ return I2C_ERROR_OK ;
321330}
322331
323332uint32_t i2cGetFrequency (i2c_t * i2c )
0 commit comments