embedded runtime_error ai_generated true

HardFault: Unaligned flash write at address 0xNNNNNNNN

ID: embedded/flash-write-alignment

Also available as: JSON · Markdown
85%Fix Rate
84%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

Flash memory write failed due to incorrect alignment (most MCUs require word-aligned writes).

generic

Workarounds

  1. 90% success Ensure write address is aligned to flash word size
    addr & ~(FLASH_WORD_SIZE-1) for alignment check
  2. 85% success Use HAL flash programming functions that handle alignment
    HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, aligned_addr, data)

Dead Ends

Common approaches that don't work:

  1. Write individual bytes to flash 88% fail

    Most flash controllers require 32-bit or 64-bit aligned writes.

  2. Cast any pointer to flash write address 85% fail

    Misaligned address causes HardFault on Cortex-M.