cancel
Showing results for 
Search instead for 
Did you mean: 

Communication between two stm32 boards

CircuitFreak
Associate III

 

Hello Everyone,

I'm reaching out for some guidance on a project that I've been working on. Despite considerable effort, I'm still struggling to find an effective solution.

My project involves developing a controller design to manage different models on each STM32 microcontroller. The goal is to synchronize these controllers. I have successfully built the models, and they are functioning well. The key task is for each board to transmit data to another board. This data is crucial for calculations, leading to the generation of new data. This process of data exchange needs to be continuous. The image below shows how the each stm32 work. 

CircuitFreak_1-1706636236779.png

Here's the challenge: I'm having difficulties with the data transmission and reception between the boards. The data, which is in double format.

I would greatly appreciate any advice or suggestions on this matter, as I'm starting to run low on ideas.

Thank you in advance!

 

P.S. This is the code i am using for both boards. Just to check if i can transmit and receive at the same time. 

CircuitFreak_0-1706646591206.png

 

19 REPLIES 19

Use circular buffers to transmit pending data, and receive it.

Use a protocol like SLIP, which is self synchronizing, so that you can send packets back and forth. Feed whole packets to your processing loop.

Have some method to track if you've lost packets, and either request new copies, or have some recovery scheme to rebroadcast or heart-beat, should the link go down or lose bytes/synchronization.

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

@CircuitFreak wrote:

i not sure where "..." is. .  


It's here:

AndrewNeil_2-1706693574443.png

After you press it, you get an extra set of icons - including the one for posting source code:

AndrewNeil_3-1706693621074.png

 

Andrew Neil
Evangelist III

@CircuitFreak wrote:

P.S. This is the code i am using for both boards. Just to check if i can transmit and receive at the same time. 

CircuitFreak_0-1706646591206.png


The fundamental problem there is that you have no synchronisation between your sender & receiver.

You just arbitrarily send - without knowing that the other end is ready to receive!

As @Tesla DeLorean said, you need some sort of protocol to ensure that the receiver has received all that the sender sent - nothing more, and nothing less, and without errors.

Thanks, Yes i am quite certain that is the issue. I dont know how to overcome this? Is there any techniques i could used? 

You still haven't shown any diagram. With the two SPI devices, you need to have a ground reference, again without a diagram, we don't know if you wired it correctly.

 

With SPI bus, technically one of your boards needs to be configured as the Master and the other needs to be the Slave. The slave needs to be able to signal the Master there is data available to be sent to it. When the Master gets a signal (GPIO interrupt), the Master can clock the data from the slave to the master. 

 

Without this signal, you can have the Master still clock data from the slave, but the slave may not have any valid data at that time. You could essentially get away without a signal from the slave but then you're now getting into writing a protocol, usually with a size/checksum to know you have valid packet. 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

@CircuitFreak wrote:

 Is there any techniques i could used? 


As @Tesla DeLorean said, you need some sort of protocol to ensure that the receiver has received all that the sender sent - nothing more, and nothing less, and without errors.


@Karl Yamashita wrote:

The slave needs to be able to signal the Master there is data available to be sent to it.  


Or, conversely, needs to be able to signal that it has no data to send.

 

Cool, Thanks for the help. I think we have find a solution. i did this. 

		SPITxCompleteFlag=0;
		HAL_SPI_Transmit_IT(&hspi1, TX_Data, sizeof(TX_Data));

		while(SPITxCompleteFlag !=1){

		}

		HAL_SPI_Receive(&hspi1, RX_Data, sizeof(RX_Data),100);


	void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
		if (hspi->Instance == hspi1) {
			SPITxCompleteFlag = 1;  // Set the flag
		}
	}

Which seems to work. 

There's no point in using HAL_SPI_Transmit_IT() and then immediately doing a busy-wait:  you might as well just use HAL_SPI_Transmit() - which includes the wait.

Also remember that SPI is inherently full-duplex - that's wasted if you do a transmit and then a separate receive.

Yeah I agree.  I don't know why this works and the other doesn't.