Search code examples
stm32stm32cubemx

In STM32 CubeMX generated code what does ADC_IRQn refer to in the user code section comments?


Inside the [devicename]_it.c file generated by Cube MX there are multiple user code sections inside each IRQ handler such as below for the ADC_IRQHandler

void ADC_IRQHandler(void)
{
   /* USER CODE BEGIN ADC_IRQn 0 */

   /* USER CODE END ADC_IRQn 0 */

   /* USER CODE BEGIN ADC_IRQn 1 */

   /* USER CODE END ADC_IRQn 1 */
}

what is the intention of the multiple sections?


Solution

  • If you tick the option in CubeMX to generate a call to the HAL ADC handler, and regenerate the code, it'd put the call to HAL_ADC_IRQHandler() between the two user code sections. You can have user code both before and after the HAL stuff.

    However, I find it a poor idea to handle an interrupt both by HAL code and user code in the IRQHandler function, because that'd usually mean reading registers twice, which could have some unwanted side effects. If the HAL handler is called, then it'd be better to put user code in the appropriate callback function, which would be called when HAL has found out the cause of the interrupt.