AnsweredAssumed Answered

ADC with DMA for Nucleo F303RE

Question asked by grossi.marco on Mar 31, 2017

Hello, I need to write software (using the Standard Pheripheral Libraries) for STM32 Nucleo F303RE (equipped with the microcontroller STM32F303RET6). I use STM32 AC6 Workbench as IDE.

The firmware should use DMA to sample two sine-waves using ADC measured values from two channels (PA_0 and PA_1) and make measurements at regular time intervals defined by TIMER TIM3. I developed such firmware for STM32L152RE and now I have to move to STM32F303RE. However I have no idea how to modify the firmware since I have not found any such example on the web. Following is the code for STM32L152RE. Anyone knows how to modify to make it work on STM32F303RE.

 

void Sample2Period (uint16_t my_timer_period){

            ADC_InitTypeDef ADC_InitStructure;

            ADC_CommonInitTypeDef ADC_CommonInitStructure;

            NVIC_InitTypeDef NVIC_InitStructure;

            DMA_InitTypeDef DMA_InitStructure;

            TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;

 

            RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

            /* DMA1 channel1 configuration */

            DMA_DeInit(DMA1_Channel1);

            DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;

            DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_array;

            DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

            DMA_InitStructure.DMA_BufferSize = WAVE_POINTS*4;

            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_Normal;

            DMA_InitStructure.DMA_Priority = DMA_Priority_High;

            DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

            DMA_Init(DMA1_Channel1, &DMA_InitStructure);

            /* Enable DMA1 channel1 */

            DMA_Cmd(DMA1_Channel1, ENABLE);

 

            /* Enable USART DMA1 Channel Transfer complete interrupt */

            DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);

 

            /* USART DMA1 Channel enable */

            DMA_Cmd(DMA1_Channel1, ENABLE);

 

            NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

 

            /* Enable the USART DMA1 IRQn Interrupt */

            NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;

            NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

            NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

            NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

            NVIC_Init(&NVIC_InitStructure);

 

            /* TIM3 Configuration ------------------------------------------------------*/

            /* TIM3 Periph clock enable */

            RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

            /* Time base configuration */

            //50K trigger generation

            TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

            TIM_TimeBaseStructure.TIM_Prescaler = 160-1;

            TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

            TIM_TimeBaseStructure.TIM_Period = my_timer_period-1;

            TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

 

            TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

            /* TIM2 TRGO selection */

            TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Update);

 

            ADC_DeInit(ADC1);

            /* Enable ADC1 clock */

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

            /* Enable ADC1 prescaler Conversione Veloce */

            ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div1;

            ADC_CommonInit(&ADC_CommonInitStructure);

            /* ADC1 Configuration -----------------------------------------------------*/

            ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;

            ADC_InitStructure.ADC_ScanConvMode = ENABLE;

            ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

            ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;

            ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;

            ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

            ADC_InitStructure.ADC_NbrOfConversion = 2;

            ADC_Init(ADC1, &ADC_InitStructure);

            /* ADC1 regular channel configuration */

            ADC_RegularChannelConfig(ADC1,ADC_Channel_19, 1, ADC_SampleTime_9Cycles);

            ADC_RegularChannelConfig(ADC1,ADC_Channel_20, 2, ADC_SampleTime_9Cycles);

 

            /* Enable the request after last transfer for DMA Circular mode */

            //ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);

 

            /* Enable ADC1 DMA */

            ADC_DMACmd(ADC1, ENABLE);

 

            /* Enable ADC1 */

            ADC_Cmd(ADC1, ENABLE);

 

            /* Wait until the ADC1 is ready */

            while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET);

 

            /* TIM2 enable counter */

            TIM_Cmd(TIM3, ENABLE);

}

Outcomes