SPI Loopback in Simulink with STM32 Nucleo
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.
- 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.
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
on 5 Sep 2025
Accepted Answer
More Answers (0)
Categories
Find more on STMicroelectronics Discovery Boards in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

