cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0308 TIM3 Interrupt not triggering

Tom Power
Associate II
Posted on August 16, 2017 at 16:46

Hi, 

I am having trouble getting an interrupt working with the TIM3, roughly following this guide: 

http://www.micromouseonline.com/2016/02/03/tim3-arr-regular-interrupts-stm32f4/

 

but adapting for the STM32F0308. 

TIM3 is running, and reloading (verified with the commented out code in the main loop ), but the interrupt flag does not appear to be triggering. 

Any help would be greatly appreciated, my code is below:

&sharpinclude 'stm32f0xx.h'

&sharpdefine BSRR_VAL        0x0300

volatile uint16_t count = 0;

void timer_interrupt_init( void );

void timer_init( void );

void timer_start( void );

void timer_interrupt_enable( void );

void leds_init( void );

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

void timer_interrupt_init( void )

{

NVIC_InitTypeDef NVIC_InitStructure;

/*Enable timer global interrupt */

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init( &NVIC_InitStructure );

}

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

void timer_init( void )

{

/*  Set timer to 1kHz and reload at 1Hz */

uint16_t prescaler = 23999; 

uint16_t reload = 1000; 

RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3, ENABLE );

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

/* Set everything back to default values */

TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );

/* Changes from defaults */

TIM_TimeBaseStructure.TIM_Period = reload;

TIM_TimeBaseStructure.TIM_Prescaler = prescaler;

TIM_TimeBaseInit( TIM3, &TIM_TimeBaseStructure );

}

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

void timer_start( void )

{

TIM_Cmd( TIM3, ENABLE );

}

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

void timer_interrupt_enable( void )

{

/* Clear interrupt flags */

TIM_ClearITPendingBit( TIM3, TIM_IT_Update );

/* Put counter into known state */

TIM_SetCounter( TIM3, 0 );

TIM_ITConfig( TIM3, TIM_IT_Update, ENABLE );

}

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

void leds_init( void )

{

RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_StructInit( &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init( GPIOC, &GPIO_InitStructure );

}

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

void TIM3_IRQHandler( void )

{

if ( count == 0 )

{

GPIOC->BSRR = BSRR_VAL;

count = 1;

}

else

{

GPIOC->BRR = BSRR_VAL;

count = 0;

}

TIM_ClearITPendingBit( TIM3, TIM_IT_Update );

}

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

int main(void)

{

uint16_t CurrentTimerVal;

timer_init();

timer_interrupt_init();

timer_start();

timer_interrupt_enable();

leds_init();

while (1)

{

//CurrentTimerVal = TIM_GetCounter( TIM3 );

//if ( CurrentTimerVal > 500 )

// GPIOC->BSRR = BSRR_VAL;

//else

// GPIOC->BRR = BSRR_VAL;

}

}

#stm32f0 #timer #interrups #nvic
7 REPLIES 7
Posted on August 16, 2017 at 17:47

C++?

JW

Posted on August 16, 2017 at 19:29

You're using C++ syntax, calling functions before definitions.

extern 'C' void TIM3_IRQHandler( void ) // use this form if using C++ or .CPP syntax/compilation

{

// Ideally validate the source

TIM_ClearITPendingBit( TIM3, TIM_IT_Update ); // Clear first to avoid Cortex-Mx NVIC hazard

if ( count == 0 )

{

GPIOC->BSRR = BSRR_VAL;

count = 1;

}

else

{

GPIOC->BRR = BSRR_VAL;

count = 0;

}

}

Look at the .MAP file. Check the vector in startup_stm32xxxx.s

Period and Prescaler are both programmed as N-1, not N

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 16, 2017 at 17:58

No, I am writing in C

Posted on August 16, 2017 at 18:11

Okay but don't you use by mistake a C++ compiler which would mangle function names thus avoid properly linking the ISR?

You should check the compiler outputs whether the ISR is properly linked to in the interrupts table.

JW

Posted on August 16, 2017 at 18:41

I am using gcc to compile - where would I be able to check if the ISR is linked exactly? 

Posted on August 16, 2017 at 18:59

I am using gcc to compile

How exactly? (i.e. post the command-line)

where would I be able to check if the ISR is linked exactly?

The ideal place is a disassembly as provided by objdump; alternatively look into the mapfile; if still in doubts, simply zip up and post everything you've got.

JW

Tom Power
Associate II
Posted on August 17, 2017 at 11:01

Waclawek.Jan

‌,

Turvey.Clive.002

,

Thank you both for you help, I managed to get it working by adding a function prototype for the IRQ3_Handler, and also by recompiling from scratch.