Skip to content

Commit

Permalink
[adc] add support for analog watchdog (stm32f4) with callback
Browse files Browse the repository at this point in the history
  • Loading branch information
gautierhattenberger committed Nov 27, 2013
1 parent 713456b commit 9c6ade6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sw/airborne/arch/stm32/mcu_periph/adc_arch.c
Expand Up @@ -181,6 +181,13 @@ static struct adc_buf * adc2_buffers[4];
static struct adc_buf * adc3_buffers[4];
#endif

#if USE_ADC_WATCHDOG
// watchdog structure with adc bank and callback
static struct {
uint32_t adc;
adc_watchdog_callback cb;
} adc_watchdog;
#endif

/***************************************/
/*** PUBLIC FUNCTION DEFINITIONS ***/
Expand Down Expand Up @@ -327,6 +334,10 @@ void adc_init( void ) {

adc_new_data_trigger = FALSE;

#if USE_ADC_WATCHDOG
adc_watchdog.cb = NULL;
#endif

}

void adc_buf_channel(uint8_t adc_channel, struct adc_buf * s, uint8_t av_nb_sample)
Expand All @@ -352,6 +363,20 @@ void adc_buf_channel(uint8_t adc_channel, struct adc_buf * s, uint8_t av_nb_samp

}

#if USE_ADC_WATCHDOG
void register_adc_watchdog(uint32_t adc, uint8_t chan, uint16_t low, uint16_t high, adc_watchdog_callback cb) {
adc_watchdog.adc = adc;
adc_watchdog.cb = cb;

// activated adc watchdog of a single injected channel with interrupt
adc_set_watchdog_low_threshold(adc, low);
adc_set_watchdog_high_threshold(adc, high);
adc_enable_analog_watchdog_injected(adc);
adc_enable_analog_watchdog_on_selected_channel(adc, chan);
adc_enable_awd_interrupt(adc);
}
#endif

/**************************************/
/*** PRIVATE FUNCTION DEFINITIONS ***/
/**************************************/
Expand Down Expand Up @@ -543,6 +568,15 @@ void adc1_2_isr(void)
uint16_t value = 0;
struct adc_buf * buf;

#if USE_ADC_WATCHDOG
if (adc_watchdog.cb != NULL) {
if (adc_awd(adc_watchdog.adc)) {
ADC_SR(adc_watchdog.adc) &= ~ADC_SR_AWD; // clear int flag
adc_watchdog.cb();
}
}
#endif

#if USE_AD1
// Clear Injected End Of Conversion
if (ADC_SR(ADC1) & ADC_SR_JEOC){
Expand Down
17 changes: 17 additions & 0 deletions sw/airborne/arch/stm32/mcu_periph/adc_arch.h
Expand Up @@ -76,4 +76,21 @@ enum adc_channels {
NB_ADC
};

#if USE_ADC_WATCHDOG

/* Watchdog callback type definition
*/
typedef void (*adc_watchdog_callback)(void);

/* Watchdog register function
*
* @param adc adc bank to monitor
* @param chan adc channel to monitor
* @param low low threshhold for callback trigger
* @param high high threshhold for callback trigger
*/
extern void register_adc_watchdog(uint32_t adc, uint8_t chan, uint16_t low, uint16_t high, adc_watchdog_callback cb);

#endif

#endif /* ADC_ARCH_H */

0 comments on commit 9c6ade6

Please sign in to comment.