cancel
Showing results for 
Search instead for 
Did you mean: 

ADC_HandleTypeDef* h & (h = &hadc1)

StanCosgrove
Associate II

Hi I come across an ADC code as below, could anyone advise whats the meaning of pointer *h in the function argument and whats (h = &hadc1) do inside the callback function ?

main(void){
HAL_ADC_Start_IT(&hadc1);   
...
  while (1)
  {
   if (adc_valid == 1)
	  {
                  ...
		  HAL_ADC_Start_IT(&hadc1);
	  }
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* h)  
{
	if (h = &hadc1)
	{
		adc_result = HAL_ADC_GetValue(&hadc1);
		adc_valid =1;
	}
}

 

8 REPLIES 8
Andrew Neil
Evangelist III

@StanCosgrove wrote:

Hi I come across an ADC code as below,


Where did you find it? Please give a link.

 


@StanCosgrove wrote:

 what's the meaning of pointer *h in the function argument


That's just standard C: h is a pointer to an object of type ADC_HandleTypeDef - an ADC Handle.

See the HAL User Manual for details.

You can also select the type name, and use 'Go to definition' - there will be documentation alongside the definition.

 


@StanCosgrove wrote:

 what's (h = &hadc1) do inside the callback function ?

 

	if (h == &hadc1)
	{
		adc_result = HAL_ADC_GetValue(&hadc1);
		adc_valid =1;
	}

 

 

 

That looks like a classic C typo - it should most likely be '==' (ie, test for equality) not '=' (assignment)

 

	if (h == &hadc1)
	{
		adc_result = HAL_ADC_GetValue(&hadc1);
		adc_valid =1;
	}

 

 

Andrew Neil
Evangelist III

@StanCosgrove wrote:

 pointer *h

 


Note that the pointer is just h;

*h is dereferencing the pointer; ie, accessing the thing that the pointer points to.

yes it a typo, anyway what does the following means ? 

(h == &hadc1)

 

Again, standard C syntax:

It's just testing if the value of the pointer h is equal to &hadc1.

That is, it's checking that the supplied handle pointer h is pointing to the handle hadc1.

    // Is the parameter referring to hadc1?
	if (h == &hadc1)
	{
        // Yes it is - get the ADC value corresponding to hadc1
		adc_result = HAL_ADC_GetValue(&hadc1);
		adc_valid =1;
	}

 

So it will do nothing unless the supplied pointer is pointing to the Handle hadc1

which could equally be written as:

	if (h == &hadc1)
	{
		adc_result = HAL_ADC_GetValue( h );
		adc_valid =1;
	}

 

Andrew Neil
Evangelist III

@StanCosgrove wrote:

yes it a typo, anyway what does the following means ? 

(h == &hadc1)

 


	if (h == &hadc1)
	{
		adc_result = HAL_ADC_GetValue(&hadc1);
		adc_valid =1;
	}

It does mean that if the ADC1 was the source of the EOC interrupt, read the converted value.

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.

@SofLit wrote:

It does mean that if the ADC1 was the source ...

Strictly, "if the ADC associated with the Handle hadc1 was the source..."

You would, of course, hope that the ADC associated with a Handle called hadc1 would, indeed, be ADC1 - but it ain't necessarily so ...

That's the whole point of having a generic "Handle".

If you wanted to be certain that it was specifically ADC1, it would be

if( h->Instance == ADC1 )

 

This is for situations where ONE callback routine needs to determine which handle/instance is being referred too.

ie &hadc1 or &hadc2 

If those were your instances / objects.

As a pointer, you could perhaps override them and use a structure that carries ST's information and your own. Say buffers associated with the ADC1 instance, rather than use globals for everything.

This is really so you don't have to pass dozens of common parameters around, and the code scales cleanly if you need to manage 2x, 3x or 10x ADC peripherals.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..