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

Revision:
0:99f4dfbbb498
Child:
1:e4bcfca7d2df
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AnalogInCal.h	Sat Nov 18 14:26:35 2017 +0000
@@ -0,0 +1,40 @@
+#ifndef ANALOGINCAL_H
+#define ANALOGINCAL_H
+
+/* Analog input with AD converter calibration for STM32F1 and STM32F3 boards.
+ * Substitutes AnalogIn.
+ *
+ */
+
+#include "mbed.h"
+
+class AnalogInCal : public AnalogIn
+{
+public:
+
+    /** Creates an AnalogIn, connected to the specified pin
+     *
+     * @param pin AnalogIn pin to connect to
+     * @param cal Flag to request calibration. Defaults to true.
+     */
+    AnalogInCal(PinName pin, bool cal = true) : AnalogIn(pin) {
+        if (cal)
+            calibrate();
+    }
+
+    /** Calibrates the associated AD Convertor
+     *
+     */
+    void calibrate()  {
+        lock();
+#if defined(TARGET_STM32F1)
+        while(HAL_ADCEx_Calibration_Start(&(_adc.handle)) != HAL_OK);
+#elif defined(TARGET_STM32F3)
+        while(HAL_ADCEx_Calibration_Start(&(_adc.handle), ADC_SINGLE_ENDED) != HAL_OK);
+#endif
+        unlock();
+    }
+
+};
+
+#endif // ANALOGINCAL_H