I have a question on - to me - seemingly weird behavior about copying data out of the event from the soft device.
Situation: I have a 32 bit integer characteristic which is stored in the soft device and I am processing a notification and got a ble_gatts_evt_write_t structure. Now, when I try to copy the 32 bit integer from evt_write->data I can think of two ways of C doing that:
(1) Cast the uint8_t pointer into a long pointer and then just copy the value by referencing my pointer.
long timestamp = *((long*)(evt_write->data));
// Disassembly:
adds r4, #18
ldr r4, [r4]
But this causes a hard fault! Why?
(2) Using the standard library function memcpy to copy the value into my stack variable.
This works without problems:
long timestamp = -1;
// Disassembly
movs r3, #1
rsbs r3, r3 ,#0
str r3, [sp, #4]
memcpy(×tamp, evt_write->data, sizeof(long));
// Disassembly
adds r1, r4, #0
adds r1, #18
add r0, sp, #4
movs r2, #4
bl 0x000284C0 <__aeabi_memcpy4>
My question here is - why does (2) work and (1) does not? I don't fully understand the disassembly and can only acknowledge that variant 1 obviously uses the LDR instruction whereas the other variant mainly jumps to memcpy and then some magic happens.
The compiler used here is GCC 4.9.