From 0029ed768ea0b3626ba8b26c369c409962ff38b6 Mon Sep 17 00:00:00 2001
From: jaseg <git@jaseg.net>
Date: Mon, 24 Dec 2018 18:08:01 +0900
Subject: Make center ADC work in "scope mode"

---
 fw/adc.c  | 52 +++++-----------------------------------------------
 fw/main.c |  4 ++--
 2 files changed, 7 insertions(+), 49 deletions(-)

diff --git a/fw/adc.c b/fw/adc.c
index 726e9a8..9deeb72 100644
--- a/fw/adc.c
+++ b/fw/adc.c
@@ -26,7 +26,7 @@ enum adc_channels {
     TEMP_CH,
     NCH
 };
-static volatile uint16_t adc_buf[NCH];
+static volatile uint16_t adc_buf[1024];
 
 void adc_init(void) {
     /* The ADC is used for temperature measurement. To compute the temperature from an ADC reading of the internal
@@ -35,15 +35,13 @@ void adc_init(void) {
      * The ADC is triggered by compare channel 4 of timer 1. The trigger is set to falling edge to trigger on compare
      * match, not overflow.
      */
-    ADC1->CFGR1 = ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG | (2<<ADC_CFGR1_EXTEN_Pos) | (1<<ADC_CFGR1_EXTSEL_Pos);
+    ADC1->CFGR1 = ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG | (2<<ADC_CFGR1_EXTEN_Pos) | (1<<ADC_CFGR1_EXTSEL_Pos) | ADC_CFGR1_CONT;
     /* Clock from PCLK/4 instead of the internal exclusive high-speed RC oscillator. */
     ADC1->CFGR2 = (2<<ADC_CFGR2_CKMODE_Pos); /* Use PCLK/4=12MHz */
     /* Sampling time 13.5 ADC clock cycles -> total conversion time 2.17us*/
     ADC1->SMPR  = (2<<ADC_SMPR_SMP_Pos);
-    /* Internal VCC and temperature sensor channels */
-    ADC1->CHSELR = ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL1 | ADC_CHSELR_CHSEL16 | ADC_CHSELR_CHSEL17;
-    /* Enable internal voltage reference and temperature sensor */
-    ADC->CCR = ADC_CCR_TSEN | ADC_CCR_VREFEN;
+
+    ADC1->CHSELR = ADC_CHSELR_CHSEL0 | ADC_CHSELR_CHSEL1;
     /* Perform ADC calibration */
     ADC1->CR |= ADC_CR_ADCAL;
     while (ADC1->CR & ADC_CR_ADCAL)
@@ -55,7 +53,7 @@ void adc_init(void) {
     /* Configure DMA 1 Channel 1 to get rid of all the data */
     DMA1_Channel1->CPAR = (unsigned int)&ADC1->DR;
     DMA1_Channel1->CMAR = (unsigned int)&adc_buf;
-    DMA1_Channel1->CNDTR = NCH;
+    DMA1_Channel1->CNDTR = sizeof(adc_buf)/sizeof(adc_buf[0]);
     DMA1_Channel1->CCR = (0<<DMA_CCR_PL_Pos);
     DMA1_Channel1->CCR |=
           DMA_CCR_CIRC /* circular mode so we can leave it running indefinitely */
@@ -70,49 +68,9 @@ void adc_init(void) {
     NVIC_SetPriority(DMA1_Channel1_IRQn, 3<<5);
 }
 
-uint16_t buf_a[256];
-uint16_t buf_b[256];
-int bufp = 0;
-
 void DMA1_Channel1_IRQHandler(void) {
-    /* This interrupt takes either 1.2us or 13us. It can be pre-empted by the more timing-critical UART and LED timer
-     * interrupts. */
-    static int count = 0; /* oversampling accumulator sample count */
-    static uint32_t adc_aggregate[NCH] = {0}; /* oversampling accumulator */
-
     /* Clear the interrupt flag */
     DMA1->IFCR |= DMA_IFCR_CGIF1;
 
-    for (int i=0; i<NCH; i++)
-        adc_aggregate[i] += adc_buf[i];
-
-    if (++count == (1<<ADC_OVERSAMPLING)) {
-        for (int i=0; i<NCH; i++)
-            adc_aggregate[i] >>= ADC_OVERSAMPLING;
-        /* This has been copied from the code examples to section 12.9 ADC>"Temperature sensor and internal reference
-         * voltage" in the reference manual with the extension that we actually measure the supply voltage instead of
-         * hardcoding it. This is not strictly necessary since we're running off a bored little LDO but it's free and
-         * the current supply voltage is a nice health value.
-         */
-        adc_data.adc_vcc_mv = (3300 * VREFINT_CAL)/(adc_aggregate[VREF_CH]);
-
-		int64_t read = adc_aggregate[TEMP_CH] * 10 * 10000;
-		int64_t vcc = adc_data.adc_vcc_mv;
-		int64_t cal = TS_CAL1 * 10 * 10000;
-		adc_data.adc_temp_celsius_tenths = 300 + ((read/4096 * vcc) - (cal/4096 * 3300))/43000;
-
-        adc_data.adc_vmeas_a_mv = (adc_aggregate[VMEAS_A]*13300L)/4096 * vcc / 3300;
-        adc_data.adc_vmeas_b_mv = (adc_aggregate[VMEAS_B]*13300L)/4096 * vcc / 3300;
-
-		buf_a[bufp] = adc_data.adc_vmeas_a_mv;
-		buf_b[bufp] = adc_data.adc_vmeas_b_mv;
-		if (++bufp >= sizeof(buf_a)/sizeof(buf_a[0])) {
-			bufp = 0;
-		}
-
-        count = 0;
-        for (int i=0; i<NCH; i++)
-            adc_aggregate[i] = 0;
-    }
 }
 
diff --git a/fw/main.c b/fw/main.c
index b766488..07d065d 100644
--- a/fw/main.c
+++ b/fw/main.c
@@ -69,8 +69,8 @@ int main(void) {
     TIM1->CCR4  = 1;
     TIM1->DIER  = TIM_DIER_UIE;
 
-    TIM1->PSC   = SystemCoreClock/1000000 - 1; /* 1.0us/tick */
-    TIM1->ARR   = 20-1; /* 20us */
+    TIM1->PSC   = SystemCoreClock/500000 - 1; /* 0.5us/tick */
+    TIM1->ARR   = 25-1;
     /* Preload all values */
     TIM1->EGR  |= TIM_EGR_UG;
     TIM1->CR1   = TIM_CR1_ARPE;
-- 
cgit