cancel
Showing results for 
Search instead for 
Did you mean: 

HAL Timer interrupt

martin239955
Associate II
Posted on July 30, 2015 at 19:24

Hello,

I tried to use the Timer interrupt. In the examples of the STM Discovery they used the HAL_TIM_PeriodElapsedCallback handle function. But with this does'nt work (The don't used HAL_NVIC_EnableIRQ(TIM2_IRQn), why?). Then I enabled the IRQ with HAL_NVIC_EnableIRQ(TIM2_IRQn). Now it calls everytime the Default Handler even if I implented the TIM2_IRQHandler function. What is the error I make?


//TIM2: APB1

// Timer channel 2 as base timer

void
timer2Init()

{

/* Variables */

TIM_HandleTypeDef initTimBase;


/* enable Clocks */

__HAL_RCC_TIM2_CLK_ENABLE();


// every 1ms an overflow

initTimBase.Instance = TIM2;

initTimBase.Init.CounterMode = TIM_COUNTERMODE_UP;

initTimBase.Init.Period = 32;

initTimBase.Init.Prescaler = 1344;

initTimBase.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

initTimBase.Init.RepetitionCounter = 0;



//HAL_TIM_Base_Start(&initTimBase);


//__HAL_TIM_CLEAR_IT(&initTimBase, TIM_IT_UPDATE);




HAL_TIM_Base_Init(&initTimBase); 

HAL_TIM_Base_Start_IT(&initTimBase);


HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);

HAL_NVIC_EnableIRQ(TIM2_IRQn);


}



void
TIM2_IRQHandler(
void
)

{

int
i = 0;

i++;


//HAL_TIM_IRQHandler(&initTimBase);

}

9 REPLIES 9
Posted on July 30, 2015 at 19:28

What is the error I make?

Are you doing this in a .CPP file?

What part specifically are you using?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
martin239955
Associate II
Posted on July 30, 2015 at 20:58

I use a normal .c file.

I have a stm32f411re nucleo board.

martin239955
Associate II
Posted on July 30, 2015 at 21:27

This is my new Code, but it calls everytime the Default Handler and not my TIM2_IRQHandler.


TIM_HandleTypeDef initTimBase;


//TIM2: APB1

// Timer channel 2 as base timer

void
timer2Init()

{

/* Variables */



/* enable Clocks */

__HAL_RCC_TIM2_CLK_ENABLE();


// every 1ms an overflow

initTimBase.Instance = TIM2;

initTimBase.Init.CounterMode = TIM_COUNTERMODE_UP;

initTimBase.Init.Period = 32;

initTimBase.Init.Prescaler = 1344;

initTimBase.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

initTimBase.Init.RepetitionCounter = 0;


HAL_TIM_Base_Init(&initTimBase); 
//MspInit haben keinen Rückgabewert

HAL_TIM_Base_Start_IT(&initTimBase);


HAL_NVIC_SetPriority(TIM2_IRQn, 4, 1);

HAL_NVIC_EnableIRQ(TIM2_IRQn);

}



void
TIM2_IRQHandler(
void
)

{

HAL_TIM_IRQHandler(&initTimBase);

}




// just defined interrupt function

/**

* @brief Period elapsed callback in non blockfaceing mode

* @param htim : TIM handle

* @retval None

*/

void
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

static
uint32_t 
time
= 0;

time
++;


// 1sek

if
(!(
time
% 1000))

{

if
(htim->Instance == TIM2)

{

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

}

}

}

Posted on July 30, 2015 at 21:48

Setup the NVIC *before* calling HAL_TIM_Base_Start_IT(), because it's going to fire immediately

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
martin239955
Associate II
Posted on July 31, 2015 at 20:48

I tried that too but no changes.


#include <stdio.h>

#include ''diag/Trace.h''

#include ''stm32f411xe.h''

#include ''stm32f4xx_hal.h''


void
timer2Init();



// ----- main() ---------------------------------------------------------------


// Sample pragmas to cope with warnings. Please note the related line at

// the end of this function, used to pop the compiler diagnostics status.

#pragma GCC diagnostic push

#pragma GCC diagnostic ignored ''-Wunused-parameter''

#pragma GCC diagnostic ignored ''-Wmissing-declarations''

#pragma GCC diagnostic ignored ''-Wreturn-type''


TIM_HandleTypeDef initTimBase;


int
main(
int
argc, 
char
* argv[])

{

/* Variables */

uint32_t i;


HAL_Init();

gpioInit();

timer2Init();


// Button: PC13

// LED: PA5


// Infinite loop

while
(1)

{

for
(i = 0; i<500000; i++){}

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

}

}




//TIM2: APB1

// Timer channel 2 as base timer

void
timer2Init()

{


/* enable Clocks */

__HAL_RCC_TIM2_CLK_ENABLE();


// every 1ms an overflow

initTimBase.Instance = TIM2;

initTimBase.Init.CounterMode = TIM_COUNTERMODE_UP;

initTimBase.Init.Period = 32;

initTimBase.Init.Prescaler = 1344;

initTimBase.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

initTimBase.Init.RepetitionCounter = 0;


HAL_TIM_Base_Init(&initTimBase); 
//MspInit haben keinen Rückgabewert

HAL_TIM_Base_Start_IT(&initTimBase);


HAL_NVIC_SetPriority(TIM2_IRQn, 4, 1);

HAL_NVIC_EnableIRQ(TIM2_IRQn);

}



void
TIM2_IRQHandler(
void
)

{

HAL_TIM_IRQHandler(&initTimBase);

} 




// just defined interrupt function

/**

* @brief Period elapsed callback in non blockfaceing mode

* @param htim : TIM handle

* @retval None

*/

void
HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

static
uint32_t 
time
= 0;

time
++;


// 1sek

if
(!(
time
% 1000))

{

if
(htim->Instance == TIM2)

{

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

}

}

}


#pragma GCC diagnostic pop


// ----------------------------------------------------------------------------

Posted on July 31, 2015 at 21:02

Pasted code doesn't appear to reflect that, or the cause of it being multiply defined all of a sudden.

Ok, so remove one of the definitions. The auto-generation probably built the one in the stm32f4xx_it.c, review it, decide if it's doing materially what you're current one is doing, or not, and delete one of them.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
martin239955
Associate II
Posted on August 01, 2015 at 21:58

In my project folder in eclipse does not exist a file named stm32f4xx_it.c.

Posted on August 02, 2015 at 04:17

The error you posted earlier

./src/stm32f4xx_it.o:/home/martin/workspace/STM32_New/Debug/../src/stm32f4xx_it.c:12: first defined here

Has the file in

/home/martin/workspace/STM32_New/src/stm32f4xx_it.c

The HAL/Cube stuff is outside of what I'm dealing with, the linker should provide some insight via the .MAP file, and you can examine the vector table, and perhaps a listing or disassembly of the created code.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
martin239955
Associate II
Posted on August 02, 2015 at 21:28

I created this file to test if it works. But it didn't. In my project there is any stm324fxx_it.c file