cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 new project.

arganasss
Associate II
Posted on December 11, 2013 at 14:33

Hello everyone. I'm new to programming, just got this stm32f3 board. I tried to run TIM_PWM_Input example ant it seems to work just fine. That's what i want to do now - measure external signal frequency. However, when i created new project, and copied absolutely everything from TIM_PWM_Input example, with all libs and directories, the code doesn't work. When i connect external signal and run the code, it jumps to HardFault_handler. So i want to ask is there a way to fix it and why it happens? i mean i just coppied example project and it doesn't work. Will appreciate any help. Thanks in advance.

24 REPLIES 24
arganasss
Associate II
Posted on December 19, 2013 at 17:22

I didn't want to create new topic so i'll just ask here, I want to make timer interrupts happen every second. Here is the handler code:

void TIM3_IRQHandler(void)

{

  //if(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update)==1) // If UIF flag is set

  GPIOE->ODR ^= GPIO_Pin_8; // Change LED state

  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);// Clear interrupt pending bit, so it can go to interrupt again

}

Why when i comment the line: if(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update)==1) the code doesn't work? IT looks like that the code then is always in interrupt handler and it just turns off a led every second for about 0.7us. If i leave this line uncomment, everything seems to work fine. I just want to understand why it happens. Thanks for help.

Posted on December 19, 2013 at 17:41

There is a pipeline/write buffer hazard, clear the interrupt early so the tail-chaining doesn't assume the interrupt is still pending.

void TIM3_IRQHandler(void)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);// Clear interrupt pending bit, so it can go to interrupt again
GPIOE->ODR ^= GPIO_Pin_8; // Change LED state
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arganasss
Associate II
Posted on December 20, 2013 at 11:54

Thanks again for your help. Sorry for so many questions, but im really new to arm and struggling at some points. And now im in a new problem. Again ill just ask here, because i dont want to create a new topic. So the thing is: I want to configure External signal as Timer2 clock. I expecter this code to work, but counter always stays at 0. Seems the input signal doesn't come to the pin, but it really comes. I see that with scope. So is there a problem in my code or what im doing wong?

#include ''main.h''

#include ''stdio.h''

TIM_ICInitTypeDef  TIM_ICInitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;

int main(void)

{

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Port A clock on

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // Timer2 clock on

  

  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  

  // Connect TIM pin to AF1 

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1);

  

 TIM_TimeBaseInitStructure.TIM_Period = 65535;       

 TIM_TimeBaseInitStructure.TIM_Prescaler = 0;    

 TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; 

 TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);

                    

 TIM_TIxExternalClockConfig(TIM2,TIM_TIxExternalCLK1Source_TI2,TIM_ICPolarity_Rising,0); //TI2FP2

                     

 TIM_Cmd(TIM2, ENABLE);

  

 while(1)

 {

 }

}

Posted on December 20, 2013 at 17:20

This works for me

// STM32 External Clock Input STM32F3-Discovery - sourcer32@gmail.com
// Keil SWV Semi-Hosting - STM32F3-DISCO needs SB10 Solder Bridge made
/**************************************************************************************/
#include <
stdio.h
>
#include ''stm32f3_discovery.h''
#include ''stm32f30x.h''
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA Peripheral clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Connect TIM2_CH2 pin to AF1 on pin PA1 (Free on STM32F3-DISCO)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1);
// Connect TIM3_CH2 pin to AF2 on pin PA4 (Free on STM32F3-DISCO)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_2);
}
/**************************************************************************************/
void TIM2_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Enable TIM2 Peripheral clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF; // 32-bit Maximal
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_TIxExternalClockConfig(TIM2, TIM_TIxExternalCLK1Source_TI2, TIM_ICPolarity_Rising, 0);
TIM_Cmd(TIM2, ENABLE);
}
/**************************************************************************************/
void TIM3_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* Enable TIM3 Peripheral clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // 1 MHz
TIM_TimeBaseStructure.TIM_Period = 20000 - 1; // 50 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Enable TIM3 Preload register on ARR */
TIM_ARRPreloadConfig(TIM3, ENABLE);
/* TIM PWM1 Mode configuration: Channel */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 10000; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel2 PA.4 */
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_Cmd(TIM3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
uint32_t CurrentTick, LastTick;
GPIO_Configuration();
TIM2_Configuration();
TIM3_Configuration(); // 50 Hz Source, clip PA4 to PA1
// Print a tick everytime the counter changes
LastTick = TIM_GetCounter(TIM2) - 1;
while(1)
{
CurrentTick = TIM_GetCounter(TIM2);
if (CurrentTick != LastTick)
{
printf(''Tick:%5u Delta:%5u
'', CurrentTick, (uint16_t)(CurrentTick - LastTick));
LastTick = CurrentTick;
}
}
}
/**************************************************************************************/
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
ITM_SendChar(ch);
return(ch);
}
int fgetc(FILE *f)
{
char ch;
ch = 1;
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
ITM_SendChar(ch);
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
arganasss
Associate II
Posted on December 23, 2013 at 11:31

I just wanted to say: thank you very much clive1. You really helped me out and im done with my first part of the project. I'm able to measure frequencies above 0.5MHz very precisely. Thanks again.