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

Spi improvements #91

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

amcelroy
Copy link
Contributor

Howdy,

I'd like to submit some changes to the SPI module. Long story short, I need to write 16-bit data to a TLV5614 and don't care about return data, as there is none. The default FullDuplex implementations had two issues: they only use 8-bits and they throw an error if the OVR bit is set which happens when writing but not reading from the data register.

The 8-bit-only problem was solved by creating a new SpiDataSize enum that is passed in when instantiating the SPI object.

For the second problem, the first attempt was to add trait implementations for FullDuplex<u16>, but the logic would have differed from FullDuplex<u8> and that didn't seem right.

The second attempt was to add unchecked_send_u8 and unchecked_send_u16 functions to the default impl SPI. These are functions used write to the data register blindly, it is up to the user to check the status register. However, the data was not properly outputting on the SPI bus, which required the is_busy function to check if the SPI bus is busy. This is used in the other two functions as a blocking check before writing to the data register.

Attached is a picture from a Seleae logic analyzer showing data (12 words, 0x0001 through 0x000C) written to the SPI bus using a 20MHz SPI clock using the unchecked_send_u16 function.

Screenshot 2023-11-28 at 9 42 18 AM

- Added SpiDataSize enum to allow for non 8-bit data transfers
- Moved cr2 write after cr1 write, based on section 39.5.7 in
the rm0440 manual
- Added is_busy function to check if the SPI bus is busy
- Added unchecked_send_u8 and unchecked_send_u16
that writes to the data register but doesn't check the status
 register. It does check and wait for the busy bit to be low; without
 this check the data was not transmitting correctly.
@no111u3
Copy link
Collaborator

no111u3 commented Nov 28, 2023

Looks good for begin. Also would be better to use https://github.com/stm32-rs/stm32f4xx-hal/tree/master/src/spi base methodology: Bidi + Uni direction exchange implementation for future use in some crates like displays, smart LEDs, etc..

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

Successfully merging this pull request may close these issues.

2 participants