can't get PWM going with TIM1 on the F4DISCOVERY

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Jevermeister
Posts: 13
Joined: Sun Nov 11, 2012 8:19 pm

can't get PWM going with TIM1 on the F4DISCOVERY

Postby Jevermeister » Thu Jan 10, 2013 4:21 pm

Hi,

i figured out how to enable PWM with TIM4 and the LED-Pins but i cannot find examples for using TIM1.
Iam using chibios 2.4.2

MOT1_PWM is a define for 9 (so PE9) and MOT2_PWM for 11 ( PE11)
the other stuff shouldn't matter.

according to the datasheet i need to set PAL_MODE_ALTERNATE to AF1 to get TIM1_CH1 and TIM1_CH2 working.

This is what i have so far. I know that dutyCycle is commented out, i have set it manually a few lines above and it is correct.
Don't have any PWM output on PE9 or PE11. Same code worked with TIM4 and the onboard LEDs. Everything is okay according to the datasheet?!

Code: Select all

*snip*
#define HAL_USE_PWM                 TRUE
*snip*
/*
 * PWM driver system settings.
 */
#define STM32_PWM_USE_ADVANCED              TRUE
#define STM32_PWM_USE_TIM1                  TRUE
#define STM32_PWM_USE_TIM2                  FALSE
#define STM32_PWM_USE_TIM3                  FALSE
#define STM32_PWM_USE_TIM4                  FALSE
#define STM32_PWM_USE_TIM5                  FALSE
#define STM32_PWM_USE_TIM8                  FALSE
#define STM32_PWM_TIM1_IRQ_PRIORITY         7
#define STM32_PWM_TIM2_IRQ_PRIORITY         7
#define STM32_PWM_TIM3_IRQ_PRIORITY         7
#define STM32_PWM_TIM4_IRQ_PRIORITY         7
#define STM32_PWM_TIM5_IRQ_PRIORITY         7
#define STM32_PWM_TIM8_IRQ_PRIORITY         7
*snip*


Code: Select all

static PWMConfig pwmcfgHardwareAbstraction = {
  8000000,                                    /* 8Mhz PWM clock frequency.   */
  US2ST(20),                                        /* PWM period 20us  */
  NULL,                                                       /* Callback function                   */
  {
    {PWM_OUTPUT_ACTIVE_HIGH, NULL}, // channel 1
      {PWM_OUTPUT_ACTIVE_HIGH, NULL},   // channel 2
    {PWM_OUTPUT_DISABLED, NULL},      // channel 3
    {PWM_OUTPUT_DISABLED, NULL}         // channel 4
  },
  /* HW dependent part.*/
  0
   #ifdef STM32_PWM_USE_ADVANCED
   ,0         // only needed if STM32_PWM_USE_ADVANCED is defined
   #endif
};

and

Code: Select all

/*
   * Initializes the PWM driver 1, routes the TIM1 outputs to the motor bridge.
       Alternate function mapping AF1 to set PE9 and PE11 to TIM1_CH1 and TIM1_CH2 output.
   */
   pwmStart(&PWMD1, &pwmcfgHardwareAbstraction);
  palSetPadMode(GPIOE,   MOT1_PWM,   PAL_MODE_ALTERNATE(1));    /* PE9      - MOT1_PWM   */
  palSetPadMode(GPIOE,   MOT2_PWM,   PAL_MODE_ALTERNATE(1));  /* PE11   - MOT2_PWM   */
   
   /*
      Initializes the RNVN2 pins
   */
   palSetPadMode(GPIOE, MOT1_IN1, PAL_MODE_OUTPUT_PUSHPULL);
   palSetPadMode(GPIOE, MOT1_IN2, PAL_MODE_OUTPUT_PUSHPULL);
   palSetPadMode(GPIOE, MOT2_IN1, PAL_MODE_OUTPUT_PUSHPULL);
   palSetPadMode(GPIOE, MOT2_IN2, PAL_MODE_OUTPUT_PUSHPULL);
      
  while (TRUE) {
      /* Block the thread until we receive a message */
      //PIDBalanceThread = chMsgWait();
      //dutyCycle = (dutyCycleStruct*) chMsgGet(PIDBalanceThread);
      
      setRNVN2Pins(dutyCycle->left,      MOT1);
      setRNVN2Pins(dutyCycle->right,   MOT2);
   
      pwmEnableChannelI(&PWMD1, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, dutyCycle->left));
      pwmEnableChannelI(&PWMD1, 1, PWM_PERCENTAGE_TO_WIDTH(&PWMD1, dutyCycle->right));
   
      chThdSleepMilliseconds(500);
  }

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

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby Giovanni » Thu Jan 10, 2013 5:33 pm

Hi,

Could it be a pin speed problem? try adding the high speed specifier to the palSetPadMode().

Try adding a callback and verify if it is actually called, this should prove the driver functionality at least.

Giovanni

Jevermeister
Posts: 13
Joined: Sun Nov 11, 2012 8:19 pm

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby Jevermeister » Thu Jan 10, 2013 5:41 pm

thanks for your reply.

i forgot that dutyCycle needs to be multiplied by 100 and the pwm period settings etc. were wrong too.
now it works :)

Jevermeister
Posts: 13
Joined: Sun Nov 11, 2012 8:19 pm

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby Jevermeister » Thu Jan 10, 2013 6:08 pm

I have another problem now, maybe you have a clue.

If i set PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 90));

with a clock frequency of 1khz and a period of 1ms, nothing happens.

PWM_PERCENTAGE_TO_WIDTH(&PWMD1, 100));

and above works fine. where's the problem, integer math?

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby mabl » Thu Jan 10, 2013 6:45 pm

http://chibios.sourceforge.net/docs/hal ... 8ca8d49531
Be careful with rounding errors, this is integer math not magic. You can specify tenths of thousandth but make sure you have the proper hardware resolution by carefully choosing the clock source and prescaler settings, see PWM_COMPUTE_PSC.


percentage percentage as an integer between 0 and 10000

Have you thought of this?

PS:I wonder, why is this called PWM_PERCENTAGE_TO_WIDTH

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

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby Giovanni » Thu Jan 10, 2013 7:05 pm

Fixed width precision :) from 0.00 to 100.00. I know it can be confusing but I could not think a better term at that time.

Giovanni

mabl
Posts: 417
Joined: Tue Dec 21, 2010 10:19 am
Location: Karlsruhe, Germany
Been thanked: 1 time
Contact:

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby mabl » Thu Jan 10, 2013 7:11 pm

Giovanni wrote:Fixed width precision :) from 0.00 to 100.00. I know it can be confusing but I could not think a better term at that time.

I think in this case it is called a "Basis point" or "permyriad" it even has an own unicode sign: U+2031 ‱ :mrgreen: :ugeek:

Jevermeister
Posts: 13
Joined: Sun Nov 11, 2012 8:19 pm

Re: can't get PWM going with TIM1 on the F4DISCOVERY

Postby Jevermeister » Thu Jan 10, 2013 7:21 pm

solved. had to set my timer to multiples of 256 to prevent rounding errors :) thanks


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 8 guests