2024-01-30 09:47 AM - edited 2024-01-30 12:30 PM
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.
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.
2024-01-30 01:12 PM - edited 2024-01-30 01:13 PM
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.
2024-01-31 01:34 AM
@CircuitFreak wrote:i not sure where "..." is. .
It's here:
After you press it, you get an extra set of icons - including the one for posting source code:
2024-01-31 01:39 AM
@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.
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.
2024-01-31 01:38 PM
Thanks, Yes i am quite certain that is the issue. I dont know how to overcome this? Is there any techniques i could used?
2024-01-31 02:38 PM
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.
2024-01-31 02:50 PM
@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.
2024-01-31 02:52 PM
@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.
2024-01-31 03:17 PM
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.
2024-01-31 03:24 PM - edited 2024-01-31 03:24 PM
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.
2024-01-31 03:28 PM
Yeah I agree. I don't know why this works and the other doesn't.