2025-09-16 2:28 AM - last edited on 2025-09-16 3:02 AM by Andrew Neil
Bug Report: CubeMX Code Generation Error - Incorrect DMA Link in HAL_XSPI_MspInit Function
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.
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.
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.
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:
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 */
}
}
Solved! Go to Solution.
2025-09-17 1:39 AM
Hello @YoungKwan
Actually, you should modify the DMA Handle in IP Structure to hdmatx instead of hdmarx.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-09-16 2:38 AM
Hello @YoungKwan
First let me thank you for posting and welcome to the ST Community.
For more investigation, I suggest that you provide your .IOC file.
Thanks.
Souhaib
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-09-16 8:17 PM
Same problem looks exist at HAL_XSPI_MspDeInit funtion as like below.
Please take look at it .
void HAL_XSPI_MspDeInit(XSPI_HandleTypeDef* hxspi)
{
if(hxspi->Instance==XSPI1)
{
/* USER CODE BEGIN XSPI1_MspDeInit 0 */
/* USER CODE END XSPI1_MspDeInit 0 */
.....
/* XSPI1 DMA DeInit */
HAL_DMA_DeInit(hxspi->hdmarx);
HAL_DMA_DeInit(hxspi->hdmarx); //<-- Is this correct too?
/* XSPI1 interrupt DeInit */
HAL_NVIC_DisableIRQ(XSPI1_IRQn);
/* USER CODE BEGIN XSPI1_MspDeInit 1 */
/* USER CODE END XSPI1_MspDeInit 1 */
}
.....
}
2025-09-16 8:20 PM
2025-09-17 1:39 AM
Hello @YoungKwan
Actually, you should modify the DMA Handle in IP Structure to hdmatx instead of hdmarx.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.