diff options
author | Matthias P. Braendli <matthias.braendli@mpb.li> | 2016-09-18 19:11:37 +0200 |
---|---|---|
committer | Matthias P. Braendli <matthias.braendli@mpb.li> | 2016-09-18 19:11:37 +0200 |
commit | 54caf567355d9862218b5e182d169910cd183e2c (patch) | |
tree | e8ea6c81694600b7b35ac38085abec1e593bdcc7 /src | |
parent | 52529249d2f618fdf2b13ed05f53669eead266a6 (diff) | |
download | glutte-o-matic-54caf567355d9862218b5e182d169910cd183e2c.tar.gz glutte-o-matic-54caf567355d9862218b5e182d169910cd183e2c.tar.bz2 glutte-o-matic-54caf567355d9862218b5e182d169910cd183e2c.zip |
Add ADC1 timeout and corresponding new fault source
Diffstat (limited to 'src')
-rw-r--r-- | src/common/includes/Core/common.h | 1 | ||||
-rw-r--r-- | src/glutt-o-logique/analog_input.c | 25 |
2 files changed, 22 insertions, 4 deletions
diff --git a/src/common/includes/Core/common.h b/src/common/includes/Core/common.h index adf08ea..b5dc881 100644 --- a/src/common/includes/Core/common.h +++ b/src/common/includes/Core/common.h @@ -62,6 +62,7 @@ int random_bool(void); #define FAULT_SOURCE_USART 4 #define FAULT_SOURCE_TASK_OVERFLOW 5 #define FAULT_SOURCE_CW_AUDIO_QUEUE 6 +#define FAULT_SOURCE_ADC1 7 void trigger_fault(int source); int find_last_sunday(const struct tm*); diff --git a/src/glutt-o-logique/analog_input.c b/src/glutt-o-logique/analog_input.c index baf8f9f..682472b 100644 --- a/src/glutt-o-logique/analog_input.c +++ b/src/glutt-o-logique/analog_input.c @@ -23,6 +23,8 @@ */ #include "analog_input.h" +#include "Core/delay.h" +#include "Core/common.h" #include "stm32f4xx_adc.h" #include <math.h> #include "GPIO/usart.h" @@ -77,10 +79,25 @@ static uint16_t analog_read_channel(uint8_t channel) ADC_SoftwareStartConv(ADC1); - // TODO add timeout - - /* wait for end of conversion */ - while((ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)); + /* Timeout: + * System clock is at 168MHz, ADC is on APB2 which has a prescaler of 2. + * 480 cycles at 84Mhz is about 6us. + * + * If we have no result after 10ms it is a real problem. + */ + + int ready = 0; + + for (int i = 0; i < 10000; i++) { + if (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == SET) { + ready = 1; + break; + } + } + + if (!ready) { + trigger_fault(FAULT_SOURCE_ADC1); + } return ADC_GetConversionValue(ADC1); } |