Visitor II
November 22, 2022
Solved
Possible erroneous CubeMx code generation
- November 22, 2022
- 1 reply
- 1050 views
I noticed that CubeMx (version 6.6.1 and earlier) when generating the code, may place the MX_DMA_Init() function after other ***_init functions that configure DMA registers. The problem is that those registers cannot be accessed 'before' enabling the DMA clock. For example here is a snippet of erroneous code:
MX_ADC1_Init();
MX_ADC3_Init();
MX_I2C2_Init();
MX_I2C3_Init();
MX_SPI1_Init();
MX_SPI2_Init();
MX_DMA_Init();In the example above the ADC1 and ADC3 peripheral use the DMA mode, but the DMA is not initialized yet. In attachment there is the whole main.c file containing the above snippet.
A possible workaround could be to impose the clock initialization before any other initialization:
/* USER CODE BEGIN SysInit */
/* Workaround for a CubeMx bug */
__HAL_RCC_DMA2_CLK_ENABLE();
/* USER CODE END SysInit */