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 Full Duplex #29614

Merged
merged 8 commits into from
Apr 2, 2025
Merged

SPI Full Duplex #29614

merged 8 commits into from
Apr 2, 2025

Conversation

andyp1per
Copy link
Collaborator

@andyp1per andyp1per commented Mar 25, 2025

This PR makes the use of the full duplex SPI API with DMA optimisation explicit to avoid contortions in expected behaviour by the underlying HALs and in the caller.

As a side effect this fixes a bug in the Linux HAL where the original implict semantics did not work.

@andyp1per andyp1per force-pushed the pr-spi-full-duplex branch from 5c0c6cb to 66c955d Compare March 25, 2025 13:50
@andyp1per andyp1per added the BUG label Mar 25, 2025
Copy link
Contributor

@tpwrules tpwrules left a comment

Choose a reason for hiding this comment

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

Quite happy to see this! I can follow up to touch up comments and drivers.

I assume you've evaluated all the uses of transfer to make sure this semantic quirk isn't used anywhere else? It looks like some drivers could make use of the three-argument form of transfer_fullduplex as well.

Copy link
Contributor

@tpwrules tpwrules left a comment

Choose a reason for hiding this comment

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

Good news is this works and flies great on the Stampfly, which has a BMI270. I previously had to update the ESP32 HAL to account for the quirk and make that IMU actually work.

But I think I found one more place that needs changing to transfer_fullduplex:

_dev->transfer(buffer, buffer_offset, buffer, buffer_offset);

@andyp1per
Copy link
Collaborator Author

Tested on Matek H7A3

Copy link
Contributor

@tpwrules tpwrules left a comment

Choose a reason for hiding this comment

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

I think we do need to somehow deprecate the old semantics in ChibiOS. I worry a bit about upstreaming broken drivers.

* Like #transfer(), but len bytes are both transmitted and received from/into @send_recv.
* This function is optimized for DMA-enabled multi-transfers without buffer copying
*/
bool transfer_fullduplex(uint8_t *send_recv, uint32_t len) override {
Copy link
Contributor

Choose a reason for hiding this comment

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

this is a copy of the one in Device.h, why is this here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Removed, hopefully it's not necessary for Linux or something :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It can't be removed or loads of hals do not build

Copy link
Contributor

Choose a reason for hiding this comment

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

which HAL doesn't build without the transfer_fullduplex() in AP_HAL/SPIDevice.h?

Copy link
Contributor

Choose a reason for hiding this comment

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

QURT at least had a build failure on CI. It seems to be a Clang warning that you have to override all the variations with different arguments of a particular function name to avoid ambiguity: https://stackoverflow.com/a/9995567

 [10/933] Compiling libraries/AC_AttitudeControl/AC_CommandModel.cpp
In file included from ../../libraries/AC_AttitudeControl/AC_CommandModel.cpp:1:
In file included from ../../libraries/AC_AttitudeControl/AC_CommandModel.h:6:
In file included from ../../libraries/AP_Param/AP_Param.h:27:
In file included from ../../libraries/AP_HAL/AP_HAL.h:8:
In file included from ../../libraries/AP_HAL/AP_HAL_Main.h:19:
In file included from ../../libraries/AP_HAL/HAL.h:11:
../../libraries/AP_HAL/SPIDevice.h:45:18: fatal error: 'AP_HAL::SPIDevice::transfer_fullduplex' hides overloaded virtual function [-Woverloaded-virtual]
    virtual bool transfer_fullduplex(const uint8_t *send, uint8_t *recv,
                 ^
../../libraries/AP_HAL/Device.h:137:18: note: hidden overloaded virtual function 'AP_HAL::Device::transfer_fullduplex' declared here: different number of parameters (2 vs 3)
    virtual bool transfer_fullduplex(uint8_t *send_recv, uint32_t len) {
                 ^
1 error generated.

I personally would just mark it as pure virtual = 0 instead of duplicating the implementation. More C++ learning!

@tpwrules tpwrules force-pushed the pr-spi-full-duplex branch from e6bf28a to 787e788 Compare April 1, 2025 04:02
Copy link
Contributor

@tpwrules tpwrules left a comment

Choose a reason for hiding this comment

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

Tested again and it works on the bench on ESP32 and Cube Orange. Removed the redundant function. Should be ready for merge!

@andyp1per
Copy link
Collaborator Author

@tpwrules the function you removed is necessary to make everything build correctly - I spent quite a long time fixing this - please don't make changes to other people's PR's unless you know it's correct!

@tridge tridge merged commit cfcd27c into ArduPilot:master Apr 2, 2025
204 checks passed
@IamPete1 IamPete1 removed the DevCallEU label Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants