STM32多通道输入捕获在所有通道上过度捕获(中断未执行)

STM32 multi channel input capture overcapturing on all channels (interrupts not getting executed)

我有一个 STM32F302CBT6(运行 72MHz)项目,我需要测量 4 个信号的频率,每个大约 250kHz。信号连接到 TIM1 通道 1 - 4。

现在,了解 250kHz 太快了(或者是吗?),无法同时处理所有这些输入捕获中断(因为它们可能同步或同时发生......)我想测量每个通道逐个。我在程序开始时初始化了所有通道,并想在测量每个通道后逐个启用相应的中断。这是一个合适的想法还是我错过了什么?

问题是,在为通道 1 提供第一个中断后,下一个中断永远不会得到服务,因为虽然中断未启用,但状态寄存器有多个其他中断挂起(CCxIF 和 CCXOF,还有 CxIF),而且过度捕获标志设置。我试图通过读取所有捕获值或设置 TIMx->SR = 0 来避免此问题,但没有帮助。

我将如何测量这些信号以及确保正确捕获每个通道的正确方法是什么?

我对此一头雾水,希望能对这种处理方式 is/should 有所了解,或者您能否指出我做错了什么。谢谢

我当前的相关代码就在下面。

这是中断处理程序:

void TIM1_CC_IRQHandler(void) {
if (TIM_GetITStatus(IC_TIMER, IC_CH1) == SET) {
    /* Clear TIM1 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(IC_TIMER, IC_CH1);

    //Read the capture value
    raw_captures[capture_index] = TIM_GetCapture1(IC_TIMER);
    capture_index++;

    //Also read the others to avoid overcaptures
    TIM_GetCapture2(IC_TIMER);
    TIM_GetCapture3(IC_TIMER);
    TIM_GetCapture4(IC_TIMER);

    if(capture_index == 2) {
        TIM_ITConfig(IC_TIMER, IC_CH1, DISABLE);
    }
} else if (TIM_GetITStatus(IC_TIMER, IC_CH2 == SET)) {
    TIM_ClearITPendingBit(IC_TIMER, IC_CH2);

    //Read the capture value
    raw_captures[capture_index] = TIM_GetCapture2(IC_TIMER);
    capture_index++;

    TIM_GetCapture1(IC_TIMER);
    TIM_GetCapture3(IC_TIMER);
    TIM_GetCapture4(IC_TIMER);

    if(capture_index == 4) {
        TIM_ITConfig(IC_TIMER, IC_CH2, DISABLE);
    }
} else if (TIM_GetITStatus(IC_TIMER, TIM_IT_CC3 == SET)) {

    //Read the capture value
    raw_captures[capture_index] = TIM_GetCapture3(IC_TIMER);
    capture_index++;

    TIM_GetCapture1(IC_TIMER);
    TIM_GetCapture2(IC_TIMER);
    TIM_GetCapture4(IC_TIMER);

    if(capture_index == 6) {
        TIM_ITConfig(IC_TIMER, IC_CH3, DISABLE);
    }
} else if (TIM_GetITStatus(IC_TIMER, TIM_IT_CC4 == SET)) {
    TIM_ClearITPendingBit(IC_TIMER, TIM_IT_CC4);

    //Read the capture value
    raw_captures[capture_index] = TIM_GetCapture4(IC_TIMER);
    capture_index++;

    TIM_GetCapture2(IC_TIMER);
    TIM_GetCapture3(IC_TIMER);
    TIM_GetCapture1(IC_TIMER);

    if(capture_index == 8) {
        TIM_ITConfig(IC_TIMER, IC_CH4, DISABLE);
    }
} else {
    //LOG_WARNING("Unhandled interrupt in the TIM1_CC_IRQHandler"NL);
    IC_TIMER->SR = 0; //Clear all other pending interrupts
}
}

这是我的初始化代码,主要基于 Std_Periph_Example:

void input_capture_setup(void) {

GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;

/* TIM clock enable */
RCC_APB2PeriphClockCmd(IC_CLK, ENABLE);

/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(IC_PORT_CLK, ENABLE);

/* TIM1 channels 1 - 4  pins PA8 - PA11 configuration */
GPIO_InitStructure.GPIO_Pin = IC1_PIN | IC2_PIN | IC3_PIN | IC4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* Connect TIM pins to AF1 */
GPIO_PinAFConfig(IC_PORT, IC1_PINSRC, GPIO_AF_6);
GPIO_PinAFConfig(IC_PORT, IC2_PINSRC, GPIO_AF_6);
GPIO_PinAFConfig(IC_PORT, IC3_PINSRC, GPIO_AF_6);
GPIO_PinAFConfig(IC_PORT, IC4_PINSRC, GPIO_AF_11);

/* Enable the TIM global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* TIM configuration: Input Capture mode ---------------------
 The external signals are connected to TIM1 CH1 - CH4 pin (PA8 - PA11)
 The Rising edge is used as active edge,
 The TIM1 CCR1 - CCR4 are used to compute the frequency value
 ------------------------------------------------------------ */
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8;
TIM_ICInitStructure.TIM_ICFilter = 0x0;

//Initialize all channels one by one
TIM_ICInitStructure.TIM_Channel =  IC1;
TIM_ICInit(IC_TIMER, &TIM_ICInitStructure);

TIM_ICInitStructure.TIM_Channel =  IC2;
TIM_ICInit(IC_TIMER, &TIM_ICInitStructure);

TIM_ICInitStructure.TIM_Channel =  IC3;
TIM_ICInit(IC_TIMER, &TIM_ICInitStructure);

TIM_ICInitStructure.TIM_Channel =  IC4;
TIM_ICInit(IC_TIMER, &TIM_ICInitStructure);

/* TIM enable counter */
TIM_Cmd(IC_TIMER, ENABLE);

}

在主循环中,我有以下代码在注册前一个频道后触发下一个频道:

void node_handle_capture(void) {

if(!capture_enabled) {
    TIM_ITConfig(IC_TIMER, IC_CH1, ENABLE);
    capture_enabled = true;
}
else {
    switch (capture_index) {
        case 2:
            LOG_DEBUG("CH1 captured"NL);
            TIM_ITConfig(IC_TIMER, IC_CH2, ENABLE);
            break;
        case 4:
            LOG_DEBUG("CH2 captured"NL);
            TIM_ITConfig(IC_TIMER, IC_CH3, ENABLE);
            break;
        case 6:
            LOG_DEBUG("CH3 captured"NL);
            TIM_ITConfig(IC_TIMER, IC_CH4, ENABLE);
            break;
        case 8:
            LOG_DEBUG("All channels captured"NL);
            capture_index = 0;
            break;
        default:
            break;
    }
}
}

这里的主要问题似乎是中断处理程序中通道 2-4 的括号位置中的错字,TIM_GetITStatus(IC_TIMER, IC_CHx == SET) 而不是 TIM_GetITStatus(IC_TIMER, IC_CHx)

另一个问题是中断处理程序以任何顺序接受任何已启用通道上的数据,因此可能会跳过通道禁用步骤,因为第二/第四/第六或第八个样本已在另一个通道上捕获,然后继续引发缓冲区溢出。

我的建议是重写中断处理程序,以便以任何顺序接受捕获的数据。在 250 kHz 时,在 72 MHz 的四个通道上,每次捕获产生 72 个周期,这应该可以通过仔细编写代码来实现。

这些可能是完全未经测试的内容:

enum { SAMPLES_PER_CHANNEL = 2 };
struct Capture_Buffer_t {
    volatile size_t index;
    volatile uint32_t data[SAMPLES_PER_CHANNEL];
} capture_channels[4];

void TIM1_CC_IRQHandler(void) {
    // Determine and acknowledge all latched channels still enabled
    TIM_TypeDef *const timer = IC_TIMER;
    uint_fast16_t enable = timer->DIER;
    uint_fast16_t status = timer->SR & enable;
    timer->SR = ~status;

    // Process each flagged channel in order
    do {
        // Extract the first set status bit
        uint_fast16_t flag = status & -status;
        status &= ~flag;

        // Read out the capture value and decode the status bit into a channel index
        uint_fast32_t sample;
        struct Capture_Buffer_t *buffer;
        switch(flag) {
        case IC_CH4:
            sample = timer->CCR1;
            buffer = &capture_channels[0];
            break;
        case IC_CH3:
            sample = timer->CCR2;
            buffer = &capture_channels[1];
            break;
        case IC_CH2:
            sample = timer->CCR3;
            buffer = &capture_channels[2];
            break;
        case IC_CH1:
        default:
            sample = timer->CCR4;
            buffer = &capture_channels[3];
            break;
        }

        // Store the sample into the appropriate buffer
        size_t index = buffer->index;
        buffer->data[index++] = sample;
        buffer->index = index;

        // Disable interrupts for the channel once its buffer has been filled
        if(index == SAMPLES_PER_CHANNEL)
            enable &= ~status;
    // Continue until all flagged channels have been inspected
    } while(status);

    // Finally commit the new interrupt status
    timer->DIER = enable;
}

...

// Have all channels completed yet?
if(!IC_TIMER->DIER) {
    // Then process the data..
}

或者,您可以尝试对四个 DMA 通道进行编程,以自动从每个源通道并行捕获数据到目标缓冲区,而无需 CPU 干预。如果丢失捕获事件的问题仍然存在,此选项可提供可靠的低延迟计时。但是我的经验是,这些外设在编程上可能有些微妙,并且受到各种限制,所以走这条路不是我的第一选择。