cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4-discovery strange problem with timer interrupt HELP Please!

pushok
Associate II
Posted on February 26, 2014 at 18:49

Hello!

I am stm32f4 beginner. Learning timer interrupt (IAR 6.3). Task is this: once per second by a timer interruptincrease some value and LED (GPIO_Pin_12) to light, as a works fine signal. Problem so far. The code below compiles (only compiles) and downloads in tostm32f4-discovery board fine. But (big hairy but), when debugger quitTIM2_Configuration()stm32f4 board starts to flashing red and green LED (led near mini-usb port) and nothing happend (as if the timer does not want to work with the interruption). ControlGPIO_Pin_12 led does not ignite. I tried to do it with other timers with different variations of code, but it always turned the timer as it does not want to work with the interruption. I look at other sites and write code that should works well with interruptions. But everythingthe same. What is wrong?

void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* GPIOA/D clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = ((SystemCoreClock / 1000000) / 2) - 1; // 1 MHz timebase
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 KHz update
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable TIM2 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
extern ''C''
{
void TIM2_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
nBuf++;
GPIO_SetBits(GPIOD, GPIO_Pin_12);
}
}
int main()
{
GPIO_InitTypeDef GPIO_InitStructurePortD;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 
GPIO_InitStructurePortD.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14; 
GPIO_InitStructurePortD.GPIO_Mode = GPIO_Mode_OUT; 
GPIO_InitStructurePortD.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP;//
GPIO_Init(GPIOD, &GPIO_InitStructurePortD);
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
TIM2_Configuration();
while(1)
{
}
}

8 REPLIES 8
Posted on February 26, 2014 at 19:18

Why the use of C++ syntax? Make sure the project is built as C, and there is correct linkage via the Vector table as expressed in startup_stm32f4xx.s

The interrupt periodicity is set at 1 KHz not 1 Hz, you'd be better toggling the GPIO, so you can observe it on a scope.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
pushok
Associate II
Posted on February 26, 2014 at 20:11

Clive 1, you are  voodoo man, real-real voodoo man!! For two consecutive days at least i tried to light an LED, and now it lights!!! Ta-da!! Ha-ha!! Thank you, thank you, thank you for this fulminant and accurate smashing the target response. I turn project to C and plugin startup_stm32f4xx.s and it lights! E

arlier

 already tried to connect startup_stm32f4xx.s, but gets nothing.

But (little hairy but), i've got next question: how do I correctly set interrupt period for 1 second. if I get from function RCC_GetClocksFreq(&RCC_Clocks) value RCC_Clocks.PCLK1_Frequency ==  131250000?? 

Posted on February 26, 2014 at 22:29

Let me preface this by saying you really need to get the firmware library for the STM32F4-DISCO board, and review the template project for the IAR EWARM tools.

You need to get localized copies of system_stm32f4xx.c and stm32f4xx_conf.h into your project. You need to make sure HSE_VALUE for the project is 8000000 to reflect the crystal speed used on the board, and that the PLL settings reflect the 8 MHz rather than 25 MHz.

To get to 1 Hz, you want to get the prescaler to get to 42 KHz instead of 1 MHz, and the period is 42000 - 1, to reflect getting that 42 KHz clock to a 1 second period.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
pushok
Associate II
Posted on February 27, 2014 at 07:39

Clive 1, that's what I did, please check my logic and at the same time result. My stm32f4xx_conf.h does not contain any value about clock, so i used ''stm32f4xx/41xx Configuration V 1.1.0. tool'' There I has established the following value: HSE = 8Mhz, PLL_N =192, PLL_P = 8, PLL_Q = 8 (last three values are default and unchangeable). Dots was set on HSE (Pll Source Clock block) and PLL (System Clock Mux block). HCLK = 16 Mhz. With APB1_Prescaler == 1 i recived result PCLK1 == 16 (42 Mhz max - inscription in red letters). Further - generated system_stm32f4xx.h and plug it to project. In stm32f4xx.h changed HSE_VALUE to 8000000 and plug it to project to. Now RCC_GetClocksFreq function gives the value of RCC_Clocks.PCLK1_Frequency == 16000000.

TIM_TimeBaseInitTypeDef structure filled so:

TIM_TimeBaseStructure.TIM_Prescaler = 16000 - 1;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_Period = 1000 - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

I'm i right? Will now 1 second response time?

Posted on February 27, 2014 at 15:44

The PLL numbers frankly don't make a lot of sense to me, but if the TIMCLK is 16 MHz, dividing it down by 16000000 will get you a 1 second update interrupt periodicity.

http://www.st.com/web/en/catalog/tools/PF257904

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_TimeBase\stm32f4xx_conf.h

STM32F4-Discovery_FW_V1.1.0\Project\Peripheral_Examples\TIM_TimeBase\system_stm32f4xx.c

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

Сlive1, Thanks a lot, I will dig and explore.

Ehsan Daneshvar
Associate II
Posted on November 11, 2015 at 03:07

Hi Clive ... I've a big problem with the startup file ... I'm using IAR EWARM 6.21 and in my project i have to use C++ because of many reasons such as my last libraries have been developed in C++ .but unfortunately when i change the compiler option to C++ mode my ISRs are not works ... how can i set my startup file to solve this problem?

or would you please help me where ( in which topic) i could read issues about this topic?

Posted on November 11, 2015 at 04:31

I would probably remove the weak definitions so it becomes more apparent what's not linking.

Make sure you avoid name mangling on the IRQ Handlers, either by having them in .C files or using the extern ''C'' void USART1_IRQHandler(void) { } form

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..