2015-04-04 03:47 PM
Hello, I'm Leonardo, from Argentina.
I've just started with STM32F4 Nucleo board and I'm trying to use CubeMX for Code generation. I configure TIM5 and 4 IC channel to generate Interrupt. So I'm studying the generated code and I found this: void TIM5_IRQHandler(void) { /* USER CODE BEGIN TIM5_IRQn 0 */ /* USER CODE END TIM5_IRQn 0 */ HAL_TIM_IRQHandler(&htim5); /* USER CODE BEGIN TIM5_IRQn 1 */ /* USER CODE END TIM5_IRQn 1 */ } Then I go to HAL_TIM_IRQHandler and I found this: void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim) { /* Capture compare 1 event */ if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET) { if(__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) !=RESET) { { __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1); htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1; /* Input capture event */ if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00) { HAL_TIM_IC_CaptureCallback(htim); } /* Output compare event */ else { HAL_TIM_OC_DelayElapsedCallback(htim); HAL_TIM_PWM_PulseFinishedCallback(htim); } htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED; } } } /* Capture compare 2 event */ if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC2) != RESET) What is HAL_TIM_IC_CaptureCallback(htim);? where do I have to put my code? BR BTW: The box where I write this message is too small!!!! And I don't see ''Insert Code'' to get beter format on it.... come on! #i-hate-this-word(tm)-editor2015-04-04 04:09 PM
BTW: The box where I write this message is too small!!!! And I don't see ''Insert Code'' to get better format on it.... come on!
The ''Format Code Block'' is the obvious ''Paintbrush [<>]'' icon in the top right of the editor. A quality piece of forum software from Microsoft.2015-04-04 04:37 PM
Ok, I see it!!!
And... what about the function callback? BR2015-04-05 05:03 PM
The callback is just a function which is called when this point in the ISR is reached.
You can declare the function where ever you want it with your own user specific code inside. Fromstm32f4xx_hal_tim.c it tells you. This is just a dummy declaration which does nothing and is declared as 'weak' so when you declare your own it will be preferred.__weak void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
/* NOTE : This function Should not be modified, when the callback is needed,
the __HAL_TIM_IC_CaptureCallback could be implemented in the user file
*/
}
So maybe in main.c somewhere you could put.
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
static counter = 0;
if( htim == &htim5 )
{
counter++;
}
}
or
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
static counter = 0;
if( htim->Instance == TIM5 )
{
counter++;
}
}
2015-04-07 02:31 PM
Thank! that is what I'm looking for!
Can you tell me how do I do to obtain the chanel that fire the ISR? I write this code on stm32f4xxit.cvoid
TIM5_IRQHandler(
void
)
{
HAL_TIM_IRQHandler(&htim5);
}
/* USER CODE BEGIN 1 */
void
HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
uint16_t
time
= 0;
static
uint16_t rise=0;
//Get the Captured timer
time
= HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_1);
//Check if it it Rising edge
if
(GPIOA->IDR & (1 << 0)){
// There is a problem with this aproach.
// if previous capture were 65000 and current capture is 23 so
// 23-65000 -> overflow. I think that this is filtered out by
// noise filter
rise =
time
;
}
else
{
//falling edge
//Return captured timer
IC1Capture =
time
;
}
}
But I would like to write code for each IC channel
BR!
2015-04-08 03:07 AM
if((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00)
{
HAL_TIM_IC_CaptureCallback(htim);
}
...
if((htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0x00)
{
HAL_TIM_IC_CaptureCallback(htim);
}
...
if((htim->Instance->CCMR2 & TIM_CCMR2_CC3S) != 0x00)
{
HAL_TIM_IC_CaptureCallback(htim);
}
...
if((htim->Instance->CCMR2 & TIM_CCMR2_CC4S) != 0x00)
{
HAL_TIM_IC_CaptureCallback(htim);
}
You could repeat this test inside your
HAL_TIM_IC_CaptureCallback
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim){
uint16_t time = 0;
static uint16_t rise=0;
if( htim->Instance == TIM5 )
{
/* CHANNEL 1 called ISR */
if( (htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0)
{
//Get the Captured timer
time = HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_1);
//Check if it it Rising edge
if(GPIOA->IDR & (1 <<
0
)){
// There is a problem with this aproach.
// if previous capture were 65000 and current capture is 23 so
// 23-65000 -> overflow. I think that this is filtered out by
// noise filter
rise = time;
}else{ //falling edge
//Return captured timer
IC1Capture = time;
}
}
/* CHANNEL 2 called ISR */
if( (htim->Instance->CCMR1 & TIM_CCMR1_CC2S) != 0)
{
//Get the Captured timer
time = HAL_TIM_ReadCapturedValue(&htim5, TIM_CHANNEL_2);
//Check if it it Rising edge
if(GPIOA->IDR & (1 <<
1
)){
// There is a problem with this aproach.
// if previous capture were 65000 and current capture is 23 so
// 23-65000 -> overflow. I think that this is filtered out by
// noise filter
rise = time;
}else{ //falling edge
//Return captured timer
IC2Capture = time;
}
}
// etc etc
}
}
Something along these lines. You could probably use ''else if'' instead of seperate if's or whatever suits your needs.
2015-04-09 05:01 PM
Ooooh, I see! I think that I allready understand!
Thank!2015-04-09 05:50 PM
i try to avoid register access if using the HAL, so that would be
if (htim->Instance == TIM2 && htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {
int32_t v = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
2015-04-10 06:40 PM
Ok, it's look better, I'll try it!
Thk