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;
}