cancel
Showing results for 
Search instead for 
Did you mean: 

What can cause HAL_ADCEx_Calibration_Start() to fail?

KiptonM
Lead

Here is the code snippet.

        HAL_Delay(5000);
	if (HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED) != HAL_OK)
	{
		print_debug_str("Did not calibrate right!");
	}
	else
	{
		char buf[32];
		sprintf(buf,"Calibration Factor: %lu\r\n",ADC1->CALFACT);
		print_debug_str(buf);
	}

if I let this run it fails.

If I put a break point on the

if (HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED) != HAL_OK)

and step over it works.

If I remove the HAL_Delay(5000) it does not work unless I put in a break. It will work (return HAL_OK) after the break even if I immediately tell it to continue and run where the delay is much less than 5 seconds.

I put in the delay because I thought it needed more time to settle down. That does not seem to be the case. I even tried to make the delay 15 seconds. It still will not return HAL_OK

Thanks for the response.

Kip

1 ACCEPTED SOLUTION

Accepted Solutions
KiptonM
Lead

Beginner mistake. The start ADC was in the Tick timer interrupt, so it was starting immediately after the HW was set up, but before the rest of the initialization was set up it was calling the start ADC.

Sorry for the trouble.

View solution in original post

4 REPLIES 4
KiptonM
Lead

I re arranged the code and HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED) returns 1

KiptonM
Lead

I changed the code to this:

{
		int16_t rc;
		char buf[60];
		do {
 
			rc = HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED) ;
			if (rc != HAL_OK)
			{
				sprintf(buf,"*** Did not calibrate right! *** rc = %d\r\n",rc);
				print_debug_str(buf);
				HAL_Delay(1000);
			}
			else
			{
				char buf[32];
				sprintf(buf,"Calibration Factor: %lu\r\n",ADC1->CALFACT);
				print_debug_str(buf);
 
			}
		} while (rc != HAL_OK);
	}

and it never escapes the while (rc != HAL_OK);

Stepping through the code the only way the function returns HAL_ERROR (or 1) is if ADC_Disable(hadc); returns an error. But it does not return an error when I step through the code.

KiptonM
Lead

I do not know why but when I added

rc = ADC_Disable(&hadc1);

sprintf(buf,"ADC_Disable(&hadc1); returns %d\r\n",rc);

print_debug_str(buf);

rc always returns 1 (Error)

But then the rest works correctly.

I am running this after the HAL initialization (MX_ADC1_Init()), but before I start the ADC to make a conversion for the first time.

This was not a problem on any other processor I have used to date. Only the STM32L443CCT7.

I took the code directly from the STM32G051K8T where it worked without an issue. (Except I had to add the second variable to the HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED) call. The STM32G051K8T only needed one variable passed.

{
    int16_t rc;
 
    char buf[60];
    rc = ADC_Disable(&hadc1);
    sprintf(buf,"ADC_Disable(&hadc1); returns %d\r\n",rc);
    print_debug_str(buf);
    do {
         rc = HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED) ;
         if (rc != HAL_OK)
         {
               sprintf(buf,"*** Did not calibrate right! *** rc = %d\r\n",rc);
               print_debug_str(buf);
               HAL_Delay(1000);
        }
        else
        {
               char buf[32];
               sprintf(buf,"Calibration Factor: %lu,  \r\n",ADC1->CALFACT);
                print_debug_str(buf);
         }
    } while (rc != HAL_OK);
}

This seems to work.

KiptonM
Lead

Beginner mistake. The start ADC was in the Tick timer interrupt, so it was starting immediately after the HW was set up, but before the rest of the initialization was set up it was calling the start ADC.

Sorry for the trouble.