Maximum PWM clock frequency on STM32F4

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
manton
Posts: 6
Joined: Thu May 24, 2012 10:18 pm

Maximum PWM clock frequency on STM32F4

Post by manton »

Hi,

I 'm not sure if you have come across this issue. It seems that I couldn't use more than 2 MHz for clock frequency when config the PWM.

I tried to create PWM signal of 10kHz frequency with complementary output + dead time so I config PWM as below (using 20MHz pwm clock frequency). But it couldn't run pass pwmStart().
If I change the clock to 2MHz, it runs fine - I get the outputs from CH1 and CH1N. But whenever I use something more than 2MHz, it fails.

Could you please verify if I have something wrong inside my codes.

My board is STM32f4 discovery.
Eclipse+Yagarto GCC 4.6.2 to compile Chibios 2.5.0.

Many thanks in advance.

Man

Code: Select all

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


static PWMConfig pwmcfg = {
      20000000,                                    /*PWM clock frequency.   */
      2000,                                    /* Initial PWM period     */
      NULL,
      {
            {PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH | PWM_OUTPUT_ACTIVE_HIGH, NULL},
            {PWM_OUTPUT_DISABLED, NULL},
            {PWM_OUTPUT_DISABLED, NULL},
            {PWM_OUTPUT_DISABLED, NULL}
      },
      0,
      200
};


/*
 * Application entry point.
 */
int main(void) {

   /*
    * System initializations.
    * - HAL initialization, this also initializes the configured device drivers
    *   and performs the board-specific initializations.
    * - Kernel initialization, the main() function becomes a thread and the
    *   RTOS is active.
    */
   halInit();
   chSysInit();

   
   palSetPadMode(GPIOE, 8, PAL_MODE_ALTERNATE(1));
   palSetPadMode(GPIOE, 9, PAL_MODE_ALTERNATE(1));
   while (TRUE) {
      palClearPad(GPIOD, GPIOD_LED4);

      pwmStart(&PWMD1, &pwmcfg);
      chThdSleepMilliseconds(2000);
      palSetPad(GPIOD, GPIOD_LED4);
      chThdSleepMilliseconds(2000);

      /*
       * Starts the PWM channel 0 using 75% duty cycle.
       */
      pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 7500));
      chThdSleepMilliseconds(2000);

      /*
       * Changes the PWM channel 0 to 50% duty cycle.
       */
      pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 5000));
      chThdSleepMilliseconds(2000);

      /*
       * Changes the PWM channel 0 to 25% duty cycle.
       */
      pwmEnableChannel(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 2500));
      chThdSleepMilliseconds(5000);

      /*
       * Changes PWM period to half second the duty cycle becomes 50%
       * implicitly.
       */
      pwmChangePeriod(&PWMD1, 5000);
      chThdSleepMilliseconds(5000);

      /*
       * Disables channel 0 and stops the drivers.
       */
      pwmDisableChannel(&PWMD1, 0);
      pwmStop(&PWMD1);
      
      chThdSleepMilliseconds(2000);
   }
   return 0;
}
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Maximum PWM clock frequency on STM32F4

Post by Giovanni »

Hi,

I assume that you are working with assertions enabled or the driver would not stop inside pwmStart().

Are you sure that the PWM input frequency is exactly 20MHz? inside pwmStart() there is an assertion that makes sure that the input frequency is a multiple of the configured frequency. If this is not true then execution is stopped.

The input frequency is the programmed clock for APB1 or APB2 depending on the timer you are using. Verify your APB settings in mcuconf.h.

Giovanni
manton
Posts: 6
Joined: Thu May 24, 2012 10:18 pm

Re: Maximum PWM clock frequency on STM32F4

Post by manton »

Thanks very much Giovanni.

I think I found my mistake. I set APB2 at 168MHz so I should use the frequency that makes the prescaler an integer.
Initially I thought that I could use any frequency that is less than APB2 and the prescaler calculation will take care of it. Obviously, if I use 20MHz, the psc is 7.4. That's why I got stuck.

Cheers.

Man
DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: Maximum PWM clock frequency on STM32F4

Post by DrunkenDonkey »

I simply nuked that assert, as I call for varying frequencies and anything nearby suits me, does not have to be exact and it works fine.
hkerner
Posts: 24
Joined: Tue Jul 22, 2014 12:05 am

Re: Maximum PWM clock frequency on STM32F4

Post by hkerner »

I have determined that my application is halting on this same assert, but I am not sure where APB1 and 2 are set, and there is nothing about APB in mcuconf. Could someone please point me to where I can see the frequency of these? Thank you!
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Maximum PWM clock frequency on STM32F4

Post by Giovanni »

Hi,

In mcuconf.h look for:

#define STM32_PPRE1 STM32_PPRE1_DIV4
#define STM32_PPRE2 STM32_PPRE2_DIV2

Giovanni
Post Reply