cancel
Showing results for 
Search instead for 
Did you mean: 

Timer 2 clocks timer 5 in Nucleo L476RG

Tommino
Senior

Hello,

I would like to make timer 2 compare event in channel 1 to clock timer 5. Timer 2 compare events also sends DMA request and timer 5 is also used to output a PWM waveform.

Here you can find the Init function that I wrote and the main code. At the end something is not working because I can't see any waveform at PAO (the one supposed to output the PWM). I double checked the Timer registers and they seem fine to me. Can you help me in finding the errors? I am not able to undesrtand wheter the problem is on the TIM2 or TIM5 (both the timer synchronization part and the GPIO configuration)

I shared only the relvant code for the two timers and removed

 
 
 
 
 
 
// timer2 initialization
void TIM2Init (void){
	 // Enable the timer clock
	 RCC->APB1ENR1 |= (1<<0); // Enable the tim2 clock writing 1 in bit 0
	 // Set the prescaler and the ARR to set the frequency of the sinewave
	 TIM2 -> PSC = 79; // 80 MHz/80= 1MHz clock --> 1us count
	 TIM2-> ARR = 5; // ARR value uguale a CCR va bene? o deve essere più alto?
	 TIM2 -> CCR1= 5; // ogni volta che c'è il compare riparte da zero
	 TIM2 -> CCR2= 5; // ogni volta che c'è il compare riparte da zero
	 TIM2 -> CR2 |= (1<<6)|(0<<5)|(0<<4); // Master mode selection Bits 6:4 MMS[2:0]=100
	 // TIM2 -> SMCR |= (1<<7); (TBC)
	 TIM2 -> EGR |= (1<<1) | (1<<2); // compare event generation enabled 
	 TIM2 -> DIER |= (1<<9); // Enable the DMA request when compare 1
	 TIM2 -> DIER |= (1<<10); // Enable the DMA request when compare 2
}
 
// Tim5 is the slave timer clocked by tim 2 PWM Mode 
void TIM5Init (void){
	 // Enable the timer clock (not necessary?)
	 //RCC->APB1ENR1 |= (1<<3); // Enable the tim5 clock writing 1 in bit 3
	 // Set the period (ARR) and duty (CCR1): Frequency TimClock/ ARR. Duty cycle set by CCR/ARR
	 TIM5-> ARR = 100; // ARR value --> period equal to the number of samples of the sinusoidal waveform
	 TIM5-> CCR1= 50;
	 // set the slave mode: the clock is the compare event of tim2_ch1
	 TIM5 -> SMCR|= (1<<0)|(1<<1)|(1<<2); // SMS=111 external clock mode 1 and TS=000 internal trigger 0
	 // set the PWM mode
	 TIM5 -> CCMR1 |= (0<<0); // to set the output compare mode
	 TIM5 -> CCMR1 |= (1<<3); // OC1PE bit to enable the preload register
	 TIM5 -> CCMR1 |= (6<<4); // OC1M bits to set the PWM mode in ch1
	 TIM5 -> CR1 |= (1<<7); // ARPE BIT:ARR register is buffered
	 //TIM5-> EGR |= (1<<1); // Enable the compare event
	 TIM5 -> CCER |= (0<<1); // CC1P bits to set the polarity (write 0 in bit 1 to have active high)
	 TIM5-> CCER |= (1<<0); // output on the corresponding output pin enable
	 // Enable the GPIOA Clock
	 RCC-> AHB2ENR |= (1<<0); // Enable the GPIOA clock writing 1 in bit 0
	 // Configure the GPIO (PAO AlternateFunction2) to output the PWM
	 GPIOA -> MODER |= (1<<1);  // PAO configured as alternate function
	 GPIOA -> AFR[0] |= (1<<1); // Set alternate Function 2 (AFR[0] is called GPIOx_AFRL in the manual)
	 GPIOA -> OTYPER |= (0<<0); // output as push-pull
	 GPIOA -> PUPDR |= (0<<0); // No pullup or pulldown
	 GPIOA -> OSPEEDR |= (1<<0); // Medium Speed
	 //
}
 
 
 
 
 
 //
 
 
/* USER CODE END 0 */
 
 
int main(void)
{
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  MX_SPI2_Init();
  /* USER CODE BEGIN 2 */
  TIM2Init();
  TIM5Init();
  DMA1_Init();
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
 
   TIM2 -> CR1|= (1<<0); // Enable Timer2
   TIM5 -> CR1|= (1<<0); // Enable Timer5 for PWM
 
 
 
 
 
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
 
  }
  /* USER CODE END 3 */
}
 
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB buses 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_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief SPI1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI1_Init(void)
{
 
  /* USER CODE BEGIN SPI1_Init 0 */
 
  /* USER CODE END SPI1_Init 0 */
 
  /* USER CODE BEGIN SPI1_Init 1 */
 
  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */
 
  /* USER CODE END SPI1_Init 2 */
 
}
 
/**
  * @brief SPI2 Initialization Function
  * @param None
  * @retval None
  */
static void MX_SPI2_Init(void)
{
 
  /* USER CODE BEGIN SPI2_Init 0 */
 
  /* USER CODE END SPI2_Init 0 */
 
  /* USER CODE BEGIN SPI2_Init 1 */
 
  /* USER CODE END SPI2_Init 1 */
  /* SPI2 parameter configuration*/
  hspi2.Instance = SPI2;
  hspi2.Init.Mode = SPI_MODE_MASTER;
  hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
  hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 7;
  hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI2_Init 2 */
 
  /* USER CODE END SPI2_Init 2 */
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
  /*Configure GPIO pin : B1_Pin */
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
 
  /*Configure GPIO pins : USART_TX_Pin USART_RX_Pin */
  GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_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_USART2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}
 
#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 /* USE_FULL_ASSERT */
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

1 ACCEPTED SOLUTION

Accepted Solutions
Tommino
Senior

Solved 🙂

Timer 5 shall be enabled before tim2

View solution in original post

1 REPLY 1
Tommino
Senior

Solved 🙂

Timer 5 shall be enabled before tim2