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 generates too many clocks in Bidirectional mode #11

Open
dsabala opened this issue Jan 31, 2025 · 3 comments
Open

SPI generates too many clocks in Bidirectional mode #11

dsabala opened this issue Jan 31, 2025 · 3 comments
Assignees
Labels
bug Something isn't working hal HAL-LL driver-related issue or pull-request. needs clarification Needs clarification or inputs from the user spi Serial Peripheral Interface

Comments

@dsabala
Copy link

dsabala commented Jan 31, 2025

Describe the set-up

  • Bidirectional SPI, high clock frequency, SPI device with FIFO registers (STM manufactured accelerometer)

Describe the bug
Function HAL_SPI_Receive generate clock on SPI clock line a few octets too long, for example reading 6 octets generates 9 octets of clock, thus transmissions "with side effect" like for example reading FIFO from accelerometer can result in extremely confusing bugs

More detailed info:
http://efton.sk/STM32/gotcha/g32.html

How To Reproduce
Setup bidirectional SPI working at high clock and observe MISO/MOSI line with logic analyzer, use CubeMX autogenerated code.

Additional context
Instead of fixing it you can inform CubeMX users of the problem or even remove bidirectional SPI choice from CubeMX

@KRASTM KRASTM self-assigned this Feb 3, 2025
@KRASTM KRASTM added bug Something isn't working spi Serial Peripheral Interface labels Feb 3, 2025
@KRASTM
Copy link
Contributor

KRASTM commented Feb 3, 2025

Hello @dsabala,

Thank you for the report.

Could you please share more details about this phenomenon (scrennshot with or from logic analyzer and a snippet of code for the transmit or receive function), and your IOC file for further analysis. In the meantime, we will try to check with our team.

With regards,

@ALABSTM ALABSTM added the hal HAL-LL driver-related issue or pull-request. label Feb 3, 2025
@KRASTM KRASTM moved this from To do to Analyzed in stm32cube-mcu-hal-dashboard Feb 4, 2025
@KRASTM KRASTM added the needs clarification Needs clarification or inputs from the user label Feb 11, 2025
@KRASTM
Copy link
Contributor

KRASTM commented Feb 14, 2025

Hello @dsabala,

Any news please?

With regards

@dsabala
Copy link
Author

dsabala commented Feb 17, 2025

Hello @KRASTM

I solved the problem by creating a custom SPI driver that manages half-duplex SPI communication without introducing unnecessary SPI clock cycles. I used HAL LL (low layer) routines to configure the SPI and TIM peripherals to the desired states.

The key aspect of the solution is disabling the SPI peripheral within a very strict timing regime. To achieve this, I utilized a high-resolution hardware timer configured in single-pulse mode. The timer is set to trigger an interrupt after a duration equal to the time required for transmitting a single byte * (number of bytes - 0,5). Within the timer interrupt handler, the SPI peripheral is disabled.

The entire read transfer is executed in a "subcritical" section by using the basepri register, ensuring that only the timer interrupt remains enabled during this process.

Below are the results of reading measurements from the LIS2MDL magnetometer. The device is expected to receive 6 bytes, and while it does receive these 6 bytes, it continues to emit clock signals for an additional 3 cycles. If you are reading registers with side effects, such as FIFO registers in another STM MEMS sensor, this behavior could lead to difficult to debug bugs.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hal HAL-LL driver-related issue or pull-request. needs clarification Needs clarification or inputs from the user spi Serial Peripheral Interface
Projects
Development

No branches or pull requests

3 participants