Mar*_*in1 0 c timer interrupt stm32
我有一个STM32F302CBT6(运行在72MHz)项目,我需要测量4个信号的频率,每个信号约250kHz.信号连接到TIM1通道1-4.
现在,了解250kHz太快(或者是?)同时处理所有那些输入捕获中断(因为它们可能同步或同时发生......)我想逐个测量每个通道.我在程序开始时初始化了所有通道,并考虑在每个通道测量后逐个启用相应的中断.这是一个合适的想法还是我错过了什么?
问题是在为通道1提供第一个中断之后,下一个中断从未得到服务,因为虽然没有使能中断,但状态寄存器还有多个其他待处理中断(CCxIF和CCXOF,以及CxIF)以及设置的过捕获标志.我试图通过读取所有捕获值或设置TIMx-> SR = 0但没有帮助来避免此问题.
我将如何测量这些信号以及确保正确捕获每个通道的正确方法是什么?
我对此非常感兴趣,并希望了解如何进行这种处理或者如果你能指出我做错了什么.谢谢.
我目前的相关代码如下.
这是中断处理程序:
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
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的初始化代码,主要基于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);
}
Run Code Online (Sandbox Code Playgroud)
在主循环中,我有以下代码在上一个通道注册后触发下一个通道:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里的主要问题似乎是中断处理程序中通道2-4的括号放置中的拼写错误,TIM_GetITStatus(IC_TIMER, IC_CHx == SET)而不是TIM_GetITStatus(IC_TIMER, IC_CHx)
另一个问题是中断处理程序以任何顺序接受任何已启用通道上的数据,因此可能会跳过通道禁用步骤,因为第二/第四/第六或第八个样本已在另一个通道上捕获,然后继续引发缓冲区-溢出.
我的建议是重写中断处理程序,以便以任何顺序接受捕获的数据.在42 MHz的四个通道上以250 kHz的频率每次捕获产生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..
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以尝试编程四个DMA通道,以自动将来自每个源通道的数据并行捕获到目标缓冲区中,而无需CPU干预.如果丢失的捕获事件仍然存在问题,则此选项可提供可靠的低延迟时序.然而,我的经验是,这些外围设备可能会有点微妙的编程和受到各种限制,所以走这条路线不是我的第一选择.