使用 DMA 在我的 nucleo-L432KC 上设置时钟以进行 5 通道 adc 转换

setting up the clock on my nucleo-L432KC for a 5 channel adc conversion using DMA

所以我一直在尝试在我的 Nucleo-L432KC 上设置我的 ADC 转换,但遇到了困难。我最近一直在研究 Nucleo-F303RE,它是一个 64 针核,我的 5 通道 ADC 转换使用 DMA,效果很好。 然后我决定降级为 nucleo-L432KC(32 针核)。我使用了与 F303 相同的步骤,但这次它不起作用。我查看了一下,发现它永远不会进入 HAL_ADC_ConvCpltCallback。另一个 post 在谈论它 . the solution of this post says that the core clock was to slow compared to the adc conversion clock and so did not wave the time to call the function. i was looking into the clock systeme on CubeMx and have a hard time with the clock set up. the f303RE was more strait forward and this one i get kinda lost. any one have some tips on what i should change to make this work. here is the cube mx set up: voila。 这是我的 DMA 设置:

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);

}

这是我的 adc 设置:

static void MX_ADC1_Init(void)
{
  ADC_ChannelConfTypeDef sConfig;

     /**Common config 
      */
  hadc1.Instance = ADC1;
  hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
  hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  hadc1.Init.LowPowerAutoWait = DISABLE;
  hadc1.Init.ContinuousConvMode = DISABLE;
  hadc1.Init.NbrOfConversion = 5;
  hadc1.Init.DiscontinuousConvMode = DISABLE;
  hadc1.Init.NbrOfDiscConversion = 1;
  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;
  if (HAL_ADC_Init(&hadc1) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
 }

  /**Configure Regular Channel 
   */
  sConfig.Channel = ADC_CHANNEL_8;
  sConfig.Rank = ADC_REGULAR_RANK_1;
  sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5;
  sConfig.SingleDiff = ADC_SINGLE_ENDED;
  sConfig.OffsetNumber = ADC_OFFSET_NONE;
  sConfig.Offset = 0;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_9;
  sConfig.Rank = ADC_REGULAR_RANK_2;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_10;
  sConfig.Rank = ADC_REGULAR_RANK_3;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_11;
  sConfig.Rank = ADC_REGULAR_RANK_4;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }

    /**Configure Regular Channel 
    */
  sConfig.Channel = ADC_CHANNEL_12;
  sConfig.Rank = ADC_REGULAR_RANK_5;
  if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  {
    //_Error_Handler(__FILE__, __LINE__);
  }

}

最后是我的代码:

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef& hadc){
    if(hadc.Instance == ADC1){
        adc_IT_flag = 1;
    }
}
void filtreRead(void){
    fenGlisflex(ADC_BUF[0],flex0);
    fenGlisflex(ADC_BUF[1],flex1);
    fenGlisflex(ADC_BUF[2],flex2);
    fenGlisflex(ADC_BUF[3],flex3);
    fenGlisflex(ADC_BUF[4],flex4);
    data_stable(flex0);
    data_stable(flex1);
    data_stable(flex2);
    data_stable(flex3);
    data_stable(flex4);
    adc_IT_flag = 0;
    HAL_ADC_Start_IT(&hadc1);
}
int main(void)
{
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART2_UART_Init();
  MX_I2C1_Init();
  MX_USART1_UART_Init();
  MX_ADC1_Init();

  HAL_ADC_Start_DMA(&hadc1,(uint32_t*)ADC_BUF,5);
  HAL_ADC_Start_IT(&hadc1);

  while (1)
  {
      if(adc_IT_flag){
          filtreRead();
      }
  }
}

这是我的新 DMA Init 函数:

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



   hdma_adc1.Instance = DMA1_Channel1;
   hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
   hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
   hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
   hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
   hdma_adc1.Init.MemDataAlignment = DMA_PDATAALIGN_WORD;
   hdma_adc1.Init.Mode = DMA_CIRCULAR;
   hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
   HAL_DMA_DeInit(&hdma_adc1);
   HAL_DMA_Init(&hdma_adc1);

   //__HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1);
  /* DMA interrupt init */
  /* DMA1_Channel1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);

}

在您的 MX_DMA_Init() 函数中,您正在启用 DMA IT,但您缺少实际的 DMA 配置:

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

        hdma_adc1.Instance = DMA1_Channel1;
        hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
        hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
        hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
        hdma_adc1.Init.Mode = DMA_NORMAL;
        hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
        HAL_DMA_DeInit(&hdma_adc1);
        HAL_DMA_Init(&hdma_adc1);

        __HAL_LINKDMA(hadc, DMA_Handle, hdma_adc1);

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

}

编辑:其他一些事情 如果你想使用 DMA,你应该调用 HAL_ADC_Start_DMA() 而不是 HAL_ADC_Start_IT()。 如果缺少,您还应该将处理程序添加到 stm32xxxx_it.c 文件

void DMA1_Channel1_IRQHandler(void)
{
    /* Calling the peripheral interrupt handler */
    HAL_DMA_IRQHandler(&hdma_adc1);
}

我还建议为 DMA 使用 4 字节的倍数长度。

所以我发现了问题。 DMA 中断的 HAL 函数在 C++ 中不起作用。我已经用 C++ 转换了我的项目,但它无法工作,因为我用 Nucleo-F303RE 制作的版本是用 C 语言编写的。当我将它转换为 C++ 时,它也停止工作了。

编辑: 哇,你永远猜不到错误是什么。

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef& hadc){
    if(hadc.Instance == ADC1){
        adc_IT_flag = 1;
    }
}

实际上应该是:

void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc){
    if(hadc.Instance == ADC1){
        adc_IT_flag = 1;
    }
}

由于指针的原因,中断函数不会被调用