- Add a `Scope` block from the Simulink library and configure it to have two input ports.
- Connect the first port to the signal going into the `SPI Controller Transfer` block.
- Connect the second port to the signal coming out of the `SPI Controller Transfer` block.
SPI Loopback in Simulink with STM32 Nucleo
13 views (last 30 days)
Show older comments
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
on 5 Sep 2025
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.
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.
Accepted Answer
Abhishek
on 5 Sep 2025
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:
- Add a `Scope` block from the Simulink library and configure it to have two input ports.
- Connect the first port to the signal going into the `SPI Controller Transfer` block.
- 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:
- Serial Configuration for Monitor & Tune and PIL for STM32 Processor-Based Boards: https://www.mathworks.com/help/releases/R2025a/ecoder/stmicroelectronicsstm32f4discovery/ug/External-mode-PIL.html
- Delay and Latency: https://www.mathworks.com/help/releases/R2025a/dsp/ug/delay-and-latency.html
- Simulink Data Inpsector: https://mathworks.com/help/releases/R2025a/simulink/slref/simulationdatainspector.html
I hope this helps!.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!