AnsweredAssumed Answered

ADC values reading with DMA

Question asked by brent Kim on Jan 21, 2017
Latest reply on Jan 25, 2017 by Philippe Cherbonnel

I'm using STM32CubeMX to set 4 input channels of  ADC1 with DMA.

As my understanding, After "HAL_ADC_Start_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4)" execution, 

ADC result values can read form the buffer continuously without any control.

But only one time stored to the buffer by DMA.

 

I attaced my test code below. Please take a look and help me if anyone knows what is the problem

Thank you in advance.

 

int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration----------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* Configure the system clock */
SystemClock_Config();

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
MX_TIM1_Init();
MX_TIM2_Init();

/* USER CODE BEGIN 2 */
//-----------------------------------------------------------------------------

// HAL_GPIO_WritePin(GPIOB, RDRV_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GDRV_Pin, GPIO_PIN_SET);
// HAL_GPIO_WritePin(GPIOB, BDRV_Pin, GPIO_PIN_SET);


CbInit(&g_UartRxBuff, MAX_PACKET_SIZE);
MX_USART1_UART_Init();
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
// __HAL_UART_ENABLE_IT(&huart1, UART_IT_TXE);

 

// if(HAL_ADC_Start(&hadc1) != HAL_OK)
{
// printf("HAL_ADC_Start() has an Error!\r\n");
}

// HAL_TIM_Base_Start_IT(&htim1);

if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4) != HAL_OK)
{
printf("HAL_ADC_Start_DMA() has an Error!\r\n");
}

// HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/10);

 

// if(HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4) != HAL_OK)
{
// printf("HAL_ADCEx_MultiModeStart_DMA)_ has an Error!\r\n");
}

// HAL_UART_Transmit_IT(&huart1, "test 1234",9);
// HAL_UART_Transmit(&huart1,"1234 test", 9 ,10);
printf("---------- System Initialized!\r\n");

g_BoardID = GetBoardID();

// HAL_GPIO_WritePin(GPIOB, RDRV_Pin, GPIO_PIN_SET);
// Cputs("\r\123456789\r\n");
//-----------------------------------------------------------------------------
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// Txd(0x55);
// Txd(0xaa);

// AdcHandler();
CmdPacketParsing();
PacketHandler();


TestFunctionSelect();
switch(g_Cmd)
{
case 0x00:

break;
case 0x01:
printf("Adc1 Testing....\r\n");

// if(HAL_ADC_Start(&hadc1) != HAL_OK)
{
// printf("HAL_ADC_Start() has an Error!\r\n");
}
HAL_ADC_PollForConversion(&hadc1, 500);
uint32_t val = HAL_ADC_GetValue(&hadc1);
printf("ADC1 : %d\r\n", val);
// HAL_ADC_Stop(&hadc1);
break;

case 0x02:
if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4) != HAL_OK)
{
printf("HAL_ADC_Start_DMA() has an Error!\r\n");
}

printf("ADC1 : %d, %d, %d, %d\r\n",
g_Adc1Buff[0], g_Adc1Buff[1], g_Adc1Buff[2], g_Adc1Buff[3]);

if(HAL_ADC_Stop_DMA(&hadc1) != HAL_OK)
{
printf("HAL_ADC_Stop_DMA() has an Error!\r\n");
}

 

break;
case 0x03:

HAL_ADC_Stop(&hadc1);

if(HAL_ADC_Start(&hadc1) != HAL_OK)
{
printf("HAL_ADC_Start() has an Error!\r\n");
}

// HAL_ADC_Stop_DMA(&hadc1);
if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4) != HAL_OK)
{
printf("HAL_ADC_Start_DMA() has an Error!\r\n");
}

break;

case 0x04:
HAL_ADC_Stop_DMA(&hadc1);
if(HAL_ADC_Start_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4) != HAL_OK)
{
printf("HAL_ADC_Start_DMA() has an Error!\r\n");
}

if(HAL_ADC_Start(&hadc1) != HAL_OK)
{
printf("HAL_ADC_Start() has an Error!\r\n");
}

break;

case 0x05:
HAL_ADCEx_MultiModeStart_DMA(&hadc1, (uint32_t*)g_Adc1Buff, 4);

break;


case 0x06:
HAL_GPIO_WritePin(GPIOA, PHLSel_Pin, GPIO_PIN_SET);
break;
case 0x07:
HAL_GPIO_WritePin(GPIOA, PHLSel_Pin, GPIO_PIN_RESET);
break;
case 0x08:
HAL_GPIO_WritePin(GPIOA, NHLSel_Pin, GPIO_PIN_SET);
break;
case 0x09:
HAL_GPIO_WritePin(GPIOA, NHLSel_Pin, GPIO_PIN_RESET);
break;

}
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

}
/* USER CODE END 3 */

}

/** System Clock Configuration
*/
void SystemClock_Config(void)
{

RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}

/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}

PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_TIM1
|RCC_PERIPHCLK_ADC12;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
PeriphClkInit.Adc12ClockSelection = RCC_ADC12PLLCLK_DIV1;
PeriphClkInit.Tim1ClockSelection = RCC_TIM1CLK_HCLK;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}

/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* ADC1 init function */
static void MX_ADC1_Init(void)
{

ADC_MultiModeTypeDef multimode;
ADC_ChannelConfTypeDef sConfig;

/**Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 4;
hadc1.Init.DMAContinuousRequests = ENABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_SEQ_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}

/**Configure the ADC multi-mode
*/
multimode.DMAAccessMode = ADC_DMAACCESSMODE_DISABLED;
multimode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_1CYCLE;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
Error_Handler();
}

/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = 2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = 3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

/**Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = 4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}

}

/* TIM1 init function */
static void MX_TIM1_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;

htim1.Instance = TIM1;
htim1.Init.Prescaler = 47;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 10000;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}

}

/* TIM2 init function */
static void MX_TIM2_Init(void)
{

TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_SlaveConfigTypeDef sSlaveConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;

htim2.Instance = TIM2;
htim2.Init.Prescaler = 1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1000;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}

sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}

if (HAL_TIM_OC_Init(&htim2) != HAL_OK)
{
Error_Handler();
}

sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
sSlaveConfig.InputTrigger = TIM_TS_ITR2;
if (HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}

sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}

sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}

}

/* USART1 init function */
static void MX_USART1_UART_Init(void)
{

huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}

}

/* USART3 init function */
static void MX_USART3_UART_Init(void)
{

huart3.Instance = USART3;
huart3.Init.BaudRate = 460800;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}

}

/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();

/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

}

/** Configure pins as
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{

GPIO_InitTypeDef GPIO_InitStruct;

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, PHLSel_Pin|NHLSel_Pin, GPIO_PIN_RESET);

/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, BDRV_Pin|GDRV_Pin|RDRV_Pin, GPIO_PIN_RESET);

/*Configure GPIO pins : PHLSel_Pin NHLSel_Pin */
GPIO_InitStruct.Pin = PHLSel_Pin|NHLSel_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/*Configure GPIO pins : DipSw3_Pin DipSw2_Pin DipSw1_Pin DipSw0_Pin */
GPIO_InitStruct.Pin = DipSw3_Pin|DipSw2_Pin|DipSw1_Pin|DipSw0_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/*Configure GPIO pins : BDRV_Pin GDRV_Pin RDRV_Pin */
GPIO_InitStruct.Pin = BDRV_Pin|GDRV_Pin|RDRV_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* USER CODE END Error_Handler */
}

#ifdef USE_FULL_ASSERT

/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */

}

#endif

Outcomes