2023-04-20 03:06 AM - edited 2023-11-20 07:38 AM
I configure the ADC1 of the STM32H735 like bellow:
The ADC Clock = 20MHz
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
/* ADC1 init function */
void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.NbrOfDiscConversion = 1;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DMA_CIRCULAR;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc1.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
sConfig.OffsetSignedSaturation = DISABLE;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
{
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(adcHandle->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_ADC;
PeriphClkInitStruct.PLL3.PLL3M = 2;
PeriphClkInitStruct.PLL3.PLL3N = 40;
PeriphClkInitStruct.PLL3.PLL3P = 2;
PeriphClkInitStruct.PLL3.PLL3Q = 5;
PeriphClkInitStruct.PLL3.PLL3R = 25;
PeriphClkInitStruct.PLL3.PLL3RGE = RCC_PLL3VCIRANGE_3;
PeriphClkInitStruct.PLL3.PLL3VCOSEL = RCC_PLL3VCOWIDE;
PeriphClkInitStruct.PLL3.PLL3FRACN = 0;
PeriphClkInitStruct.AdcClockSelection = RCC_ADCCLKSOURCE_PLL3;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* ADC1 clock enable */
__HAL_RCC_ADC12_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC1 GPIO Configuration
PA0_C ------> ADC1_INP0
*/
HAL_SYSCFG_AnalogSwitchConfig(SYSCFG_SWITCH_PA0, SYSCFG_SWITCH_PA0_OPEN);
/* ADC1 DMA Init */
/* ADC1 Init */
hdma_adc1.Instance = DMA1_Stream1;
hdma_adc1.Init.Request = DMA_REQUEST_ADC1;
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc1.Init.Mode = DMA_CIRCULAR;
hdma_adc1.Init.Priority = DMA_PRIORITY_MEDIUM;
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
}
The ADC1_DR register = 0xFFE (I put 3.3V in PA0_C pin) it is ok.
but in the buffer, the dma transfer only the first byte :
I did not understand why the DMA always transfers a Byte despite I made several configuration combinations.
Best Regards,
2023-04-21 02:44 AM
Hello sir , My Dout is
1*. We must and should Enable the Bellow interrupt to Collect ADC Data ??
( In H7XX series and CubeIDE )
/* DMA interrupt init */
/* DMA1_Stream0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
2*. If we Disable the the DMA interrupt is it possible Recieve ADC data or Not ??
Please can you Clear this Dout
Thank You ,
Best Regards
Sharath A V
2023-04-21 02:59 AM
Where is your Callback Function ?? if your not wrote in your code write callback function in User code 4 and Check .
/* USER CODE BEGIN 4 */
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
{
for(int i=0; i<=4; i++)
{
aADCxConvertedData[i] = HAL_ADC_GetValue(&hadc1);
}
}
2023-04-21 03:11 AM
@SA V.1 Your code is ok as debug, but when use DMA we consciously avoid calling HAL_ADC_Get_Value. if use HAL_ADC_Start_DMA(&hadc1,(uint32_t*)aADCxConvertedData, 4 ) the function automatically fill aADCxConvertedData.
I have simillar problem far time ago and Enabling "lowPowerAutopWait" helped me
2023-04-21 04:16 AM
Yes, Your right but to simply check weather data is getting or not so that's why i said above function . And what you will write inside the Callback function ?? in my code i wrote
SCB_InvalidateDCache_by_Addr((uint32_t *) &ADC_Data[0], BUFFER_SIZE); can you share what you write ?? everything fine but my big Dout is DMA interrupt still i didnt get any clarity . i tried many without DMA interrupt not get any data .
Thank you for your support
best regards
Sharath A V
2023-04-21 04:21 AM
You don't need callback function to receive data through DMA.
It's OPTIONAL. Please read reference manual deeper
2023-04-23 06:51 AM
doubt..
The DMA proceeds regardless of the interrupt/callback.
You can check the HT/TC status for the DMA peripheral directly
2023-04-23 10:27 PM
@Saif Did you have any success?
2023-04-25 12:27 AM
Hi all
I don't know what I'm going to say, but maybe you don't believe me.
I put back the same configuration that I wrote in my question, and the ADC works correctly now despite there being no change.
Apparently, I will spend some time reproducing the problem to understand.
Best Regards,
2023-04-25 04:54 AM
Nice that it's working now!
Are you sure that you didn't change stuff elsewhere, like:
Oh, I just see the old CubeMX problem, putting DMA init after peripheral init using DMA.
In your 2nd source code post it is:
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC1_Init();
MX_DMA_Init(); <<<< this must be before any peripheral using DMA!
I guess you changed your main, maybe by updating CubeMX?
Known bug, and it bugs me that I haven't seen that earlier! ;)
2023-04-26 11:40 PM
@Saif really curious if it was the "infamous CubeMX DMA init order bug"... :D