We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi. I know that Cortex M0 memory transfers must be aligned.
Knowing that, I'm still not sure if the code below is safe? It compiles and it works, but I'm not sure if I'm just lucky?
unsigned int MCUSerialNumber; unsigned char data[8] = {0}; *((unsigned int *)&data[0]) = MCUSerialNumber;
Is the code above identical in every way to this one?
data[0] = MCUSerialNumber & 0xFF; data[1] = (MCUSerialNumber >> 8) & 0xFF; data[2] = (MCUSerialNumber >> 16) & 0xFF; data[3] = (MCUSerialNumber >> 24) & 0xFF;
The M0, like the ARM7/9, can be intolerant of reads/writes of 32-bit words to unaligned addresses, as are a number of MIPS implementations. The use of memcpy() is the portable way to do this. The endian-ness of the core will also impact byte order.