modify ADC driver for two channels, untested
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 16 Jul 2014 00:56:21 +0000 (00:56 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 16 Jul 2014 00:56:21 +0000 (00:56 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1762 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/stm32/inc/stm32f4_adc.h
codec2-dev/stm32/inc/stm32f4_dac.h
codec2-dev/stm32/src/adc_rec.c
codec2-dev/stm32/src/dac_ut.c
codec2-dev/stm32/src/power_ut.c
codec2-dev/stm32/src/stm32f4_adc.c
codec2-dev/stm32/src/stm32f4_dac.c

index b14110cb59005cc22b1f526eb9cf228e7b5a3c3b..17c318e05450f84d0972aad188a3ccd84ef7e4ef 100644 (file)
@@ -4,7 +4,7 @@
   AUTHOR......: David Rowe
   DATE CREATED: 30 May 2014
 
-  ADC driver module for STM32F4.
+  Two channel FIFO buffered ADC driver module for STM32F4.
 
 \*---------------------------------------------------------------------------*/
 
@@ -29,6 +29,7 @@
 #define __STM32F4_ADC__
 
 void adc_open(void);
-int adc_read(short buf[], int n); 
+int adc1_read(short buf[], int n); /* ADC1 Pin PA1 */
+int adc2_read(short buf[], int n); /* ADC2 Pin PA2 */
 
 #endif
index 3a298321fa05dfd178110e36f2127f5a77c7655b..9950f36a3b346f85d839d18430968b71e17caa08 100644 (file)
@@ -4,7 +4,7 @@
   AUTHOR......: David Rowe
   DATE CREATED: 1 June 2013
 
-  DAC driver module for STM32F4.
+  Two channel FIFO buffered DAC driver module for STM32F4.
 
 \*---------------------------------------------------------------------------*/
 
@@ -29,6 +29,7 @@
 #define __STM32F4_DAC__
 
 void dac_open(void);
-int dac_write(short buf[], int n); 
+int dac1_write(short buf[], int n); /* DAC1 pin PA4 */
+int dac2_write(short buf[], int n); /* DAC2 pin PA5 */
 
 #endif
index 3d5992f9717301ae3660cea18d168c1efd60f6a5..c20516d99d184b5dbe0778068c468324451ce421 100644 (file)
@@ -4,7 +4,7 @@
   AUTHOR......: David Rowe\r
   DATE CREATED: 30 May 2014\r
 \r
-  Recordss a 16 kHz sample rate raw file from the STM32F4 ADC.\r
+  Records a 16 kHz sample rate raw file from the STM32F4 ADC Pin PA1.\r
 \r
 \*---------------------------------------------------------------------------*/\r
 \r
@@ -49,7 +49,7 @@ int main(void){
 \r
     printf("Starting!\n");\r
     for(i=0; i<bufs; i++) {\r
-        while(adc_read(buf, N) == -1);\r
+        while(adc1_read(buf, N) == -1);\r
         fwrite(buf, sizeof(short), N, frec);  \r
         printf(".");\r
     }\r
index 58f8ac4d4c6d8466675963b502d063d246f99de0..2da1131f5e7d845d0cb3652f37881446dfdd8965 100644 (file)
@@ -50,7 +50,7 @@ int main(void) {
 \r
         /* keep DAC FIFO topped up */\r
 \r
-        dac1_write((short*)aSine, SINE_SAMPLES);\r
+        dac2_write((short*)aSine, SINE_SAMPLES);\r
     }\r
    \r
 }\r
index fc45a8ea4ebac99e43dcf0f6f88c8709ba174b3c..a42286ea49cc137c9839deed673b3b114d85b643 100644 (file)
@@ -90,7 +90,7 @@ static void c2speedtest(int mode, char inputfile[])
     pinbuf = inbuf;\r
     for(f=0; f<nframes; f++) {\r
         //printf("read ADC\n");\r
-        while(adc_read(dummy_buf, nsam*2) == -1);  /* runs at Fs = 16kHz */\r
+        while(adc1_read(dummy_buf, nsam*2) == -1);  /* runs at Fs = 16kHz */\r
 \r
         //printf("Codec 2 enc\n");\r
        GPIOD->ODR = (1 << 13);\r
index 62fe8a990455cca8df524835dfeb3a0bf03ca175..264186a2efdf486c90b0765dba2aa5e2cf93dfe6 100644 (file)
@@ -6,24 +6,6 @@
 
   ADC driver module for STM32F4.
 
-  TODO:
-  [X] just get ADC to run at all, prove its sampling something....
-  [X] as above with DMA
-  [X] half and finished interrupts, ISR
-  [X] timer config to drive ADC conversion, measure sample rate and confirm 16kHz
-      + larger ADC DMA buffer
-      + fifos
-      + work out a way to unit test
-  [ ] ADC working at same time as DAC
-  [X] remove (or make optional) the TIM_Config() code that sends PWM output to pins
-  [ ] check comments still valid
-  [X] convert to driver
-  [ ] way to determine which timers are used so they don't get re-sued
-  [ ] way to select different pins/ADCs for multiple channels, multiple channel support
-  [ ] access functions for halff/full/overflow to trap any issues
-  [ ] should FIFOs be in this drivr or in UTs connected to stdio?  SmartMic will just need
-      40ms of buffering
-
 \*---------------------------------------------------------------------------*/
 
 /*
 #include "stm32f4_adc.h"
 
 #define ADC_BUF_SZ   320
-#define FIFO_SZ      1000
+#define FIFO_SZ      4*ADC_BUF_SZ
 
-struct FIFO *DMA2_Stream0_fifo;
-unsigned short adc_buf[ADC_BUF_SZ];
-int adc_overflow;
-int half,full;
+static struct FIFO *adc1_fifo;
+static struct FIFO *adc2_fifo;
+static unsigned short adc_buf[ADC_BUF_SZ];
+static int adc_overflow;
+static int half,full;
 
 #define ADCx_DR_ADDRESS          ((uint32_t)0x4001204C)
 #define DMA_CHANNELx             DMA_Channel_0
 #define DMA_STREAMx              DMA2_Stream0
-#define ADCx                     ADC1
 
 #define TIM1_CCR3_ADDRESS    0x4001223C
 
-TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
-TIM_OCInitTypeDef  TIM_OCInitStructure;
-uint16_t uhTimerPeriod;
-uint16_t aSRC_Buffer[3] = {0, 0, 0};
-
-void Timer1Config();
-void adc_configure();
+static void Timer1Config();
+static void adc_configure();
 
 void adc_open(void) {
-    DMA2_Stream0_fifo = fifo_create(FIFO_SZ);
-    assert(DMA2_Stream0_fifo != NULL);
+    adc1_fifo = fifo_create(FIFO_SZ);
+    adc2_fifo = fifo_create(FIFO_SZ);
+    assert(adc1_fifo != NULL);
+    assert(adc2_fifo != NULL);
 
     Timer1Config();
     adc_configure();
@@ -89,11 +68,17 @@ void adc_open(void) {
 
 /* n signed 16 bit samples in buf[] if return != -1 */
 
-int adc_read(short buf[], int n) {   
-    return fifo_read(DMA2_Stream0_fifo, buf, n);
+int adc1_read(short buf[], int n) {   
+    return fifo_read(adc1_fifo, buf, n);
+}
+
+int adc2_read(short buf[], int n) {   
+    return fifo_read(adc2_fifo, buf, n);
 }
 
 void Timer1Config() {
+    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
+    uint16_t                 uhTimerPeriod;
 
     /* TIM1 example -------------------------------------------------
   
@@ -106,32 +91,21 @@ void Timer1Config() {
        TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
        SystemCoreClock is set to 168 MHz for STM32F4xx devices.
 
-       The objective is to configure TIM1 channel 3 to generate complementary PWM
-       signal with a frequency equal to F KHz:
-       - TIM1_Period = (SystemCoreClock / F) - 1
+       The objective is to configure TIM1 channel 3 to generate a
+       clock with a frequency equal to F KHz:
+       
+         TIM1_Period = (SystemCoreClock / F) - 1
 
-       The number of this repetitive requests is defined by the TIM1 Repetion counter,
-       each 3 Update Requests, the TIM1 Channel 3 Duty Cycle changes to the next new 
-       value defined by the aSRC_Buffer.
-  
-       Note: 
-       SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
-       Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
-       function to update SystemCoreClock variable value. Otherwise, any configuration
-       based on this variable will be incorrect.  
        -----------------------------------------------------------------------------*/
   
-    /* Compute the value to be set in ARR regiter to generate signal frequency at 16.00 Khz */
+    /* Compute the value to be set in ARR regiter to generate signal frequency at 16.00 KHz */
+
     uhTimerPeriod = (SystemCoreClock / 16000 ) - 1;
-    /* Compute CCR1 value to generate a duty cycle at 50% */
-    aSRC_Buffer[0] = (uint16_t) (((uint32_t) 5 * (uhTimerPeriod - 1)) / 10);
-    /* Compute CCR1 value to generate a duty cycle at 37.5% */
-    aSRC_Buffer[1] = (uint16_t) (((uint32_t) 375 * (uhTimerPeriod - 1)) / 1000);
-    /* Compute CCR1 value to generate a duty cycle at 25% */
-    aSRC_Buffer[2] = (uint16_t) (((uint32_t) 25 * (uhTimerPeriod - 1)) / 100);
 
     /* TIM1 Peripheral Configuration -------------------------------------------*/
+
     /* TIM1 clock enable */
+
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 
     /* Time Base configuration */
@@ -145,33 +119,12 @@ void Timer1Config() {
 
     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
 
-    /* Channel 3 Configuration in PWM mode */
-
-    /* I think we just ned to enable channel 3 somehow, but without
-       (or optionally with) actual ouput to a GPIO pin.  */
-
-    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
-    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
-    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
-    TIM_OCInitStructure.TIM_Pulse = aSRC_Buffer[0];
-    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
-    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
-    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
-    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
-
-    TIM_OC3Init(TIM1, &TIM_OCInitStructure);
-
-    /* Enable preload feature */
-    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
-  
     /* TIM1 counter enable */
+
     TIM_Cmd(TIM1, ENABLE);
-  
-    /* Main Output Enable */
-    TIM_CtrlPWMOutputs(TIM1, ENABLE);
 }
 
-void adc_configure(){
+void adc_configure() {
     ADC_InitTypeDef  ADC_init_structure; 
     GPIO_InitTypeDef GPIO_initStructre; 
     DMA_InitTypeDef  DMA_InitStructure;
@@ -179,18 +132,19 @@ void adc_configure(){
 
     // Clock configuration
 
-    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
-    RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE);
-    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
+    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,  ENABLE);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
+    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,  ENABLE);
 
-    // Analog pin configuration
+    // Analog pin configuration ADC1->PA1, ADC2->PA2 
 
-    GPIO_initStructre.GPIO_Pin = GPIO_Pin_0;        // ADC Channel 10 is connected to PC0
+    GPIO_initStructre.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;        
     GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN;     
     GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; 
-    GPIO_Init(GPIOC,&GPIO_initStructre);            
+    GPIO_Init(GPIOA,&GPIO_initStructre);            
 
-    // ADC structure configuration
+    // ADC structure configuration.  Note we are just using one ADC, which samples
+    // two analog channels when triggered by the timer.
 
     ADC_DeInit();
     ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Left;
@@ -198,18 +152,19 @@ void adc_configure(){
     ADC_init_structure.ADC_ContinuousConvMode = DISABLE; 
     ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC3;
     ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
-    ADC_init_structure.ADC_NbrOfConversion = 1;
-    ADC_init_structure.ADC_ScanConvMode = DISABLE;
-    ADC_Init(ADCx,&ADC_init_structure);
+    ADC_init_structure.ADC_NbrOfConversion = 2;
+    ADC_init_structure.ADC_ScanConvMode = ENABLE;
+    ADC_Init(ADC1,&ADC_init_structure);
 
-    // Select the channel to be read from
+    // Select the channels to be read from
 
-    ADC_RegularChannelConfig(ADCx,ADC_Channel_10,1,ADC_SampleTime_144Cycles);
+    ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_144Cycles);
+    ADC_RegularChannelConfig(ADC1,ADC_Channel_2,2,ADC_SampleTime_144Cycles);
 
     /* DMA  configuration **************************************/
 
-    DMA_DeInit(DMA_STREAMx);
-    DMA_InitStructure.DMA_Channel = DMA_CHANNELx;  
+    DMA_DeInit(DMA2_Stream0);
+    DMA_InitStructure.DMA_Channel = DMA_Channel_0;  
     DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS;
     DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_buf;
     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
@@ -224,19 +179,19 @@ void adc_configure(){
     DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
     DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
     DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
-    DMA_Init(DMA_STREAMx, &DMA_InitStructure);
+    DMA_Init(DMA2_Stream0, &DMA_InitStructure);
 
     /* Enable DMA request after last transfer (Single-ADC mode) */
 
-    ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE);
+    ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
 
     /* Enable ADC1 DMA */
 
-    ADC_DMACmd(ADCx, ENABLE);
+    ADC_DMACmd(ADC1, ENABLE);
 
     /* DMA2_Stream0 enable */
 
-    DMA_Cmd(DMA_STREAMx, ENABLE);
+    DMA_Cmd(DMA2_Stream0, ENABLE);
 
     /* Enable DMA Half & Complete interrupts */
 
@@ -260,8 +215,9 @@ void adc_configure(){
 */
 
 void DMA2_Stream0_IRQHandler(void) {
-    int i, sam;
-    short signed_buf[ADC_BUF_SZ/2];
+    int i, j, sam1, sam2;
+    short signed_buf1[ADC_BUF_SZ/4];
+    short signed_buf2[ADC_BUF_SZ/4];
 
     /* Half transfer interrupt */
 
@@ -270,15 +226,19 @@ void DMA2_Stream0_IRQHandler(void) {
 
         /* convert to signed */
 
-        for(i=0; i<ADC_BUF_SZ/2; i++) {
-            sam = (int)adc_buf[i] - 32768;
-            //sam = (int)adc_buf[i];
-            signed_buf[i] = sam;
+        for(i=0,j=0; i<ADC_BUF_SZ/4; i++,j+=2) {
+            sam1 = (int)adc_buf[j]   - 32768;
+            sam2 = (int)adc_buf[j+1] - 32768;
+            signed_buf1[i] = sam1;
+            signed_buf2[i] = sam2;
         }
 
-       /* write first half to fifo */
+        /* write first half to fifo */
 
-        if (fifo_write(DMA2_Stream0_fifo, signed_buf, ADC_BUF_SZ/2) == -1) {
+        if (fifo_write(adc1_fifo, signed_buf1, ADC_BUF_SZ/4) == -1) {
+            adc_overflow++;
+        }
+        if (fifo_write(adc2_fifo, signed_buf2, ADC_BUF_SZ/4) == -1) {
             adc_overflow++;
         }
 
@@ -294,15 +254,19 @@ void DMA2_Stream0_IRQHandler(void) {
 
         /* convert to signed */
 
-        for(i=0; i<ADC_BUF_SZ/2; i++) {
-            sam = (int)adc_buf[ADC_BUF_SZ/2 + i] - 32768;
-            //sam = (int)adc_buf[ADC_BUF_SZ/2 + i];
-            signed_buf[i] = sam;
+        for(i=0,j=ADC_BUF_SZ/2; i<ADC_BUF_SZ/4; i++,j+=2) {
+            sam1 = (int)adc_buf[j]   - 32768;
+            sam2 = (int)adc_buf[j+1] - 32768;
+            signed_buf1[i] = sam1;
+            signed_buf2[i] = sam2;
         }
 
         /* write second half to fifo */
 
-        if (fifo_write(DMA2_Stream0_fifo, signed_buf, ADC_BUF_SZ/2) == -1) {
+        if (fifo_write(adc1_fifo, signed_buf1, ADC_BUF_SZ/4) == -1) {
+            adc_overflow++;
+        }
+        if (fifo_write(adc2_fifo, signed_buf2, ADC_BUF_SZ/4) == -1) {
             adc_overflow++;
         }
 
index 973dd5b87995129f4eb2f61bc77987ad548bfd85..f79c51df4a8df819274562ec1190e08971e255d5 100644 (file)
 #define FIFO_SZ      4*DAC_BUF_SZ\r
 #define DAC_MAX      4096            /* maximum amplitude */\r
 \r
-DAC_InitTypeDef  DAC_InitStructure;\r
-struct FIFO *dac1_fifo;\r
-struct FIFO *dac2_fifo;\r
+static struct FIFO *dac1_fifo;\r
+static struct FIFO *dac2_fifo;\r
 \r
-unsigned short dac1_buf[DAC_BUF_SZ];\r
-unsigned short dac2_buf[DAC_BUF_SZ];\r
+static unsigned short dac1_buf[DAC_BUF_SZ];\r
+static unsigned short dac2_buf[DAC_BUF_SZ];\r
 \r
 static void tim6_config(void);\r
 static void dac1_config(void);\r
@@ -137,7 +136,8 @@ static void tim6_config(void)
 \r
 static void dac1_config(void)\r
 {\r
-  DMA_InitTypeDef DMA_InitStructure;\r
+  DAC_InitTypeDef  DAC_InitStructure;\r
+  DMA_InitTypeDef  DMA_InitStructure;\r
   NVIC_InitTypeDef NVIC_InitStructure;\r
   \r
   /* DAC channel11Configuration */\r
@@ -195,6 +195,7 @@ static void dac1_config(void)
 \r
 static void dac2_config(void)\r
 {\r
+  DAC_InitTypeDef  DAC_InitStructure;\r
   DMA_InitTypeDef DMA_InitStructure;\r
   NVIC_InitTypeDef NVIC_InitStructure;\r
   \r