cancel
Showing results for 
Search instead for 
Did you mean: 

Enable ADC1 in DMA mode on STEVAL-STLKT01

BBecs.1
Associate II

Hi,

I am trying to make the ADC1 on the SensorTile Evaluation board work in DMA mode. I have already gotten great help from @berendi​ on the interrupt mode for the ADC. I am still following this tutorial more or less: https://visualgdb.com/tutorials/arm/stm32/adc/

I have declared all necessary CB function, I think, and I double checked the spelling. The program does not fall into an error or hard fault handler anywhere, it just seems like the interrupts never trigger. The DMA configuration seems slightly different from the tutorial, so so far I was trying to get all the parameters right. Does anyone have experience with the ADC1 in DMA mode on the SensorTile board?

Sincerely,

void ConfigureDMA()
{
	__DMA2_CLK_ENABLE();
	hdma2.Instance = DMA2_Channel3;
	hdma2.ChannelIndex = 3;
	hdma2.DmaBaseAddress = DMA2;
 
	hdma2.Init.Request = DMA_REQUEST_0;
	hdma2.Init.Direction = DMA_PERIPH_TO_MEMORY;
	hdma2.Init.PeriphInc = DMA_PINC_DISABLE;
	hdma2.Init.MemInc = DMA_MINC_ENABLE;
	hdma2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
	hdma2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
	hdma2.Init.Mode = DMA_CIRCULAR;
	hdma2.Init.Priority = DMA_PRIORITY_HIGH;
 
	if (HAL_DMA_Init(&hdma2) != HAL_OK)
	{
		Error_Handler();
	}
 
	__HAL_LINKDMA(&hadc1, DMA_Handle, hdma2);
 
	HAL_NVIC_SetPriority(DMA2_Channel3_IRQn, 0x00, 0x00);
	HAL_NVIC_EnableIRQ(DMA2_Channel3_IRQn);
}
static void MX_ADC1_Init(void)
{
	ADC_ChannelConfTypeDef sConfig = {0};
	/** Common config **/
	hadc1.Instance = ADC1;
	hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
	hadc1.Init.Resolution = ADC_RESOLUTION_12B;
	hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
	hadc1.Init.ContinuousConvMode = ENABLE;
	hadc1.Init.DiscontinuousConvMode = DISABLE;
	hadc1.Init.NbrOfDiscConversion = 0;
	hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
	hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1;
	hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
	hadc1.Init.NbrOfConversion = 1;
	hadc1.Init.DMAContinuousRequests = ENABLE;
	hadc1.Init.EOCSelection = DISABLE;
	/* GPIOs and clocks are initialized herein */
	if (HAL_ADC_Init(&hadc1) != HAL_OK)
	{
		Error_Handler();
	}
 
	/** Configure Regular Channel
	 */
	sConfig.Channel = ADC_CHANNEL_3;
	sConfig.Rank = ADC_REGULAR_RANK_1;
	sConfig.SamplingTime = ADC_SAMPLETIME_247CYCLES_5;
	sConfig.OffsetNumber = ADC_OFFSET_NONE;
	sConfig.Offset = 0;
	if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
	{
		Error_Handler();
	}	
 
        ConfigureDMA();
	if (HAL_ADC_Start_DMA(&hadc1, g_ADCBuffer, ADC_BUFFER_LENGTH) != HAL_OK)
	{
		Error_Handler();
	}
 
}
void ADC1_IRQHandler()
{
	HAL_ADC_IRQHandler(&hadc1);
}
 
void DMA2_Channel3_IRQHandler()
{
	HAL_DMA_IRQHandler(&hdma2);
}
 
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
	if (hadc->Instance == ADC1)
	{
                Force = 0;
		for(int i=0; i<ADC_BUFFER_LENGTH; ++i)
		{
			Force += (g_ADCBuffer[i] / ADC_BUFFER_LENGTH);
		}
	}
}

0 REPLIES 0