@@ -35,8 +35,9 @@ static const PinMap PinMap_I2C_SCL[] = {
35
35
#define I2C_SCLL (x , val ) (x->i2c->SCLL = val)
36
36
#define I2C_SCLH (x , val ) (x->i2c->SCLH = val)
37
37
38
- static const uint32_t I2C_addr_offset [4 ] = {
39
- 0x0C , 0x20 , 0x24 , 0x28
38
+ static const uint32_t I2C_addr_offset [2 ][4 ] = {
39
+ {0x0C , 0x20 , 0x24 , 0x28 },
40
+ {0x30 , 0x34 , 0x38 , 0x3C }
40
41
};
41
42
42
43
static inline void i2c_conclr (i2c_t * obj , int start , int stop , int interrupt , int acknowledge ) {
@@ -124,14 +125,19 @@ inline int i2c_start(i2c_t *obj) {
124
125
}
125
126
126
127
inline int i2c_stop (i2c_t * obj ) {
128
+ int timeout = 0 ;
129
+
127
130
// write the stop bit
128
131
i2c_conset (obj , 0 , 1 , 0 , 0 );
129
132
i2c_clear_SI (obj );
130
133
131
134
// wait for STO bit to reset
132
- while (I2C_CONSET (obj ) & (1 << 4 ));
133
-
134
- return 0 ;
135
+ while (I2C_CONSET (obj ) & (1 << 4 )) {
136
+ timeout ++ ;
137
+ if (timeout > 100000 ) return 1 ;
138
+ }
139
+
140
+ return 0 ;
135
141
}
136
142
137
143
@@ -197,13 +203,13 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
197
203
198
204
if ((status != 0x10 ) && (status != 0x08 )) {
199
205
i2c_stop (obj );
200
- return status ;
206
+ return I2C_ERROR_BUS_BUSY ;
201
207
}
202
208
203
209
status = i2c_do_write (obj , (address | 0x01 ), 1 );
204
210
if (status != 0x40 ) {
205
211
i2c_stop (obj );
206
- return status ;
212
+ return I2C_ERROR_NO_SLAVE ;
207
213
}
208
214
209
215
// Read in all except last byte
@@ -212,7 +218,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
212
218
status = i2c_status (obj );
213
219
if (status != 0x50 ) {
214
220
i2c_stop (obj );
215
- return status ;
221
+ return count ;
216
222
}
217
223
data [count ] = (char ) value ;
218
224
}
@@ -222,7 +228,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
222
228
status = i2c_status (obj );
223
229
if (status != 0x58 ) {
224
230
i2c_stop (obj );
225
- return status ;
231
+ return length - 1 ;
226
232
}
227
233
228
234
data [count ] = (char ) value ;
@@ -232,7 +238,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
232
238
i2c_stop (obj );
233
239
}
234
240
235
- return 0 ;
241
+ return length ;
236
242
}
237
243
238
244
int i2c_write (i2c_t * obj , int address , const char * data , int length , int stop ) {
@@ -242,31 +248,33 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
242
248
243
249
if ((status != 0x10 ) && (status != 0x08 )) {
244
250
i2c_stop (obj );
245
- return status ;
251
+ return I2C_ERROR_BUS_BUSY ;
246
252
}
247
253
248
254
status = i2c_do_write (obj , (address & 0xFE ), 1 );
249
255
if (status != 0x18 ) {
250
256
i2c_stop (obj );
251
- return status ;
257
+ return I2C_ERROR_NO_SLAVE ;
252
258
}
253
259
254
260
for (i = 0 ; i < length ; i ++ ) {
255
261
status = i2c_do_write (obj , data [i ], 0 );
256
262
if (status != 0x28 ) {
257
263
i2c_stop (obj );
258
- return status ;
264
+ return i ;
259
265
}
260
266
}
261
267
262
- i2c_clear_SI (obj );
268
+ // clearing the serial interrupt here might cause an unintended rewrite of the last byte
269
+ // see also issue report https://mbed.org/users/mbed_official/code/mbed/issues/1
270
+ // i2c_clear_SI(obj);
263
271
264
272
// If not repeated start, send stop.
265
273
if (stop ) {
266
274
i2c_stop (obj );
267
275
}
268
276
269
- return 0 ;
277
+ return length ;
270
278
}
271
279
272
280
void i2c_reset (i2c_t * obj ) {
@@ -344,7 +352,7 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
344
352
345
353
i2c_clear_SI (obj );
346
354
347
- return ( count - 1 ) ;
355
+ return count ;
348
356
}
349
357
350
358
int i2c_slave_write (i2c_t * obj , const char * data , int length ) {
@@ -373,7 +381,7 @@ void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask) {
373
381
uint32_t addr ;
374
382
375
383
if ((idx >= 0 ) && (idx <= 3 )) {
376
- addr = ((uint32_t )obj -> i2c ) + I2C_addr_offset [idx ];
384
+ addr = ((uint32_t )obj -> i2c ) + I2C_addr_offset [0 ][ idx ];
377
385
* ((uint32_t * ) addr ) = address & 0xFF ;
378
386
}
379
387
}
0 commit comments