cancel
Showing results for 
Search instead for 
Did you mean: 

Correct order of using HAL_ADC_RegisterCallback function

Rookie38
Associate III

Dear ST Community,

as part of a project to capture analog microphone data, I would like to use my own callback funtions instead of hal callbacks. For this I register my ADC callbacks with the HAL function HAL_ADC_RegisterCallback as follows:

/*************** ADC Section Begin ********************/
	hadc1.Instance = ADC1;
        ...
	hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
 
	/* Register TIM Callbacks */
	HAL_ADC_RegisterCallback( &hadc1, HAL_ADC_MSPINIT_CB_ID, Microphone_ADC_MspInit );
	HAL_ADC_RegisterCallback( &hadc1, HAL_ADC_MSPDEINIT_CB_ID, Microphone_ADC_MspDeInit );
	HAL_ADC_RegisterCallback( &hadc1, HAL_ADC_CONVERSION_COMPLETE_CB_ID, Microphone_ADC_ConvCpltCallback );
	HAL_ADC_RegisterCallback( &hadc1, HAL_ADC_CONVERSION_HALF_CB_ID, Microphone_ADC_ConvHalfCpltCallback );
	HAL_ADC_RegisterCallback( &hadc1, HAL_ADC_ERROR_CB_ID, Microphone_ADC_ErrorCallback );
 
	status = HAL_ADC_Init( &hadc1 );
	if( status != HAL_OK)
	{
		_SYSTEM_LOG( "HAL_ADC_Init Error Code: [%d]", status );
		return status;
	}
	else
	{
		/* Everything Okay. Do Nothing */
	}

If I register all five callbacks right before HAL_ADC_Init(), the Half and Complete are never called.

But if I register the Half + Full Complete callback after HAL_ADC_Init the AD conversion works and all own callbacks are called.

At the moment it is not clear to me in which order I have to register which callback where. (Independently from ADC TIM or other callbacks)

Thank you for the support

1 ACCEPTED SOLUTION

Accepted Solutions
Rookie38
Associate III

I figured it out. I'm too stupid to read:

UM1905 ADC Firmware driver API description:

Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only. Exception done MspInit/ MspDeInit functions that can be registered/unregistered in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state, thus registered (user) MspInit/DeInit callbacks can be used during the Init/ DeInit. Then, the user first registers the MspInit/MspDeInit user callbacks using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit() or @ref HAL_ADC_Init() function.

View solution in original post

1 REPLY 1
Rookie38
Associate III

I figured it out. I'm too stupid to read:

UM1905 ADC Firmware driver API description:

Callbacks can be registered/unregistered in @ref HAL_ADC_STATE_READY state only. Exception done MspInit/ MspDeInit functions that can be registered/unregistered in @ref HAL_ADC_STATE_READY or @ref HAL_ADC_STATE_RESET state, thus registered (user) MspInit/DeInit callbacks can be used during the Init/ DeInit. Then, the user first registers the MspInit/MspDeInit user callbacks using @ref HAL_ADC_RegisterCallback() before calling @ref HAL_ADC_DeInit() or @ref HAL_ADC_Init() function.