This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

typecast uint8_t array into uint64_t variable

I am working on STM32L432KB with Keil uVesion5 and need to write its internal flash using IAP method.
For that I use below API:
HAL_FLASH_Program (uint32_t TypeProgram, uint32_t Address, uint64_t Data)

The last argument(data) of the API accepts data of type uint64_t.
Data to be written in flash in stored in and array of type uint8_t.

While developing logic for the same, I am facing 2 issues.

1. Data is stored in uint8_t buf[500] and I want to copy its 8 consecutive bytes in uint64_t data variable.
The code for the same is:

uint8_t buf[500] = {1,2,3,4,5,6,7,8,9,0};
uint64_t data, *ptr;

ptr = (uint64_t*)&buf[0];
data = *ptr;

Here, when control reaches to the line data = *ptr;, it stuck there and never execute next line (the next line not shown here).

Can you guide me why control stuck there ?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2. Instead of typecasting the data into uint64_t, when I try typecasting it in uint16_t or uint32_t, problem discussed in point 1 is solved.
Mean the controller can now process data = *ptr line (where ptr is of type uint16_t or uint32_t)
But here, I am facing issues of endianness.
See code below for more understanding:

uint8_t buf[500] = {1,2,3,4,5,6,7,8,9,0};
uint16_t data, *ptr;

ptr = (uint16_t*)&buf[0];
data = *ptr;

here, I except data = 0x0102, but actual output is data = 0x0201.
Why swapping of bytes takes place here ? Any Idea ?
Please guide me how I can achieve excepted answer ?

0