Page 1 of 1

Can't enable PWM on STM32F103

Posted: Wed May 09, 2018 1:01 pm
by Spider
Hello! I'm trying to enable PWM on TIM2_CH3, but can't. Yes, I know that ChibiOS use TIM2 for systemticks, but I'm writing FW for already completed board. What I have:
  • STM32F103RBT6 based board
  • No external OSCILATORS, only HSI
  • Buzzer on PA2
► Show Spoiler


I has changed #define STM32_ST_USE_TIMER 3 for free TIM2
And trying to enable PWM on TIM2_CH3:

Code: Select all

#define GPIOA_BEEP             2

static PWMConfig pwmcfg = {
  10000,
  10,
  NULL,
  {
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_ACTIVE_HIGH, NULL},
   {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
  0,
#if STM32_PWM_USE_ADVANCED
  0
#endif
};

void beep(void)
{
    palSetPadMode(GPIOA, GPIOA_BEEP, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
    palClearPad(GPIOA, GPIOA_BEEP);
    pwmStart(&PWMD2, &pwmcfg);
    pwmEnableChannel(&PWMD2, 3, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));//DC 50%
    //pwmDisableChannel(&PWMD2, 3);
}


But PWM not starting. What I do wrong?

Re: Can't enable PWM on STM32F103

Posted: Wed May 09, 2018 1:03 pm
by Giovanni
Hi,

Have you programmed the output pin to alternate mode?

Giovanni

Re: Can't enable PWM on STM32F103

Posted: Wed May 09, 2018 1:07 pm
by Spider
What about in function?
palSetPadMode(GPIOA, GPIOA_BEEP, PAL_MODE_STM32_ALTERNATE_PUSHPULL);

Re: Can't enable PWM on STM32F103

Posted: Wed May 09, 2018 1:33 pm
by Giovanni
Try enabling a callback and see if it is called (breakpoint, led), if the driver is working then it should be triggered.

Giovanni

Re: Can't enable PWM on STM32F103

Posted: Thu May 10, 2018 9:29 am
by FXCoder
The configuration is set for channel 2.
The code is enabling channel 3?

Re: Can't enable PWM on STM32F103

Posted: Fri May 11, 2018 5:17 pm
by Spider
FXCoder wrote:The configuration is set for channel 2.
The code is enabling channel 3?

Why? :o

Re: Can't enable PWM on STM32F103

Posted: Sat May 12, 2018 1:23 am
by FXCoder
Channels are 0 numbered in the software.
So TIMx_CH3 (H/W) is index 2 in function parameter and config struct.

Your code should look like this to use H/W designated TIM2_CH3...

Code: Select all

#define GPIOA_BEEP             2

static PWMConfig pwmcfg = {
  10000,
  10,
  NULL,
  {
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_DISABLED, NULL},
   {PWM_OUTPUT_ACTIVE_HIGH, NULL},
   {PWM_OUTPUT_DISABLED, NULL}
  },
  0,
  0,
#if STM32_PWM_USE_ADVANCED
  0
#endif
};

void beep(void)
{
    palSetPadMode(GPIOA, GPIOA_BEEP, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
    palClearPad(GPIOA, GPIOA_BEEP);
    pwmStart(&PWMD2, &pwmcfg);
    pwmEnableChannel(&PWMD2, 2, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));//DC 50%
    //pwmDisableChannel(&PWMD2, 2);
}