首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在IRQHandler中,执行从不跳转到DMA stm32f4105x。

在IRQHandler中,执行从不跳转到DMA stm32f4105x。
EN

Stack Overflow用户
提问于 2016-05-04 13:13:24
回答 3查看 2.1K关注 0票数 0

我试图使用DMA事务从STM32f401c-disco的ADC1 of STM32f4105VC获得1000个样本序列。我希望DMA在传输完成时生成一个中断,然后停止,这样就不会覆盖任何数据。以下是代码:

代码语言:javascript
复制
/* Private macro -------------------------------------------------------------*/
#define M 5
#define F_S 42000000
#define LOG_SIZE 1000
#define MEAS_PERIODS 1
#define DMA_BUFFER_SIZE 1000
/* Private variables ---------------------------------------------------------*/
__IO uint16_t uhADCxConvertedValue[DMA_BUFFER_SIZE] = {0};
__IO uint32_t uwADCxConvertedVoltage = 0;
double voltage=0;
double S=0, rms=0, F=0; // S quadratic sums, rms - the rms value, F the frequency
uint32_t N; //number of samplings
double unscaled_voltage=0;
uint32_t max_N=0, steady_N = 0;

typedef char MyString[50];
MyString Log[LOG_SIZE];



/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
static void ADC_Config(void);


void DMA2_Stream0_IRQHandler(void) {
    if(DMA_GetITStatus(DMA_STREAMx,DMA_IT_TCIF0)!=RESET) {
   DMA_ClearITPendingBit(DMA_STREAMx,DMA_STREAMx,DMA_IT_TCIF0|DMA_IT_HTIF0);
    }
}


/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main. 
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */



    double R = 3.0/0xfff; //conversion ratio
    double dc_offset = 1.5;
    double ac_scaling = 0.0022;
    double mv_filter[M]={0};//moving average filter
  uint8_t wave_pos=0; //0 upper half wave, 1 lower half wave
    uint32_t i=0;
    uint8_t i_period = 1;
  /* ADC configuration */
  ADC_Config();

  /* Start ADC Software Conversion */ 
//   ADC_SoftwareStartConv(ADCx);
    ADC_SoftwareStartConv(ADCx);
  while (1) {}
}

static void ADC_Config(void)
{
  ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
  GPIO_InitTypeDef      GPIO_InitStructure;
    NVIC_InitTypeDef            NVIC_InitStructure;

  /* Enable ADCx, DMA and GPIO clocks ****************************************/ 
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  RCC_AHB1PeriphClockCmd(ADCx_CHANNEL_GPIO_CLK, ENABLE);  
  RCC_APB2PeriphClockCmd(ADCx_CLK, ENABLE);

    /* Enable the DMA2 Stream0 Global Interrupt (to handle the Transfer Complete Interrupt TCIF) */
    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* DMA2 Stream0 channel15 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_CHANNELx;  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCxConvertedValue[0];
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = DMA_BUFFER_SIZE;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA_STREAMx, &DMA_InitStructure);
  DMA_Cmd(DMA_STREAMx, ENABLE);

  /* Configure ADC1 Channel15 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIO_PORT, &GPIO_InitStructure);

  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADCx, &ADC_InitStructure);

  /* ADC1 regular channel15 configuration **************************************/
  ADC_RegularChannelConfig(ADCx, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles);

//  /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE);

//   /* Enable ADC1 DMA */
  ADC_DMACmd(ADCx, ENABLE);

  /* Enable ADC1 */
  ADC_Cmd(ADCx, ENABLE);

    DMA_ITConfig(DMA_STREAMx,DMA_IT_TC,ENABLE);
}

当我调试程序一段时间后,DMA停止,标志TCIF0HTIF0设置在寄存器DMA_LISR中,尽管DMA2_Stream0_IRQHandler函数中的断点只被激活一次。为什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-04 14:55:50

只是为了那些面临类似问题的人。当DMA_IT_TCIF0同时被触发时,ADC中有一个溢出中断。因此,程序需要根据RM0090页403,13.8.1中所描述的顺序,使用DMA恢复程序。下面是更新的IRQHandler:

代码语言:javascript
复制
void DMA2_Stream0_IRQHandler(void) {
    if(DMA_GetITStatus(DMA_STREAMx,DMA_IT_TCIF0)!=RESET) {
        //clear the ADC->CR2 DDS and DMA flag to disable further DMA requests
        ADCx->CR2 &= ~(ADC_CR2_DDS|ADC_CR2_DMA);
        //do processing
        DMA_ClearITPendingBit(DMA_STREAMx,DMA_IT_TCIF0|DMA_IT_HTIF0);
        //abjust the DMA NDTR counter
        DMA_STREAMx->NDTR = (uint32_t)DMA_BUFFER_SIZE;
        DMA_STREAMx->CR |= (uint32_t)DMA_SxCR_EN;
        //set the DMA bit
        ADCx->CR2 |= (ADC_CR2_DMA|ADC_CR2_DDS);
        //clear the overload
        ADCx->SR &= ~(ADC_SR_OVR|ADC_SR_STRT);
        //start the ADC
        ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART;
    }
    ;
}

除此之外,其他一切都是一样的。

票数 0
EN

Stack Overflow用户

发布于 2016-05-04 13:32:55

您没有正确配置DMA中断。您需要发送IT配置函数DMA2_Stream0。将行DMA_ITConfig(DMA_CHANNELx, DMA_IT_TC, ENABLE);更改为DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);

票数 0
EN

Stack Overflow用户

发布于 2016-05-04 13:35:28

确保它是在startup_stmf4xx.S文件中定义的,并在正确的向量点声明。

代码语言:javascript
复制
    .long    DMA2_Stream0_IRQHandler           // DMA2 Stream 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37028884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档