Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
actuators_pwm_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 The Paparazzi Team
3  *
4  * This file is part of Paparazzi.
5  *
6  * Paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * Paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
27 
28 #include <stm32/gpio.h>
29 #include <stm32/rcc.h>
30 #include <stm32/flash.h>
31 #include <stm32/misc.h>
32 #include <stm32/tim.h>
33 
34 #define PCLK 72000000
35 #define ONE_MHZ_CLK 1000000
36 #ifndef SERVO_HZ
37 #define SERVO_HZ 40
38 #endif
39 
40 #ifndef PWM_5AND6_TIMER
41 #define PWM_5AND6_TIMER TIM4
42 #define PWM_5AND6_RCC RCC_APB1Periph_TIM4
43 #define PWM_5AND6_GPIO GPIOB
44 #define PWM5_OC 3
45 #define PWM6_OC 4
46 #define PWM5_Pin GPIO_Pin_8
47 #define PWM6_Pin GPIO_Pin_9
48 #endif
49 
50 #define _TIM_OC_INIT(n) TIM_OC##n##Init
51 #define TIM_OC_INIT(n) _TIM_OC_INIT(n)
52 
53 #define _TIM_OC_PRELOADCONFIG(n) TIM_OC##n##PreloadConfig
54 #define TIM_OC_PRELOADCONFIG(n) _TIM_OC_PRELOADCONFIG(n)
55 
56 #define _TIM_SETCOMPARE(n) TIM_SetCompare##n
57 #define TIM_SETCOMPARE(n) _TIM_SETCOMPARE(n)
58 
60 
61  /* TIM3 and TIM4 clock enable */
62  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
63  RCC_APB1PeriphClockCmd(PWM_5AND6_RCC, ENABLE);
64 #if USE_SERVOS_7AND8
65  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
66 #endif
67 
68  /* GPIO A,B and C clock enable */
69  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
70  RCC_APB2Periph_AFIO, ENABLE);
71  /* GPIO C */
72  /* PC6=servo1 PC7=servo2 PC8=servo3 PC9=servo4 */
73  GPIO_InitTypeDef GPIO_InitStructure;
74  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
75  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
76  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
77  GPIO_Init(GPIOC, &GPIO_InitStructure);
78  /* need to remate alternate function, pins 37, 38, 39, 40 on LQFP64 */
79  GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
80 
81  /* PWM 5/6 GPIO */
82  /* PB8=servo5 PB9=servo6 */
83  GPIO_InitStructure.GPIO_Pin = PWM5_Pin | PWM6_Pin;
84  GPIO_Init(PWM_5AND6_GPIO, &GPIO_InitStructure);
85 
86 #if USE_SERVOS_7AND8
87  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
88  GPIO_Init(GPIOB, &GPIO_InitStructure);
89 #endif
90 
91  /* Time base configuration */
92  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
93  TIM_TimeBaseStructure.TIM_Prescaler = (PCLK / ONE_MHZ_CLK) - 1; // 1uS
94  TIM_TimeBaseStructure.TIM_Period = (ONE_MHZ_CLK / SERVO_HZ) - 1;
95  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
96  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
97 
98  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
99 
100 #ifdef SERVO_HZ_SECONDARY
101  TIM_TimeBaseStructure.TIM_Period = (ONE_MHZ_CLK / SERVO_HZ_SECONDARY) - 1;
102 #endif
103  TIM_TimeBaseInit(PWM_5AND6_TIMER, &TIM_TimeBaseStructure);
104 #if USE_SERVOS_7AND8
105  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
106 #endif
107 
108  /* PWM1 Mode configuration: All Channels */
109  TIM_OCInitTypeDef TIM_OCInitStructure;
110  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
111  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
112  TIM_OCInitStructure.TIM_Pulse = 0; // default low (no pulse)
113  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
114 
115  /* PWM1 Mode configuration: TIM3 Channel1 */
116  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
117  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
118 
119  /* PWM2 Mode configuration: TIM3 Channel2 */
120  TIM_OC2Init(TIM3, &TIM_OCInitStructure);
121  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
122 
123  /* PWM3 Mode configuration: TIM3 Channel3 */
124  TIM_OC3Init(TIM3, &TIM_OCInitStructure);
125  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
126 
127  /* PWM4 Mode configuration: TIM3 Channel4 */
128  TIM_OC4Init(TIM3, &TIM_OCInitStructure);
129  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
130 
131  /* PWM5 Mode configuration: TIM4 Channel3 */
132  TIM_OC_INIT(PWM5_OC) (PWM_5AND6_TIMER, &TIM_OCInitStructure);
133  TIM_OC_PRELOADCONFIG(PWM5_OC)(PWM_5AND6_TIMER, TIM_OCPreload_Enable);
134 
135  /* PWM6 Mode configuration: TIM4 Channel4 */
136  TIM_OC_INIT(PWM6_OC)(PWM_5AND6_TIMER, &TIM_OCInitStructure);
137  TIM_OC_PRELOADCONFIG(PWM6_OC)(PWM_5AND6_TIMER, TIM_OCPreload_Enable);
138 
139 #if USE_SERVOS_7AND8
140  /* PWM7 Mode configuration: TIM4 Channel3 */
141  TIM_OC1Init(TIM4, &TIM_OCInitStructure);
142  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
143 
144  /* PWM8 Mode configuration: TIM4 Channel4 */
145  TIM_OC2Init(TIM4, &TIM_OCInitStructure);
146  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
147 
148  /* TIM4 enable */
149  TIM_ARRPreloadConfig(TIM4, ENABLE);
150  TIM_CtrlPWMOutputs(TIM4, ENABLE);
151  TIM_Cmd(TIM4, ENABLE);
152 #endif
153 
154  /* PWM1-4 enable */
155  TIM_ARRPreloadConfig(TIM3, ENABLE);
156  TIM_CtrlPWMOutputs(TIM3, ENABLE);
157  TIM_Cmd(TIM3, ENABLE);
158 
159  /* PWM5/6 enable */
160  TIM_ARRPreloadConfig(PWM_5AND6_TIMER, ENABLE);
161  TIM_CtrlPWMOutputs(PWM_5AND6_TIMER, ENABLE);
162  TIM_Cmd(PWM_5AND6_TIMER, ENABLE);
163 
164 }
165 
166 /* set pulse widths from actuator values, assumed to be in us */
168  TIM_SetCompare1(TIM3, actuators_pwm_values[0]);
169  TIM_SetCompare2(TIM3, actuators_pwm_values[1]);
170  TIM_SetCompare3(TIM3, actuators_pwm_values[2]);
171  TIM_SetCompare4(TIM3, actuators_pwm_values[3]);
172 
175 
176 #if USE_SERVOS_7AND8
177  TIM_SetCompare1(TIM4, actuators_pwm_values[6]);
178  TIM_SetCompare2(TIM4, actuators_pwm_values[7]);
179 #endif
180 }
#define TIM_SETCOMPARE(n)
int32_t actuators_pwm_values[ACTUATORS_PWM_NB]
#define PWM6_OC
#define PWM5_OC
#define PWM5_Pin
#define PWM_5AND6_GPIO
#define SERVO_HZ
#define ONE_MHZ_CLK
#define PCLK
#define PWM6_Pin
#define PWM_5AND6_RCC
void actuators_pwm_arch_init(void)
#define TIM_OC_PRELOADCONFIG(n)
#define TIM_OC_INIT(n)
#define PWM_5AND6_TIMER
void actuators_pwm_commit(void)