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

Update embedded-hal #94

Merged
merged 4 commits into from
Sep 22, 2023
Merged

Conversation

LehMaxence
Copy link
Contributor

Removed write-only and read-only spi implementations
Handled Delay operation between transfers

@LehMaxence LehMaxence marked this pull request as ready for review August 1, 2023 17:00
@LehMaxence LehMaxence requested a review from a team as a code owner August 1, 2023 17:00
@LehMaxence
Copy link
Contributor Author

I tested this with hardware today on a Raspberry Pi 4 and a ADXL355 accelerometer connected over SPI with this code:

    let mut spi_dev = Spidev::open(SPI_PATH)?;
    let options = SpidevOptions::new()
        .bits_per_word(8)
        .max_speed_hz(10_000_000)
        .mode(SpiModeFlags::SPI_MODE_0)
        .build();
    spi_dev.configure(&options)?;

    let mut buf_1 = [0u8; 1];
    let mut buf_2 = [0u8; 1];

    let mut operations = [
        SpiOperation::DelayUs(1_000_000),
        SpiOperation::Write(&[(0x2D << 1), 0x01]),
        SpiOperation::DelayUs(500),
        SpiOperation::Write(&[(0x2D << 1) | 0x01]),
        SpiOperation::Read(&mut buf_1),
        SpiOperation::DelayUs(500),
        SpiOperation::Write(&[(0x2D << 1), 0x00]),
        SpiOperation::DelayUs(500),
        SpiOperation::Write(&[(0x2D << 1) | 0x01]),
        SpiOperation::Read(&mut buf_2),
        SpiOperation::DelayUs(5_000_000),
    ];

    spi_dev.transaction(&mut operations)?;

    println!("buf_1: {:X}", buf_1[0]);
    println!("buf_2: {:X}", buf_2[0]);

It worked as expected.

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

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

Thank you for your work here.
Unfortunately, this approach is suboptimal in that it requires multiple transfers and flushes on the bus every time there is a delay, even though the underlying linux device supports delay transfers.

The optimal approach here would be to first extend spidev: rust-embedded/rust-spidev#40 to support delay operations and then updating this crate in line with what was outlined in: #87 (comment)

Sorry for the confusion and lack of more explicit guidance.

@LehMaxence
Copy link
Contributor Author

Thanks for the feedback! I agree with you.
I'll wait for the rust-spidev update and then update this PR, or we can close it if you prefer.

@@ -132,6 +96,9 @@ mod embedded_hal_impl {
};
SpidevTransfer::read_write(tx, buf)
}
SpiOperation::DelayUs(us) => {
SpidevTransfer::delay((*us).try_into().unwrap_or(u16::MAX))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I chose to max out the delay to u16::MAX but that may not be what we want here.
Let me know what you think.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can leave at that here but please document in this method and in the Spidev struct that delays are capped to 65535 microseconds.

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

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

Thank you!

@@ -132,6 +96,9 @@ mod embedded_hal_impl {
};
SpidevTransfer::read_write(tx, buf)
}
SpiOperation::DelayUs(us) => {
SpidevTransfer::delay((*us).try_into().unwrap_or(u16::MAX))
Copy link
Member

Choose a reason for hiding this comment

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

I think we can leave at that here but please document in this method and in the Spidev struct that delays are capped to 65535 microseconds.

eldruin
eldruin previously approved these changes Sep 20, 2023
Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

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

Sorry for the delay.
Looks good to me, thank you!

@eldruin
Copy link
Member

eldruin commented Sep 20, 2023

Ach, CI has to be migrated to GitHub merge queue for this to be merged.

@eldruin eldruin mentioned this pull request Sep 20, 2023
@eldruin
Copy link
Member

eldruin commented Sep 22, 2023

@LehMaxence could you rebase this to master now that CI is fixed?

@LehMaxence
Copy link
Contributor Author

@LehMaxence could you rebase this to master now that CI is fixed?

Rebased :-)

Copy link
Member

@eldruin eldruin left a comment

Choose a reason for hiding this comment

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

Great, thank you!

@eldruin eldruin added this pull request to the merge queue Sep 22, 2023
Merged via the queue into rust-embedded:master with commit 86ab035 Sep 22, 2023
8 checks passed
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