SPI Loopback in Simulink with STM32 Nucleo

13 views (last 30 days)
Hi
I'm trying to do a simple loopback (I have connected pin D11 &D12) using a Nucleo F411RE and the SPI blocks in the STM32 support package. I am using the Spi Controller Transfer and I cannot seem to read back the data correctly, it just shows 0.
I've checked the output of the MOSI on an oscilloscope and it's definitely sending the data out. I think I'm missing something with the way to read SPI in Simulink Coder.
My settings:
Does anyone have any ideas?
Cheers
Amir
  2 Comments
Abhishek
Abhishek on 5 Sep 2025
Hello @Amir,
Based on your description, this behavior could be related to a timing delay in how the Simulink model interacts with the SPI hardware. It's possible that the '0' you are seeing is because the looped-back data from a step is not available to the model until the next execution time step.
A good way to investigate this is to visualize the input and output signals together over time. This will help you confirm if a delay is the root cause.
You can test this hypothesis by replacing your `Display` block with a `Scope` block.
  1. Add a `Scope` block from the Simulink library and configure it to have two input ports.
  2. Connect the first port to the signal going into the `SPI Controller Transfer` block.
  3. Connect the second port to the signal coming out of the `SPI Controller Transfer` block.
When you run the model, observe the two waveforms on the Scope. If the output signal is a time-shifted version of the input signal, it would strongly suggest that there is a processing delay. This test is a reliable way to determine the exact behavior of the block in your specific setup.
Amir
Amir on 5 Sep 2025
thanks, @Abhishek - but if there is a delay wouldn't it eventually display a non-zero value?

Sign in to comment.

Accepted Answer

Abhishek
Abhishek on 5 Sep 2025
Hello @Amir,
I understand that you are facing an issue with reading data from STM32 microcontroller. There could be multiple reasons for the issue.
Whenever you use a `Scope` block, make sure to disable the Use a dedicated timer to improve time stamp accuracy parameter in the model's configuration settings. The same applies, if you do use a `Display` block.
Secondly, as stated in the comment, there could be a possibility of timing delay in how the Simulink model interacts with the SPI hardware. It's possible that the '0' you are seeing is because the looped-back data from a step is not available to the model until the next execution time step.
You can test this by doing the following:
  1. Add a `Scope` block from the Simulink library and configure it to have two input ports.
  2. Connect the first port to the signal going into the `SPI Controller Transfer` block.
  3. Connect the second port to the signal coming out of the `SPI Controller Transfer` block.
You can refer to the below model to check the above hypothesis:
And last, but not the least, make sure to uncheck the ‘Limit data points to last’ and check the zoom scale. You can also log signals to Simulink Data Inspector as an alternative to Scopes.
For more information, you can refer to the official MATLAB documentation:
I hope this helps!.
  1 Comment
Amir
Amir on 5 Sep 2025
@Abhijit Navalekar - IT WORKED!
The trick was to disable the Use a dedicated timer to improve time stamp accuracy.
Thank you so much for your help. I definitely think this should be highlighted in the documentation or Nucleo video in future releases.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!