Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2C abort reason #1370

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

peterharperuk
Copy link
Contributor

Fixes #1049

andygpz11
andygpz11 previously approved these changes May 17, 2023
Copy link
Contributor

@andygpz11 andygpz11 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me.

@peterharperuk
Copy link
Contributor Author

Fixed build issue

@kilograham kilograham self-assigned this May 26, 2023
@kilograham kilograham marked this pull request as draft May 26, 2023 13:38
@kilograham
Copy link
Contributor

assigning to myself; i think i'd rather return the abort reason in the top half of a 64 bit rc (and do some API massaging)

@kilograham kilograham modified the milestones: 1.5.1, 1.6.0 May 26, 2023
@kilograham kilograham changed the title Add last_abort_reason Add I2C last_abort_reason May 19, 2024
@kilograham kilograham modified the milestones: 1.6.1, 1.6.0 May 19, 2024
@peterharperuk
Copy link
Contributor Author

assigning to myself; i think i'd rather return the abort reason in the top half of a 64 bit rc (and do some API massaging)

I don't think you liked this change? I could add a variant of the functions to return int64_t if that sounds like what you wanted?

@kilograham
Copy link
Contributor

yeah, i guess returning a struct with two elements in a replacement APIs and picking the result out for the existing APIs is good
(Note a two uint32_t struct will be returned correctly in r0/r1 i believe)

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 raspberrypi#1049
@peterharperuk peterharperuk changed the title I2C abort_reason I2C abort reason Nov 11, 2024
@peterharperuk
Copy link
Contributor Author

I've had another go at this. I decided I couldn't face adding new overloads to an already complicated API, just to get a not-often used reason for an abort. Instead I've added a new PICO_ERROR_ABORT error code. This is only used if PICO_I2C_RETURN_ABORT_REASON=1 in which case the abort reason is taken from this. So you can obtain the reason with a bit of maths.

if (ret <= PICO_ERROR_ABORT)
    abort_reason = PICO_ERROR_ABORT - ret; // one of I2C_IC_TX_ABRT_SOURCE_ABRT_*_LSB

@peterharperuk peterharperuk marked this pull request as ready for review November 11, 2024 12:48
@kilograham
Copy link
Contributor

interesting... i think perhaps i'd prefer to make something more generic (and not overlaying existing/future return codes)...

e.g. assign codes 0x80000000 to 0xbfffffff as "data" error codes which return 30 bits of function specific result (similar to the way that any positive value can be success)

can then add a pico_error_has_data(code) and pico_error_get_data(code) macros... the I2C API can add a more specific API that uses this

@peterharperuk
Copy link
Contributor Author

peterharperuk commented Nov 11, 2024

Sounds good, will have a go at that. The one issue is that the docs specifies that PICO_ERROR_GENERIC is returned if there's no ack (abort basically), that's the reason I haven't enabled this by default as it's basically an API breakage?

@kilograham
Copy link
Contributor

yes; we have hit this elsewhere - hard to change from -1, though we want to move people towards < 0 ..
so yeah, you can still leave this optional

@@ -42,7 +42,8 @@ enum pico_error_codes {
PICO_ERROR_UNSUPPORTED_MODIFICATION = -18, ///< Write is impossible based on previous writes; e.g. attempted to clear an OTP bit
PICO_ERROR_LOCK_REQUIRED = -19, ///< A required lock is not owned
PICO_ERROR_VERSION_MISMATCH = -20, ///< A version mismatch occurred (e.g. trying to run PIO version 1 code on RP2040)
PICO_ERROR_RESOURCE_IN_USE = -21 ///< The call could not proceed because requires resourcesw were unavailable
PICO_ERROR_RESOURCE_IN_USE = -21, ///< The call could not proceed because requires resourcesw were unavailable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: "requires resourcesw" -> "required resources"

@@ -130,6 +130,15 @@ void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr) {
i2c->hw->enable = 1;
}

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

harware_i2c -> hardware_i2c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants