AnsweredAssumed Answered

Mistake?

Question asked by Femol on Dec 10, 2014
Latest reply on Dec 18, 2014 by Femol
STM32F30x_DSP_StdPeriph_Lib_V1.1.0 gives an example of TIM Synchronization (cascaded timer). The readme.txt indicates that this example can be run for STM32F3xx. It can't be run for STM32F302. No TIM3 or TIM4 available for that STM32F302!
I've tried to change TIM3 and TIM4 to TIM15 and TIM16 without success.
Any help is welcome!
Code as follow:

/* Includes ------------------------------------------------------------------*/
#include "stm32f30x.h"

/* Private function prototypes -----------------------------------------------*/
static void TIM_Config(void);

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */


/**
  * @brief  Configure the TIMs.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
 
  /* TIM2, TIM3 and TIM4 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 , ENABLE);

  /* GPIOA and GPIOB clocks enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);

  /* GPIOA Configuration: PA6(TIM3 CH1) as alternate function push-pull ------*/
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;



  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;    //TIM2_CH1 (PA0)
    GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_1); //TIM2_CH1 PA0 AF1


  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    //TIM15_CH1 (PA2)
    GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_9); //TIM15_CH1 PA2 AF9

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;    //TIM16_CH1 (PA6)
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1); //TIM16_CH1 PA6 AF1
 
 
  /* Timers synchronisation in cascade mode ----------------------------
     1/TIM2 is configured as Master Timer:
         - PWM Mode is used
         - The TIM2 Update event is used as Trigger Output  

     2/TIM15 is slave for TIM2 and Master for TIM16,
         - PWM Mode is used
         - The ITR1(TIM2) is used as input trigger
         - Gated mode is used, so start and stop of slave counter
           are controlled by the Master trigger output signal(TIM2 update event).
         - The TIM15 Update event is used as Trigger Output.

     3/TIM16 is slave for TIM15,
         - PWM Mode is used
         - The ITR2(TIM15) is used as input trigger
         - Gated mode is used, so start and stop of slave counter
           are controlled by the Master trigger output signal(TIM15 update event).

     In this example TIM2 input clock (TIM2CLK) is set to APB1 clock (PCLK1).   
     TIM2CLK = PCLK1  
     PCLK1 = HCLK
     => TIM2CLK = HCLK = SystemCoreClock

       The Master Timer TIM2 is running at TIM2 counter clock:
       TIM2 frequency = (TIM2 counter clock)/ (TIM2 period + 1) = 281.25 KHz
       and the duty cycle = TIM2_CCR1/(TIM2_ARR + 1) = 25%.

       The TIM15 is running:
       - At (TIM2 frequency)/ (TIM15 period + 1) = 28.125 KHz and a duty cycle
         equal to TIM15_CCR1/(TIM15_ARR + 1) = 40%

       The TIM16 is running:
       - At (TIM15 frequency)/ (TIM16 period + 1) = 7.031 KHz and a duty cycle
         equal to TIM16_CCR1/(TIM16_ARR + 1) = 50%
 
     Note:
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f30x.c file.
     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
     function to update SystemCoreClock variable value. Otherwise, any configuration
     based on this variable will be incorrect.
  --------------------------------------------------------------------------- */

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 255;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Period = 9;
  TIM_TimeBaseInit(TIM15, &TIM_TimeBaseStructure);

  TIM_TimeBaseStructure.TIM_Period = 3;
  TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);

  /* Init TIM_OCInitStructure */
  TIM_OCStructInit(&TIM_OCInitStructure);
 
  /* Master Configuration in PWM1 Mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 64;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

  /* Select the Master Slave Mode */
  TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);

  /* Master Mode selection */
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);

  /* Slaves Configuration: PWM1 Mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 4;

  TIM_OC1Init(TIM15, &TIM_OCInitStructure);

  /* Slave Mode selection: TIM15 */
  TIM_SelectSlaveMode(TIM15, TIM_SlaveMode_Gated);
  TIM_SelectInputTrigger(TIM2, TIM_TS_ITR1);

  /* Select the Master Slave Mode */
  TIM_SelectMasterSlaveMode(TIM15, TIM_MasterSlaveMode_Enable);

  /* Master Mode selection */
  TIM_SelectOutputTrigger(TIM15, TIM_TRGOSource_Update);

  /* Slaves Configuration: PWM1 Mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 2;

  TIM_OC1Init(TIM16, &TIM_OCInitStructure);

  /* Slave Mode selection: TIM16 */
  TIM_SelectSlaveMode(TIM16, TIM_SlaveMode_Gated);
  TIM_SelectInputTrigger(TIM15, TIM_TS_ITR2);
 
  /* TIM enable counter */
  TIM_Cmd(TIM15, ENABLE);
  TIM_Cmd(TIM2, ENABLE);
  TIM_Cmd(TIM16, ENABLE);
}
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f30x.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f30x.c file
     */

  /* TIMs Configuration */
  TIM_Config();

  /* Infinite loop */
  while (1);
 }

#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 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) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/





Outcomes