-
Notifications
You must be signed in to change notification settings - Fork 244
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
Question: Generic Traits and I2C #886
Comments
Upon further research and looking through the Embassy Project. I believe the rp-hal took a different approach to handling I2C, meaning that I cannot abstract the I2C type away any further. Based on a dig through Embassy and esp_hal and finding the following snippets: // ESP HAL
///I2C driver
pub struct I2c<'d, Dm: DriverMode, T = AnyI2c> {
i2c: PeripheralRef<'d, T>,
phantom: PhantomData<Dm>,
config: Config,
guard: PeripheralGuard,
}
//embassy-rp
/// I2C driver.
pub struct I2c<'d, T: Instance, M: Mode> {
phantom: PhantomData<(&'d mut T, M)>,
} This structure is what allows the generics at my level. However the way the rp-hal is setup, currently doesn't support this. Therefore the best way to abstract away the implementation details is the following: type PicoI2c = I2C<
I2C1, // I Dont like this type should care if its I2C1 or other
(
Pin<Gpio18, FunctionI2c, PullUp>, // Or this
Pin<Gpio19, FunctionI2c, PullUp>, // Or This
),
>; If anyone has any better ideas please respond to this post :) |
Hi there, thank you for your comment. The rp-hal’s I2C driver captures at compile time both the I2C block (I2C0/I2C1) as well as the pin mapping. It garanties at compile time that the pin used are correct. So at the point of integration (where you select which peripheral & pins) are used, you need to know that those pins+peripheral are. That being said your Alternatively, IIRC the embedded-hal’s I2c trait is dyn compatible so you should be able to make fn generic_test_device_id(i2c: &mut dyn I2c); |
Hi all,
First off thank you very much for the awesome work in getting rust on the RP2040 and following pico boards. Im very excited to get using it.
My question is if there is a better way to handle I2C creation and the types that go along with it. Im currently working on a testing structure using the embedded-test crate. The code in question can be found in this PR dysonltd/tmag5273#53.
Ive highlighted a snippet of it below:
In this code snippet in order to pass into my function I had to declare a PicoI2C type, however this type is very hardcoded such that its got I2C1, its got the pins declared in it. But the rest of the functions shouldn't care about this. I would like the code to be more generic such that you had it an I2C instance. In the esp-hal you can do this equivelant:
This allows each of the test functions to not carte about the I2c instance and thus i can swap it in the init.
Is there a better way to do this in the rp-hal
The text was updated successfully, but these errors were encountered: