Skip to main content
nicekwell
Associate II
March 23, 2021
Solved

MCU stuck when convert point to double

  • March 23, 2021
  • 4 replies
  • 3029 views

There is a array:

uint8_t dat[8] = {0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0x5e, 0x40};

It means a double variable data in memery. The value is 123.45.

This code can be run in my computer:

int main(int argc, char *argv[])

{

  uint8_t dat[] = {0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0x5e, 0x40};

  double test = *((double*)dat);

  printf("%.3f\r\n", test);

  return 0;

}

It will put: 123.450

But on stm32f103rct6. It will stuck when I run this:

 double test = *((double*)dat);

But use float will not stuck:

 float test = *((float*)dat);

So, how can I convert memery to double rightly on stm32f103? (I use stm32cubeide)

Thanks!

This topic has been closed for replies.
Best answer by Piranha

As Jan said, most likely the problem is the lack of proper alignment. That can be solved by making a buffer to be of a double type. Or even better by implementing a union, which forces the alignment to the largest element and also seamlessly converts the data.

4 replies

Ozone
Principal
March 23, 2021

> This code can be run in my computer: ...

> But on stm32f103rct6. It will stuck when I run this: ...

Do you know what endianess means ?

>  uint8_t dat[] = {0xcd, 0xcc, 0xcc, 0xcc, 0xcc, 0xdc, 0x5e, 0x40};

>  double test = *((double*)dat);

Are you aware of the IEEE754 ?

nicekwell
nicekwellAuthor
Associate II
March 23, 2021

I am sorry. I'm not sure what you mean.

I need transport data between PC and MCU. MCU send memery data and PC restore it to value. Also PC send memery data and MCU restore it.

I use python on PC. There is "pack" and "unpack" in python to convert between variable and memery.

On MCU, I use point to convert. And it works well. Such as:

  1. var to memery: (uint8_t*)(&var)
  2. memery to var: *((float*)dat), *((uint32_t*)dat), ......All types works well except double.
waclawek.jan
Super User
March 23, 2021

How do you know it sticks exactly on that line which performs the conversion?

What does "stuck"exactly mean? Hardfault?

Single-step in debugger disasm view and note which instruction causes it to go to hardfault if that was the case.

JW

nicekwell
nicekwellAuthor
Associate II
March 23, 2021

It is a hardfault. ​

0693W000008xdW6QAI.gif

waclawek.jan
Super User
March 23, 2021

Single step in disassembler view, to view which instruction caused the hardfault and what was the content of registers at that moment.

A wild guess, compiler used LDRD wich requires that the array be 64-bit aligned.

JW

Piranha
PiranhaBest answer
Principal III
March 24, 2021

As Jan said, most likely the problem is the lack of proper alignment. That can be solved by making a buffer to be of a double type. Or even better by implementing a union, which forces the alignment to the largest element and also seamlessly converts the data.

nicekwell
nicekwellAuthor
Associate II
March 24, 2021

Thank you! I have solved this problem by making a buffer.

0693W000008xmTpQAI.pngBut I still don't understand why.

'dat' is given by other progress. I'v printed and checked the content is right. But I'll get hardfault.

'buf' is copied from 'dat'. And it works fine.

Piranha
Principal III
March 24, 2021

Copying one uint8_t array to another array of the same type will not solve anything. You will have to learn about alignment and unions. :) But you can start with an example from there:

https://stackoverflow.com/questions/8072238/decomposition-of-double-precision-float-into-bytes-using-union-in-c