Reputation: 11
Using HAL with two chips: STM32F373RBT6
and STM32F042K6T6
. SPI
with DMA
is set up on both.
I use the following struct in the code for each processor:
typedef __packed struct {
u16 command;
u16 status;
s32 motorPos;
} motorParams;
In debug (ST-LINK), I can see the first packet come in correctly, and then the bytes change order in the struct. All the data is there, it's just in the wrong order.
Example:
,
the command is one for motor enable, and then on the next packet:
,
the enable shows up in the position field. And finally,
,
the position is now divided up between the command and status fields.
I have checked the output on the SPI bus on the oscope, looking at the chip select and data signals. All the data is there and in the correct order going into the M0 chip.
Receiving in the M0
, the SPI has a pointer directly to a variable to receive the struct.
Is there any intermediate checks I can do to see why the data is changing location within the struct? Thank you.
Upvotes: 1
Views: 1028
Reputation: 11
Since the data was not being read in the correct order, I wondered if leftover data was interfering. I clear the FIFO by reading the DR (data register) before a receive/transmit.
That did solve it. I haven't had any problems since. I will try the byte buffer solution from ECO listed above as well, without the DR read, to see if that is a solution.
Upvotes: 0
Reputation: 371
My advice is that you should really change how you do serializations, I guess that you are assuming that because you are packing the struct and pointing it with an u8 pointer the result is going to be nicely packed in 8 bytes. This code is not portable and will be endianess dependent. Only primitive types should be serialized directly as bytes (i.e. u8, s8, u16 etc etc)
You should first define the endianess of your communication protocol, then for that struct you should create a 8 bytes buffer (or bigger if you want to send several packages at once), then copy each struct field to that buffer as bytes in your desired endianess. That buffer is the one that is sent via DMA.
Upvotes: 1