From 8062fc9e29a3f72efa81e3e8851d33fd68947b4a Mon Sep 17 00:00:00 2001 From: drowe67 Date: Wed, 12 Jun 2013 21:58:24 +0000 Subject: [PATCH] TRY2 appears to be working now, debugging TRY3 git-svn-id: https://svn.code.sf.net/p/freetel/code@1315 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/stm32/src/stm32f4_adc.c | 127 ++++++++++++++++------------- 1 file changed, 71 insertions(+), 56 deletions(-) diff --git a/codec2-dev/stm32/src/stm32f4_adc.c b/codec2-dev/stm32/src/stm32f4_adc.c index 9c6a17dc..43b3f55d 100644 --- a/codec2-dev/stm32/src/stm32f4_adc.c +++ b/codec2-dev/stm32/src/stm32f4_adc.c @@ -43,7 +43,7 @@ #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" -//#define TRY3 +#define TRY3 #ifdef TRY3 #define ADCx ADC1 @@ -54,7 +54,7 @@ static void ADC_Config(void); -volatile unsigned short uhADCConvertedValue[10]; +volatile unsigned short uhADCConvertedValue; int adc_convert(){ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion @@ -72,9 +72,7 @@ int main(void) while (1) { //uhADCConvertedValue = adc_convert(); - int i; - for(i=0; i<10; i++) - printf("ADC: %d \n", uhADCConvertedValue[i]); + printf("try 3: %d\n", uhADCConvertedValue); } } @@ -89,11 +87,13 @@ static void ADC_Config(void); * @param None * @retval None */ + static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; + /* Enable peripheral clocks *************************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); @@ -105,7 +105,7 @@ static void ADC_Config(void) DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; - DMA_InitStructure.DMA_BufferSize = 10; + DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; @@ -118,15 +118,15 @@ static void ADC_Config(void) DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA_STREAMx, &DMA_InitStructure); - /* DMA2_Stream0 enable */ - DMA_Cmd(DMA_STREAMx, ENABLE); - +#define TMP +#ifdef TMP /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); +#endif /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; @@ -138,10 +138,7 @@ static void ADC_Config(void) ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADCx, &ADC_InitStructure); - /* Enable ADC1 DMA */ - ADC_DMACmd(ADCx, ENABLE); - -#define VBAT + //#define VBAT #ifdef VBAT /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADCx, ADC_Channel_Vbat, 1, ADC_SampleTime_15Cycles); @@ -156,59 +153,50 @@ static void ADC_Config(void) /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE); + /* Enable ADC1 DMA */ + ADC_DMACmd(ADCx, ENABLE); + + /* DMA2_Stream0 enable */ + DMA_Cmd(DMA_STREAMx, ENABLE); + + /* Enable ADC1 **************************************************************/ ADC_Cmd(ADCx, ENABLE); } #endif -#define TRY2 +//#define TRY2 #ifdef TRY2 int ConvertedValue = 0; //Converted value readed from ADC #define ADCx_DR_ADDRESS ((uint32_t)0x4001204C) -volatile unsigned short uhADCxConvertedValue[10]; +volatile unsigned short uhADCConvertedValue; #define DMA_CHANNELx DMA_Channel_0 #define DMA_STREAMx DMA2_Stream0 +#define ADCx ADC1 + +void adc_configure(){ ADC_InitTypeDef ADC_init_structure; //Structure for adc confguration GPIO_InitTypeDef GPIO_initStructre; //Structure for analog input pin DMA_InitTypeDef DMA_InitStructure; -void adc_configure(){ - + // Clock configuration - //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);//The ADC1 is connected the APB2 peripheral bus thus we will use its clock source - RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE);//Clock for the ADC port!! Do not forget about this one ;) - /* DMA2 configuration **************************************/ - DMA_InitStructure.DMA_Channel = DMA_CHANNELx; - DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS; - DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCxConvertedValue; - DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; - DMA_InitStructure.DMA_BufferSize = 10; - DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; - DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; - DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; - DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; - DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; - DMA_InitStructure.DMA_Priority = DMA_Priority_High; - DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; - 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_Cmd(DMA_STREAMx, ENABLE); + // Analog pin configuration - //Analog pin configuration GPIO_initStructre.GPIO_Pin = GPIO_Pin_0;//The channel 10 is connected to PC0 GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN; //The PC0 pin is configured in analog mode GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down GPIO_Init(GPIOC,&GPIO_initStructre);//Affecting the port with the initialization structure configuration - //ADC structure configuration + // ADC structure configuration + ADC_DeInit(); ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096 @@ -217,35 +205,62 @@ void adc_configure(){ ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion ADC_init_structure.ADC_NbrOfConversion = 1;//I think this one is clear :p ADC_init_structure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel - ADC_Init(ADC1,&ADC_init_structure);//Initialize ADC with the previous configuration + ADC_Init(ADCx,&ADC_init_structure);//Initialize ADC with the previous configuration + + // Select the channel to be read from + + ADC_RegularChannelConfig(ADCx,ADC_Channel_10,1,ADC_SampleTime_144Cycles); + + /* DMA configuration **************************************/ + + 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)&uhADCConvertedValue; + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; + DMA_InitStructure.DMA_BufferSize = 1; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + 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); /* Enable DMA request after last transfer (Single-ADC mode) */ - ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); + + ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE); /* Enable ADC1 DMA */ - ADC_DMACmd(ADC1, ENABLE); - //Enable ADC conversion + ADC_DMACmd(ADCx, ENABLE); + + /* DMA2_Stream0 enable */ + + DMA_Cmd(DMA_STREAMx, ENABLE); + + // Enable ADC conversion + ADC_Cmd(ADC1,ENABLE); - //Select the channel to be read from - ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_144Cycles); } int adc_convert(){ - while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion - return ADC_GetConversionValue(ADC1); //Return the converted data + while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion + return ADC_GetConversionValue(ADC1); //Return the converted data } int main(void){ - adc_configure();//Start configuration - ADC_SoftwareStartConv(ADC1);//Start the conversion - while(1){//loop while the board is working - //ConvertedValue = adc_convert();//Read the ADC converted value - //printf("ConvertedValue = %d\n", ConvertedValue); - int i; - for(i=0; i<10; i++) - printf("ADC: %d \n", uhADCxConvertedValue[i]); + adc_configure(); + ADC_SoftwareStartConv(ADC1); + while(1){ + //ConvertedValue = adc_convert(); + //printf("ConvertedValue = %d\n", ConvertedValue); + printf("Try 2: %d\n",uhADCConvertedValue); } } #endif -- 2.25.1