cancel
Showing results for 
Search instead for 
Did you mean: 

TIM3 interrupt BusFault_Handler

todortur
Associate II
Posted on February 10, 2014 at 18:16

Hi all,

I'm new to STM32 and i'm having some problems with making TIM3 interrupt work on my STM32F3DISCOVERY.

The problem is that when i run the code on the discovery i get in the BusFault_Handler.

My code:

GPIO_InitTypeDef GPIO_InitStruct;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

NVIC_InitTypeDef NVIC_Struct;

void TIM3_IRQHandler(void)

{

  if(TIM3->SR & TIM_SR_UIF) // if UIF flag is set

  {

    TIM3->SR &= ~TIM_SR_UIF; // clear UIF flag

    GPIOE->ODR ^= (1<<7);

  }    

}

int main(void)

{

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE|RCC_AHBPeriph_GPIOA, ENABLE);

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;

  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;

  GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOE, &GPIO_InitStruct);

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

                    TIM3 code bellow

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

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  TIM_TimeBaseInitStruct.TIM_Prescaler = 7999;

  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInitStruct.TIM_Period = 1000;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);

 

  TIM_ClearFlag(TIM3, TIM_FLAG_Update);

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

 

  TIM_Cmd(TIM3, ENABLE);

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

                    NVIC code bellow

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

  NVIC_Struct.NVIC_IRQChannel = TIM3_IRQn;

  NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_Struct.NVIC_IRQChannelSubPriority = 1;

  NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_Struct);

  NVIC_EnableIRQ(TIM3_IRQn);  

  while (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0);

  GPIO_WriteBit(GPIOE, GPIO_Pin_8, Bit_SET);

  while(1)

  {

  }

 

  return 0;

}

Any suggestions?

Thanks in advance!

8 REPLIES 8
Posted on February 10, 2014 at 18:32

Using what tools?

C++?

Unrelated, but

Don't do this

  TIM3->SR &= ~TIM_SR_UIF; // clear UIF flag

do this

  TIM3->SR = ~TIM_SR_UIF; // clear UIF flag
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
todortur
Associate II
Posted on February 10, 2014 at 18:37

I'm using IAR Embedded Workbench.

Language : C (dialect C99)

Posted on February 10, 2014 at 18:53

Yeah, would probably move the NVIC initialization up before I enabled the TIM3's interrupt. Would lose

NVIC_EnableIRQ(TIM3_IRQn);

as it's pointless, you just enabled it with the NVIC_Init()

void TIM3_IRQHandler(void)
{
if(TIM3->SR & TIM_SR_UIF) // if UIF flag is set
{
TIM3->SR = ~TIM_SR_UIF; // clear UIF flag
GPIOE->ODR ^= (1<<7);
} 
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStruct);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_Struct;
NVIC_Struct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_Struct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_Struct.NVIC_IRQChannelSubPriority = 1;
NVIC_Struct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_Struct);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_Prescaler = 8000 - 1;
TIM_TimeBaseInitStruct.TIM_Period = 1000 - 1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
TIM_ClearFlag(TIM3, TIM_FLAG_Update);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
int main(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_Configuration();
NVIC_Configuration();
TIM_Configuration();
// while(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0); // Initialized where?
GPIO_WriteBit(GPIOE, GPIO_Pin_8, Bit_SET);
while(1)
{
}
return 0;
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
todortur
Associate II
Posted on February 10, 2014 at 19:13

Thanks for the help.

I tried your code, but still the same result

Posted on February 10, 2014 at 19:15

Then you'll need to ponder more carefully the construction of your project. Check you're using the right startup files, and the .MAP file indicates that things like the vector table are correctly placed, and there is linkage to your interrupt handler.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
todortur
Associate II
Posted on February 10, 2014 at 19:27

OK. Really thank you for the help.

chen
Associate II
Posted on February 11, 2014 at 10:30

Hi

For BusFaults, you need to consult the ARM reference manual. You have not told us which part you are using, ST use 3 different ARM Cortex M cores you will need the right one for you device. It will help you determine the type of bus fault.

IAR should give you some help, open up the bus fault register and IAR should make a good interpretation of the register bits. This will give you an idea of what generated the bus fault.

FYI I am surprised no one else has mentioned that a classic cause of bus fault is stack corruption. Specifically, the stack is too small and it is corrupted by the code because it has overflowed into RAM used in the code.

Posted on February 11, 2014 at 16:49

The OP is using an STM32F3-DISCO with an STM32F303VC part on it.

The code as presented looks quite reasonable, I think the next thing to do is step back and review the project, what's in it, what the settings are.

I would recommend starting with the IAR Project Template within the STM32F3-DISCO firmware package, and clone that, and add in the code to be tested. This way we'd have a known starting point, that should be working.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..