cancel
Showing results for 
Search instead for 
Did you mean: 

TIM2 is not generating any interrupts on STM32f051

evert2
Associate II
Posted on December 04, 2012 at 15:56

Dear all,

I've an issue with TIM2 on the STM32f051

I did a port from STM32f103, however the interrupt is not activated anymore.

A big difference is that I use a f0discovery board, so I make use of an internal RC occilator.

I did some changes to use the HSI for clocking the PLL at the correct frequency

Since TIM14 is working, it looks like initialising the clock is working fine.

This is how the clock is initialized:

static void Clk_Init(void)

{

RCC_HSICmd(ENABLE);

while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

/* Init PLL (8MHZ/2)*12 = 48MHZ */

RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12);

RCC_PLLCmd(ENABLE);

while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

FLASH_PrefetchBufferCmd(ENABLE);

FLASH_SetLatency(FLASH_Latency_1);

/* TIM2,3,14 at 48MHz */

RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2 |

RCC_APB1Periph_TIM3 |

RCC_APB1Periph_TIM14 | RCC_APB1Periph_USART2, ENABLE);

}

This is how the initalisation of the timer is done

void init_timer(void)

{

/* Peripherals InitStructure define */

static TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

static NVIC_InitTypeDef NVIC_InitStructure;

TIM_TimeBaseStructure.TIM_Period = 48;

TIM_TimeBaseStructure.TIM_Prescaler = 0;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* set up Timer 2 */

NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPriority = 2;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure); /* Prescaler configuration */

TIM_PrescalerConfig(TIM2, (NR_MSEC_FOR_MAIN * MSEC), TIM_PSCReloadMode_Immediate);

TIM_InternalClockConfig(TIM2);

TIM_Cmd(TIM2, ENABLE); /* switch timer on */

}

After this initialisation, the micro never enters the TIM2_IRQHandler()

Does someone has any idea? did I forgot any initalisation?

TIM14 is working fine, however it does not make use of an IRQHandler().

Regards,

Evert Huijben

#irq-tim2-discoveryf0
23 REPLIES 23
Posted on January 29, 2014 at 21:56

Your post seems to be off topic for this thread, in the future please open a new thread, and cite the old one if it has any relevance. I'm guessing you just got a Google hit on this one.

Your Hard Fault handler seems to be broken, and coded incorrectly. Pretty sure R0 (parameter#1) points to nothing useful on entry to the C code.

So what's the value of AppEntry immediately before the fault?

The scatter file for the boot loader really needs it's RAM allocation changed so you don't corrupt your own variables with the vector table.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hkamba
Associate II
Posted on January 30, 2014 at 10:34

Dear Clive,

I thought this thread was appropriate to ask my question as I did not want to create another thread related to the same topic. Apologies if it does not seem appropriate to you. The Hard Fault Handler is not broken, in fact it indicated to me where the fault was.R0 had __initial_sp which is the top of the stack. It made me realise that I had to initialise my application's Stack pointerin the bootloaderbefore jumping to the app. After doing that I can now successfully jump to the app from the bootloader. The jump address also has to be (for some reason I do not quite understand) the application address + 4. Also, there is no need for me to change the bootloader RAM allocation as whatever the bootloader stores in RAM is irrelevant when I jump to the application. I wanted the bootloader to use the full RAM. The vectors are relocated to the SRAM and mapped at 0x00000000 in the main app which does not care about bootloader RAM stored values. See my new bootloaderINTFLASH_execute_main_app function below:

void INTFLASH_execute_main_app(const char mode)
{
MyFunc_ptr Jump_To_Application;
uint32_t JumpAddress;
// __disable_irq();
JumpAddress = *(__IO uint32_t*) (IAP_APP_ADDRESS + 4);
Jump_To_Application = (MyFunc_ptr) JumpAddress;
if( mode || intflash_check_main_app() )
{
App_ptr = (uint8_t*)Jump_To_Application;
if( (*App_ptr != 0xFF) && (App_ptr) )
{ 
__set_MSP( *(__IO uint32_t*)IAP_APP_ADDRESS ); // Initialise app's Stack Pointer
Jump_To_Application();
}
} 
// __enable_irq();
}

Many Thanks
Posted on January 30, 2014 at 15:56

The Hard Fault Handler is not broken

It is, you just don't understand why, you're dumping the vector table content, not the stack because you're passing in a NULL (R0 = 0). It's dumping a supposed value for R0, but it's pulling that from the Stack Pointer entry in the vector table. Breakpoint it if you don't believe me. In other circumstances it'll pass in whatever random value is in R0 when it faults. You should re-read the app note or book the handler came from, and call the C routine with the proper context.

You jump to the location pointed to by IAP_APP_ADDRESS + 4, because it holds the Reset_Handler subroutine address from the vector table situated at IAP_APP_ADDRESS.

+0 SP

+4 PC

+8 NMI

..

It crashed before, not because the stack was broken, but that AppEntry pointed at something bogus the processor couldn't execute.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hkamba
Associate II
Posted on January 30, 2014 at 17:07

Cheers for that! Also, how can I contact you? I want to ask you something but it is not related to this topic. I am new to the forum..