一、问题
- ADC配置DMA后,发现一直没有数据更新
二、排查
-
一路排查初始化配置,连续转换(注意需要2路以上才可以打开这个选项),DMA传送地址,传送方向,传送内存大小,传送内存地址递增,ADC1转换的数据是16位,传送的目的地址是16位宽度,DMA循环模式,等等等等,都没问题
但是这里能看到一个奇怪的现象,DMA的初始化,只是打开时钟,设置NVIC
而DMA配置,位于ADC初始化里面static void MX_DMA_Init(void) { /* Init with LL driver */ /* DMA controller clock enable */ LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_DMA1); /* DMA interrupt init */ /* DMA1_Channel1_IRQn interrupt configuration */ NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0)); NVIC_EnableIRQ(DMA1_Channel1_IRQn); }
static void MX_ADC1_Init(void) { /* USER CODE BEGIN ADC1_Init 0 */ /* USER CODE END ADC1_Init 0 */ 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}; /* Peripheral clock enable */ LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1); LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA); /**ADC1 GPIO Configuration PA4 ------> ADC1_IN4 PA5 ------> ADC1_IN5 */ GPIO_InitStruct.Pin = LL_GPIO_PIN_4|LL_GPIO_PIN_5; GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG; LL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* ADC1 DMA Init */ /* ADC1 Init */ 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); /* USER CODE BEGIN ADC1_Init 1 */ /* USER CODE END ADC1_Init 1 */ /** Common config */ 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); /** Configure Regular Channel */ 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); /** Configure Regular Channel */ 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); /* USER CODE BEGIN ADC1_Init 2 */ /* USER CODE END ADC1_Init 2 */ }
-
查看到生成的初始化,先执行了ADC初始化,再执行DMA初始化,而此时,DMA时钟未打开,配置无效,从而导致了问题!若配置DMA参数时,DMA时钟还没打开,则配置不生效!
/* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_USART1_UART_Init(); MX_DMA_Init(); MX_USART3_UART_Init();
-
这里给出一下需要用户自己额外设置的函数,可用于STM32F103系列
///> adc 部分宏定义 #define ADC_DMA_BUF_CH 2 /* 一共2个通道 */ #define ADC_DMA_BUF_LEN 8 /* 每个通道8个数据缓存 */ #define ADC_DMA_BUF_SIZE (ADC_DMA_BUF_LEN*ADC_DMA_BUF_CH) /* 总BUF大小 */ static uint16_t tAD[ADC_DMA_BUF_CH]; static uint16_t adcBuf[ADC_DMA_BUF_SIZE]; void adc_init() { /* Set DMA transfer addresses of source and destination */ 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);//配置DMA,将DMA与ADC1链接到一起 /* Set DMA transfer size */ LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_1,ADC_DMA_BUF_SIZE); //LL_DMA_SetPeriphAddress(DMA1,LL_DMA_CHANNEL_1,LL_ADC_DMA_GetRegAddr(ADC1,LL_ADC_DMA_REG_REGULAR_DATA)); //LL_DMA_SetMemoryAddress(DMA1,LL_DMA_CHANNEL_1,(uint32_t)&adcBuf[0]); /* Enable the DMA transfer */ LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_1);//使能DMA传输 LL_ADC_Enable(ADC1);//打开ADC LL_ADC_StartCalibration(ADC1); //开始校准 while (LL_ADC_IsCalibrationOnGoing(ADC1)); //等待校准完成 /* Start ADC group regular conversion */ LL_ADC_REG_StartConversionSWStart(ADC1);//启动ADC组常规转换,SWStart 与cubmx配置中 ADCs_Regular_ConversionMode 的 External Trigger Conversion Source ADC相对应 LL_ADC_REG_SetDMATransfer(ADC1,LL_ADC_REG_DMA_TRANSFER_UNLIMITED);//开始转换 //LL_ADC_REG_StartConversion(ADC1); } 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); /* 注意memcpy 还是按照8位copy 所以要*2进行copy */ return 1; } return 0; }