CubeMX Generates Incorrect DMA Link in HAL_XSPI_MspInit Function
Bug Report: CubeMX Code Generation Error - Incorrect DMA Link in HAL_XSPI_MspInit Function
- Environment
The software environment is STM32CubeIDE
Version: 1.19.0 Build: 25607_20250703_0907 (UTC)
and the FW used is STM32CubeH7RS Firmware Package V1.2.0 / 05-February-2025.
- Problem Description
When CubeMX generates the
HAL_XSPI_MspInit
function, it incorrectly links
handle_HPDMA1_Channel0
, which is the transmit (TX) DMA channel, to the receive (RX) pointer
hdmarx
of the XSPI handle. This causes manually modified code to be overwritten with the incorrect code every time the
.ioc
file is saved and the code is regenerated.
- Incorrectly Generated Code:
__HAL_LINKDMA(hxspi, hdmarx, handle_HPDMA1_Channel0); - Correct Code:
__HAL_LINKDMA(hxspi, hdmatx, handle_HPDMA1_Channel0);
- Cause
There is an error in the CubeMX code generation logic that incorrectly links the TX DMA channel to the RX pointer within the
HAL_XSPI_MspInit
function.
- Solution
Utilize
USER CODE
blocks to protect manual modifications. By placing the correct code within
USER CODE
blocks, CubeMX will not overwrite the contents of these blocks during code regeneration.
Cleanest Modification Method:
- Open the
stm32h7rsxx_hal_msp.c
file. - Find the
hxspi->Instance==XSPI2
block within the
HAL_XSPI_MspInit
function.
Leave the code generated by CubeMX as is, and add the following correct link code inside the
/* USER CODE BEGIN XSPI2_MspInit 1 */
block:
/* stm32h7rsxx_hal_msp.c */
void HAL_XSPI_MspInit(XSPI_HandleTypeDef* hxspi)
{
// ... (XSPI2 MspInit top part code) ...
else if(hxspi->Instance==XSPI2)
{
/* USER CODE BEGIN XSPI2_MspInit 0 */
/* USER CODE END XSPI2_MspInit 0 */
// ... (CubeMX generated clock, GPIO, DMA Channel 1 (RX) initialization code) ...
// ...
__HAL_LINKDMA(hxspi, hdmarx, handle_HPDMA1_Channel1);
// ...
// ... (CubeMX generated DMA Channel 0 (TX) initialization code) ...
if (HAL_DMA_Init(&handle_HPDMA1_Channel0) != HAL_OK)
{
Error_Handler();
}
// ▼▼▼ CubeMX generated buggy code (leave as is) ▼▼▼
__HAL_LINKDMA(hxspi, hdmarx, handle_HPDMA1_Channel0);
if (HAL_DMA_ConfigChannelAttributes(&handle_HPDMA1_Channel0, DMA_CHANNEL_NPRIV) != HAL_OK)
{
Error_Handler();
}
/* XSPI2 interrupt Init */
HAL_NVIC_SetPriority(XSPI2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(XSPI2_IRQn);
/* USER CODE BEGIN XSPI2_MspInit 1 */
/* ==================== Final Solution Code ==================== */
// Overwrite the incorrectly linked hdmarx with the correct hdmatx.
// This code will be preserved even upon regeneration.
__HAL_LINKDMA(hxspi, hdmatx, handle_HPDMA1_Channel0);
/* ========================================================== */
/* USER CODE END XSPI2_MspInit 1 */
}
}

