CubeL4 ADC HAL_ADC_MspInit and HAL_ADC_MspDeInit issue
Dear all,
I found an interesting effect using the CubeL4 and CubeMX generated files for a STM32L496 and use ADC1 and ADC3 (not simultaneously):
What we do:
- We turn the ADCs on only when we sample data, otherwise they are turned off.
- To turn the ADC off, we use HAL_ADC_DeInit which calls HAL_ADC_MspDeInit.
Issue:
HAL_ADC_MspInit and HAL_ADC_MspDeInit keep track of active ADCs through static variable HAL_RCC_ADC_CLK_ENABLED. Init increases the count, deinit decreases it. If, however, init and fails but we still call deinit the count wraps around to 0xFFFF FFFF - upon next init it wraps around to 0, but then the ADC clock is not turned on and initialization fails again. See the code snippets below:
HAL_ADC_MspInit
HAL_RCC_ADC_CLK_ENABLED++;
if(HAL_RCC_ADC_CLK_ENABLED==1){
__HAL_RCC_ADC_CLK_ENABLE();
}
HAL_ADC_MspDeInit
HAL_RCC_ADC_CLK_ENABLED--;
if(HAL_RCC_ADC_CLK_ENABLED==0){
__HAL_RCC_ADC_CLK_DISABLE();
}
Suggestion:
- Check for underflow of HAL_RCC_ADC_CLK_ENABLED in HAL_ADC_MspDeInit
- In HAL_ADC_MspInit add a check if ADC-clock is off - then turn it on -> that is what we want to do with initialization -> alternatively only check for ADC-clock and disregard the count completely.
Versions:
- I checked with CubeMX Version 6.17 and CubeL4 version 1.18.2 (latest as of today)
What do you think? Let me know if you need additional information.
Looking forward to your opinion on this case, best regards
Markus
