]> git.earman.xyz Git - sensor-watch.git/commitdiff
Remove ADC_* defines in favor of hardware-specific mapping
authorAlexsander Akers <me@a2.io>
Tue, 25 Jan 2022 00:06:54 +0000 (19:06 -0500)
committerAlexsander Akers <me@a2.io>
Tue, 25 Jan 2022 22:28:15 +0000 (17:28 -0500)
watch-library/hardware/watch/watch_adc.c
watch-library/shared/watch/watch_adc.h

index 5ba7abdf94ec507111a908a67ab0b718671939f4..32f066805028596aefe6418a24e6d40081b31732 100644 (file)
@@ -23,6 +23,7 @@
  */
 
 #include "watch_adc.h"
+#include "driver_init.h"
 
 static void _watch_sync_adc(void) {
     while (ADC->SYNCBUSY.reg);
@@ -138,13 +139,30 @@ void watch_set_analog_sampling_length(uint8_t cycles) {
     _watch_sync_adc();
 }
 
+static inline uint32_t _watch_adc_get_reference_voltage(const watch_adc_reference_voltage reference) {
+    switch (reference) {
+        case ADC_REFERENCE_INTREF:
+            return ADC_REFCTRL_REFSEL_INTREF_Val;
+            break;
+        case ADC_REFERENCE_VCC_DIV1POINT6:
+            return ADC_REFCTRL_REFSEL_INTVCC0_Val;
+            break;
+        case ADC_REFERENCE_VCC_DIV2:
+            return ADC_REFCTRL_REFSEL_INTVCC1_Val;
+            break;
+        case ADC_REFERENCE_VCC:
+            return ADC_REFCTRL_REFSEL_INTVCC2_Val;
+            break;
+    }
+}
+
 void watch_set_analog_reference_voltage(watch_adc_reference_voltage reference) {
     ADC->CTRLA.bit.ENABLE = 0;
 
     if (reference == ADC_REFERENCE_INTREF) SUPC->VREF.bit.VREFOE = 1;
     else SUPC->VREF.bit.VREFOE = 0;
 
-    ADC->REFCTRL.bit.REFSEL = reference;
+    ADC->REFCTRL.bit.REFSEL = _watch_adc_get_reference_voltage(reference);
     ADC->CTRLA.bit.ENABLE = 1;
     _watch_sync_adc();
     // throw away one measurement after reference change (the channel doesn't matter).
index d4c8586d4e120cfa620d205dc9785f5890d0b775..ea4fa9e33f0c12e1b443ee22dde4f1b89fe0d375 100644 (file)
 
 #include "watch.h"
 
-// matches adc.h
-#ifndef ADC_REFCTRL_REFSEL_INTREF_Val
-#define ADC_REFCTRL_REFSEL_INTREF_Val 0x0
-#endif
-
-#ifndef ADC_REFCTRL_REFSEL_INTVCC0_Val
-#define ADC_REFCTRL_REFSEL_INTVCC0_Val 0x1
-#endif
-
-#ifndef ADC_REFCTRL_REFSEL_INTVCC1_Val
-#define ADC_REFCTRL_REFSEL_INTVCC1_Val 0x2
-#endif
-
-#ifndef ADC_REFCTRL_REFSEL_INTVCC2_Val
-#define ADC_REFCTRL_REFSEL_INTVCC2_Val 0x5
-#endif
-
 /** @addtogroup adc Analog Input
   * @brief This section covers functions related to the SAM L22's analog-to-digital converter,
   *        as well as configuring and reading values from the five analog-capable pins on the
@@ -112,10 +95,10 @@ void watch_set_analog_num_samples(uint16_t samples);
 void watch_set_analog_sampling_length(uint8_t cycles);
 
 typedef enum {
-    ADC_REFERENCE_INTREF = ADC_REFCTRL_REFSEL_INTREF_Val,
-    ADC_REFERENCE_VCC_DIV1POINT6 = ADC_REFCTRL_REFSEL_INTVCC0_Val,
-    ADC_REFERENCE_VCC_DIV2 = ADC_REFCTRL_REFSEL_INTVCC1_Val,
-    ADC_REFERENCE_VCC = ADC_REFCTRL_REFSEL_INTVCC2_Val,
+    ADC_REFERENCE_INTREF = 0,
+    ADC_REFERENCE_VCC_DIV1POINT6,
+    ADC_REFERENCE_VCC_DIV2,
+    ADC_REFERENCE_VCC,
 } watch_adc_reference_voltage;