Page 1 of 1

STM32: Timer 1 PWM complementary outputs

Posted: Tue Oct 04, 2011 10:05 am
by OlivierD
Hello,

I am currently trying to generate triphased PWM on a STM32 using Timer 1. I sucessfully generated the signals on the positive outputs. Now I'm trying to find out if the complementary output feature is implemented in ChibiOS. I've been searching for some time now and I can't seem to find it. Can someone help me out?

Thanks,

Olivier

Re: STM32: Timer 1 PWM complementary outputs

Posted: Tue Oct 04, 2011 11:13 am
by Giovanni
Hi,

It is supported in 2.3.3 but not in the stable branch 2.2.x.

You have to activate the option STM32_PWM_USE_ADVANCED in mcuconf.h and then configure the output mode using the extra modes:
PWM_COMPLEMENTARY_OUTPUT_DISABLED
PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH
PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW

Please see the online documentation of the PWM driver for details. The code is new, please report any problem you may find.

Giovanni

Re: STM32: Timer 1 PWM complementary outputs

Posted: Tue Oct 04, 2011 11:15 am
by OlivierD
Well I was using the stable branch, sorry for that I never saw it in the documentation. I'll try it out!

Re: STM32: Timer 1 PWM complementary outputs

Posted: Tue Oct 04, 2011 2:57 pm
by OlivierD
Okay I made the upgrade to the unstable branch. Unfortunately I won't have access to an oscilloscope for some time now, can you tell me if the syntax I used to configure the complementary output mode correct?

Code: Select all

static PWMConfig pwm1cfg = {
  8000000,          /* 8MHz PWM clock frequency.   */
  US2ST(20),          /* PWM period 20us.       */
  NULL,
  {
  {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, NULL}, /* PWM4, CH1 */
  {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, NULL}, /* PWM4, CH2 */
  {PWM_OUTPUT_ACTIVE_HIGH | PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, NULL}, /* PWM4, CH3 */
  {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
  0
};


...

Code: Select all

  palSetPadMode(GPIOA, 8, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  palSetPadMode(GPIOA, 9, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  palSetPadMode(GPIOA, 10, PAL_MODE_STM32_ALTERNATE_PUSHPULL);

  palSetPadMode(GPIOB, 13, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  palSetPadMode(GPIOB, 14, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  palSetPadMode(GPIOB, 15, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
  pwmStart(&PWMD1, &pwm1cfg);
  pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH (&PWMD1, 1000));
  pwmEnableChannel(&PWMD1, 1, PWM_PERCENTAGE_TO_WIDTH (&PWMD1, 7000));
  pwmEnableChannel(&PWMD1, 2, PWM_PERCENTAGE_TO_WIDTH (&PWMD1, 7000));

Re: STM32: Timer 1 PWM complementary outputs

Posted: Tue Oct 04, 2011 3:47 pm
by Giovanni
I think it is correct but you will know for sure after trying.

Giovanni

Re: STM32: Timer 1 PWM complementary outputs

Posted: Sun Jul 28, 2013 7:03 am
by abhi
hi OlivierD

i am facing same kind of problem

did you get 3 phase out put?

am using chibios on STM32F4-Discovery board

trying to generate 3 phase using 6 PWM channel

but i can't get phase shift in each pair of CHx and CHxN (complementary)

if i configure 2 PWMConfig and put some delay in two respective pwmStart()

then i got phase shift between channels respected to 2 different PWMConfig

but don't understand how to get another phase shift . . :?

please help me . . .it's critical situation on my side . .

Code: Select all

    #include "ch.h"
    #include "hal.h"

    static PWMConfig pwmcfg1 = {
      10000, // 10Khz PWM clock frequency
      20000, // PWM period 1 second 200,*  50 Hz (20.0 ms) initial frequency
      NULL,  // No callback
      {
        {PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, NULL},
        {PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, NULL},
        {PWM_COMPLEMENTARY_OUTPUT_ACTIVE_LOW, NULL},
        {PWM_OUTPUT_DISABLED, NULL},
      },
      0,
      0
    };
    static PWMConfig pwmcfg2 = {
      10000, /* 10Khz PWM clock frequency */
      20000, /* PWM period 1 second */
      NULL,  /* No call back */
      {
        {PWM_OUTPUT_ACTIVE_HIGH, NULL},
        {PWM_OUTPUT_ACTIVE_HIGH, NULL},
        {PWM_OUTPUT_ACTIVE_HIGH, NULL},
        {PWM_OUTPUT_DISABLED, NULL},
      },
      0,
      0
    };

    int main(void)
    {
         halInit();
         chSysInit();
       
          /*for TIM1/2  PAL_MODE_ALTERNATE(n) = 1 */
         palSetPadMode(GPIOE, 8, PAL_MODE_ALTERNATE(1));//TIM1, channel 1N
         palSetPadMode(GPIOE, 10, PAL_MODE_ALTERNATE(1));//TIM1, channel 2N
         palSetPadMode(GPIOE, 12, PAL_MODE_ALTERNATE(1));//TIM1, channel 3N

      /*  TIM8
       * for TIM8/9/10/11  PAL_MODE_ALTERNATE(n) = 3  */

         palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(3));//TIM8, channel 1
         palSetPadMode(GPIOC, 7, PAL_MODE_ALTERNATE(3));//TIM8, channel 2
         palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(3));//TIM8, channel 3

         pwmStart(&PWMD1, &pwmcfg1);
         pwmEnableChannel(&PWMD1, 0, 10000));
         pwmEnableChannel(&PWMD1, 1, 10000);
         pwmEnableChannel(&PWMD1, 2, 10000);

         chThdSleepMilliseconds(500);

        pwmStart(&PWMD8, &pwmcfg2);
         pwmEnableChannel(&PWMD8, 0, 10000);
         pwmEnableChannel(&PWMD8, 1, 10000);
         pwmEnableChannel(&PWMD8, 2, 10000);

      while (TRUE)
      {
        chThdSleepMilliseconds(5000);
      }
    }

is it possible solution by change in TIMx_CCRx . ..

give me some idea phase shifting

Re: STM32: Timer 1 PWM complementary outputs

Posted: Wed Dec 02, 2015 1:02 pm
by rew
Giovanni wrote:Please see the online documentation of the PWM driver for details. The code is new, please report any problem you may find.
I checked the documentation but could not find anything about what I need to put in "static PWMConfig pwmcfg". It complained about a missing "DIER", and I added an extra zero at the end which removed the warning, and seems to work, but it'd be nice to be able to verify things like this against the documentation.

Another reason I'm looking at the documentation is that I need "dead time". Is there an interface for that?

Re: STM32: Timer 1 PWM complementary outputs

Posted: Wed Dec 02, 2015 1:35 pm
by Giovanni
Those are features specific of the device so there is not an API, everything is configured into the PWMConfig structure.

Giovanni