cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CubeF4 OnePulse mode on TIMER12

alan
Associate
Posted on April 09, 2014 at 17:09

Hi all,

I have been trying to configure TIM12 to work in OnePulse mode and I am not able to accomplish that. With elder version of device drivers I was able to achieve what I needed in such a way:

/** Create appropriate configuration structures. */
TIM_TimeBaseInitTypeDef xTimeBaseStructure;
TIM_OCInitTypeDef xOCInitStructure;
/** Call timer's PINs configuration. */
prvConfigurePulseTimerPins();
/** Time base configuration */
xTimeBaseStructure.TIM_Period = 400;
xTimeBaseStructure.TIM_Prescaler = (uint16_t) ((SystemCoreClock / 2) / 40000000) - 1;
xTimeBaseStructure.TIM_ClockDivision = 0;
xTimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM12, &xTimeBaseStructure);
/** Timer PWM2 Mode configuration: Channel1 */
xOCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
xOCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
xOCInitStructure.TIM_Pulse = 1;
xOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(fphTimersHal_PulseTimer, &xOCInitStructure);
/** One Pulse Mode selection */
TIM_SelectOnePulseMode(fphTimersHal_PulseTimer, TIM_OPMode_Single);
/** Slave Mode selection: Trigger Mode */
TIM_InternalClockConfig(fphTimersHal_PulseTimer);

And it was enough to force TIM12 to produce one pulse on its OC1 output after call:

TIM_Cmd(TIM12, ENABLE);

Now, with the new drivers (HAL) I am not able to achieve what I want. First of all, how to force timer to be triggered by internal clock in OPM? It seems thatHAL_TIM_OnePulse_ConfigChannel() expects input and output channel to be passed. I have no input channel in my solution, so I tried to overcome that by such a code:

TIM_OC_InitTypeDef sConfigOC;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_ClockConfigTypeDef sClockSourceConfig;
/* Time base initialization. */
OnePulsOutputTimerHdlr.Instance = TIM12;
OnePulsOutputTimerHdlr.Init.Prescaler = (uint16_t) ((SystemCoreClock / 2)/ 40000000) - 1;
OnePulsOutputTimerHdlr.Init.CounterMode = TIM_COUNTERMODE_UP;
OnePulsOutputTimerHdlr.Init.Period = 400;
OnePulsOutputTimerHdlr.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_OnePulse_Init(&OnePulsOutputTimerHdlr, TIM_OPMODE_SINGLE);
/* Timer PWM2 Mode configuration: Channel 1 */
sConfigOC.OCMode = TIM_OCMODE_PWM2;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.Pulse = 1;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&OnePulsOutputTimerHdlr, &sConfigOC, TIM_CHANNEL_1);
/* Initialize master-slave mode. */
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1;
HAL_TIMEx_MasterConfigSynchronization(&OnePulsOutputTimerHdlr,&sMasterConfig);

Having also MSP init prepared in such a way:

void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStruct;
if (htim->Instance == TIM12)
{
/* Enable the TIM12 clock */
__TIM12_CLK_ENABLE();
/* Enable GPIO Port channel1 Clock */
__GPIOB_CLK_ENABLE();
/* TIM12 GPIO Configuration */
/* PB14 ------> TIM8_CH1 */
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF9_TIM14;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}

After I call:

HAL_TIM_OnePulse_Start(&OnePulsOutputTimerHdlr, TIM_CHANNEL_1);

Nothing happens... Any ideas? ps. Of course

OnePulsOutputTimerHdlr

is a global variable.
4 REPLIES 4
Posted on April 11, 2014 at 12:20

Hi,

There is wrong configuration within the MSP init, you are using TIM12, but configurating the GPIO AF of TIM

void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)

{

GPIO_InitTypeDef GPIO_InitStruct;

if (htim->Instance == TIM12)

{

/* Enable the TIM12 clock */

__TIM12_CLK_ENABLE();

/* Enable GPIO Port channel1 Clock */

__GPIOB_CLK_ENABLE();

/* TIM12 GPIO Configuration */

/* PB14 ------> TIM8_CH1 */

GPIO_InitStruct.Pin = GPIO_PIN_14;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

GPIO_InitStruct.Alternate = GPIO_AF9_TIM14;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

}

Regards,

pestov
Associate II
Posted on May 26, 2014 at 12:06

Hello!

If I want repeat One Pulse, , how can i do it with this init?

Posted on May 26, 2014 at 19:07

Hi,

Can you provide some details about the TIM mode to be used,

and about the event that will generate the one pulse ?

With regards.

pestov
Associate II
Posted on May 27, 2014 at 05:03

Hi.

Timer briefly activates the relay via a transistor (by user command). Ichose One Pulse mode for it.

TIM_HandleTypeDef TimHandle;
TIM_OC_InitTypeDef sConfig;
TIM_MasterConfigTypeDef sMasterConfig;
GPIO_InitTypeDef GPIO_InitStruct;
uwPrescalerValue = ((SystemCoreClock /2) / 42000000) - 1; // Compute the prescaler value
__TIM3_CLK_ENABLE();
__GPIOC_CLK_ENABLE();
// GPIOC Configuration: TIM3 CH2 (PC7)
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
// Set TIM3 instance
TimHandle.Instance = TIM3;
TimHandle.Init.Period = 0xFFFF;
TimHandle.Init.Prescaler = uwPrescalerValue;
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_OnePulse_Init(&TimHandle, TIM_OPMODE_SINGLE);
// Configure the Output Compare channels
sConfig.OCMode = TIM_OCMODE_PWM2;
sConfig.Pulse = 1;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2);
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
sMasterConfig.MasterOutputTrigger = TIM_TRGO_OC1;
HAL_TIMEx_MasterConfigSynchronization(&TimHandle, &sMasterConfig);
TIM_CCxChannelCmd(TIM3, TIM_CHANNEL_2, TIM_CCx_ENABLE);

It works 1 time, but i cant repeat pulse.