2025-08-14 3:03 PM
I'm doing some CAN messaging between the 2 CANs of F105 where I'm forwarding the message from one to the other but perform some modifications.
Right now I set a flag in the FIFO callback functions that a message was received and the in the main while() I perform the actual work. Question is, is it safe to simply modify the original data param from HAL_CAN_GetRxMessage and then pass it to HAL_CAN_AddTxMessage or do I need to create a copy of the data and modify/pass that to HAL_CAN_AddTxMessage.
Modifying the original data param works in development, but is it safe in real world application?
2025-08-14 3:19 PM
You have all the source, check it
aData and pHeader are ram structures you provide, so will have whatever scope you choose for them to have.
2025-08-15 2:37 AM
Hello @pulsar
In context of interrupts or multiple threads, you need to ensure that the buffer is not being accessed simultaneously from different contexts (e.g., CAN receive interrupt overwriting the buffer before transmission is complete).
2025-08-15 8:49 AM
Yes, that's basically the reason for my post, things getting overwritten before sent is complete.
Now, if I create a copy of the receiving buffer in the CAN FIFO callback, and use the copy in the main loop to send modified message, isn't there still the possibility a new message arrives and ends up overwriting the buffer copy before main loop message sent is complete?
2025-08-15 10:46 AM
If you use a common/shared global buffer, probably yes unless you guard it, the Rx function on the HAL I looked at pulled the data into a buffer you provide. And no longer in the FIFO.
You probably want to use a QUEUE if there's a chance of getting multiple messages prior to you committing the data back into the peripheral for transmission.