AnsweredAssumed Answered

STM32F446 PWM timer trigger input not working

Question asked by Blessinger.Matthew on Jan 8, 2017
Latest reply on Jan 8, 2017 by waclawek.jan

I'm trying to start PWM on channels 2-4 of Timer8 of a STM32F446ZE by using the Channel 1 pin as the trigger input. As far as I understand, this should be possible with the TIM_TS_TI1FP1 input trigger setting. I've used STM32CubeMX to generate the code, but when I put 3.3V high on the Timer8 Channel 1 pin (PC6), the PWM does not start. Rest of the settings appear to be fine since I can call HAL_TIM_PWM_Startfor each channel, and they start outputting the PWM signal.

Does anyone have experience setting up an external trigger for timers? Thank you for any help!

 

/* Includes ------------------------------------------------------------------*/

#include "main.h"

#include "stm32f4xx_hal.h"

 

/* Private variables ---------------------------------------------------------*/

TIM_HandleTypeDef htim7;

TIM_HandleTypeDef htim8;

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

void Error_Handler(void);

static void MX_GPIO_Init(void);

static void MX_TIM8_Init(void);

                                    

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);

 

int main(void)

{

  /* MCU Configuration----------------------------------------------------------*/

 

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

 

  /* Configure the system clock */

  SystemClock_Config();

 

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM8_Init();

 

  while (1)

  {

 

  }

 

}

 

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

 

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

 

    /**Configure the main internal regulator output voltage 

    */

  __HAL_RCC_PWR_CLK_ENABLE();

 

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 

    /**Initializes the CPU, AHB and APB busses clocks 

    */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = 16;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

  RCC_OscInitStruct.PLL.PLLM = 8;

  RCC_OscInitStruct.PLL.PLLN = 180;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

  RCC_OscInitStruct.PLL.PLLQ = 2;

  RCC_OscInitStruct.PLL.PLLR = 2;

  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

  {

    Error_Handler();

  }

 

    /**Activate the Over-Drive mode 

    */

  if (HAL_PWREx_EnableOverDrive() != HAL_OK)

  {

    Error_Handler();

  }

 

    /**Initializes the CPU, AHB and APB busses clocks 

    */

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

 

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)

  {

    Error_Handler();

  }

 

    /**Configure the Systick interrupt time 

    */

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

 

    /**Configure the Systick 

    */

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

 

 

/* TIM8 init function */

static void MX_TIM8_Init(void)

{

 

  TIM_ClockConfigTypeDef sClockSourceConfig;

  TIM_SlaveConfigTypeDef sSlaveConfig;

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_OC_InitTypeDef sConfigOC;

  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

 

  htim8.Instance = TIM8;

  htim8.Init.Prescaler = 5;

  htim8.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim8.Init.Period = 999;

  htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim8.Init.RepetitionCounter = 0;

  if (HAL_TIM_Base_Init(&htim8) != HAL_OK)

  {

    Error_Handler();

  }

 

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

  if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK)

  {

    Error_Handler();

  }

 

  if (HAL_TIM_PWM_Init(&htim8) != HAL_OK)

  {

    Error_Handler();

  }

 

  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;

  sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;

  sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;

  sSlaveConfig.TriggerFilter = 15;

  if (HAL_TIM_SlaveConfigSynchronization(&htim8, &sSlaveConfig) != HAL_OK)

  {

    Error_Handler();

  }

 

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK)

  {

    Error_Handler();

  }

 

  sConfigOC.OCMode = TIM_OCMODE_PWM1;

  sConfigOC.Pulse = 499;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;

  if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)

  {

    Error_Handler();

  }

 

  if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

  {

    Error_Handler();

  }

 

  if (HAL_TIM_PWM_ConfigChannel(&htim8, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)

  {

    Error_Handler();

  }

 

  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;

  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;

  sBreakDeadTimeConfig.DeadTime = 0;

  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;

  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;

  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;

  if (HAL_TIMEx_ConfigBreakDeadTime(&htim8, &sBreakDeadTimeConfig) != HAL_OK)

  {

    Error_Handler();

  }

 

  HAL_TIM_MspPostInit(&htim8);

 

}

 

void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)

{

 

  GPIO_InitTypeDef GPIO_InitStruct;

  if(htim_base->Instance==TIM8)

  {

 

    /* Peripheral clock enable */

    __HAL_RCC_TIM8_CLK_ENABLE();

  

    /**TIM8 GPIO Configuration    

    PC6     ------> TIM8_CH1 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_6;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

    GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;

    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 

  }

}

 

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)

{

 

  GPIO_InitTypeDef GPIO_InitStruct;

  if(htim->Instance==TIM8)

  {  

    /**TIM8 GPIO Configuration    

    PC7     ------> TIM8_CH2

    PC8     ------> TIM8_CH3

    PC9     ------> TIM8_CH4 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

    GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;

    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

 

  }

}

 

static void MX_GPIO_Init(void)

{

 

  GPIO_InitTypeDef GPIO_InitStruct;

 

  /* GPIO Ports Clock Enable */

  __HAL_RCC_GPIOC_CLK_ENABLE();

  __HAL_RCC_GPIOH_CLK_ENABLE();

  __HAL_RCC_GPIOB_CLK_ENABLE();

  __HAL_RCC_GPIOD_CLK_ENABLE();

  __HAL_RCC_GPIOG_CLK_ENABLE();

  __HAL_RCC_GPIOA_CLK_ENABLE();

 

  /*Configure GPIO pin : User_Blue_Button_Pin */

  GPIO_InitStruct.Pin = User_Blue_Button_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  HAL_GPIO_Init(User_Blue_Button_GPIO_Port, &GPIO_InitStruct);

 

  /*Configure GPIO pins : LD3_Pin LD2_Pin */

  GPIO_InitStruct.Pin = LD3_Pin|LD2_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 

  /*Configure GPIO pins : STLK_RX_Pin STLK_TX_Pin */

  GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF7_USART3;

  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

 

  /*Configure GPIO pin : USB_PowerSwitchOn_Pin */

  GPIO_InitStruct.Pin = USB_PowerSwitchOn_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(USB_PowerSwitchOn_GPIO_Port, &GPIO_InitStruct);

 

  /*Configure GPIO pin : USB_OverCurrent_Pin */

  GPIO_InitStruct.Pin = USB_OverCurrent_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  HAL_GPIO_Init(USB_OverCurrent_GPIO_Port, &GPIO_InitStruct);

 

  /*Configure GPIO pins : USB_SOF_Pin USB_ID_Pin USB_DM_Pin USB_DP_Pin */

  GPIO_InitStruct.Pin = USB_SOF_Pin|USB_ID_Pin|USB_DM_Pin|USB_DP_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

  /*Configure GPIO pin : USB_VBUS_Pin */

  GPIO_InitStruct.Pin = USB_VBUS_Pin;

  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  HAL_GPIO_Init(USB_VBUS_GPIO_Port, &GPIO_InitStruct);

 

  /*Configure GPIO pin Output Level */

  HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET);

 

  /*Configure GPIO pin Output Level */

  HAL_GPIO_WritePin(USB_PowerSwitchOn_GPIO_Port, USB_PowerSwitchOn_Pin, GPIO_PIN_RESET);

 

}

 

/**

  * @brief  This function is executed in case of error occurrence.

  * @param  None

  * @retval None

  */

void Error_Handler(void)

{

  /* USER CODE BEGIN Error_Handler */

  /* User can add his own implementation to report the HAL error return state */

  while(1) 

  {

  }

  /* USER CODE END Error_Handler */ 

}

 

#ifdef USE_FULL_ASSERT

 

/**

   * @brief Reports the name of the source file and the source line number

   * where the assert_param error has occurred.

   * @param file: pointer to the source file name

   * @param line: assert_param error line source number

   * @retval None

   */

void assert_failed(uint8_t* file, uint32_t line)

{

  /* USER CODE BEGIN 6 */

  /* User can add his own implementation to report the file name and line number,

    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* USER CODE END 6 */

 

}

 

#endif

Outcomes