STM32 Lab by RedCan

DMA and the data cache on STM32H7: why your buffer holds “stale” data and how to fix it

5 min read

Illustration: dma cache embedded electronics workbench

An STM32H7 project works – until someone enables the D-cache to speed up the maths. Then the DMA-driven UART starts receiving zeros, SPI sends the previous frame and Ethernet drops packets. It is not a hardware bug but a lack of coherency between the core’s cache and DMA – described in ST’s AN4839. Here is how we deal with it.

Where the problem comes from

The Cortex-M7 core in STM32F7 and H7 has a level 1 data cache with 32-byte lines. The default policy for SRAM is write-back: a CPU write lands in the cache first and reaches RAM only when the line is evicted. DMA, on the other hand, reads and writes RAM directly, bypassing the cache. As a result:

  • transmit (memory → peripheral): the CPU prepared the data, but it is still in the cache – DMA sends the old RAM contents;
  • receive (peripheral → memory): DMA wrote new data to RAM, but the CPU reads the line from the cache and sees old values.

The symptoms depend on what happens to be in the cache, so the bug is intermittent and disappears when you add a printf or stop in the debugger.

Fix 1: cache maintenance operations

CMSIS provides functions that operate on an address range. The rule is simple:

  • before a DMA transmit – clean (write cache lines back to RAM): SCB_CleanDCache_by_Addr();
  • after a DMA receive – invalidate (discard lines so the CPU reads RAM): SCB_InvalidateDCache_by_Addr().
/* buffer aligned to a cache line, size a multiple of the line */
ALIGN_32BYTES(static uint8_t tx_buf[256]);
ALIGN_32BYTES(static uint8_t rx_buf[256]);

/* transmit */
prepare_frame(tx_buf);
SCB_CleanDCache_by_Addr((uint32_t *)tx_buf, sizeof(tx_buf));
HAL_SPI_Transmit_DMA(&hspi1, tx_buf, sizeof(tx_buf));

/* receive: before starting and after the transfer completes */
SCB_InvalidateDCache_by_Addr((uint32_t *)rx_buf, sizeof(rx_buf));
HAL_UART_Receive_DMA(&huart1, rx_buf, sizeof(rx_buf));
/* ... in the receive-complete callback: */
SCB_InvalidateDCache_by_Addr((uint32_t *)rx_buf, sizeof(rx_buf));

Two things decide whether this works:

  • 32-byte alignment. The operations act on whole lines. If the buffer shares a line with another variable, an invalidate also discards that variable’s unwritten changes – and a bug appears somewhere completely different. So we align DMA buffers (the HAL ALIGN_32BYTES macro or __attribute__((aligned(32)))) and round their size up to a multiple of 32 B.
  • Invalidate after the transfer completes. The Cortex-M7 can speculatively load data into the cache, including while DMA is running. Invalidating only before the start is not enough – we do it again when the receive completes. With circular DMA we invalidate half the buffer in the half-complete and complete callbacks.

Fix 2: a non-cacheable MPU region

For heavily used buffers (Ethernet, USB, audio) it is more convenient to disable caching for a dedicated RAM area through the MPU (AN4838). The region is configured as Normal, non-cacheable (TEX = 001, C = 0, B = 0), and the buffers are placed in it with a dedicated linker section:

MPU_Region_InitTypeDef r = {0};
HAL_MPU_Disable();
r.Enable           = MPU_REGION_ENABLE;
r.Number           = MPU_REGION_NUMBER1;
r.BaseAddress      = 0x30000000;          /* D2-domain SRAM (STM32H743) */
r.Size             = MPU_REGION_SIZE_32KB;
r.TypeExtField     = MPU_TEX_LEVEL1;
r.IsCacheable      = MPU_ACCESS_NOT_CACHEABLE;
r.IsBufferable     = MPU_ACCESS_NOT_BUFFERABLE;
r.IsShareable      = MPU_ACCESS_NOT_SHAREABLE;
r.AccessPermission = MPU_REGION_FULL_ACCESS;
r.DisableExec      = MPU_INSTRUCTION_ACCESS_DISABLE;
HAL_MPU_ConfigRegion(&r);
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);

/* buffer in a section mapped to 0x30000000 in the linker script */
__attribute__((section(".dma_buffer"))) static uint8_t eth_buf[1536];

An MPU region’s size must be a power of two and its base address aligned to that size. CPU access to such an area is slower, so only DMA buffers go there, not ordinary variables.

A third option is a write-through policy for the region: it solves the transmit side, but receive still needs an invalidate.

The STM32H7 trap: DTCM

On STM32H743/753 (and related parts) the DMA1 and DMA2 controllers cannot access DTCM (0x2000 0000), and the BDMA in the D3 domain can reach only SRAM4. Yet many projects’ linker scripts put .data, .bss and the stack in DTCM because it is the fastest. A buffer declared as an ordinary global then lands in DTCM, and the DMA transfer errors out or moves no data – regardless of cache settings. We place DMA buffers in AXI SRAM (0x2400 0000) or SRAM1–3 in the D2 domain (0x3000 0000). The memory map differs between H7 lines (e.g. H7A3/B0, H72x/73x), so always check the reference manual of the exact device.

On STM32F7, DTCM is reachable by DMA but is not cached – buffers in DTCM need no clean/invalidate.

RedCan checklist: DMA with the D-cache enabled

  • Memory map: which DMA controllers can reach which RAM areas.
  • On STM32H7, DMA buffers outside DTCM, in an area the given DMA can reach.
  • Buffers aligned to 32 B, size rounded up to a multiple of 32 B.
  • Clean before transmit, invalidate before and after receive (half/complete in circular mode).
  • Alternatively: a non-cacheable MPU region and a linker section for DMA buffers.
  • Background MPU configuration as recommended in AN4838 (protection from speculative accesses).
  • Testing with I-cache and D-cache enabled, at full compiler optimisation.

Related reading: bootloader and firmware updates and STM32 board bring-up.

Further reading

We develop firmware for high-performance STM32H7 microcontrollers as part of STM32 and embedded software development.

Written by the RedCan engineering team – we design electronics, PCBs and firmware on STM32 for device manufacturers.

Building a device on STM32?

We can help with microcontroller selection, schematic and PCB, prototype bring-up, firmware and tests.

STM32 device design →
Discuss your STM32 project

STM32 is a registered trademark of STMicroelectronics International N.V. or its affiliates in the EU and/or elsewhere. STM32 Lab by RedCan is an independent series written by RedCan engineers.