diff options
-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); } |