From b2a9d143f4ac1df924cefb86d2d641b0e69897e6 Mon Sep 17 00:00:00 2001
From: Alberto Nidasio <alberto.nidasio@skywarder.eu>
Date: Thu, 20 Apr 2023 01:10:04 +0200
Subject: [PATCH] [InternalADC] Added calibration voltage definition and error
 if missing `V_DDA_VOLTAGE`

---
 src/shared/drivers/adc/InternalADC.cpp | 21 ++++++++++++++++-----
 src/shared/drivers/adc/InternalADC.h   |  2 --
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/shared/drivers/adc/InternalADC.cpp b/src/shared/drivers/adc/InternalADC.cpp
index abc344d41..d97632d06 100644
--- a/src/shared/drivers/adc/InternalADC.cpp
+++ b/src/shared/drivers/adc/InternalADC.cpp
@@ -25,16 +25,20 @@
 #include <drivers/timer/TimestampTimer.h>
 #include <utils/ClockUtils.h>
 
+#define ADC_RESOLUTION 4095
+
 #if defined(STM32F407xx) || defined(STM32F429xx)
 #define TEMP30_CAL_VALUE ((uint16_t*)((uint32_t)0x1FFF7A2C))
 #define TEMP110_CAL_VALUE ((uint16_t*)((uint32_t)0x1FFF7A2E))
 #define TEMP30 30
 #define TEMP110 110
+#define CALIBRATION_V_DDA 3.3f
 #elif defined(STM32F767xx) || defined(STM32F769xx)
 #define TEMP30_CAL_VALUE ((uint16_t*)((uint32_t)0x1FF0F44C))
 #define TEMP110_CAL_VALUE ((uint16_t*)((uint32_t)0x1FF0F44E))
 #define TEMP30 30
 #define TEMP110 110
+#define CALIBRATION_V_DDA 3.3f
 #else
 #warning This micro controller does not have a calibrated temperature sensor or is not currently supported by this driver
 #define WITHOUT_CALIBRATION
@@ -51,15 +55,22 @@
 #define VBAT_DIV 4.0f
 #endif
 
+// Error the user if the current target is missing the V_DDA_VOLTAGE macro
+// If it is missing you need to define it, preferably in the board_settings.h
+// file in miosix. Check your board schematic to find the voltage value.
+#ifndef V_DDA_VOLTAGE
+#error Missing V_DDA_VOLTAGE definition for current target
+#endif
+
 #ifndef WITHOUT_CALIBRATION
 namespace InternalADCConsts
 {
 // Factory calibration values
 // Read "Temperature sensor characteristics" chapter in the datasheet
 static const float voltage30 =
-    static_cast<float>(*TEMP30_CAL_VALUE) * 3.3 / 4095;
+    static_cast<float>(*TEMP30_CAL_VALUE) * CALIBRATION_V_DDA / ADC_RESOLUTION;
 static const float voltage110 =
-    static_cast<float>(*TEMP110_CAL_VALUE) * 3.3 / 4095;
+    static_cast<float>(*TEMP110_CAL_VALUE) * CALIBRATION_V_DDA / ADC_RESOLUTION;
 static const float slope = (voltage110 - voltage30) / (TEMP110 - TEMP30);
 }  // namespace InternalADCConsts
 #endif
@@ -185,7 +196,7 @@ void InternalADC::disableVbat()
 InternalADCData InternalADC::getVoltage(Channel channel)
 {
     return {timestamp, channel,
-            channelsRawValues[channel] * V_DDA_VOLTAGE / RESOLUTION};
+            channelsRawValues[channel] * V_DDA_VOLTAGE / ADC_RESOLUTION};
 }
 
 TemperatureData InternalADC::getTemperature()
@@ -195,7 +206,7 @@ TemperatureData InternalADC::getTemperature()
 
     if (temperatureRawValue != 0)
     {
-        data.temperature = temperatureRawValue * V_DDA_VOLTAGE / RESOLUTION;
+        data.temperature = temperatureRawValue * V_DDA_VOLTAGE / ADC_RESOLUTION;
 
 #ifdef WITHOUT_CALIBRATION
         // Default conversion
@@ -218,7 +229,7 @@ TemperatureData InternalADC::getTemperature()
 InternalADCData InternalADC::getVbatVoltage()
 {
     return {timestamp, VBAT_CH,
-            vbatVoltageRawValue * V_DDA_VOLTAGE / RESOLUTION * VBAT_DIV};
+            vbatVoltageRawValue * V_DDA_VOLTAGE / ADC_RESOLUTION * VBAT_DIV};
 }
 
 inline void InternalADC::resetRegisters()
diff --git a/src/shared/drivers/adc/InternalADC.h b/src/shared/drivers/adc/InternalADC.h
index 276b35b6d..5746a54fb 100644
--- a/src/shared/drivers/adc/InternalADC.h
+++ b/src/shared/drivers/adc/InternalADC.h
@@ -145,8 +145,6 @@ private:
     uint16_t temperatureRawValue = 0;
     uint16_t vbatVoltageRawValue = 0;
     uint64_t timestamp           = 0;
-
-    static constexpr int RESOLUTION = 4095;  ///< 12 bits
 };
 
 }  // namespace Boardcore
-- 
GitLab