Page 1 of 1

PWM config on STM32

Posted: Sun Jan 15, 2012 5:38 pm
by Cyrille
Hi,

How to configure PWM driver on stm32 ?
I have tested the following code without success.

In halconf.h : HAL_USE_PWM TRUE
In mcuconf.h : STM32_PWM_USE_TIM2 TRUE

In my code

Code: Select all

//PWM
static PWMConfig pwmcfg = {
      10000,          /* Hz PWM clock frequency.   */
      US2ST(20),          /* PWM period 20us.       */
      NULL,
      { {PWM_OUTPUT_ACTIVE_HIGH, NULL},
        {PWM_OUTPUT_ACTIVE_HIGH, NULL},
        {PWM_OUTPUT_ACTIVE_HIGH, NULL},
        {PWM_OUTPUT_ACTIVE_HIGH, NULL}
      },
      0,
      0
};

...

     //***********************************************//
     //             PWM Configuration                //
     //**********************************************//
     pwmObjectInit(&PWMD2);
     pwmStart(&PWMD2, &pwmcfg);

     palSetPadMode(IOPORT1, 1, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
     palSetPadMode(IOPORT1, 2, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
     palSetPadMode(IOPORT1, 3, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
     palSetPadMode(IOPORT2, 5, PAL_MODE_STM32_ALTERNATE_PUSHPULL);


     pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
     pwmEnableChannel(&PWMD2, 1, 1000);
     pwmEnableChannel(&PWMD2, 2, 1000);
     pwmEnableChannel(&PWMD2, 3, 1000);


But no PWM ?

So how to properly configure and use PWM ?

Thanks
Cyrille

Re: PWM config on STM32

Posted: Sun Jan 15, 2012 7:10 pm
by Giovanni
Hi,

What STM32 model? probably you didn't configure properly I/O pins, also:

1) US2ST() is not a PWM macro.
2) You can't have a cycle of 20uS with a 10KHz clock.

I recommend that you follow the PWM demo for your platform more closely.

Giovanni