PWM input or input capture mode

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

my64
Posts: 9
Joined: Sun Mar 06, 2011 12:08 am

PWM input or input capture mode

Postby my64 » Thu Mar 17, 2011 12:10 am

Hello,
For STM32 mcu, are the PWM input or input capture modes supported ?
Is there any example how to use them ?
Thanks,
Olivier

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: PWM input or input capture mode

Postby Giovanni » Thu Mar 17, 2011 8:55 am

Hi Olivier,

The PWM driver is meant for output only, PWM capture is not currently implemented. This functionality will be implemented into a new driver model (ICU, Input Capture Unit) currently in the to-do queue. It should not be complicated, creating a good driver specification is usually the hardest part.

Anyway, the plan is to add it during the 2.3.x development branch.

Giovanni

my64
Posts: 9
Joined: Sun Mar 06, 2011 12:08 am

Re: PWM input or input capture mode

Postby my64 » Thu Mar 17, 2011 12:53 pm

Thanks Giovanni.
Meanwhile, can I program myself directly the registers for input capture ? No interference with PWM output, assuming I"ll use a different timer ?
Thanks,

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: PWM input or input capture mode

Postby Giovanni » Thu Mar 17, 2011 1:10 pm

Of course you can, just make sure that the timer is not enabled by either PWM or GPT drivers into mcuconf.h.

Giovanni

my64
Posts: 9
Joined: Sun Mar 06, 2011 12:08 am

Re: PWM input or input capture mode

Postby my64 » Thu Mar 17, 2011 4:53 pm

and can i use the original ST stm32 libs, examples and include files with input capture together and Chibios ? Do you foresee any conflits ? Is there any favorite method to do that cleanly ?
Thanks

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

Re: PWM input or input capture mode

Postby mabl » Thu Mar 17, 2011 6:07 pm

Sure you can, just have a look into the Makefile. In short: You will have have to extract the stmlib in the ext/ directory and enable USE_FWLIB in the Makefile of the project .

my64
Posts: 9
Joined: Sun Mar 06, 2011 12:08 am

Re: PWM input or input capture mode

Postby my64 » Fri Mar 18, 2011 12:15 am

Great, thanks, It works, it compiles now with STM32 libs.

My next problem is that as soon as an interrupt for the PWM input triggers, it locks the mcu.
I reused almost all the PWM_input example from STlibs.
Can it be a conflict with NVIC settings ? The was no "misc.c" file in the STM32 libs makefile, I added a line to the makefile.
The code has:

Code: Select all

void tim2InitForCapture()
{

  /* System Clocks Configuration */
  RCC_Configuration();

  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* TIM2 configuration: PWM Input mode ------------------------
     The external signal is connected to TIM2 CH1 pin (PA.01),
     The Rising edge is used as active edge,
     The TIM3 CCR2 is used to compute the frequency value
     The TIM3 CCR1 is used to compute the duty cycle value
  ------------------------------------------------------------ */

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_1  ; // PA0
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; // each time edge detected
  TIM_ICInitStructure.TIM_ICFilter = 0x0;

  TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);

  /* Select the TIM2 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM2, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);

}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCC_Configuration(void)
{
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* GPIOA clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}

/**
  * @brief  Configure the GPIOD Pins.
  * @param  None
  * @retval None
  */
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* TIM2 channel 1 pin (PA.01) configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/**
  * @brief  Configure the nested vectored interrupt controller.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  /* Enable the TIM2 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 15;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


and interrupt code:

Code: Select all

void TIM2_IRQHandler(void)
{
  /* Clear TIM2 Capture compare interrupt pending bit */
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC3);
  TIM_ClearITPendingBit(TIM2, TIM_IT_CC4);

  /* Get the Input Capture value */
  IC2Value = TIM_GetCapture1(TIM2);

  if (IC2Value != 0)
  {
    /* Duty cycle computation */
    DutyCycle = (TIM_GetCapture1(TIM2) * 100) / IC2Value;

    /* Frequency computation */
    Frequency = SystemCoreClock / IC2Value;
  }
  else
  {
    DutyCycle = 0;
    Frequency = 0;
  }
}


Thanks

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: PWM input or input capture mode

Postby Giovanni » Fri Mar 18, 2011 7:50 am

Few notes:
- No not initialize the GPIOA clock, the PAL driver already does that.
- Use the OS provided functions to activate an interrupt on the NVIC (yes, it is possible there is a conflict).
- Write interrupt handlers using the OS macros (if you want to use OS APIs from the handler).

Giovanni

my64
Posts: 9
Joined: Sun Mar 06, 2011 12:08 am

Re: PWM input or input capture mode

Postby my64 » Sat Mar 19, 2011 1:03 am

Thanks,
Finally I decide to use that method:
- initialize just like if I was using a PWM output (with pwm_start)
- then initialize the needed registers for PWM input capture

It works better, PWM input capture now trigger the call backs functions on each CaptureChannel,
however the value returned by Tim2_CCR1 is not coherent. the clock prescaler and cycle period values are probably wrong. But even, with that, the value is not accurate compared the PWM pulse length.

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: PWM input or input capture mode

Postby Giovanni » Sat Mar 19, 2011 10:47 am

A possible problem is that when the timer is active (after pwmStart()) some fields into control registers are no more modifiable.

Anyway I think it is better to use the ST example code, just use ChibiOS functions for NVIC setup and the macros for ISR declarations, it should work fine.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 8 guests