cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery: HAL Timer Setup for Interrupt

groufosse
Associate II
Posted on June 13, 2014 at 17:58

Hi,

I'm hoping to get some help on this problem as the ST documents I can find do not provide anything more than commented code, and no sign of a working sample (as I need it) to be found. Even searching for HAL: on this forum is difficult because it returns every hit from someone called ''baird.hal''

Can anyone on this forum provide a working example, or the steps, to set up a simple peripheral countdown (or up) timer that runs an interrupt handler? And please explain just where the *user* code goes in the interrupt handler?

Arrrggghhhh....I didn't have this much trouble with the LPC1768, but understand this HAL layer is pretty new stuff. Could really use some input here...thanks!!

#hal-timer-interrupt-handler
5 REPLIES 5
Posted on June 13, 2014 at 18:30

Is there any particular reason why do you want to use the Cube/HAL stuff? Did you use this sort of ''library'' with the LPCxxxx?

There is a specific subforum at https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/AllItems.aspx

JW

groufosse
Associate II
Posted on June 13, 2014 at 20:45

Right. Unfortunately, all the current code samples for the Discovery board are using HAL...that's why.

That link is pretty much no good, as it's for development of cube, and not related to this issue.

Looks like ST has a winner in the HAL stuff. No one on the forum seems to know anything about it...they don't provide any documentation...and no sample code that works without breaking something else.

From: waclawek.jan

Posted: Friday, June 13, 2014 6:30 PM

Subject: STM32F4 Discovery: HAL Timer Setup for Interrupt

Is there any particular reason why do you want to use the Cube/HAL stuff? Did you use this sort of ''library'' with the LPCxxxx?

There is a specific subforum at https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/AllItems.aspx

JW

Posted on June 13, 2014 at 21:06

> Right. Unfortunately, all the current code samples for the Discovery board are using HAL...that's why.

So what. They are apparently useless for you.

There used to be a set of examples for the Discovery, it is certainly still floating around somewhere.

The ''Standard Peripheral Library'' itself contains a much more extensive set of examples, and while they are targeted at the EVAL line of boards, they are illustrative enough to grep the point out of them, and usually easy to port if not specific to EVAL's hardware.

I personally despise even the ''Standard peripheral library'', I see no reason to use something which is only a thin wrapper on the direct hardware access, while similarly badly documented. You should start with studying the user manual anyway, and once you're done, given a set of register definitions (which is provided in a single stm32f4xxx.h header), you already know how to write a timer/interrupt blinkey. YMMV.

> That link is pretty much no good, as it's for development of cube, and not related to this issue.

Hardly so, this is a user forum and cube is an inside project of ST. Also, it appears to be more closely monitored (by the Cube developers - but don't expect a timely answer, more like they do a couple of random picks once in week or so), so that's why I believe it's a more appropriate place to complain about the poor quality of documentation, if you don't like the autovomited doxygen stuff.

JW

raptorhal2
Lead
Posted on June 14, 2014 at 04:20

I didn't know I was such a big hit. But my birth certificate is older than HAL's.

Here is a timer capture example extraction from the F4 Standard Peripheral Library. It answers your questions about setting up the timer and what might go into the peripheral handler. It is not in HAL format, so it gets you closer to the peripheral.

Cheers, Hal

int main(void)

{

  /*!< At this stage the microcontroller clock setting is already configured,

       this is done through SystemInit() function which is called from startup

       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)

       before to branch to application main.

       To reconfigure the default setting of SystemInit() function, refer to

       system_stm32f4xx.c file

     */     

       

  /* TIM1 Configuration */

  TIM_Config();

  /* TIM1 configuration: Input Capture mode ---------------------

     The external signal is connected to TIM1 CH2 pin (PE.11)  

     The Rising edge is used as active edge,

     The TIM1 CCR2 is used to compute the frequency value

  ------------------------------------------------------------ */

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;

  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_ICInit(TIM1, &TIM_ICInitStructure);

 

  /* TIM enable counter */

  TIM_Cmd(TIM1, ENABLE);

  /* Enable the CC2 Interrupt Request */

  TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);

  while (1);

}

/**

  * @brief  Configure the TIM1 Pins.

  * @param  None

  * @retval None

  */

static void TIM_Config(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

 

  /* TIM1 clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  /* GPIOA clock enable */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

 

  /* TIM1 channel 2 pin (PE.11) configuration */

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOE, &GPIO_InitStructure);

  /* Connect TIM pins to AF2 */

  GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_TIM1);

 

  /* Enable the TIM1 global Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

 

}

/******************************************************************************/

/*            STM32F4xx Peripherals Interrupt Handlers                        */

/******************************************************************************/

/**

  * @brief  This function handles TIM1 global interrupt request.

  * @param  None

  * @retval None

  */

void TIM1_CC_IRQHandler(void)

{

  if(TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET)

  {

    /* Clear TIM1 Capture compare interrupt pending bit */

    TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);

    if(uhCaptureNumber == 0)

    {

      /* Get the Input Capture value */

      uhIC3ReadValue1 = TIM_GetCapture2(TIM1);

      uhCaptureNumber = 1;

    }

    else if(uhCaptureNumber == 1)

    {

      /* Get the Input Capture value */

      uhIC3ReadValue2 = TIM_GetCapture2(TIM1);

      

      /* Capture computation */

      if (uhIC3ReadValue2 > uhIC3ReadValue1)

      {

        uwCapture = (uhIC3ReadValue2 - uhIC3ReadValue1);

      }

      else if (uhIC3ReadValue2 < uhIC3ReadValue1)

      {

        uwCapture = ((0xFFFF - uhIC3ReadValue1) + uhIC3ReadValue2);

      }

      else

      {

        uwCapture = 0;

      }

      /* Frequency computation */

      uwTIM1Freq = (uint32_t) SystemCoreClock / uwCapture;

      uhCaptureNumber = 0;

    }

  }

}

groufosse
Associate II
Posted on June 16, 2014 at 21:38

Space Odyssey this isn't. 8--)

Thanks for the code example. In the last couple of days, I've discovered that HAL is pretty much beta code. *Extremely* annoying that they should release this with so many buggy things, and so much not tested. It should make a developer's life easier, but goes the other way.

I found another post by clive2 that had a link to the STD peripherals library code, which I just downloaded and installed. That, in combination with your snippet, should be enough to get things going.

Thanks!