2018-09-04 01:43 AM
Hi i want to enable pwm output on PA10, as in below code, will do necessary init by MX_TIM1_Init, and then call HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); to start.
but PA10 is kept low measured by Osi.
could you help review and any suggestion ? thanks.
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);
/* TIM1 init function */
void MX_TIM1_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
htim1.Instance = TIM1;
htim1.Init.Prescaler = 1080-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 2000-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim1);
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);
HAL_TIM_MspPostInit(&htim1);
}
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
{
if(htim_pwm->Instance==TIM1)
{
/* USER CODE BEGIN TIM4_MspInit 0 */
/* USER CODE END TIM4_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* USER CODE BEGIN TIM4_MspInit 1 */
/* USER CODE END TIM4_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(htim->Instance==TIM1)
{
/* USER CODE BEGIN TIM4_MspPostInit 0 */
/* USER CODE END TIM4_MspPostInit 0 */
/**TIM4 GPIO Configuration
PB6 ------> TIM4_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN TIM4_MspPostInit 1 */
/* USER CODE END TIM4_MspPostInit 1 */
}
}
GPIO clock have been enabled before timer init: __HAL_RCC_GPIOA_CLK_ENABLE();
2018-09-04 07:35 AM
Read back and check/post the relevant GPIO and TIM registers' content.
JW
2018-09-04 08:23 AM
Hi JW
thanks your feedback, are you confirming there seems no issue in these code i referenced from some demo project ?
especially the gpio setting for PA10, GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
2018-09-04 08:46 AM
> are you confirming there seems no issue in these code
No.
I don't Cube and won't review Cube-related code.
I just recommend you to read out the registers settings and check against the expected ones. The chip does not understand Cube either, it works out of the registers.
JW
PS. A quick glance at the code with no attempt to understand: sConfigOC.Pulse = 0; is suspicious, PWM with pulse length 0 is exactly what you observe, isn't it?
2018-09-04 06:45 PM
thx.
any expert can see any missing or wrong config here ?
2018-09-04 07:35 PM
Make sure you clear the initialization structures, or max sure ALL fields are initialized.
TIM1/TIM8 are Advanced Timers, there is a Repetition Count, and the PWM Input/Output must be explicitly Enabled.
/* Enable the main output */
__HAL_TIM_MOE_ENABLE(htim);
For TIM1 PWM initialization is going to look more like this
/* Select the Timer instance */
TimHandle.Instance = TIM1;
TimHandle.Init.Prescaler = 0;
TimHandle.Init.Period = uwPeriodValue;
TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.RepetitionCounter = 0;
TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-2- Configure the PWM channels #########################################*/
/* Common configuration for all channels */
sPWMConfig.OCMode = TIM_OCMODE_PWM1;
sPWMConfig.OCFastMode = TIM_OCFAST_DISABLE;
sPWMConfig.OCPolarity = TIM_OCPOLARITY_LOW;
sPWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sPWMConfig.OCIdleState = TIM_OCIDLESTATE_SET;
sPWMConfig.OCNIdleState= TIM_OCNIDLESTATE_RESET;
/* Set the pulse value for channel 3 */
sPWMConfig.Pulse = uwPulse3;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sPWMConfig, TIM_CHANNEL_3) != HAL_OK)
{
/* Configuration Error */
Error_Handler();
}
/* Start channel 3 */
if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)
{
/* Starting Error */
Error_Handler();
}
2018-09-05 01:08 AM
>> PS. A quick glance at the code with no attempt to understand: sConfigOC.Pulse = 0; is suspicious, PWM with pulse length 0 is exactly what you observe, isn't it?
> any expert can see any missing or wrong config here ?
Did you try to change that pulse length?
Have you already read out and checked the registers' content?
JW