I am using an STM32G431 in dual mode, attempting to capture a 10kHz wave. I have been having a hard time getting control of the sampling rate via the HAL library interface/prescalers. I noticed I can achieve the rates and control over said rates if I have the scan feature on. Without the scan feature, rates seem capped at ~300ksps and prescaler does little to affect it. With scan mode one, the data is better but I'm left with a bunch of other data (from the sequence of channels) that is garbage. If I reduce the # of conversions of the scan sequence, the sampling rate drops in proportion. Is there a way for me to get the sampling rate I need and am getting (>1msps) with the scan mode feature activated without using this feature?
I've attached my ADC init functions and an imagine of what my data is looking like (left shows low sampling rate, right shows high sampling rate with the garbage data)
[![
void
MX_ADC1_Init
(
void
)
{
ADC_MultiModeTypeDef multimode = {0};
ADC_ChannelConfTypeDef sConfig = {0};
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.GainCompensation = 0;
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc1.Init.EOCSelection =ADC_EOC_SINGLE_CONV;
hadc1.Init.LowPowerAutoWait = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.NbrOfConversion = 10;//ADC_BUFFER_LEN;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc1.Init.OversamplingMode = DISABLE;
/* oversmp.Ratio=ADC_OVERSAMPLING_RATIO_16;
oversmp.RightBitShift =0;
oversmp.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
oversmp.OversamplingStopReset = ADC_REGOVERSAMPLING_RESUMED_MODE;
hadc1.Init.Oversampling =oversmp;*/
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
}
// Configure the ADC multi-mode
multimode.Mode = ADC_DUALMODE_REGSIMULT;
multimode.DMAAccessMode = ADC_DMAACCESSMODE_12_10_BITS;
multimode.TwoSamplingDelay = ADC_TWOSAMPLINGDELAY_1CYCLE;
if (HAL_ADCEx_MultiModeConfigChannel(&hadc1, &multimode) != HAL_OK)
{
}
// Configure Regular Channel
sConfig.Channel = ADC_CHANNEL_15;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
//Error_Handler();
}
HAL_ADC_Start(&hadc2);
}
//=============================================================================
/**
@return
- none
Initializes adc2
*/
void
MX_ADC2_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
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV2;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc2.Init.EOCSelection =ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = ENABLE;
hadc2.Init.NbrOfConversion = 2;//ADC_BUFFER_LEN;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
hadc2.Init.OversamplingMode = DISABLE;
/*hadc2.Init.Oversampling =oversmp;*/
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
// Error_Handler();
}
sConfig.Channel = ADC_CHANNEL_17;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_6CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
//Error_Handler();
}
/* USER CODE END ADC1_Init 2 */
HAL_ADC_Start(&hadc2);
}