Skip to main content
Associate III
February 26, 2024
Solved

ADC_HandleTypeDef* h & (h = &hadc1)

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;
	}
}

 

This topic has been closed for replies.
Andrew Neil
Andrew NeilBest answer
Super User
February 26, 2024

@mƎALLEm 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 )

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.

8 replies

Andrew Neil
Super User
February 26, 2024

@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;
	}

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Associate III
February 26, 2024

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

(h == &hadc1)

 

Andrew Neil
Super User
February 26, 2024

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;
	}

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
February 26, 2024

@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.

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
Andrew Neil
Super User
February 26, 2024
A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.