@@ -130,7 +130,7 @@ void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr) {
130
130
i2c -> hw -> enable = 1 ;
131
131
}
132
132
133
- static int i2c_write_blocking_internal (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
133
+ static i2c_result_t i2c_write_blocking_internal (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
134
134
check_timeout_fn timeout_check , struct timeout_state * ts ) {
135
135
invalid_params_if (HARDWARE_I2C , addr >= 0x80 ); // 7-bit addresses
136
136
invalid_params_if (HARDWARE_I2C , i2c_reserved_addr (addr ));
@@ -146,7 +146,7 @@ static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint
146
146
bool abort = false;
147
147
bool timeout = false;
148
148
149
- uint32_t abort_reason = 0 ;
149
+ i2c_result_t result = { 0 , 0 } ;
150
150
int byte_ctr ;
151
151
152
152
int ilen = (int )len ;
@@ -177,8 +177,8 @@ static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint
177
177
178
178
// If there was a timeout, don't attempt to do anything else.
179
179
if (!timeout ) {
180
- abort_reason = i2c -> hw -> tx_abrt_source ;
181
- if (abort_reason ) {
180
+ result . abort_reason = i2c -> hw -> tx_abrt_source ;
181
+ if (result . abort_reason ) {
182
182
// Note clearing the abort flag also clears the reason, and
183
183
// this instance of flag is clear-on-read! Note also the
184
184
// IC_CLR_TX_ABRT register always reads as 0.
@@ -215,58 +215,77 @@ static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint
215
215
break ;
216
216
}
217
217
218
- int rval ;
219
-
220
218
// A lot of things could have just happened due to the ingenious and
221
219
// creative design of I2C. Try to figure things out.
222
220
if (abort ) {
223
221
if (timeout )
224
- rval = PICO_ERROR_TIMEOUT ;
225
- else if (!abort_reason || abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS ) {
222
+ result . rval = PICO_ERROR_TIMEOUT ;
223
+ else if (!result . abort_reason || result . abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS ) {
226
224
// No reported errors - seems to happen if there is nothing connected to the bus.
227
225
// Address byte not acknowledged
228
- rval = PICO_ERROR_GENERIC ;
229
- } else if (abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_TXDATA_NOACK_BITS ) {
226
+ result . rval = PICO_ERROR_GENERIC ;
227
+ } else if (result . abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_TXDATA_NOACK_BITS ) {
230
228
// Address acknowledged, some data not acknowledged
231
- rval = byte_ctr ;
229
+ result . rval = byte_ctr ;
232
230
} else {
233
- //panic("Unknown abort from I2C instance @%08x: %08x\n", (uint32_t) i2c->hw, abort_reason);
234
- rval = PICO_ERROR_GENERIC ;
231
+ //panic("Unknown abort from I2C instance @%08x: %08x\n", (uint32_t) i2c->hw, result. abort_reason);
232
+ result . rval = PICO_ERROR_GENERIC ;
235
233
}
236
234
} else {
237
- rval = byte_ctr ;
235
+ result . rval = byte_ctr ;
238
236
}
239
237
240
238
// nostop means we are now at the end of a *message* but not the end of a *transfer*
241
239
i2c -> restart_on_next = nostop ;
242
- i2c -> last_abort_reason = abort_reason ;
243
- return rval ;
240
+ return result ;
244
241
}
245
242
246
- int i2c_write_blocking (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ) {
243
+ i2c_result_t i2c_write_result_blocking (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ) {
247
244
return i2c_write_blocking_internal (i2c , addr , src , len , nostop , NULL , NULL );
248
245
}
249
246
250
- int i2c_write_blocking_until (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
247
+ int i2c_write_blocking (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ) {
248
+ i2c_result_t r = i2c_write_result_blocking (i2c , addr , src , len , nostop );
249
+ return r .rval ;
250
+ }
251
+
252
+ i2c_result_t i2c_write_result_blocking_until (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
251
253
absolute_time_t until ) {
252
254
timeout_state_t ts ;
253
255
return i2c_write_blocking_internal (i2c , addr , src , len , nostop , init_single_timeout_until (& ts , until ), & ts );
254
256
}
255
257
256
- int i2c_write_timeout_per_char_us (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
258
+ int i2c_write_blocking_until (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
259
+ absolute_time_t until ) {
260
+ i2c_result_t r = i2c_write_result_blocking_until (i2c , addr , src , len , nostop , until );
261
+ return r .rval ;
262
+ }
263
+
264
+ i2c_result_t i2c_write_result_timeout_per_char_us (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
257
265
uint timeout_per_char_us ) {
258
266
timeout_state_t ts ;
259
267
return i2c_write_blocking_internal (i2c , addr , src , len , nostop ,
260
268
init_per_iteration_timeout_us (& ts , timeout_per_char_us ), & ts );
261
269
}
262
270
263
- int i2c_write_burst_blocking (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len ) {
264
- int rc = i2c_write_blocking_internal (i2c , addr , src , len , true, NULL , NULL );
271
+ int i2c_write_timeout_per_char_us (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len , bool nostop ,
272
+ uint timeout_per_char_us ) {
273
+ i2c_result_t r = i2c_write_result_timeout_per_char_us (i2c , addr , src , len , nostop , timeout_per_char_us );
274
+ return r .rval ;
275
+ }
276
+
277
+ i2c_result_t i2c_write_result_burst_blocking (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len ) {
278
+ i2c_result_t r = i2c_write_blocking_internal (i2c , addr , src , len , true, NULL , NULL );
265
279
i2c -> restart_on_next = false;
266
- return rc ;
280
+ return r ;
281
+ }
282
+
283
+ int i2c_write_burst_blocking (i2c_inst_t * i2c , uint8_t addr , const uint8_t * src , size_t len ) {
284
+ i2c_result_t r = i2c_write_result_burst_blocking (i2c , addr , src , len );
285
+ return r .rval ;
267
286
}
268
287
269
- static int i2c_read_blocking_internal (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ,
288
+ static i2c_result_t i2c_read_blocking_internal (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ,
270
289
check_timeout_fn timeout_check , timeout_state_t * ts ) {
271
290
invalid_params_if (HARDWARE_I2C , addr >= 0x80 ); // 7-bit addresses
272
291
invalid_params_if (HARDWARE_I2C , i2c_reserved_addr (addr ));
@@ -279,7 +298,7 @@ static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *ds
279
298
280
299
bool abort = false;
281
300
bool timeout = false;
282
- uint32_t abort_reason = 0 ;
301
+ i2c_result_t result = { 0 , 0 } ;
283
302
int byte_ctr ;
284
303
int ilen = (int )len ;
285
304
for (byte_ctr = 0 ; byte_ctr < ilen ; ++ byte_ctr ) {
@@ -298,7 +317,7 @@ static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *ds
298
317
I2C_IC_DATA_CMD_CMD_BITS ; // -> 1 for read
299
318
300
319
do {
301
- abort_reason = i2c -> hw -> tx_abrt_source ;
320
+ result . abort_reason = i2c -> hw -> tx_abrt_source ;
302
321
abort = (bool ) i2c -> hw -> clr_tx_abrt ;
303
322
if (timeout_check ) {
304
323
timeout = timeout_check (ts , false);
@@ -312,46 +331,64 @@ static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *ds
312
331
* dst ++ = (uint8_t ) i2c -> hw -> data_cmd ;
313
332
}
314
333
315
- int rval ;
316
-
317
334
if (abort ) {
318
335
if (timeout )
319
- rval = PICO_ERROR_TIMEOUT ;
320
- else if (!abort_reason || abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS ) {
336
+ result . rval = PICO_ERROR_TIMEOUT ;
337
+ else if (!result . abort_reason || result . abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS ) {
321
338
// No reported errors - seems to happen if there is nothing connected to the bus.
322
339
// Address byte not acknowledged
323
- rval = PICO_ERROR_GENERIC ;
340
+ result . rval = PICO_ERROR_GENERIC ;
324
341
} else {
325
- // panic("Unknown abort from I2C instance @%08x: %08x\n", (uint32_t) i2c->hw, abort_reason);
326
- rval = PICO_ERROR_GENERIC ;
342
+ // panic("Unknown abort from I2C instance @%08x: %08x\n", (uint32_t) i2c->hw, result. abort_reason);
343
+ result . rval = PICO_ERROR_GENERIC ;
327
344
}
328
345
} else {
329
- rval = byte_ctr ;
346
+ result . rval = byte_ctr ;
330
347
}
331
348
332
349
i2c -> restart_on_next = nostop ;
333
- i2c -> last_abort_reason = abort_reason ;
334
- return rval ;
350
+ return result ;
335
351
}
336
352
337
- int i2c_read_blocking (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ) {
353
+ i2c_result_t i2c_read_result_blocking (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ) {
338
354
return i2c_read_blocking_internal (i2c , addr , dst , len , nostop , NULL , NULL );
339
355
}
340
356
341
- int i2c_read_blocking_until (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop , absolute_time_t until ) {
357
+ int i2c_read_blocking (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ) {
358
+ i2c_result_t r = i2c_read_result_blocking (i2c , addr , dst , len , nostop );
359
+ return r .rval ;
360
+ }
361
+
362
+ i2c_result_t i2c_read_result_blocking_until (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop , absolute_time_t until ) {
342
363
timeout_state_t ts ;
343
364
return i2c_read_blocking_internal (i2c , addr , dst , len , nostop , init_single_timeout_until (& ts , until ), & ts );
344
365
}
345
366
346
- int i2c_read_timeout_per_char_us (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ,
367
+ int i2c_read_blocking_until (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop , absolute_time_t until ) {
368
+ i2c_result_t r = i2c_read_result_blocking_until (i2c , addr , dst , len , nostop , until );
369
+ return r .rval ;
370
+ }
371
+
372
+ i2c_result_t i2c_read_result_timeout_per_char_us (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ,
347
373
uint timeout_per_char_us ) {
348
374
timeout_state_t ts ;
349
375
return i2c_read_blocking_internal (i2c , addr , dst , len , nostop ,
350
376
init_per_iteration_timeout_us (& ts , timeout_per_char_us ), & ts );
351
377
}
352
378
353
- int i2c_read_burst_blocking (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len ) {
354
- int rc = i2c_read_blocking_internal (i2c , addr , dst , len , true, NULL , NULL );
379
+ int i2c_read_timeout_per_char_us (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len , bool nostop ,
380
+ uint timeout_per_char_us ) {
381
+ i2c_result_t r = i2c_read_result_timeout_per_char_us (i2c , addr , dst , len , nostop , timeout_per_char_us );
382
+ return r .rval ;
383
+ }
384
+
385
+ i2c_result_t i2c_read_result_burst_blocking (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len ) {
386
+ i2c_result_t r = i2c_read_blocking_internal (i2c , addr , dst , len , true, NULL , NULL );
355
387
i2c -> restart_on_next = false;
356
- return rc ;
388
+ return r ;
389
+ }
390
+
391
+ int i2c_read_burst_blocking (i2c_inst_t * i2c , uint8_t addr , uint8_t * dst , size_t len ) {
392
+ i2c_result_t r = i2c_read_result_burst_blocking (i2c , addr , dst , len );
393
+ return r .rval ;
357
394
}
0 commit comments