AnsweredAssumed Answered

STM32F3 TIMER1 TIM1_UP_TIM16_IRQn does not work

Question asked by alferez.ian on May 28, 2015
Latest reply on May 28, 2015 by alferez.ian
Hello Everyone,

I was looking for a solution for the whole day and unfortunately I was no able to find one, moreover there are no topics in the forum which relates to my problem.

The objective of the code is to create a TIM1 IRQ, however TIMER1 is one of the special timers and I am not sure if I have configured it correctly.

The Interrupt was not executed, and also I cannot add any breakpoints to the Interrupt function.
Do you have any suggestions to my code?

Thanks in advance.



/**
  ******************************************************************************
  * @file    TIM_ComplementarySignals/main.c 
  * @author  MCD Application Team
  * @version V1.0.0
  * @date    20-June-2014
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */


/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f3348_discovery.h"


/** @addtogroup STM32F3348_DISCOVERY_Examples
  * @{
  */


/** @addtogroup TIM_ComplementarySignals
  * @{
  */


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TIM_Config(void);
void EnableTimerInterrupt(void);
void TIM1_IRQHandler(void);
/* Private functions ---------------------------------------------------------*/




/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{  
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f334x8.s) before to branch to application main. 
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f30x.c file
     */  


  /* TIM Configuration ---------------------------------------------------------
  TIM1 Configuration to:


  1/ Generate 3 complementary PWM signals with 3 different duty cycles:
  
    TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2).   
    TIM1CLK =  PCLK2  
    PCLK2 = HCLK  
    => TIM1CLK = HCLK = SystemCoreClock
  
    TIM1CLK is fixed to SystemCoreClock, the 
     TIM1 Prescaler is equal to 0 so the 
    TIM1 counter clock used is SystemCoreClock (72MHz).


    The objective is to generate PWM signal at 17.57 KHz:
    - TIM1_Period = (SystemCoreClock / 17570) - 1


    The Three Duty cycles are computed as the following description: 


    The channel 1 duty cycle is set to 50% so channel 1N is set to 50%.
    The channel 2 duty cycle is set to 25% so channel 2N is set to 75%.
    The channel 3 duty cycle is set to 12.5% so channel 3N is set to 87.5%.
    
    The Timer pulse is calculated as follows:
      - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100


  2/ Insert a dead time equal to (11/SystemCoreClock) ns


  3/ Configure the break feature, active at High level, and using the automatic 
     output enable feature


  4/ Use the Locking parameters level1. 
  
  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. 
  --------------------------------------------------------------------------- */
  TIM_Config();
  EnableTimerInterrupt();




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


/**
  * @brief  Configure the TIM IRQ Handler.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
  TIM_OCInitTypeDef         TIM_OCInitStructure;
  TIM_BDTRInitTypeDef       TIM_BDTRInitStructure;
  GPIO_InitTypeDef          GPIO_InitStructure;
  
  uint32_t TimerPeriod = 0;
  uint32_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0;
  
  /* GPIOA and GPIOB clocks enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
  
  /* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  
  /* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
  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_Init(GPIOA, &GPIO_InitStructure);
  
  /* GPIOA Configuration: BKIN and channel 1N as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  /* GPIOB Configuration: 2N and 3N as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* Connect TIM pins to AF6 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_6);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_6); 
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_6);
  
  /* ---------------------------------------------------------------------------
  TIM1 Configuration to:
  
  1/ Generate 3 complementary PWM signals with 3 different duty cycles:
  
  TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2).   
  TIM1CLK =  PCLK2  
  PCLK2 = HCLK  
  => TIM1CLK = HCLK = SystemCoreClock
  
  TIM1CLK is fixed to SystemCoreClock, the TIM1 Prescaler is equal to 0 so the 
  TIM1 counter clock used is SystemCoreClock (72MHz).
  
  The objective is to generate PWM signal at 17.57 KHz:
  - TIM1_Period = (SystemCoreClock / 17570) - 1
  
  The Three Duty cycles are computed as the following description: 
  
  The channel 1 duty cycle is set to 50% so channel 1N is set to 50%.
  The channel 2 duty cycle is set to 25% so channel 2N is set to 75%.
  The channel 3 duty cycle is set to 12.5% so channel 3N is set to 87.5%.
  
  The Timer pulse is calculated as follows:
  - ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
  
  2/ Insert a dead time equal to (11/SystemCoreClock) ns
  
  3/ Configure the break feature, active at High level, and using the automatic 
  output enable feature
  
  4/ Use the Locking parameters level1.  
  --------------------------------------------------------------------------- */
  
  /* Compute the value to be set in ARR register to generate signal frequency at 17.57 Khz */
  //TimerPeriod = (SystemCoreClock / 17570) - 1;
     TimerPeriod = (SystemCoreClock / 65000) - 1;
  
  /* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
  Channel1Pulse = (((uint32_t) (5 * (TimerPeriod - 1))) / 10);
  
  /* Compute CCR2 value to generate a duty cycle at 25%  for channel 2 */
  Channel2Pulse = (((uint32_t) (25 * (TimerPeriod - 1))) / 100);
  
  /* Compute CCR3 value to generate a duty cycle at 12.5%  for channel 3 */
  Channel3Pulse = (((uint32_t) (125 * (TimerPeriod - 1))) / 1000);
     
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  
  /* Channel 1, 2 and 3 Configuration in PWM mode */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
  
  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  
  TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
  
  TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);
  
  /* Automatic Output enable, Break, dead time and lock configuration*/
  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
  TIM_BDTRInitStructure.TIM_DeadTime = 11;
  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
  
  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
  
  /* TIM1 counter enable */
  TIM_Cmd(TIM1, ENABLE);
  
  /* Main Output Enable */
  TIM_CtrlPWMOutputs(TIM1, ENABLE);  
     
     /* Enable TIM IRQ*/
     TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
}


void EnableTimerInterrupt()
{     
    NVIC_InitTypeDef nvicStructure;
    nvicStructure.NVIC_IRQChannel = TIM1_UP_TIM16_IRQn;
    nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
    nvicStructure.NVIC_IRQChannelSubPriority = 1;
    nvicStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvicStructure);  
     
}


void TIM1_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM1,TIM_IT_Update);
    STM_EVAL_LEDToggle(LED4);
  }
}


#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