STM32H7 DMA Buffers in DTCM: Why Transfers Fail and How to Fix the Linker Script
You enable DMA on an STM32H7 and nothing arrives: the receive buffer stays empty, a UART or SPI transfer ends with HAL_DMA_ERROR_TE, or an ADC reports overruns. The same code worked on an F4. In many cases the DMA configuration is fine and the buffer simply lives in DTCM, a memory that DMA1 and DMA2 cannot reach. This article shows how to recognise it, prove it in thirty seconds, and fix it in the linker script without breaking cache coherency.
The symptom
The exact failure depends on the peripheral, which is why it takes so long to find. Typical reports on the ST Community for DMA1/DMA2 on STM32H7 are:
- A HAL
*_DMAcall returnsHAL_OK, the DMA interrupt fires, but the data never appears on the wire or in the buffer. - The error callback runs with the DMA error code
HAL_DMA_ERROR_TE(transfer error). - An ADC with DMA keeps raising overrun errors, because the DMA stream is not draining the data register.
- A bare-metal stream stops on its own: the
ENbit inDMA_SxCRreads 0 after you set it, and the transfer-error flag for that stream is set inDMA_LISR/DMA_HISR.
What they share: the project builds, the peripheral configuration looks identical to a working F4 project, and the failure is deterministic. The first thing to look at is not the DMA registers but the address of the buffer.
Root cause: DMA1/DMA2 are in D2, and DTCM is private to the core
STM32H7 (the dual-domain parts such as STM32H743/H753, RM0433) has several RAMs with different owners. ST's FAQ "DMA is not working on STM32H7 devices" summarises which DMA can reach what:
| Controller | Domain | Can access |
|---|---|---|
| DMA1, DMA2 | D2 | Almost everything except ITCM and DTCM |
| BDMA | D3 | Only SRAM4 and backup SRAM |
| MDMA | D1 | All memories, including ITCM and DTCM |
For the STM32H743 the memory map is, per the datasheet and RM0433:
| Memory | Address | Size | DMA1/DMA2 |
|---|---|---|---|
| ITCM RAM | 0x00000000 | 64 KB | No |
| DTCM RAM | 0x20000000 | 128 KB | No |
| AXI SRAM (D1) | 0x24000000 | 512 KB | Yes |
| SRAM1/2/3 (D2) | 0x30000000 | 128 + 128 + 32 KB | Yes (preferred) |
| SRAM4 (D3) | 0x38000000 | 64 KB | BDMA's memory |
The TCMs are connected directly to the Cortex-M7 core, not to the bus matrix that the D2 DMA masters use. A DMA access to 0x2000xxxx therefore is not routed to a RAM the DMA can see and ends in a bus error, which the DMA reports as a transfer error (as reported in the ST Community threads below). HAL does not check buffer addresses, so HAL_UART_Transmit_DMA() happily accepts a pointer the controller cannot use.
Other H7 variants (single-core H72x/H73x with RM0468, H7S, dual-core H745) have different memory maps and different bus-master tables. ST Community threads even point out an error in the RM0468 system architecture table, so for any part other than H743/H753 check the bus-matrix table in your reference manual rather than copying addresses from this article.
Why your buffer ends up in DTCM
You never wrote "put this in DTCM". The linker did it for you. ST's FAQ lists "memory placed in DTCM RAM" as a common cause because DTCM is often the default RAM in generated projects and examples. Check your own .ld: if .data, .bss and the heap/stack section end with >DTCMRAM (the region name in ST-generated scripts), every object below is in DTCM:
- Global and static buffers (
.bss/.data), the obvious case. - Local arrays on the stack passed to a
_DMA()function. The stack is in DTCM too, so a local buffer fails exactly like a global one. malloc()memory, because the heap sits in the same region.- The FreeRTOS heap. With
heap_4the heap is a static array (ucHeap), so it lands in.bssunless you place it elsewhere. Anything frompvPortMalloc(), including queues and task stacks, is then in DTCM. - Library-owned buffers, for example descriptors or packet buffers inside a USB or Ethernet stack, depending on how the middleware declares them.
Confirm it in thirty seconds
Do not read DMA registers first. Print or watch the address.
- In the debugger, evaluate the buffer pointer you pass to HAL. If it starts with
0x2000(or0x0000), you are done. - Or ask the toolchain:
arm-none-eabi-nm -n firmware.elf | grep -i dmaand look at the addresses of your buffers, or open the.mapfile. - Confirm on the hardware: with the stream enabled, read
DMA_SxM0AR. If it holds a 0x2000xxxx value and the transfer-error flag is set, the diagnosis is certain.
A compile-time-independent guard is a small run-time check you call in every _DMA() wrapper. This one is specific to H743-class parts and lists only the RAMs DMA1/DMA2 can use; adjust the ranges to your device:
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
/* STM32H743: AXI SRAM (D1) and SRAM1..3 (D2), per RM0433 memory map.
DTCM (0x20000000) and ITCM (0x00000000) are deliberately excluded. */
static bool dma12_can_reach(const void *p, size_t len)
{
uintptr_t a = (uintptr_t)p;
uintptr_t e = a + len; /* one past the end */
if (len == 0U) return false;
if (a >= 0x24000000UL && e <= 0x24080000UL) return true; /* AXI SRAM 512 KB */
if (a >= 0x30000000UL && e <= 0x30048000UL) return true; /* SRAM1+2+3 288 KB */
return false;
}
#define DMA_BUF_CHECK(p, n) do { if (!dma12_can_reach((p), (n))) { __BKPT(0); } } while (0)
The sizes add up as follows: 0x24000000 + 0x80000 (512 KB) = 0x24080000; SRAM1 + SRAM2 + SRAM3 = 128 + 128 + 32 = 288 KB = 0x48000, so the end is 0x30048000. This assumes the three D2 SRAMs are contiguous as in the H743 map.
AXI SRAM is cacheable. As soon as the buffer is outside DTCM, the D-cache comes into play and you can swap one invisible failure for another (stale reads, lost writes). Apply the cache maintenance or MPU rules from STM32H7 D-Cache Coherency as well.
The fix: a dedicated DMA section in D2 RAM
The least invasive fix, also the one ST describes, is a separate output section mapped to a DMA-reachable region and a section attribute on the buffers that need it. In the linker script, inside SECTIONS, with the region names of the ST-generated script (RAM_D2 is SRAM1..3 at 0x30000000; check that your script defines it):
.dma_buffer (NOLOAD) :
{
. = ALIGN(32);
*(.dma_buffer)
*(.dma_buffer*)
. = ALIGN(32);
} >RAM_D2
NOLOAD keeps the section out of the flash image: it is not initialised and not zeroed by the standard startup code, which only handles .data and .bss. Treat these buffers as uninitialised memory. The 32-byte alignment is the Cortex-M7 cache line size, needed for the maintenance operations below.
In C:
#define DMA_BUFFER __attribute__((section(".dma_buffer"), aligned(32)))
DMA_BUFFER static uint8_t uart_rx[256]; /* 256 is a multiple of 32 */
void start_rx(UART_HandleTypeDef *huart)
{
DMA_BUF_CHECK(uart_rx, sizeof uart_rx);
HAL_UART_Receive_DMA(huart, uart_rx, sizeof uart_rx);
}
Keep the size a multiple of the cache line, otherwise a neighbouring variable shares the line with the DMA buffer and invalidation can discard its data.
Variant: move the whole RAM
If your application does not use the D-cache, or you accept the cost, you can replace DTCMRAM with RAM_D1 in the > target of .data, .bss and the heap/stack section. ST's FAQ describes this. Everything, including stack and heap, is then DMA-reachable, but you lose DTCM's zero-wait-state determinism for the stack and hot variables. If the initial stack pointer is defined by a separate _estack symbol, it has to move with the stack section. I prefer the dedicated section, because the fast memory stays where the CPU needs it.
Variant: the heap and RTOS objects
- FreeRTOS heap: put the
ucHeaparray into a section in a DMA-reachable region, or useheap_5withvPortDefineHeapRegions()and give it only D1/D2 RAM. Hand-placing individual DMA buffers is cleaner than allocating them dynamically. - Stack arrays: never pass a local array to a
_DMA()function on H7. It is also unsafe on other MCUs, because the frame is gone when the function returns.
Variant: use MDMA
MDMA reaches DTCM, and it can be chained with DMA1/DMA2, but ST Community answers describe this as complicated. I would use it only when DTCM placement is a hard requirement.
Check that the destination RAM is clocked
The D2 SRAMs have their own clock enables in the RCC AHB2 enable register (RM0433, RCC section; the HAL macro names, __HAL_RCC_D2SRAM1_CLK_ENABLE() or __HAL_RCC_SRAM1_CLK_ENABLE(), depend on the HAL version, so grep your stm32h7xx_hal_rcc_ex.h). If the clock is off, accesses to the section do not behave as RAM. Enable the clocks early, before any D2 RAM is touched. Be especially careful if you also put .bss or .data in D2 RAM, because the startup code zeroes .bss before main(). I could not verify the reset state and clock requirements for every H7 part, so confirm this for your device.
Verify and guard against regression
- After the change, the map file must show
.dma_bufferat 0x3000xxxx and the buffers inside it. - Keep
DMA_BUF_CHECK()in every DMA entry point in debug builds. It breaks at the offending call site instead of leaving you to find a transfer error later. - In the DMA error callback, log
HAL_DMA_GetError(hdma).HAL_DMA_ERROR_TEwith a 0x2000xxxx address inM0ARis now a self-explaining message. - A CI step is cheap:
arm-none-eabi-nmthe ELF and fail if any symbol whose name matches your DMA-buffer naming convention (*_dma_*,*dma_buf*) has an address in 0x2000xxxx. - A transfer error can also come from a transfer size that does not match the data width, so if the buffer is correctly placed and TEIF persists, check
NDTRagainst the data size.
Practical checklist
- On H7, print the buffer address before debugging any DMA register.
- DMA1/DMA2 cannot access ITCM or DTCM (0x00000000, 0x20000000); BDMA reaches only SRAM4 and backup SRAM; MDMA reaches everything.
- Check where the linker script puts
.data,.bss, heap and stack. - Check stack arrays,
malloc()and the RTOS heap, not just globals. - Use a dedicated aligned,
NOLOADsection in D2 SRAM for DMA buffers and treat it as uninitialised. - Align and size DMA buffers to 32 bytes, then handle D-cache maintenance or an MPU non-cacheable region.
- Make sure the D2 SRAM clocks are enabled.
- Add a run-time address assert and a CI check on the ELF.
- For any H7 part other than H743/H753, read the bus-matrix table of your own reference manual.
How I would approach this on a client project
On a new H7 board I look at the linker script and the map file before writing any peripheral code, and I decide on a memory plan: what goes in DTCM (stack, hot state, RTOS kernel objects), what goes in AXI SRAM (large buffers, framebuffers), what goes in D2 SRAM (DMA buffers), and which regions are non-cacheable through the MPU. I put that plan in the repository next to the linker script, so that the next engineer does not have to rediscover it. I add the address assert in the DMA wrappers from the start, since the cost is a few instructions in debug builds, and I make the CI fail when a DMA buffer symbol lands in the wrong region. If the project is already showing mysterious DMA failures, checking the buffer addresses is the first thing I do, before touching the peripheral configuration.
Sources
- ST Community, DMA is not working on STM32H7 devices (FAQ: DMA1/DMA2/BDMA/MDMA memory access, linker script options,
.dma_buffersection). - ST Community, The most probable reason to have an issue with DMA or BDMA.
- ST Community, Using DMA with UART on STM32H753ZI and getting HAL_DMA_ERROR_TE (buffer at 0x20000000, DTCM not reachable).
- ST Community, STM32H723/735 RM0468 system architecture table wrong (why to verify the bus-matrix table per device).
- ST, RM0433 STM32H742, STM32H743/753 and STM32H750 Value line advanced Arm-based 32-bit MCUs: memory map, system architecture (bus matrix), DMA and RCC chapters.
- ST, AN4891 STM32H7x3 system architecture (cited by the ST FAQ; check the revision for your device).
- Related on this blog: STM32H7 D-Cache Coherency, STM32 MPU configuration.
Reply to this article by email — I read and respond to every message.