一、问题
- ADC配置DMA后,发现一直没有数据更新
二、排查
-
一路排查初始化配置,连续转换(注意需要2路以上才可以打开这个选项),DMA传送地址,传送方向,传送内存大小,传送内存地址递增,ADC1转换的数据是16位,传送的目的地址是16位宽度,DMA循环模式,等等等等,都没问题 但是这里能看到一个奇怪的现象,DMA的初始化,只是打开时钟,设置NVIC 而DMA配置,位于ADC初始化里面 static void MX_DMA_Init(void)
{
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1);
NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}
static void MX_ADC1_Init(void)
{
LL_ADC_InitTypeDef ADC_InitStruct = {0};
LL_ADC_CommonInitTypeDef ADC_CommonInitStruct = {0};
LL_ADC_REG_InitTypeDef ADC_REG_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
GPIO_InitStruct.Pin = LL_GPIO_PIN_4|LL_GPIO_PIN_5;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_HIGH);
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_CIRCULAR);
LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_HALFWORD);
ADC_InitStruct.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
ADC_InitStruct.SequencersScanMode = LL_ADC_SEQ_SCAN_ENABLE;
LL_ADC_Init(ADC1, &ADC_InitStruct);
ADC_CommonInitStruct.Multimode = LL_ADC_MULTI_INDEPENDENT;
LL_ADC_CommonInit(__LL_ADC_COMMON_INSTANCE(ADC1), &ADC_CommonInitStruct);
ADC_REG_InitStruct.TriggerSource = LL_ADC_REG_TRIG_SOFTWARE;
ADC_REG_InitStruct.SequencerLength = LL_ADC_REG_SEQ_SCAN_ENABLE_2RANKS;
ADC_REG_InitStruct.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
ADC_REG_InitStruct.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;
ADC_REG_InitStruct.DMATransfer = LL_ADC_REG_DMA_TRANSFER_UNLIMITED;
LL_ADC_REG_Init(ADC1, &ADC_REG_InitStruct);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_4);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_1CYCLE_5);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, LL_ADC_CHANNEL_5);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_5, LL_ADC_SAMPLINGTIME_1CYCLE_5);
}
-
查看到生成的初始化,先执行了ADC初始化,再执行DMA初始化,而此时,DMA时钟未打开,配置无效,从而导致了问题!若配置DMA参数时,DMA时钟还没打开,则配置不生效!
MX_GPIO_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
MX_DMA_Init();
MX_USART3_UART_Init();
-
这里给出一下需要用户自己额外设置的函数,可用于STM32F103系列
#define ADC_DMA_BUF_CH 2
#define ADC_DMA_BUF_LEN 8
#define ADC_DMA_BUF_SIZE (ADC_DMA_BUF_LEN*ADC_DMA_BUF_CH)
static uint16_t tAD[ADC_DMA_BUF_CH];
static uint16_t adcBuf[ADC_DMA_BUF_SIZE];
void adc_init()
{
LL_DMA_ConfigAddresses(DMA1,
LL_DMA_CHANNEL_1,
LL_ADC_DMA_GetRegAddr(ADC1, LL_ADC_DMA_REG_REGULAR_DATA),
(uint32_t)&adcBuf,
LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_1,ADC_DMA_BUF_SIZE);
LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_1);
LL_ADC_Enable(ADC1);
LL_ADC_StartCalibration(ADC1);
while (LL_ADC_IsCalibrationOnGoing(ADC1));
LL_ADC_REG_StartConversionSWStart(ADC1);
LL_ADC_REG_SetDMATransfer(ADC1,LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
}
uint8_t adc_get( uint16_t *adc_data_buf)
{
if ( LL_DMA_IsActiveFlag_TC1(DMA1) )
{
uint16_t i,j;
uint16_t * tadBuf;
tadBuf = & adcBuf[0];
for(i = 0; i < ADC_DMA_BUF_CH; i++)
{
tAD[i] = 0;
}
for(i = 0; i < ADC_DMA_BUF_CH; i++)
{
for(j = 0; j < ADC_DMA_BUF_LEN; j++)
{
tAD[i] = tAD[i] + tadBuf[j*ADC_DMA_BUF_CH + i];
}
tAD[i] = tAD[i] / ADC_DMA_BUF_LEN;
}
memcpy(adc_data_buf,tAD,ADC_DMA_BUF_CH*2);
return 1;
}
return 0;
}
|