This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

transfer a “32bit float” variable through SPI

Hi,

I need to transfer many "32 bit float" variables between two DSPs through SPI.

However, the register is alaways 16bit that I need to split the "32bit float" variable into two parts.

I used the C type union like

union mytype {
  float a;
  uint16_t b[2];
};

It works well but I get the new problem that it is hard to know whether the received 16bit data is for b[0] or b[1] at startup. (The two DSP may power on at different time, so the 1st data may not always be b[0]).

I tried to split the 32bit float into 4 bytes, and when I transfer the 16bit, I add a tag before the 8bit data. That also works perfect. But I got performance slow down doubled.

So what I want is to split a float into two 16-bit chunks, each of them also carrying information about if it's the first chunk or the second. Or use some pointer? Can someone help me?

Thanks in advance.

  • Hi Lebing,

    Float requires 32 bits. So when you send 32 bits there is no way to encode any additional information. You can add synchronization in hardware or in software. As you already figured out doing synchronization in software cost you at least one extra packet (16 bits). Doing it in hardware will require son changes in hardware You need at least one GPIO, which would trigger an interrupt in which you would enable a SPI, thus you would always start listening at the beginning.

    But the simplest solution by far is to convert float to 16 bit integer (fractional notation Q style) 16 bit integers still have quite good resolution for most task.

    Regards, Mitja

  • Hi Mitja,

    Thank you so much for your reply.

    If I want to implement the synchronization in software, do I need to do this ' hand shake' only once when the program starts, then all the data can be transferred and received in sequence? Or I need to check the received data every time, as I'm doing now (split the 32-bit data into 4 8-bit parts and add a tag at the 9th and 10th bits)? The thing is I'm not using the SPI interrupt to trigger data transmission, I just transmit data in the main program. If I do synchronization only once at the beginning, can it make sure all the data transmission is in sequence later on?

    Besides, by mentioning the simplest solution, do you mean just neglecting the fractional part?

    Looking forward to your reply.

    Regards,

    Lebing