2024-02-06 08:49 AM - edited 2024-02-06 08:52 AM
Hello,
I have set Timer2 Channel 1 as input capture for STM32F407G-DISC1 board and generated a code with STM32CobeMX.
Source code link: stm32-intro/timer2_general_input/Core/Src/main_app.c at 5d95347ebcc34a3142ddb65ea15c5016199e8dca · freecode23/stm32-intro · GitHub
1) In generated code, why am I getting HAL_TIM_Base_MspInit instead of HAL_TIM_IC_MspInit ?
2) In my main code I have to start the base timer then only IC callback is working.
3) Code always remains in callback and never come out of it, even it is not going into the while loop after starting the timer.
Thanks!
2024-02-06 09:01 AM - edited 2024-02-06 09:02 AM
Hello,
3) Code always remains in callback and never come out of it, even it is not going into the while loop after starting the timer.
It could be due to the frequency of the signal you are measuring. So the CPU is overloaded by executing the interrupt.
I invite you to refer to the example provided in STM32CubeF4 located under Projects\STM324xG_EVAL\Examples\TIM\TIM_InputCapture
You can even share your project including .ioc file to let community members to help you efficiently.
2024-02-06 09:30 AM - edited 2024-02-06 09:31 AM
From your program on github:
/**
* Callback when interrupt is triggered in input capture of timer2.
*/
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
// logUART("\ncount is=%d\r\n", count);
if (!is_capture_done) {
if (count == 1) {
input_captures[0] = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1);
count++;
logUART("\ncount == 1; count++%d\r\n", count);
} else if (count == 2) {
input_captures[1] = __HAL_TIM_GET_COMPARE(htim, TIM_CHANNEL_1);
count = 1;
is_capture_done = TRUE;
logUART("\ncount == 2; capture is done\r\n");
}
}
}
Never print (logUART()) in interrupt.
JW
2024-02-06 10:12 AM
Using long-term blocking functions in interrupt or callback context is never a good plan.
Use some setting or toggling GPIO/LEDs that can be set immediately and leave. High frequency toggling might be beyond visual comprehension, could use two LEDs with inverse phase, or use a scope.