cancel
Showing results for 
Search instead for 
Did you mean: 

Analog watchdog is not firing more than once

Patrick Morwald
Associate II
Posted on January 19, 2018 at 17:21

Hi,

i want to use the analog watchdog (ADC) on my STM32F4. I have an analog sensor attached to channel 1 of ADC1. I dont want to use CPU to make constant function calls to ADC, even with ADC interrupt mode i have to handle every 'conversion finished' interrupt even if the level of the input doesnt change. 

In my situation, the analog sensor is a door switch with most of the time doesnt change. So i want a solution that needs no CPU interference whatsoever as long as the analog level doesnt change. So i wanted to use the analog watchdog and define a guard band between the voltage level for 'door open' and 'door closed'. When switching between those states, the voltage would traverse the boundaries of the guard band and fire the interrupt of the analog watchdog.

So far is the plan. I implemented a prototype, but the analog watchdog only fires once when starting the application. I am trying to pinpoint the problem but have no idea why its not firing again. See my code attached. 

/* ADC1 init function */

static void MX_ADC1_Init(void)

{

ADC_ChannelConfTypeDef sConfig;

/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)

*/

hadc1.Instance = ADC1;

hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;

hadc1.Init.Resolution = ADC_RESOLUTION_12B;

hadc1.Init.ScanConvMode = DISABLE;

hadc1.Init.ContinuousConvMode = ENABLE;

hadc1.Init.DiscontinuousConvMode = DISABLE;

hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;

hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;

hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;

hadc1.Init.NbrOfConversion = 1;

hadc1.Init.DMAContinuousRequests = DISABLE;

hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;

if (HAL_ADC_Init(&hadc1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.

*/

sConfig.Channel = ADC_CHANNEL_11;

sConfig.Rank = 1;

sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;

if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/** Configure the analog watchdog witch fires an interrupt when a certain

* guard band (defined by LowThreshold and HighThreshold) is left.

*/

ADC_AnalogWDGConfTypeDef aWDGConfig;

aWDGConfig.WatchdogNumber = 0; /**< Reserved for future use, can be set to 0 */

aWDGConfig.Channel = ADC_CHANNEL_11;

aWDGConfig.HighThreshold = 3500; //TODO: Use defines

aWDGConfig.LowThreshold = 2500; //TODO: Use defines

aWDGConfig.WatchdogMode = ADC_ANALOGWATCHDOG_SINGLE_REG;

aWDGConfig.ITMode = ENABLE;

if (HAL_ADC_AnalogWDGConfig(&hadc1, &aWDGConfig) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

#stm32f4 #adc #analog-watchdog
3 REPLIES 3
Posted on January 20, 2018 at 12:31

> have no idea why its not firing again

The reason may be hidden in Cube/HAL which I'm not going to investigate.

Basically, a continuous conversion with WD enabled and ISR enabled continues to fire interrupts each time the conversion ends with a value within the two thresholds. So, review the interrupt code (I mean that inside Cube/HAL). If in doubts, read out the ADC registers after the ISR and check if the WD and interrupts are still enabled.

JW

Posted on January 22, 2018 at 14:16

Basically, a continuous conversion with WD enabled and ISR enabled continues to fire interrupts each time the conversion ends with a value within the two thresholds

According to datasheet it fires when the conversion ends outside the safe band defined by the threshold values, so as i understand it the analog watchdog interrupt fires whenever there is a transition from within thresholds to outside of thresholds, but not on reentering the 'safe band' again. 

It still doesnt work though, i will try to analyze the corresponding HAL function.

Posted on January 22, 2018 at 15:09

According to datasheet it fires when the conversion ends outside the safe band [...]

You're right - I didn't remember it correctly.

Still, it should continue to fire continuously while the conditions are fulfilled, i.e. upon each conversion end if the results lies outside the safe band.

JW