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
[spoil]

Code: Select all

/*
 * HAL driver system settings.
 */
#define STM32_NO_INIT                       FALSE
#define STM32_HSI_ENABLED                   TRUE
#define STM32_LSI_ENABLED                   TRUE
#define STM32_HSE_ENABLED                   FALSE
#define STM32_LSE_ENABLED                   FALSE
#define STM32_SW                            STM32_SW_PLL
#define STM32_PLLSRC                        STM32_PLLSRC_HSI
#define STM32_PLLXTPRE                      STM32_PLLXTPRE_DIV1
#define STM32_PLLMUL_VALUE                  9
#define STM32_HPRE                          STM32_HPRE_DIV1
#define STM32_PPRE1                         STM32_PPRE1_DIV2
#define STM32_PPRE2                         STM32_PPRE2_DIV2
#define STM32_ADCPRE                        STM32_ADCPRE_DIV4
#define STM32_USB_CLOCK_REQUIRED            FALSE
#define STM32_USBPRE                        STM32_USBPRE_DIV1P5
#define STM32_MCOSEL                        STM32_MCOSEL_NOCLOCK
#define STM32_RTCSEL                        STM32_RTCSEL_LSI
#define STM32_PVD_ENABLE                    FALSE
#define STM32_PLS                           STM32_PLS_LEV0
[/spoil]

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