cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly use SPI communication in STM32L053 ??

almog.itzko
Associate II

Hello.

I am working on a project and using STM32L053 board.

For now, my goal is to create a channel of SPI communication, master & slave, an to test my code.

I am using only 1 ST board which serves both as master and slave. 

I have defined all necessary GPIOs using the CubeMX program - miso, mosi, clk, (software nss).

As part of the requirements i need to use the DMA in order to transmitte and receive date.

Each SPI has it's own Rx & Tx buffers which are connected to the relevant channel of the DMA.

The problem:

I have been able to send data only in one direction: Master --> Slave - I load the Tx buffer of the master and send it to the slave. The slave receives it properly into it's Rx buffer. I also see the DMA channels do what they are suppose to do.

The other way around does not seem to work. No matter what i have tried, the master wont get back a reply for the slave.

Any ideas to what should i pay more attention or check in my code ??

5 REPLIES 5

The master has to shift data out, or otherwise generate a clock, for the slave to be able to respond.

This is often achieved by sending dummy data.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
almog.itzko
Associate II

Thank you for your answer.

Of course i have tried doing that. My current test is sending a few messages one after the other, one is valid and the other is a dummy.

The data does not shift back to the master in this case.

At first i thought that my DMA initialization was wrong but i went over my parameters many times and they are ok.

Do you see the data coming from slave on the MISO line when observed by an oscilloscope/logic analyzer?

Do you see those data in the data register of SPI in a debugger? (But don't look at SPI registers by debugger when you want the DMA to work).

JW

almog.itzko
Associate II

No, i cant see the MISO line when observed by an oscilloscope.

When i send data from master to slave, at the end of the transmission i can see inside the slave's DR the last char that was sent.

almog.itzko
Associate II

I think i found the problem.

I seems that the master's Rx and slave's Tx should always be enabled during the entire process.

Previously i have enabled them in different places in my code, now i enabling the relevant DMA channels in the beginning, where i initialize all DMA parameters and never touch them again.

Thank you