2024-02-26 5:49 AM
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;
}
}
Solved! Go to Solution.
2024-02-26 10:07 AM
@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 )
2024-02-26 6:38 AM - edited 2024-02-26 7:00 AM
@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;
}
2024-02-26 6:41 AM
@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.
2024-02-26 6:41 AM
yes it a typo, anyway what does the following means ?
(h == &hadc1)
2024-02-26 6:50 AM
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;
}
2024-02-26 6:56 AM - edited 2024-02-26 6:57 AM
UM2217: User Manual - Description of STM32H7 HAL and low-layer drivers:
2024-02-26 9:05 AM
@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.
2024-02-26 10:07 AM
@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 )
2024-02-26 10:17 AM
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.