rust-embedded / embedded-hal

A Hardware Abstraction Layer (HAL) for embedded systems
Apache License 2.0
2.01k stars 202 forks source link

Question: How to support Word: u16 #369

Closed milesgranger closed 2 years ago

milesgranger commented 2 years ago

Hi there :wave:

I'm just getting started with embedded Rust, and I see it is common (standard?) to mention the following statement for FullDuplex trait:

Some SPIs can work with 8-bit and 16-bit words. You can overload this trait with different Word types to allow operation in both modes.

In stm32l4xx-hal the word is u8.

While attempting to write an driver (SPI) for ISM43362 (pg. 18-19), the word is required to be u16 and I'm struggling to figure out how to support both/change to u16 for transfer operations. Are there existing examples or some hints someone can direct me towards? :pray:

Sorry if this is a terribly noob question. :smile:

Dirbaio commented 2 years ago

If you need Word=u16, you'd have to add support for that in l4xx-hal.

You probably don't really need it though. With word=u8 you can do everything. if you want to send a u16, send 2 bytes instead, with .to_be_bytes() or .to_le_bytes() depending on the endianness needed.

milesgranger commented 2 years ago

Okay, thanks. This was my impression, that within that macro, adding a parameter for the type (u8, u16) for the implementation of FullDuplex<$type_> but the statement gave me pause that perhaps there was another standard way of getting the same result.

Appreciate your help.