The symptom
The same pattern shows up again and again. With the data cache switched off, Ethernet works. With it switched on, the board answers a ping or two and then goes quiet, or received packets contain old data, or transmitted frames go out with the wrong contents. Nothing reports an error.
There are two separate rules behind this. Breaking either one produces the same confusing behaviour, so both have to be right.
Rule 1: the DMA must be able to reach the memory
The Ethernet peripheral moves frames with its own DMA. That DMA is a bus master, and it cannot reach every RAM in the chip. On the STM32H743 and H753 the memories are:
| Memory | Start address | Size | Note |
|---|---|---|---|
| DTCM | 0x20000000 | 128 KB | Not reachable by the Ethernet DMA, DMA1 or DMA2 |
| AXI SRAM | 0x24000000 | 512 KB | Domain D1 |
| SRAM1 | 0x30000000 | 128 KB | Domain D2 |
| SRAM2 | 0x30020000 | 128 KB | Domain D2 |
| SRAM3 | 0x30040000 | 32 KB | Domain D2 |
| SRAM4 | 0x38000000 | 64 KB | Domain D3 |
DTCM is the trap. It is the fastest RAM and some linker scripts put all variables there by default, but the Ethernet DMA cannot read or write it, and neither can the general-purpose DMA1 and DMA2 controllers. A descriptor table or frame buffer that lands in DTCM will never work. The Ethernet peripheral sits in domain D2, so the D2 SRAMs are the natural home for everything its DMA touches.
Rule 2: the CPU and the DMA must see the same data
The Cortex-M7 in the H7 has a data cache. When it is enabled, the CPU reads and writes a cached copy of memory, not the RAM itself. The DMA knows nothing about that cache. It reads and writes the RAM directly. Two things then go wrong:
- On receive, the DMA writes a new frame into RAM, but the CPU reads its old cached copy of that buffer.
- On transmit, the CPU writes a frame that is still sitting in the cache, and the DMA sends whatever was in RAM before.
There are two ways to fix it. One is cache maintenance: clean the cache before every transmit and invalidate it after every receive, on buffers aligned to the 32-byte cache line. The other is to tell the processor not to cache that memory at all. The second is simpler and harder to get wrong, and it is what I use.
Step 1: give the buffers their own section
Add a section to the linker script that places these buffers in a D2 RAM. SRAM3 is a convenient size for it.
/* In the MEMORY block: name the RAM that will hold DMA buffers. */
RAM_NOCACHE (xrw) : ORIGIN = 0x30040000, LENGTH = 32K
/* In the SECTIONS block: collect everything marked .nocache there. */
.nocache (NOLOAD) :
{
. = ALIGN(32); /* Start on a cache-line boundary. */
*(.nocache) /* Every object placed in this section. */
. = ALIGN(32); /* End on a cache-line boundary as well. */
} >RAM_NOCACHE
Then mark each buffer the DMA uses, so the linker puts it there:
/** @brief Receive buffer written by the Ethernet DMA.
* Placed in the non-cacheable region and aligned to a cache line. */
static uint8_t eg_eth_rx_buffer[EG_ETH_RX_BUFFER_SIZE]
__attribute__((section(".nocache"), aligned(32)));
The descriptor tables and the pool that LwIP's receive buffers come from need the same treatment. The rule is simple: any memory whose address is handed to the DMA belongs in this section.
Step 2: make the region non-cacheable
The memory protection unit sets the cache behaviour for a range of addresses. This marks the whole of SRAM3 as normal memory that is never cached.
/**
* @brief Mark SRAM3 as non-cacheable so the CPU and DMA share one view of it.
* @note Call once at start-up, before the data cache is enabled and before
* the Ethernet peripheral is initialised.
*/
static void eg_mpu_config_nocache(void)
{
MPU_Region_InitTypeDef region = {0}; /* Start with every field cleared. */
HAL_MPU_Disable(); /* The MPU must be off while a region is changed. */
region.Enable = MPU_REGION_ENABLE; /* This region is active. */
region.Number = MPU_REGION_NUMBER0; /* Use the first region slot. */
region.BaseAddress = 0x30040000UL; /* Start of SRAM3. */
region.Size = MPU_REGION_SIZE_32KB; /* The whole of SRAM3. */
region.AccessPermission = MPU_REGION_FULL_ACCESS; /* Read and write allowed. */
region.TypeExtField = MPU_TEX_LEVEL1; /* With the two flags below: normal memory... */
region.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; /* ...that is never cached... */
region.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; /* ...and never write-buffered. */
region.IsShareable = MPU_ACCESS_NOT_SHAREABLE; /* Single core, so not shared. */
region.SubRegionDisable = 0x00U; /* All eight sub-regions enabled. */
region.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE; /* Data only: no code runs from here. */
HAL_MPU_ConfigRegion(®ion); /* Write the settings to the MPU. */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT); /* MPU on; other memory keeps its default map. */
}
An MPU region has to be a power of two in size, and its base address has to be a multiple of that size. A 32 KB region at 0x30040000 meets both.
Checking the result
- Open the map file and confirm the descriptors and buffers have addresses inside the region you configured. This is the check that finds a buffer still sitting in DTCM.
- Confirm the MPU function runs before the data cache is enabled.
- Run a long ping with a large payload, not a single ping. A cache problem often lets the first few frames through.
The same rule for every DMA
None of this is special to Ethernet. An ADC, a UART or an SPI that uses DMA on an H7 with the data cache on has the same two requirements: memory the DMA can reach, and one view of it shared by the CPU and the DMA. Ethernet is only where most people meet the problem first.