Why is analog dma data coming into buffer at 1 index position late?
(((Yes, I'm still using Atollic, STM32CubeMX, and HAL. Now is not the time in this project to change these things.)))
I'm individually driving my AN0..AN5 analog inputs, and I've confirmed that the ADC results are showing up in ADC_DMA_BUFFER[1..6]. I would have expected them in ADC_DMA_BUFFER[0..5]. Meanwhile, I'm configured for 7 conversions, the 7th being AN14. It's not showing up in either ADC_DMA_BUFFER[6] (the normally expected location) or ADC_DMA_BUFFER[7] (an otherwise buffer overflow location that was hinted at by the behavior of the first 6 channels). Finally, I'm unable to test if AN14 is erroneously going into ADC_DMA_BUFFER[0] (which would be a looped-back index location).
I've analyzed everything and I believe it's all lined up properly. Why might this be happening? Below is select cut/paste/images from my code and circuit. Thanks in advance for your help!
----------------------------------------------------------------------------------------
static void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** Common config
*/
hadc1.Instance = ADC1;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 7;
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_239CYCLES_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = ADC_REGULAR_RANK_2;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_2;
sConfig.Rank = ADC_REGULAR_RANK_3;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_4;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_5;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_5;
sConfig.Rank = ADC_REGULAR_RANK_6;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_14;
sConfig.Rank = ADC_REGULAR_RANK_7;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
HGF_ADC_INIT();
/* USER CODE END ADC1_Init 2 */
}
----------------------------------------------------------------------------------------
void HGF_ADC_INIT(void)
{
memset(ADC_DMA_BUFFER,0,sizeof(ADC_DMA_BUFFER)); // Clear DMA memory (mostly just for debugging purposes to make sure all array entries get non-zero data)
HAL_ADCEx_Calibration_Start(&hadc1); // Calibration the ADC on power up for better accuracy
}
----------------------------------------------------------------------------------------
#define ADC_NUMBER_CHANNELS (7) /* Using 7 analog channels: AN0, AN1, AN2, AN3, AN4, AN5, AN14 */
----------------------------------------------------------------------------------------
void HGF_ADC_START(void)
{
HAL_ADC_Start_DMA(&hadc1, ADC_DMA_BUFFER, ADC_NUMBER_CHANNELS); // Start ADC Conversion with DMA xfer to AD_RES (buffer length = # analog channels)
}
----------------------------------------------------------------------------------------
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
for (index_adc=0; index_adc<ADC_NUMBER_CHANNELS; ++index_adc) {
... do stuff with: ADC_DMA_BUFFER[index_adc];
}
}
----------------------------------------------------------------------------------------

