Skip to content

Commit 4ac16d6

Browse files
committed
Make it possible to get i2c abort reason
Currently we're returning PICO_ERROR_GENERIC for an i2c abort such as "No ack". Add a new PICO_ERROR_ABORT and use this if PICO_I2C_RETURN_ABORT_REASON is true. The abort reason comes from tx_abrt_source (I2C_IC_TX_ABRT_SOURCE_ABRT_*_BITS) so take the zero count from PICO_ERROR_ABORT, so the reason can be determined from the return code. Fixes #1049
1 parent f642b76 commit 4ac16d6

File tree

2 files changed

+15
-5
lines changed
  • src
    • common/pico_base_headers/include/pico
    • rp2_common/hardware_i2c

2 files changed

+15
-5
lines changed

src/common/pico_base_headers/include/pico/error.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ enum pico_error_codes {
4242
PICO_ERROR_UNSUPPORTED_MODIFICATION = -18, ///< Write is impossible based on previous writes; e.g. attempted to clear an OTP bit
4343
PICO_ERROR_LOCK_REQUIRED = -19, ///< A required lock is not owned
4444
PICO_ERROR_VERSION_MISMATCH = -20, ///< A version mismatch occurred (e.g. trying to run PIO version 1 code on RP2040)
45-
PICO_ERROR_RESOURCE_IN_USE = -21 ///< The call could not proceed because requires resourcesw were unavailable
45+
PICO_ERROR_RESOURCE_IN_USE = -21, ///< The call could not proceed because requires resourcesw were unavailable
46+
PICO_ERROR_ABORT = -22, ///< The call could not proceed so was aborted
4647
};
4748

4849
#endif // !__ASSEMBLER__

src/rp2_common/hardware_i2c/i2c.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr) {
130130
i2c->hw->enable = 1;
131131
}
132132

133+
// PICO_CONFIG: PICO_I2C_RETURN_ABORT_REASON, change i2c functions to return the abort reason via a return code less than or equal to PICO_ERROR_ABORT, type=bool, default=0, group=harware_i2c
134+
#if PICO_I2C_RETURN_ABORT_REASON
135+
// if (ret <= PICO_ERROR_ABORT) abort_reason = PICO_ERROR_ABORT - ret; // one of I2C_IC_TX_ABRT_SOURCE_ABRT_*_LSB
136+
#define PICO_I2C_MAKE_ABORT_ERROR(A) (PICO_ERROR_ABORT - __builtin_ctz(A))
137+
#else
138+
// By default, for compatibility, return PICO_ERROR_GENERIC if an abort occurs
139+
#define PICO_I2C_MAKE_ABORT_ERROR(A) PICO_ERROR_GENERIC
140+
#endif
141+
133142
static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop,
134143
check_timeout_fn timeout_check, struct timeout_state *ts) {
135144
invalid_params_if(HARDWARE_I2C, addr >= 0x80); // 7-bit addresses
@@ -225,13 +234,13 @@ static int i2c_write_blocking_internal(i2c_inst_t *i2c, uint8_t addr, const uint
225234
else if (!abort_reason || abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS) {
226235
// No reported errors - seems to happen if there is nothing connected to the bus.
227236
// Address byte not acknowledged
228-
rval = PICO_ERROR_GENERIC;
237+
rval = PICO_I2C_MAKE_ABORT_ERROR(I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS);
229238
} else if (abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_TXDATA_NOACK_BITS) {
230239
// Address acknowledged, some data not acknowledged
231240
rval = byte_ctr;
232241
} else {
233242
//panic("Unknown abort from I2C instance @%08x: %08x\n", (uint32_t) i2c->hw, abort_reason);
234-
rval = PICO_ERROR_GENERIC;
243+
rval = PICO_I2C_MAKE_ABORT_ERROR(abort_reason);
235244
}
236245
} else {
237246
rval = byte_ctr;
@@ -319,10 +328,10 @@ static int i2c_read_blocking_internal(i2c_inst_t *i2c, uint8_t addr, uint8_t *ds
319328
else if (!abort_reason || abort_reason & I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS) {
320329
// No reported errors - seems to happen if there is nothing connected to the bus.
321330
// Address byte not acknowledged
322-
rval = PICO_ERROR_GENERIC;
331+
rval = PICO_I2C_MAKE_ABORT_ERROR(I2C_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK_BITS);
323332
} else {
324333
// panic("Unknown abort from I2C instance @%08x: %08x\n", (uint32_t) i2c->hw, abort_reason);
325-
rval = PICO_ERROR_GENERIC;
334+
rval = PICO_I2C_MAKE_ABORT_ERROR(abort_reason);
326335
}
327336
} else {
328337
rval = byte_ctr;

0 commit comments

Comments
 (0)