소스 검색

Added configurable ADC single-shot mode support

Heinrich Blatt 8 달 전
부모
커밋
a298bb8933
4개의 변경된 파일61개의 추가작업 그리고 43개의 파일을 삭제
  1. 8 0
      src/config.h
  2. 9 3
      src/peripherals/adc/adc.c
  3. 43 39
      src/peripherals/adc/adc_hal.c
  4. 1 1
      src/peripherals/adc/adc_interface.h

+ 8 - 0
src/config.h

@@ -74,6 +74,14 @@
 // ADC_TARGET_BASE_ADDRESS+1 slot 1, etc.
 #define ADC_TARGET_BASE_ADDRESS 0x68
 
+// ADC Measurement mode:
+// can be single shot or continuous
+// if used continuous, a matching delay cycles needs to be set in order to
+// ensure that the measurement is ready. (set the define to 0 if it should be one-shot)
+// (it could be the case that the channel is switched and wrong data is fetched)
+#define ADC_MEASUREMENT_IS_CONTINUOUS 1
+#define ADC_CONTINUOUS_DELAY_CYCLES (32000) // 32000 is 1ms (32MHz clock)
+
 // Packet buffer sizes for RX and TX for the
 // controller mode only
 // (target for the other MCU is treated differently)

+ 9 - 3
src/peripherals/adc/adc.c

@@ -20,12 +20,18 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
         case ADC_STATE_CONFIGURE:
             adc_params.channel = channel;
             adc_params.resolution = 12;
-            //adc_params.continuous = 1;
+            adc_params.continuous = ADC_MEASUREMENT_IS_CONTINUOUS;
             adc_params.gain = 1;
             //printf("Config: Memory address of batteries: %p\n", &batteries[0]);
             adc_hal.configure(slot, &adc_params);
-            //adc_state = ADC_STATE_WAIT;
-            adc_state = ADC_STATE_READ;
+            if (adc_params.continuous == 1) {
+                // in one shot mode we wait first to get the result
+                adc_state = ADC_STATE_WAIT;
+            } else {
+                // in continuous mode we can directly read
+                adc_state = ADC_STATE_READ;
+                delay_cycles(ADC_CONTINUOUS_DELAY_CYCLES);
+            }
             break;
 
         case ADC_STATE_WAIT:

+ 43 - 39
src/peripherals/adc/adc_hal.c

@@ -63,48 +63,52 @@ D15.
 */
 static uint8_t construct_config_byte(ADC_Params *params) {
 
-  uint8_t config = 0;
-  
-  config |= ((params->channel) << 5); // Channel Selection (Bits 6-5)
-  
-
-  config |= (1 << 4); // Continous mode
-  //config |= (1 << 7); // One-Shot Mode: enable measurement (set read/not ready byte)
-  
-  
-  switch (params->resolution) {
+    uint8_t config = 0;
     
-    case 12:
-        config |= (0b00 << 2);
-        break;
-    case 14:
-        config |= (0b01 << 2);
-        break;
-    case 16:
-        config |= (0b10 << 2);
-        break;
-    default:
-        //printf("ERROR: Invalid Resolution!\n");
-        return 0;
-  }
-   
-  switch (params->gain) {
+    config |= ((params->channel) << 5); // Channel Selection (Bits 6-5)
     
-    case 1:
-        config |= (0b00);
-        break;
-    case 2:
-        config |= (0b01);
-        break;
-    case 4:
-        config |= (0b10);
-        break;
-    default:
-        //printf("ERROR: Invalid Gain!\n");
-        return 0;
-  }
+    if (params->continuous == 1) {
+        config |= (1 << 4); // Continous mode
+    } else {
+        // One-Shot mode
+        // Bit set to zero, BUT the Ready bit needs to be set to start meausrement
+        // (read/not write bit)
+        config |= (1 << 7);
+    }
+    
+    switch (params->resolution) {
+        
+        case 12:
+            config |= (0b00 << 2);
+            break;
+        case 14:
+            config |= (0b01 << 2);
+            break;
+        case 16:
+            config |= (0b10 << 2);
+            break;
+        default:
+            //printf("ERROR: Invalid Resolution!\n");
+            return 0;
+    }
+    
+    switch (params->gain) {
+        
+        case 1:
+            config |= (0b00);
+            break;
+        case 2:
+            config |= (0b01);
+            break;
+        case 4:
+            config |= (0b10);
+            break;
+        default:
+            //printf("ERROR: Invalid Gain!\n");
+            return 0;
+    }
 
-  return config;
+    return config;
 }
 
 /* Tansmit Data from MCU to ADC:  Function to SET configuration to ADC over

+ 1 - 1
src/peripherals/adc/adc_interface.h

@@ -9,7 +9,7 @@
 typedef struct {
     uint8_t channel;
     uint8_t resolution;
-    //bool continuous;
+    bool continuous;
     uint8_t gain;
 } ADC_Params;