cancel
Showing results for 
Search instead for 
Did you mean: 

TIM8_CH2 and TIM8_CH2N at the same time ?

SBACO
Associate III

Hello,

I am trying to drive a piezo with complementary output from a timer. I am using TIM8_CH2 and TIM8_CH2 as control logic for my piezo command. I expect it to be complementary to increase volume of the piezo. TIM8 is configured as slave of TIM2 that generate the "melody".

I check the hardware output, but the thing is that I only have one of the two GPIO with a working PWM. The other one remains at '0'.

Is that possible to have complementary output on the same channel on the TIM8 (PJ6 and PJ7) on the STM32h750XB I am using ?

Both GPIO are configure as Alternate function AF3_TIM8, in Push-pull.

Here is the configuration code I am using :

static void BSP_Horn_Init_Timer(void)
{
	/* Timer Output Compare Configuration Structure declaration */
	TIM_OC_InitTypeDef sConfig;
 
	/* Master and slave configurations */
	TIM_SlaveConfigTypeDef sSlaveConfig;
	TIM_MasterConfigTypeDef sMasterConfig;
 
	uint32_t uhPrescalerValueWav;
 
	// Configure the WAV_TIMx 4MHz timer base
	WAV_TIMx_CLK_ENABLE();
	WavTimHandle.Instance = WAV_TIMx;
 
	uhPrescalerValueWav = (uint32_t) (SystemCoreClock / WAV_FREQUENCY) - 1;
 
	WavTimHandle.Init.Period = 100;
	WavTimHandle.Init.Prescaler = uhPrescalerValueWav;
	WavTimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	WavTimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
	if (HAL_TIM_PWM_Init(&WavTimHandle) != HAL_OK)
	{
		Error_Handler();
	}
 
	sConfig.OCMode = TIM_OCMODE_PWM1;
	sConfig.Pulse = 50;			// (WavTimHandle.Init.Period / 2)
	sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
	sConfig.OCFastMode = TIM_OCFAST_DISABLE;
	sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
 
	if (HAL_TIM_PWM_ConfigChannel(&WavTimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
	{
		Error_Handler();
	}
 
	/* Configure TIM2 as master & use the update event as Trigger Output (TRGO - ITR1) */
	sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
	sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
	if (HAL_TIMEx_MasterConfigSynchronization(&WavTimHandle, &sMasterConfig) != HAL_OK)
	{
		Error_Handler();
	}
 
	HAL_NVIC_SetPriority(WAV_IRQ, WAV_IRQ_PRIO, 0U);
	HAL_NVIC_EnableIRQ(WAV_IRQ);
 
	/* HORN_TIMx Configuation. */
 
	HORN_TIMx_CLK_ENABLE();
 
	TimHornHandle.Instance = HORN_TIMx;
	TimHornHandle.Init.Prescaler = 0;
	TimHornHandle.Init.Period = 10;
	TimHornHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	TimHornHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
	TimHornHandle.Init.RepetitionCounter = 0;
	if (HAL_TIM_PWM_Init(&TimHornHandle) != HAL_OK)
	{
		Error_Handler();
	}
 
	sConfig.OCMode = TIM_OCMODE_PWM1;
	sConfig.Pulse = 5; 
	sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfig.OCNPolarity = TIM_OCNPOLARITY_LOW;
	sConfig.OCFastMode = TIM_OCFAST_DISABLE;
	sConfig.OCIdleState = TIM_OCIDLESTATE_RESET;
	sConfig.OCNIdleState = TIM_OCNIDLESTATE_SET;
 
	if (HAL_TIM_PWM_ConfigChannel(&TimHornHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
	{
		Error_Handler();
	}
 
	/* Configure HORN_TIMx in Gated slave mode &
	 use the Internal Trigger 1 (ITR1) as trigger source */
	sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
	sSlaveConfig.InputTrigger = TIM_TS_ITR1;
	sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
	sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
	sSlaveConfig.TriggerFilter = 0;
	if (HAL_TIM_SlaveConfigSynchronization(&TimHornHandle, &sSlaveConfig) != HAL_OK)
	{
		Error_Handler();
	}
 
}

1 ACCEPTED SOLUTION

Accepted Solutions

Read out and check the TIM registers' content. Did you enable CH2N in TIMx_CCER? I don't cube.

JW

View solution in original post

3 REPLIES 3

Read out and check the TIM registers' content. Did you enable CH2N in TIMx_CCER? I don't cube.

JW

SBACO
Associate III

Hello,

Thanks for your quick reply. You're right, the TIM8->CCER->CC2NE was not enabled. I activate it manually in debug, and it works. I do not find any way yet to configure it using HAL, I will spend some times looking into it, or I will do it manually in the code apart from HAL. Thanks a lot

This has come up here previously and there's some HAL incantation for it, it's just that I loathe Cube so I am not really willing to remember these things.

JW