Hello, I'm stuck with hardfault handler.
When I try to execute read value from pointer address:
struct S2E_Packet { uint8_t module_type[3]; uint8_t fw_ver[3]; } __attribute__((packed)) S2E_Packet;
struct S2E_Packet s2e_packet;
AddressSrc=(uint32_t)&s2e_packet; uint32_t test = *(uint32_t*)AddressSrc; HAL_FLASH_Unlock(); HAL_FLASH_Program(TYPEPROGRAM_WORD, AddressDes, test);
program goes to hardfault handler. No errors or warnings during compilation.
I have no idea.....
Just make sure you align the data at an address evenly dividable by 4. Or make use of a uint8_t pointer to extract the four bytes and then combine them into a uint32_t value.
The memory controller on x86 systems is nice and hides issues with unaligned data (except for potential bandwidth loss). But lots of 32-bit processors do not have a nice memory controller that makes two 32-bit accesses to be able to combine some bytes from the first word and some bytes from the second word before handing over to the processor core.
The compiler+linker will normally align the data. When the compiler sees that data is packed, then it knows it can't trust the data to be properly aligned so it will instead produce alternative (larger/slower) code that can handle unaligned accesses. But your type cast throws away that attribute so the compiler assumes the pointer points to aligned data and is safe to use as-is.