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

add default SPI word types #320

Closed
wants to merge 2 commits into from
Closed

Conversation

burrbull
Copy link
Member

@burrbull burrbull commented Nov 3, 2021

Thought about this reading #295

cc @eldruin @Sh3Rm4n @ryankurte

Should we restrict types be some trait?
Can also split U16 to U16LE and U16BE

@burrbull burrbull requested a review from a team as a code owner November 3, 2021 06:17
@rust-highfive
Copy link

r? @ryankurte

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added S-waiting-on-review Review is incomplete T-hal labels Nov 3, 2021
Comment on lines +8 to +13
/// 9-bit SPI Word
pub struct U9;
/// 16-bit SPI Word
pub struct U16;
/// 18-bit SPI Word
pub struct U18;
Copy link

Choose a reason for hiding this comment

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

How are these zero-sized types supposed to be used? I think you'd need something more like

pub struct U9(pub u16);
pub type U16 = u16;
pub struct U18(pub u32);

Copy link
Member Author

@burrbull burrbull Nov 3, 2021

Choose a reason for hiding this comment

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

You are right it was stupid for me. But you suggestion will not work also.
It should be something like:

pub trait SpiWord {
   type Bytes;
}
pub struct U9;
impl SpiWord for U9 {
    type Bytes = u16;
}

Copy link

Choose a reason for hiding this comment

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

I see, this solution also works. However, now there isn't any implicit type annotation anymore so when calling, for example, Transfer::transfer() you'll have to be explicit about the datatype. This means you cannot call it like

spi.transfer(&buf);

but you'll have to call it as

Transfer::<U8>::transfer(&mut spi, &buf);

My suggestion was to have newtype wrappers around the odd bitwidths and then store an array of those. This would allow the compiler to infer the type automatically:

let buf: [U9; 16] = [U9(0), U9(23), ...];
spi.transfer(&buf);

Copy link
Member Author

Choose a reason for hiding this comment

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

Wrapping each element of array is a pain

@burrbull burrbull mentioned this pull request Nov 3, 2021
@Dirbaio
Copy link
Member

Dirbaio commented Nov 3, 2021

  1. u32 should be added: stm32h7xx-hal has an impl for Word=u32.
  2. This is fixing the in-RAM representation of u9 etc. Perhaps a chip wants the 9 bits be msb-aligned inside the u16 instead of lsb-aligned to be able to DMA. Without the bound, a HAL can define their own WeirdU9 newtype that handles this and take &[WeirdU9] which already has the right repr to DMA straight out of it. Restricting word types makes this impossible.
  3. I'm not sure if the benefit is worth the extra complexity, with new types, a new trait, and associated types.

@burrbull burrbull marked this pull request as draft November 4, 2021 04:42
bors bot added a commit that referenced this pull request Nov 4, 2021
321: u8 as default SPI Word r=eldruin a=burrbull

Part of #320 

Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
@eldruin eldruin added this to the v1.0.0 milestone Feb 14, 2022
@eldruin eldruin mentioned this pull request Feb 14, 2022
@burrbull burrbull closed this Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Review is incomplete T-hal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants