Skip to main content
leogarberoglio
Associate III
April 4, 2015
Question

Function callback on ISR generated by CubeMX

  • April 4, 2015
  • 8 replies
  • 1763 views
Posted on April 05, 2015 at 00:47

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)-editor
    This topic has been closed for replies.

    8 replies

    Tesla DeLorean
    Guru
    April 4, 2015
    Posted on April 05, 2015 at 01:09

    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.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    leogarberoglio
    Associate III
    April 4, 2015
    Posted on April 05, 2015 at 01:37

    Ok, I see it!!!

    And... what about the function callback?

    BR

    joe
    Associate III
    April 6, 2015
    Posted on April 06, 2015 at 02:03

    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++;
    }
    }

    leogarberoglio
    Associate III
    April 7, 2015
    Posted on April 07, 2015 at 23:31

    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.c

    void
    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!
    joe
    Associate III
    April 8, 2015
    Posted on April 08, 2015 at 12:07 Just looking insidevoid HAL_TIM_IRQHandler() whereHAL_TIM_IC_CaptureCallback(htim); get called for each channel.

    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.
    leogarberoglio
    Associate III
    April 10, 2015
    Posted on April 10, 2015 at 02:01

    Ooooh, I see! I think that I allready understand!

    Thank!

    noobee
    Associate II
    April 10, 2015
    Posted on April 10, 2015 at 02:50

    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);

    leogarberoglio
    Associate III
    April 11, 2015
    Posted on April 11, 2015 at 03:40

    Ok, it's look better, I'll try it!

    Thk