Analog input with AD converter calibration for STM32F1 and STM32F3 boards.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AnalogInCal.h Source File

AnalogInCal.h

00001 #ifndef ANALOGINCAL_H
00002 #define ANALOGINCAL_H
00003 
00004 /* Analog input with AD converter calibration for STM32F1 and STM32F3 boards.
00005  *
00006  * Substitutes AnalogIn.
00007  *
00008  */
00009 
00010 #include "AnalogIn.h"
00011 
00012 class AnalogInCal : public AnalogIn
00013 {
00014 public:
00015 
00016     /** Creates an AnalogInCal, connected to the specified pin
00017      *
00018      * @param pin AnalogIn pin to connect to
00019      * @param cal Flag to request calibration. Defaults to true.
00020      */
00021     AnalogInCal(PinName pin, bool cal = true) : AnalogIn(pin) {
00022         if (cal)
00023             calibrate();
00024     }
00025 
00026     /** Calibrates the associated AD Convertor
00027      *
00028      */
00029     void calibrate()  {
00030         lock();
00031 #if defined(TARGET_STM32F1)
00032         while (HAL_ADCEx_Calibration_Start(&(_adc.handle)) != HAL_OK);
00033 #elif defined(TARGET_STM32F3)
00034         while (HAL_ADCEx_Calibration_Start(&(_adc.handle), ADC_SINGLE_ENDED) != HAL_OK);
00035 #endif
00036         unlock();
00037     }
00038 };
00039 
00040 #endif // ANALOGINCAL_H