23
23
#include "i2c.h"
24
24
#include "variant.h"
25
25
26
- #define TIMEOUT 0xFFFFF
26
+ #define TIMEOUT 16
27
27
28
28
int i2c_getadapter (uint32_t i2c_bus_address )
29
29
{
@@ -34,7 +34,7 @@ static volatile uint8_t i2c_tx_complete;
34
34
static volatile uint8_t i2c_rx_complete ;
35
35
static volatile uint8_t i2c_err_detect ;
36
36
37
- uint8_t i2c_slave = 0 ;
37
+ static volatile uint8_t i2c_slave = 0 ;
38
38
39
39
static void ss_i2c_rx (uint32_t dev_id )
40
40
{
@@ -53,27 +53,29 @@ static void ss_i2c_err(uint32_t dev_id)
53
53
54
54
static int wait_rx_or_err (){
55
55
uint64_t timeout = TIMEOUT ;
56
- while (timeout -- )
57
- if ((i2c_rx_complete ) || (i2c_err_detect )) {
58
- if (i2c_rx_complete )
59
- return I2C_OK ;
60
- else if (i2c_err_detect )
61
- return I2C_ERROR ;
62
- break ;
56
+ while (timeout -- ) {
57
+ if (i2c_err_detect ) {
58
+ return I2C_ERROR ;
63
59
}
60
+ if (i2c_rx_complete ) {
61
+ return I2C_OK ;
62
+ }
63
+ delay (1 );
64
+ }
64
65
return I2C_TIMEOUT ;
65
66
}
66
67
67
68
static int wait_tx_or_err (){
68
69
uint64_t timeout = TIMEOUT ;
69
- while (timeout -- )
70
- if ((i2c_tx_complete ) || (i2c_err_detect )) {
71
- if (i2c_rx_complete )
72
- return I2C_OK ;
73
- else if (i2c_err_detect )
74
- return I2C_ERROR ;
75
- break ;
70
+ while (timeout -- ) {
71
+ if (i2c_err_detect ) {
72
+ return I2C_ERROR ;
73
+ }
74
+ if (i2c_tx_complete ) {
75
+ return I2C_OK ;
76
76
}
77
+ delay (1 );
78
+ }
77
79
return I2C_TIMEOUT ;
78
80
}
79
81
@@ -104,7 +106,7 @@ int i2c_openadapter(uint8_t i2c_adapter_nr)
104
106
105
107
i2c_cfg_data_t i2c_cfg ;
106
108
107
- i2c_cfg .speed = I2C_SLOW ;
109
+ i2c_cfg .speed = I2C_FAST ;
108
110
i2c_cfg .addressing_mode = I2C_7_Bit ;
109
111
i2c_cfg .mode_type = I2C_MASTER ;
110
112
i2c_cfg .cb_tx = ss_i2c_tx ;
@@ -136,19 +138,20 @@ int i2c_writebyte(uint8_t byte)
136
138
i2c_tx_complete = 0 ;
137
139
i2c_err_detect = 0 ;
138
140
ss_i2c_write (I2C_SENSING_0 , ch , 1 , i2c_slave );
141
+
139
142
ret = wait_tx_or_err ();
140
143
if (ret )
141
144
return ret ;
142
145
143
146
ret = wait_dev_ready (I2C_SENSING_0 );
144
147
if (ret )
145
- return I2C_ERROR ;
148
+ return ret ;
146
149
return 1 ; /* number of bytes */
147
150
}
148
151
149
152
int i2c_writebytes (uint8_t * bytes , uint8_t length )
150
153
{
151
- uint8_t ret ;
154
+ int ret ;
152
155
153
156
i2c_tx_complete = 0 ;
154
157
i2c_err_detect = 0 ;
@@ -165,7 +168,7 @@ int i2c_writebytes(uint8_t *bytes, uint8_t length)
165
168
int i2c_readbyte ()
166
169
{
167
170
unsigned char byte ;
168
- uint8_t ret ;
171
+ int ret ;
169
172
170
173
i2c_rx_complete = 0 ;
171
174
i2c_err_detect = 0 ;
@@ -181,7 +184,7 @@ int i2c_readbyte()
181
184
182
185
int i2c_readbytes (uint8_t * buf , int length )
183
186
{
184
- uint8_t ret ;
187
+ int ret ;
185
188
186
189
i2c_rx_complete = 0 ;
187
190
i2c_err_detect = 0 ;
@@ -197,7 +200,7 @@ int i2c_readbytes(uint8_t *buf, int length)
197
200
198
201
int i2c_transferbytes (uint8_t * tx_buf , int tx_length , uint8_t * rx_buf , int rx_length )
199
202
{
200
- uint8_t ret ;
203
+ int ret ;
201
204
202
205
i2c_tx_complete = 0 ;
203
206
i2c_rx_complete = 0 ;
0 commit comments