ferenc-nemeth / stm32-bootloader

UART bootloader for STM32 microcontroller.
MIT License
310 stars 94 forks source link

HardFault due to memory alignment issue when porting to STM32F0 (Cortex M0) #4

Closed lrodorigo closed 4 years ago

lrodorigo commented 4 years ago

Hi, first of all, thanks for your very nice and clean-code bootloader, without the typical 'embedded black magic'. I tried to port your work to an STM32F0 microcontroller and I discovered an insidious problem, when using ARM Compiler v5.06 for µVision (Keil IDE).

In details here: https://github.com/ferenc-nemeth/stm32-bootloader/blob/2e1a7acbb5ea8cad32c769f9cb99047387d2c291/Src/xmodem.c#L206

there is a misaligned pointer to received_data (because X_PACKET_DATA_INDEX=2), and this leads to HardFault at HAL_FLASH_Program of flash_write, when it tries to copy the value to the flash by dereferencing the pointer.

I solved the problem by receiving XMODEM header and XMODEM data+crc into two differents uint8_t arrays, one for the header and the other one for data+crc.

The modified version of xmodem_handle_packet function is available here: https://gist.github.com/lrodorigo/3b975205962e403b50c647728d4cef6f

I hope this hint will be useful for you and for other people that are trying to adapt this bootloader to other platforms.

ferenc-nemeth commented 4 years ago

Hi lrodorigo,

Thank you for your feedback. I improved my code based on your comment and sample code.

Best regards, Ferenc Nemeth