0x40020010 embedded config_error ai_generated true

DMA: transfer alignment error, source buffer not aligned to 4-byte boundary

ID: embedded/dma-transfer-alignment-error

Also available as: JSON · Markdown · 中文
87%Fix Rate
82%Confidence
1Evidence
2024-04-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
STM32F767 HAL 1.16.0 active
ARM GCC 12.2.1 active

Root Cause

DMA controller requires source address to be aligned to transfer size (e.g., 4 bytes for word transfer), but input buffer is misaligned due to compiler packing or stack allocation.

generic

中文

DMA控制器要求源地址与传输大小对齐(例如,字传输为4字节),但输入缓冲区因编译器打包或堆栈分配而未对齐。

Official Documentation

https://www.st.com/resource/en/reference_manual/dm00124865-stm32f7-series-reference-manual-stmicroelectronics.pdf

Workarounds

  1. 90% success Allocate source buffer with alignment attribute: uint8_t buffer[256] __attribute__((aligned(4)));
    Allocate source buffer with alignment attribute: uint8_t buffer[256] __attribute__((aligned(4)));
  2. 85% success Use memalign or posix_memalign for heap allocation: uint8_t *buf = memalign(4, 256);
    Use memalign or posix_memalign for heap allocation: uint8_t *buf = memalign(4, 256);

中文步骤

  1. Allocate source buffer with alignment attribute: uint8_t buffer[256] __attribute__((aligned(4)));
  2. Use memalign or posix_memalign for heap allocation: uint8_t *buf = memalign(4, 256);

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Adding __attribute__((aligned(4))) to variable declaration does not fix dynamic allocations; heap buffers may still be misaligned.

  2. 80% fail

    Changing DMA transfer size to byte mode works but reduces performance and may cause data corruption if hardware expects word alignment.