2013-09-22 01:46 PM
Hi,
i use a STM32f3discovery evaluation board and trying to generate 6 PWM signals. For this purpose i have choosen Timers 3 and 4. As base project i use a Demo project. (The project demonstrates a sensors functionality) Initialisation code for timers is following: void Timer_Config(void){ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Timer4 GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_10); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_10); GPIO_PinAFConfig(GPIOA, GPIO_PinSource13, GPIO_AF_10); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_TimeBaseInitStructure.TIM_Prescaler = 71; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 999; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure); //PWM Config on Timer4 CH3 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC3Init(TIM4, &TIM_OCInitStructure); //PWM Config on Timer4 CH2 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC2Init(TIM4, &TIM_OCInitStructure); //PWM Config on Timer4 CH1 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM4, &TIM_OCInitStructure); TIM_Cmd(TIM4, ENABLE); //Timer3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2); //Timer3 Config TIM_TimeBaseInitStructure.TIM_Prescaler = 71; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 999; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //PWM Config on Timer3 CH3 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC3Init(TIM3, &TIM_OCInitStructure); //PWM Config on Timer3 CH2 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC2Init(TIM3, &TIM_OCInitStructure); //PWM Config on Timer3 CH1 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_Cmd(TIM3, ENABLE); } But if I call this funktion the board stops working. Pin_to_periphery allocation description was taken from DM00058181.pdf dokument form ST site. Could someone help me make it work? P.S. I use a USART1 too, it uses pins PA9 and PA10. #pwm #stm32f3 #timers2013-09-22 02:18 PM
But if I call this funktion the board stops working.
How exactly, it crashes, have you used a debugger, where is it crashing? It has no pulse output? Not surprising Pulse = 0 is OFF, try something like 10, or 500 (50/50)2013-09-22 02:52 PM
Doesn't PA13 conflict with the debugger SWDAT signal? Need to rethink that perhaps.
// STM32 TIM3/4 PWM (PA11/12,6/7) STM32F3-Discovery - sourcer32@gmail.com
#include ''stm32f30x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable TIM3/4 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Free pins located in STM32F3-Discovery manual, UM1570
// AF located in F302/303 Data Manual (Alternate functions for port A - Table 14, Rev 5)
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_10); // TIM4_CH1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_10); // TIM4_CH2
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource13, GPIO_AF_10); // TIM4_CH3 (Conflicts with SWDAT debug signal)
// TIM4_CH3 PB8(AF2), PD14(AF2)
// GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_2); // TIM3_CH1 !! pick one
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); // TIM3_CH1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2); // TIM3_CH2
// TIM3_CH3 PB0(AF2), PC8(AF2), PE4 (AF2)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/**************************************************************************************/
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 72 to 1 MHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 MHz to 1 KHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1000 / 4; // 25%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_Cmd(TIM4, ENABLE);
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1000 / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM_Configuration();
while(1); // Don't want to exit
}
2013-09-22 04:14 PM
Wow Thanks Clive,
i will try Your variant tomorrow! I have found thatGPIO_PinAFConfig(GPIOA, GPIO_PinSource13, GPIO_AF_10);
does not work, but could not understood why, thanks for explanation.
If I comment the line mentioned above and run timer-initialisation in debug mode (one step per click), the Demo appears to work, (LEDs react on gyro / magnet sensor). But if I ''click'' run it crashes. It isnt actually a crash, the programm simply goes in to HardFault_Handler(void) exeption state.
I have an short quastion, can timers conflict with USART? Thanks.
2013-09-22 05:56 PM
The pin usage on the STM32F3-DISCO can be located
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00063382.pdf
. The USART shouldn't conflict with anything, internally you can only route one function to each pin, externally you could have other inputs/outputs do conflicting things. A Hard Fault is more indicative of a missing interrupt service routine, a proper handler will allow you to determine a more precise cause/causes. Check also for corrupting the stack, and accessing memory outside normal bounds.