From: drowe67 Date: Sat, 9 Aug 2014 03:05:35 +0000 (+0000) Subject: PA2 sampling OK on discovery, using tomer 2 TR0 X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=51326d7484b76388280ac572e60404829c384024;p=freetel-svn-tracking.git PA2 sampling OK on discovery, using tomer 2 TR0 git-svn-id: https://svn.code.sf.net/p/freetel/code@1788 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/stm32/src/stm32f4_adc.c b/codec2-dev/stm32/src/stm32f4_adc.c index 264186a2..b5ae4b1a 100644 --- a/codec2-dev/stm32/src/stm32f4_adc.c +++ b/codec2-dev/stm32/src/stm32f4_adc.c @@ -6,6 +6,18 @@ 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 + [ ] 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 + [ ] remove (or make optional) the TIM_Config() code that sends PWM output to pins + [ ] check comments still valid + \*---------------------------------------------------------------------------*/ /* @@ -35,50 +47,144 @@ #include "codec2_fifo.h" #include "gdb_stdio.h" -#include "stm32f4_adc.h" #define ADC_BUF_SZ 320 -#define FIFO_SZ 4*ADC_BUF_SZ +#define FIFO_SZ 8000 -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; +struct FIFO *DMA2_Stream0_fifo; +unsigned short adc_buf[ADC_BUF_SZ]; +int adc_overflow; +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 -static void Timer1Config(); -static void adc_configure(); +TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; +TIM_OCInitTypeDef TIM_OCInitStructure; +uint16_t uhTimerPeriod; +uint16_t aSRC_Buffer[3] = {0, 0, 0}; + +void Timer1Config(); +void adc_configure(); + +#define REC_TIME_SECS 5 +#define N 2000 +#define FS 16000 + +static void tim2_config(void); + +int main(void){ + short buf[N]; + FILE *frec; + int i, bufs; -void adc_open(void) { - adc1_fifo = fifo_create(FIFO_SZ); - adc2_fifo = fifo_create(FIFO_SZ); - assert(adc1_fifo != NULL); - assert(adc2_fifo != NULL); + DMA2_Stream0_fifo = fifo_create(FIFO_SZ); + assert(DMA2_Stream0_fifo != NULL); - Timer1Config(); + //Timer1Config(); + tim2_config(); adc_configure(); ADC_SoftwareStartConv(ADC1); + + frec = fopen("stm_out.raw", "wb"); + if (frec == NULL) { + printf("Error opening input file: stm_out.raw\n\nTerminating....\n"); + exit(1); + } + bufs = FS*REC_TIME_SECS/N; + + printf("Starting!\n"); + for(i=0; iPA1, ADC2->PA2 + // Analog pin configuration - GPIO_initStructre.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; + //GPIO_initStructre.GPIO_Pin = GPIO_Pin_1; // ADC Channel 10 is connected to PC0 + GPIO_initStructre.GPIO_Pin = GPIO_Pin_2; // ADC Channel 10 is connected to PC0 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. Note we are just using one ADC, which samples - // two analog channels when triggered by the timer. + // ADC structure configuration ADC_DeInit(); ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Left; ADC_init_structure.ADC_Resolution = ADC_Resolution_12b; ADC_init_structure.ADC_ContinuousConvMode = DISABLE; - ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC3; + //ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC3; + ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO; ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; - ADC_init_structure.ADC_NbrOfConversion = 2; - ADC_init_structure.ADC_ScanConvMode = ENABLE; - ADC_Init(ADC1,&ADC_init_structure); + ADC_init_structure.ADC_NbrOfConversion = 1; + ADC_init_structure.ADC_ScanConvMode = DISABLE; + ADC_Init(ADCx,&ADC_init_structure); - // Select the channels to be read from + // Select the channel to be read from - ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_144Cycles); - ADC_RegularChannelConfig(ADC1,ADC_Channel_2,2,ADC_SampleTime_144Cycles); + //ADC_RegularChannelConfig(ADCx,ADC_Channel_10,1,ADC_SampleTime_144Cycles); + ADC_RegularChannelConfig(ADCx,ADC_Channel_2,1,ADC_SampleTime_144Cycles); /* DMA configuration **************************************/ - DMA_DeInit(DMA2_Stream0); - DMA_InitStructure.DMA_Channel = DMA_Channel_0; + DMA_DeInit(DMA_STREAMx); + DMA_InitStructure.DMA_Channel = DMA_CHANNELx; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_buf; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; @@ -179,19 +320,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(DMA2_Stream0, &DMA_InitStructure); + DMA_Init(DMA_STREAMx, &DMA_InitStructure); /* Enable DMA request after last transfer (Single-ADC mode) */ - ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); + ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE); /* Enable ADC1 DMA */ - ADC_DMACmd(ADC1, ENABLE); + ADC_DMACmd(ADCx, ENABLE); /* DMA2_Stream0 enable */ - DMA_Cmd(DMA2_Stream0, ENABLE); + DMA_Cmd(DMA_STREAMx, ENABLE); /* Enable DMA Half & Complete interrupts */ @@ -215,9 +356,8 @@ void adc_configure() { */ void DMA2_Stream0_IRQHandler(void) { - int i, j, sam1, sam2; - short signed_buf1[ADC_BUF_SZ/4]; - short signed_buf2[ADC_BUF_SZ/4]; + int i, sam; + short signed_buf[ADC_BUF_SZ/2]; /* Half transfer interrupt */ @@ -226,19 +366,15 @@ void DMA2_Stream0_IRQHandler(void) { /* convert to signed */ - for(i=0,j=0; i