AnsweredAssumed Answered

Timers - Port from F4 to F1

Question asked by xavier.jose on Sep 2, 2013
Latest reply on Sep 27, 2013 by Amel N
The code above is part of a library to control 2 stepper motor for STM32F4. I pretend to use it with STM32F1.

1- Lines 43 to 46 configure the GPIO to Outupo with PushPull, the two lines that I added and commented are the equivalent for STM32F1?

2-Usually when programming to STM32F1 I define the GPIO to AF when want to use a timer channel on it. In that case the author configure the channels starting at line 99 but in the GPIO configuration he doesn't tell to use the AF. Is it normal in STM32F4?

I already contacted the author too but if you can help me.

Thanks :)

001./*=============================================================================
002.   
003.    @file    stm32f_stpdrv.c
004.   @author  Paulo de Almeida
005.   @version V1.3.0
006.   @date    02/08/2013
007.   @brief   Stepper Driver for STM32F family
008.   
009.   This Software is released under no garanty.
010.    You may use this software for personal use.
011.    Use for commercial and/or profit applications is strictly prohibited.
012.     
013.    If you change this code, please send an email to the author.
014.     
015.    COPYRIGHT (C) 2013 Paulo de Almeida
016.   Contact the author at:  pa@astrosite.net
017.   
018.   Atention: This header MUST NOT BE REMOVED
019.   
020.   Compiled under C99 (ISO/IEC 9899:1999) version
021.   please use the "--c99" compiler directive
022.     
023.   Description and Usage: See stm32f_stpdrv.h
024. 
025.==============================================================================*/
026. 
027.void STPDRV_Init(void)
028.{
029.    GPIO_InitTypeDef                GPIO_InitStructure;
030.    NVIC_InitTypeDef                NVIC_InitStructure;
031.    TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
032.    TIM_OCInitTypeDef           TIM_OCInitStructure;
033. 
034.    SystemCoreClockUpdate();
035. 
036.    //----- GPIO AHB1Periph clock enable
037.    RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR1_STEP_PORT) , ENABLE);
038.    RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR1_DIR_PORT)  , ENABLE);
039.    RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR2_STEP_PORT) , ENABLE);
040.    RCC_AHB1PeriphClockCmd(__GPIO2AHB1Periph(MOTOR2_DIR_PORT)  , ENABLE);
041.     
042.    // GPIO Configuration - Step PINs & DIR PINs
043.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
044.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
045.    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
046.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
047.    //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
048.    //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
049. 
050.    GPIO_InitStructure.GPIO_Pin = MOTOR1_STEP_PIN;
051.    GPIO_Init(MOTOR1_STEP_PORT, &GPIO_InitStructure);
052.    GPIO_InitStructure.GPIO_Pin = MOTOR1_DIR_PIN;
053.    GPIO_Init(MOTOR1_DIR_PORT, &GPIO_InitStructure);
054.     
055.    GPIO_InitStructure.GPIO_Pin = MOTOR2_STEP_PIN;
056.    GPIO_Init(MOTOR2_STEP_PORT, &GPIO_InitStructure);
057.    GPIO_InitStructure.GPIO_Pin = MOTOR2_DIR_PIN;
058.    GPIO_Init(MOTOR2_DIR_PORT, &GPIO_InitStructure);
059. 
060. 
061.    //----- API struc  INIT (after GPIO init)
062.    Motors[0].CurDelay = 0xffff;
063.    __ResetTargetSpeed(0);
064.    __MotorSetDir(0, dir_CW);
065.    STPDRV_SetRamp(0, 4);
066.    Motors[1].CurDelay = 0xffff;
067.    __MotorSetDir(1, dir_CW);
068.    __ResetTargetSpeed(1);
069.    STPDRV_SetRamp(1, 4);
070. 
071. 
072.    //----- TIM Periph clock enable
073.    RCC_APB1PeriphClockCmd(STPDRV_TIM_APB , ENABLE);
074.     
075.    //--- TIMx Configuration: Output Compare Timing Mode -----------------------
076.    // TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1.  
077.    //  TIM3CLK = 2 * PCLK1 
078.    //  PCLK1 = HCLK / 4
079.    //  => TIM3CLK = 2 * (HCLK / 4) = HCLK / 2 = SystemCoreClock / 2
080.   //
081.    // Para um "TIMx counter clock" de 100KHz (exemplo com resolução de 10uS) o "Prescaler" deve ser calculado assim:
082.    // Prescaler = (uint16_t) (TIMxCLK / 100000) - 1;
083.    //
084.    //  O "Period" neste caso não interessa pois a IRQ (TIMx_IRQHandler) dos CCR (capture/compare register) controla o
085.    // togles dos estados HiGH e LOW dos PINs do STEP
086.   //
087.   // A resolução do sistema é definida em "stm32f_stpdrv.h" no define STPDRV_TIMFREQ
088.   //
089.    TIM_TimeBaseStructure.TIM_Period = 65535;
090.    TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) ((SystemCoreClock / 2) / (STPDRV_TIMFREQ * 2)) - 1;
091.    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
092.    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
093.    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000;
094.    TIM_TimeBaseInit(STPDRV_TIM, &TIM_TimeBaseStructure);
095.   TIM_UpdateDisableConfig(STPDRV_TIM, ENABLE);  // deve ser ENABLE pois para desactiver o Update Event o bit deve ser 1
096.    /* Prescaler configuration */
097.    //TIM_PrescalerConfig(STPDRV_TIM, PrescalerValue, TIM_PSCReloadMode_Immediate);
098. 
099.    // Output Compare Toggle Mode configuration: All Channels
100.   TIM_OCStructInit(&TIM_OCInitStructure);
101.    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //TIM_OCMode_Toggle
102.    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable; // Disable por defeito, API liga quando necessario
103.    TIM_OCInitStructure.TIM_Pulse = 0;
104.    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
105. 
106.    // Output Compare Toggle Mode configuration: Channel1 - MOTOR 1
107.    TIM_OC1Init(STPDRV_TIM, &TIM_OCInitStructure);
108.    TIM_OC1PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable);
109. 
110.    // Output Compare Toggle Mode configuration: Channel2 - MOTOR 2
111.    TIM_OC2Init(STPDRV_TIM, &TIM_OCInitStructure);
112.    TIM_OC2PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable);
113. 
114.    // Output Compare Toggle Mode configuration: Channel3 - MOTOR 1 ACELL e DECEL
115.    TIM_OC3Init(STPDRV_TIM, &TIM_OCInitStructure);
116.    TIM_OC3PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable);
117. 
118.    // Output Compare Toggle Mode configuration: Channel4 - MOTOR 2 ACELL e DECEL
119.    TIM_OC4Init(STPDRV_TIM, &TIM_OCInitStructure);
120.    TIM_OC4PreloadConfig(STPDRV_TIM, TIM_OCPreload_Disable);
121. 
122. 
123.   // Enable the TIM gloabal Interrupt
124.    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;   // USER EDIT - Mudar o nome do IRQ se o TIMER for alterado
125.    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQ_STPDRV_PrePriority;
126.    NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQ_STPDRV_Priority;
127.    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
128.    NVIC_Init(&NVIC_InitStructure);
129. 
130.    // TIM INT enable
131.   // A interrupt é activada na função Motor_On()
132.   //TIM_ITConfig(STPDRV_TIM, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
133. 
134.    // TIM enable counter
135.    TIM_Cmd(STPDRV_TIM, ENABLE);
136.}

Outcomes