Can't enable PWM on STM32F103

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
User avatar
Spider
Posts: 25
Joined: Tue Nov 27, 2012 10:05 am
Location: Omsk, Russia
Been thanked: 1 time
Contact:

Can't enable PWM on STM32F103

Postby Spider » Wed May 09, 2018 1:01 pm

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?

User avatar
Giovanni
Site Admin
Posts: 11229
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 401 times
Been thanked: 334 times
Contact:

Re: Can't enable PWM on STM32F103

Postby Giovanni » Wed May 09, 2018 1:03 pm

Hi,

Have you programmed the output pin to alternate mode?

Giovanni

User avatar
Spider
Posts: 25
Joined: Tue Nov 27, 2012 10:05 am
Location: Omsk, Russia
Been thanked: 1 time
Contact:

Re: Can't enable PWM on STM32F103

Postby Spider » Wed May 09, 2018 1:07 pm

What about in function?
palSetPadMode(GPIOA, GPIOA_BEEP, PAL_MODE_STM32_ALTERNATE_PUSHPULL);

User avatar
Giovanni
Site Admin
Posts: 11229
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 401 times
Been thanked: 334 times
Contact:

Re: Can't enable PWM on STM32F103

Postby Giovanni » Wed May 09, 2018 1:33 pm

Try enabling a callback and see if it is called (breakpoint, led), if the driver is working then it should be triggered.

Giovanni

User avatar
FXCoder
Posts: 124
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 31 times
Been thanked: 31 times

Re: Can't enable PWM on STM32F103

Postby FXCoder » Thu May 10, 2018 9:29 am

The configuration is set for channel 2.
The code is enabling channel 3?

User avatar
Spider
Posts: 25
Joined: Tue Nov 27, 2012 10:05 am
Location: Omsk, Russia
Been thanked: 1 time
Contact:

Re: Can't enable PWM on STM32F103

Postby Spider » Fri May 11, 2018 5:17 pm

FXCoder wrote:The configuration is set for channel 2.
The code is enabling channel 3?

Why? :o

User avatar
FXCoder
Posts: 124
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 31 times
Been thanked: 31 times

Re: Can't enable PWM on STM32F103

Postby FXCoder » Sat May 12, 2018 1:23 am

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


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 2 guests