cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 discovery board using hal driver for pwm generation

shadowwalker1987
Associate II
Posted on October 18, 2015 at 01:35

The original post was too long to process during our migration. Please click on the attachment to read the original post.
8 REPLIES 8
Posted on October 18, 2015 at 01:47

I'm not at all interested in the HAL code, but it would appear that you enable the TIM3 peripheral clock far too late in the code, it should be enabled before you start trying to program the peripheral. Enable it up where you enable the GPIOC clock.

The way you use subroutines seems to unnecessarily cloud what's going on, would suggest you adopt a more linear flow of code until you get it working.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 18, 2015 at 01:50

Consider a series resistor on the LED to limit current.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shadowwalker1987
Associate II
Posted on October 18, 2015 at 02:04

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6hC&d=%2Fa%2F0X0000000btR%2FCMoBa1SOWDHnycDywIh5kM.SGE1vyPPwlA6eHtKJGNI&asPdf=false
Posted on October 18, 2015 at 03:10

Looks mostly reasonable, the 25 MHz setting for the PLL should have been 8 MHz.

Have you stepped through this with the debugger? Do it get to the while() loop, and do any of the HAL functions return errors?

#include ''stm32f4xx_hal.h''
#include ''stm32f4xx_hal_gpio.h''
#include ''stm32f4xx_hal_tim.h''
#define Period 2563
#define Percent 50
static void SystemClock_Config(void);
int main(void)
{
GPIO_InitTypeDef GPIO_InitSt;
TIM_HandleTypeDef PWM_Struct;
TIM_OC_InitTypeDef StrOutputChannel;
HAL_Init();
/*SysCLK-----------------------*/
SystemClock_Config();
/*define gpios*/
GPIO_InitSt.Pin = GPIO_PIN_6;
GPIO_InitSt.Mode = GPIO_MODE_AF_PP; /*alternade mode for PWM*/
GPIO_InitSt.Pull = GPIO_PULLUP; /*no pull up nor pull down!*/
GPIO_InitSt.Speed = GPIO_SPEED_HIGH;
GPIO_InitSt.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOC,&GPIO_InitSt);
/*define and init TIM3!*/
PWM_Struct.Instance = TIM3;
PWM_Struct.Init.Prescaler = 0xFFFF; //168MHz/2^16=2563Hz=0,39ms
PWM_Struct.Init.CounterMode = TIM_COUNTERMODE_UP;
PWM_Struct.Init.Period = Period - 1;//Time=2563*(1/2563Hz)=1s
PWM_Struct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
PWM_Struct.Init.RepetitionCounter = 0;
HAL_TIM_PWM_Init(&PWM_Struct);
/*define PWM output!*/
StrOutputChannel.OCMode = TIM_OCMODE_PWM1;
StrOutputChannel.OCPolarity = TIM_OCPOLARITY_HIGH;
StrOutputChannel.OCFastMode = TIM_OCFAST_DISABLE;
StrOutputChannel.Pulse = (uint32_t)((Period*Percent)/100);
HAL_TIM_PWM_ConfigChannel(&PWM_Struct, &StrOutputChannel, TIM_CHANNEL_1);
/*start pwm*/
HAL_TIM_PWM_Start(&PWM_Struct, TIM_CHANNEL_1);
while(1){};
}
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8; // DISCO has 8 MHz source
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks
dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
__GPIOC_CLK_ENABLE();
__TIM3_CLK_ENABLE();
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
shadowwalker1987
Associate II
Posted on October 18, 2015 at 21:08

Hi,

I copied the clock configuration of an example of ST... really strange... I tested it befor by putting on an led (without dimming) and configured the timer to toggle the pin of the led every second. (Worked fine, but also needed a lot of time to understand.. ;))

I will try to debug the code next days :)

Greets Alex

shadowwalker1987
Associate II
Posted on October 18, 2015 at 21:14

Do you know where I can find the SP libraries from ST?

I was searching for them, but all I could find was a package for the STM32065.

Thanks,

Alex
shadowwalker1987
Associate II
Posted on October 19, 2015 at 22:26

Hi,

Code is working, I have forgotten to reset the board ... u.u.

somtimes it is ...^^''
Posted on October 20, 2015 at 00:36

Most current SPL + DSP Library 1.7

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

F4-DISCO SPL, a bit on the older side but not buggy, just lacks support for newer chips you're not using.

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

Main difference to watch for is the DISCO board uses an 8 MHz clock source, but the EVAL boards targeted in the DSP/SPL release use an 25 MHz clock. So fix the PLL settings, and HSE_VALUE.

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