AnsweredAssumed Answered

STM32F4-Discovery Timer External Clock Mode 2, blocked at 1

Question asked by flenet.timothe on Mar 18, 2014
Latest reply on Mar 18, 2014 by Clive One

Hello all,

I’ve been fighting for 2 days with a STM32F4-Discovery board to get the Timer 3 counting properly in External Counting Mode 2 but I can’t figure out what’s wrong in my code or in the MCU config.

I’m trying to implement a function that counts up to N top of an external signal (f≈600KHz) and toggles timer channel 3 (Channel 1 and 2 corresponding GPIO Pins are already used on evaluation board) when CNT=N (using output compare mode).

I wrote some code to test my function at low speed: The idea is to generate a “clock” using the user button on the board to generate a clock on GPIO B1. This clock is connected to TIM3_ETR (GPIO D2). At each push on the button CNT should increase and channel 3 output (GPIO B0) should be properly toggled when the CNT equals to CCR3 value. In the main I added CNT value change capture code in order to set a breakpoint in debug mode when counter increases.

With the test code below CNT is increasing one time and remain blocked at 0x0001.

Does anybody see anything wrong in the code or any hardware configuration issue I could have made?

Regards

001./**
002.  ******************************************************************************
003.  * File Name          : main.c
004.  * Date               : 17/03/2014 12:20:04
005.  * Description        : Main program body
006.  ******************************************************************************
007.  *
008.  *
009.  ******************************************************************************
010.  */
011. 
012./* Includes ------------------------------------------------------------------*/
013.#include "stm32f4xx.h"
014.#include "stm32f4xx_rcc.h"
015.#include "stm32f4xx_tim.h"
016.#include "STM32F4-Discovery/stm32f4_discovery.h"
017. 
018. 
019./* USER CODE BEGIN 0 */
020./* Private variables ---------------------------------------------------------*/
021. 
022./* Private function prototypes -----------------------------------------------*/
023.static void PeriphClock_EN(void);
024.static void GPIO_Config(void);
025.static void TIM3_Init(void);
026./* USER CODE END 0 */
027. 
028.int main(void)
029.{
030./* USER CODE BEGIN 1 */
031.    STM_EVAL_LEDInit(LED3);
032.    STM_EVAL_LEDInit(LED4);
033.    STM_EVAL_PBInit(BUTTON_USER,BUTTON_MODE_GPIO);
034. 
035.    /* Enable peripheral clocks */
036.    PeriphClock_EN();
037. 
038.    /* Initialize all configured peripherals */
039.    GPIO_Config();
040. 
041.    /* Initialize Timers */
042.    TIM3_Init();
043./* USER CODE END 1 */
044. 
045./* USER CODE BEGIN 2 */
046.      int i=0;
047.      uint32_t cnt = 0;
048./* USER CODE END 2 */
049. 
050./* USER CODE BEGIN 3 */
051.  /* Infinite loop */
052. 
053.  while (1)
054.  {
055.      //Toggling GPIO and LED3 when phushing button
056.      if(STM_EVAL_PBGetState(BUTTON_USER)){
057.          STM_EVAL_LEDToggle(LED3);
058.          GPIO_ToggleBits(GPIOB,GPIO_Pin_1);
059. 
060.          //Avoiding multiple toggle for one push
061.          for (i=0;i<500000;i++);
062.      }
063. 
064.      //Counter Increase Capture to set debug breakpoint
065.      if(cnt != TIM_GetCounter(TIM3)){
066.          cnt =TIM_GetCounter(TIM3);
067.      }
068.  }
069.}
070. 
071. 
072. 
073.void PeriphClock_EN(void){
074.    // Enable TIM3 Clocks
075.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
076.    //Enabling Port B Clocks
077.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
078.    //Enabling Port D Clocks
079.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
080.}
081. 
082. 
083.void GPIO_Config(void){
084. 
085. 
086.        /**TIM3 GPIO Configuration**************************
087.        PB0   ------> TIM3_CH3
088.        PD2   ------> TIM3_ETR
089.        */
090. 
091.        //PB0 ------> TIM3_CH3
092.        GPIO_InitTypeDef gpioStructure;
093.        gpioStructure.GPIO_Pin = GPIO_Pin_0;
094.        gpioStructure.GPIO_Mode = GPIO_Mode_AF;
095.        gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
096.        gpioStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
097.        GPIO_Init(GPIOB, &gpioStructure);
098. 
099.        GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3);
100. 
101. 
102.        //PD2 GPIO Configuration as Input For Test
103./*      gpioStructure.GPIO_Pin = GPIO_Pin_2;
104.        gpioStructure.GPIO_Mode = GPIO_Mode_IN;
105.        gpioStructure.GPIO_Speed = GPIO_Speed_2MHz;
106.        gpioStructure.GPIO_OType = GPIO_OType_PP;
107.        gpioStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
108.        GPIO_Init(GPIOD, &gpioStructure);*/
109. 
110.        //PD2 ------> TIM3_ETR
111.        gpioStructure.GPIO_Pin = GPIO_Pin_2;
112.        gpioStructure.GPIO_Mode = GPIO_Mode_AF;
113.        gpioStructure.GPIO_Speed = GPIO_Speed_2MHz;
114.        gpioStructure.GPIO_OType = GPIO_OType_PP;
115.        gpioStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
116.        GPIO_Init(GPIOD, &gpioStructure);
117. 
118.        GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);
119. 
120. 
121.        //Setting PB1 in output Mode
122.        gpioStructure.GPIO_Pin = GPIO_Pin_1;
123.        gpioStructure.GPIO_Mode = GPIO_Mode_OUT;
124.        gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
125.        gpioStructure.GPIO_OType = GPIO_OType_PP;
126.        gpioStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
127.        GPIO_Init(GPIOB, &gpioStructure);
128.}
129. 
130. 
131.void TIM3_Init(void){
132.        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
133.        NVIC_InitTypeDef NVIC_InitStructure;
134.        TIM_OCInitTypeDef  TIM_OCInitStructure;
135. 
136.        /* Enable the TIM3 gloabal Interrupt */
137.        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
138.        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
139.        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
140.        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
141.        NVIC_Init(&NVIC_InitStructure);
142. 
143.        /* TIM3 configuration */
144.        TIM_TimeBaseStructure.TIM_Period = 3; // 133
145.        TIM_TimeBaseStructure.TIM_Prescaler = 100;
146.        TIM_TimeBaseStructure.TIM_ClockDivision = 1;
147.        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
148.        TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
149.        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
150. 
151.        // External Trigger set to External Clock, Mode 2
152.        TIM_ETRClockMode2Config (TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
153. 
154. 
155.        TIM_OCStructInit(&TIM_OCInitStructure);
156. 
157.        /* Output Compare Timing Mode configuration: Channel 3 */
158.        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; // Active
159.        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
160.        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
161.        TIM_OCInitStructure.TIM_Pulse = 2;
162.        TIM_OC3Init(TIM3, &TIM_OCInitStructure);
163. 
164.        TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Disable);
165. 
166.        TIM_UpdateDisableConfig(TIM3,ENABLE);
167. 
168.        /* Clear TIM2 update pending flag */
169.        TIM_ClearFlag(TIM3, TIM_FLAG_Update);
170. 
171.        /* Enable TIM2 Capture Compare 3 interrupt */
172.        TIM_ITConfig(TIM3, TIM_IT_CC3, DISABLE);
173. 
174. 
175.        /* TIM2 enable counter */
176.        TIM_Cmd(TIM3, ENABLE);
177. 
178. 
179.}
180. 
181.// TIM3 callback handler
182.void TIM3_IRQHandler()
183.{
184.    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
185.    {
186. 
187.    int i=0;
188.    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
189. 
190.    // toggle bit
191.    STM_EVAL_LEDToggle(LED4);
192. 
193.    for (i=0;i<1000000;i++);
194.    }
195.}

Outcomes