Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
actuators_pwm_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 AggieAir, A Remote Sensing Unmanned Aerial System for Scientific Applications
3  * Utah State University, http://aggieair.usu.edu/
4  *
5  * Michal Podhradsky (michal.podhradsky@aggiemail.usu.edu)
6  * Calvin Coopmans (c.r.coopmans@ieee.org)
7  *
8  *
9  * This file is part of paparazzi.
10  *
11  * paparazzi is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2, or (at your option)
14  * any later version.
15  *
16  * paparazzi is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with paparazzi; see the file COPYING. If not, write to
23  * the Free Software Foundation, 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  */
33 #include "modules/actuators/actuators_pwm_arch.h"
35 #include "mcu_periph/gpio.h"
36 
37 /* Default timer base frequency is 1MHz */
38 #ifndef PWM_FREQUENCY
39 #define PWM_FREQUENCY 1000000
40 #endif
41 
42 /* Default servo update rate in Hz */
43 #ifndef SERVO_HZ
44 #define SERVO_HZ 40
45 #endif
46 
47 /* For each timer the period van be configured differently */
48 #ifndef TIM1_SERVO_HZ
49 #define TIM1_SERVO_HZ SERVO_HZ
50 #endif
51 #ifndef TIM2_SERVO_HZ
52 #define TIM2_SERVO_HZ SERVO_HZ
53 #endif
54 #ifndef TIM3_SERVO_HZ
55 #define TIM3_SERVO_HZ SERVO_HZ
56 #endif
57 #ifndef TIM4_SERVO_HZ
58 #define TIM4_SERVO_HZ SERVO_HZ
59 #endif
60 #ifndef TIM5_SERVO_HZ
61 #define TIM5_SERVO_HZ SERVO_HZ
62 #endif
63 #ifndef TIM8_SERVO_HZ
64 #define TIM8_SERVO_HZ SERVO_HZ
65 #endif
66 #ifndef TIM9_SERVO_HZ
67 #define TIM9_SERVO_HZ SERVO_HZ
68 #endif
69 #ifndef TIM12_SERVO_HZ
70 #define TIM12_SERVO_HZ SERVO_HZ
71 #endif
72 
79 
80 
88 #ifndef PWM_CMD_TO_US
89 #define PWM_CMD_TO_US(_t) (PWM_FREQUENCY * _t / 1000000)
90 #endif
91 
93 
103  __attribute__((unused)) static void pwmpcb(PWMDriver *pwmp __attribute__((unused))) {}
104 
105 #if USE_PWM_TIM1
106 static PWMConfig pwmcfg1 = {
107  .frequency = PWM_FREQUENCY,
108  .period = PWM_FREQUENCY/TIM1_SERVO_HZ,
109  .callback = NULL,
110  .channels = {
111  { PWM_OUTPUT_DISABLED, NULL },
112  { PWM_OUTPUT_DISABLED, NULL },
113  { PWM_OUTPUT_DISABLED, NULL },
114  { PWM_OUTPUT_DISABLED, NULL },
115  },
116  .cr2 = 0,
117  .bdtr = 0,
118  .dier = 0
119 };
120 #endif
121 #if USE_PWM_TIM2
122 static PWMConfig pwmcfg2 = {
123  .frequency = PWM_FREQUENCY,
124  .period = PWM_FREQUENCY/TIM2_SERVO_HZ,
125  .callback = NULL,
126  .channels = {
127  { PWM_OUTPUT_DISABLED, NULL },
128  { PWM_OUTPUT_DISABLED, NULL },
129  { PWM_OUTPUT_DISABLED, NULL },
130  { PWM_OUTPUT_DISABLED, NULL },
131  },
132  .cr2 = 0,
133  .bdtr = 0,
134  .dier = 0
135 };
136 #endif
137 #if USE_PWM_TIM3
138 static PWMConfig pwmcfg3 = {
139  .frequency = PWM_FREQUENCY,
140  .period = PWM_FREQUENCY/TIM3_SERVO_HZ,
141  .callback = NULL,
142  .channels = {
143  { PWM_OUTPUT_DISABLED, NULL },
144  { PWM_OUTPUT_DISABLED, NULL },
145  { PWM_OUTPUT_DISABLED, NULL },
146  { PWM_OUTPUT_DISABLED, NULL },
147  },
148  .cr2 = 0,
149  .bdtr = 0,
150  .dier = 0
151 };
152 #endif
153 #if USE_PWM_TIM4
154 static PWMConfig pwmcfg4 = {
155  .frequency = PWM_FREQUENCY,
156  .period = PWM_FREQUENCY/TIM4_SERVO_HZ,
157  .callback = NULL,
158  .channels = {
159  { PWM_OUTPUT_DISABLED, NULL },
160  { PWM_OUTPUT_DISABLED, NULL },
161  { PWM_OUTPUT_DISABLED, NULL },
162  { PWM_OUTPUT_DISABLED, NULL },
163  },
164  .cr2 = 0,
165  .bdtr = 0,
166  .dier = 0
167 };
168 #endif
169 #if USE_PWM_TIM5
170 static PWMConfig pwmcfg5 = {
171  .frequency = PWM_FREQUENCY,
172  .period = PWM_FREQUENCY/TIM5_SERVO_HZ,
173  .callback = NULL,
174  .channels = {
175  { PWM_OUTPUT_DISABLED, NULL },
176  { PWM_OUTPUT_DISABLED, NULL },
177  { PWM_OUTPUT_DISABLED, NULL },
178  { PWM_OUTPUT_DISABLED, NULL },
179  },
180  .cr2 = 0,
181  .bdtr = 0,
182  .dier = 0
183 };
184 #endif
185 #if USE_PWM_TIM8
186 static PWMConfig pwmcfg8 = {
187  .frequency = PWM_FREQUENCY,
188  .period = PWM_FREQUENCY/TIM8_SERVO_HZ,
189  .callback = NULL,
190  .channels = {
191  { PWM_OUTPUT_DISABLED, NULL },
192  { PWM_OUTPUT_DISABLED, NULL },
193  { PWM_OUTPUT_DISABLED, NULL },
194  { PWM_OUTPUT_DISABLED, NULL },
195  },
196  .cr2 = 0,
197  .bdtr = 0,
198  .dier = 0
199 };
200 #endif
201 #if USE_PWM_TIM9
202 static PWMConfig pwmcfg9 = {
203  .frequency = PWM_FREQUENCY,
204  .period = PWM_FREQUENCY/TIM9_SERVO_HZ,
205  .callback = NULL,
206  .channels = {
207  { PWM_OUTPUT_DISABLED, NULL },
208  { PWM_OUTPUT_DISABLED, NULL },
209  { PWM_OUTPUT_DISABLED, NULL },
210  { PWM_OUTPUT_DISABLED, NULL },
211  },
212  .cr2 = 0,
213  .bdtr = 0,
214  .dier = 0
215 };
216 #endif
217 #if USE_PWM_TIM10
218 static PWMConfig pwmcfg10 = {
219  .frequency = PWM_FREQUENCY,
220  .period = PWM_FREQUENCY/TIM10_SERVO_HZ,
221  .callback = NULL,
222  .channels = {
223  { PWM_OUTPUT_DISABLED, NULL },
224  { PWM_OUTPUT_DISABLED, NULL },
225  { PWM_OUTPUT_DISABLED, NULL },
226  { PWM_OUTPUT_DISABLED, NULL },
227  },
228  .cr2 = 0,
229  .bdtr = 0,
230  .dier = 0
231 };
232 #endif
233 #if USE_PWM_TIM11
234 static PWMConfig pwmcfg11 = {
235  .frequency = PWM_FREQUENCY,
236  .period = PWM_FREQUENCY/TIM11_SERVO_HZ,
237  .callback = NULL,
238  .channels = {
239  { PWM_OUTPUT_DISABLED, NULL },
240  { PWM_OUTPUT_DISABLED, NULL },
241  { PWM_OUTPUT_DISABLED, NULL },
242  { PWM_OUTPUT_DISABLED, NULL },
243  },
244  .cr2 = 0,
245  .bdtr = 0,
246  .dier = 0
247 };
248 #endif
249 #if USE_PWM_TIM12
250 static PWMConfig pwmcfg12 = {
251  .frequency = PWM_FREQUENCY,
252  .period = PWM_FREQUENCY/TIM12_SERVO_HZ,
253  .callback = NULL,
254  .channels = {
255  { PWM_OUTPUT_DISABLED, NULL },
256  { PWM_OUTPUT_DISABLED, NULL },
257  { PWM_OUTPUT_DISABLED, NULL },
258  { PWM_OUTPUT_DISABLED, NULL },
259  },
260  .cr2 = 0,
261  .bdtr = 0,
262  .dier = 0
263 };
264 #endif
265 
266 
268 {
269  /*----------------
270  * Configure GPIO
271  *----------------*/
272 #ifdef PWM_SERVO_0
274  PWM_SERVO_0_CONF.channels[PWM_SERVO_0_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
275 #endif
276 #ifdef PWM_SERVO_1
278  PWM_SERVO_1_CONF.channels[PWM_SERVO_1_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
279 #endif
280 #ifdef PWM_SERVO_2
282  PWM_SERVO_2_CONF.channels[PWM_SERVO_2_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
283 #endif
284 #ifdef PWM_SERVO_3
286  PWM_SERVO_3_CONF.channels[PWM_SERVO_3_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
287 #endif
288 #ifdef PWM_SERVO_4
290  PWM_SERVO_4_CONF.channels[PWM_SERVO_4_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
291 #endif
292 #ifdef PWM_SERVO_5
294  PWM_SERVO_5_CONF.channels[PWM_SERVO_5_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
295 #endif
296 #ifdef PWM_SERVO_6
298  PWM_SERVO_6_CONF.channels[PWM_SERVO_6_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
299 #endif
300 #ifdef PWM_SERVO_7
302  PWM_SERVO_7_CONF.channels[PWM_SERVO_7_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
303 #endif
304 #ifdef PWM_SERVO_8
306  PWM_SERVO_8_CONF.channels[PWM_SERVO_8_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
307 #endif
308 #ifdef PWM_SERVO_9
310  PWM_SERVO_9_CONF.channels[PWM_SERVO_9_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
311 #endif
312 #ifdef PWM_SERVO_10
314  PWM_SERVO_10_CONF.channels[PWM_SERVO_10_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
315 #endif
316 #ifdef PWM_SERVO_11
317  gpio_setup_pin_af(PWM_SERVO_11_GPIO, PWM_SERVO_11_PIN, PWM_SERVO_11_AF, true);
318  PWM_SERVO_11_CONF.channels[PWM_SERVO_11_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
319 #endif
320 #ifdef PWM_SERVO_12
321  gpio_setup_pin_af(PWM_SERVO_12_GPIO, PWM_SERVO_12_PIN, PWM_SERVO_12_AF, true);
322  PWM_SERVO_12_CONF.channels[PWM_SERVO_12_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
323 #endif
324 #ifdef PWM_SERVO_13
325  gpio_setup_pin_af(PWM_SERVO_13_GPIO, PWM_SERVO_13_PIN, PWM_SERVO_13_AF, true);
326  PWM_SERVO_13_CONF.channels[PWM_SERVO_13_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
327 #endif
328 #ifdef PWM_SERVO_14
329  gpio_setup_pin_af(PWM_SERVO_14_GPIO, PWM_SERVO_14_PIN, PWM_SERVO_14_AF, true);
330  PWM_SERVO_14_CONF.channels[PWM_SERVO_14_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
331 #endif
332 #ifdef PWM_SERVO_15
333  gpio_setup_pin_af(PWM_SERVO_15_GPIO, PWM_SERVO_15_PIN, PWM_SERVO_15_AF, true);
334  PWM_SERVO_15_CONF.channels[PWM_SERVO_15_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
335 #endif
336 #ifdef PWM_SERVO_16
337  gpio_setup_pin_af(PWM_SERVO_16_GPIO, PWM_SERVO_16_PIN, PWM_SERVO_16_AF, true);
338  PWM_SERVO_16_CONF.channels[PWM_SERVO_16_CHANNEL].mode = PWM_OUTPUT_ACTIVE_HIGH;
339 #endif
340 
341  /*---------------
342  * Configure PWM
343  *---------------*/
344 #if USE_PWM_TIM1
345  pwmStart(&PWMD1, &pwmcfg1);
346 #endif
347 #if USE_PWM_TIM2
348  pwmStart(&PWMD2, &pwmcfg2);
349 #endif
350 #if USE_PWM_TIM3
351  pwmStart(&PWMD3, &pwmcfg3);
352 #endif
353 #if USE_PWM_TIM4
354  pwmStart(&PWMD4, &pwmcfg4);
355 #endif
356 #if USE_PWM_TIM5
357  pwmStart(&PWMD5, &pwmcfg5);
358 #endif
359 #if USE_PWM_TIM8
360  pwmStart(&PWMD8, &pwmcfg8);
361 #endif
362 #if USE_PWM_TIM9
363  pwmStart(&PWMD9, &pwmcfg9);
364 #endif
365 #if USE_PWM_TIM10
366  pwmStart(&PWMD10, &pwmcfg10);
367 #endif
368 #if USE_PWM_TIM11
369  pwmStart(&PWMD11, &pwmcfg11);
370 #endif
371 #if USE_PWM_TIM12
372  pwmStart(&PWMD12, &pwmcfg12);
373 #endif
374 }
375 
376 
378 {
379 #ifdef PWM_SERVO_0
381 #endif
382 #ifdef PWM_SERVO_1
384 #endif
385 #ifdef PWM_SERVO_2
387 #endif
388 #ifdef PWM_SERVO_3
390 #endif
391 #ifdef PWM_SERVO_4
393 #endif
394 #ifdef PWM_SERVO_5
396 #endif
397 #ifdef PWM_SERVO_6
399 #endif
400 #ifdef PWM_SERVO_7
402 #endif
403 #ifdef PWM_SERVO_8
405 #endif
406 #ifdef PWM_SERVO_9
408 #endif
409 #ifdef PWM_SERVO_10
411 #endif
412 #ifdef PWM_SERVO_11
413  pwmEnableChannel(&PWM_SERVO_11_DRIVER, PWM_SERVO_11_CHANNEL, PWM_CMD_TO_US(actuators_pwm_values[PWM_SERVO_11]));
414 #endif
415 #ifdef PWM_SERVO_12
416  pwmEnableChannel(&PWM_SERVO_12_DRIVER, PWM_SERVO_12_CHANNEL, PWM_CMD_TO_US(actuators_pwm_values[PWM_SERVO_12]));
417 #endif
418 #ifdef PWM_SERVO_13
419  pwmEnableChannel(&PWM_SERVO_13_DRIVER, PWM_SERVO_13_CHANNEL, PWM_CMD_TO_US(actuators_pwm_values[PWM_SERVO_13]));
420 #endif
421 #ifdef PWM_SERVO_14
422  pwmEnableChannel(&PWM_SERVO_14_DRIVER, PWM_SERVO_14_CHANNEL, PWM_CMD_TO_US(actuators_pwm_values[PWM_SERVO_14]));
423 #endif
424 #ifdef PWM_SERVO_15
425  pwmEnableChannel(&PWM_SERVO_15_DRIVER, PWM_SERVO_15_CHANNEL, PWM_CMD_TO_US(actuators_pwm_values[PWM_SERVO_15]));
426 #endif
427 #ifdef PWM_SERVO_16
428  pwmEnableChannel(&PWM_SERVO_16_DRIVER, PWM_SERVO_16_CHANNEL, PWM_CMD_TO_US(actuators_pwm_values[PWM_SERVO_16]));
429 #endif
430 }
#define PWM_SERVO_5_GPIO
Definition: board.h:287
#define PWM_SERVO_2_AF
Definition: board.h:250
#define PWM_SERVO_0
Definition: board.h:221
#define PWM_SERVO_3_AF
Definition: board.h:263
#define PWM_SERVO_4_GPIO
Definition: board.h:274
#define PWM_SERVO_3_PIN
Definition: board.h:262
#define PWM_SERVO_1
Definition: board.h:234
#define PWM_SERVO_5_AF
Definition: board.h:289
#define PWM_SERVO_1_CHANNEL
Definition: board.h:239
#define PWM_SERVO_0_PIN
Definition: board.h:223
#define PWM_SERVO_3
Definition: board.h:260
#define PWM_SERVO_3_GPIO
Definition: board.h:261
#define PWM_SERVO_1_CONF
Definition: board.h:240
#define PWM_SERVO_5_CHANNEL
Definition: board.h:291
#define PWM_SERVO_5_DRIVER
Definition: board.h:290
#define PWM_SERVO_0_AF
Definition: board.h:224
#define PWM_SERVO_1_AF
Definition: board.h:237
#define PWM_SERVO_4_PIN
Definition: board.h:275
#define PWM_SERVO_4_AF
Definition: board.h:276
#define PWM_SERVO_4_CONF
Definition: board.h:279
#define PWM_SERVO_5_PIN
Definition: board.h:288
#define PWM_SERVO_1_GPIO
Definition: board.h:235
#define PWM_SERVO_4
Definition: board.h:273
#define PWM_SERVO_2_PIN
Definition: board.h:249
#define PWM_SERVO_0_CONF
Definition: board.h:227
#define PWM_SERVO_1_PIN
Definition: board.h:236
#define PWM_SERVO_3_CONF
Definition: board.h:266
#define PWM_SERVO_2
Definition: board.h:247
#define PWM_SERVO_3_CHANNEL
Definition: board.h:265
#define PWM_SERVO_0_GPIO
Definition: board.h:222
#define PWM_SERVO_0_CHANNEL
Definition: board.h:226
#define PWM_SERVO_3_DRIVER
Definition: board.h:264
#define PWM_SERVO_4_CHANNEL
Definition: board.h:278
#define PWM_SERVO_2_CONF
Definition: board.h:253
#define PWM_SERVO_2_GPIO
Definition: board.h:248
#define PWM_SERVO_5
Definition: board.h:286
#define PWM_SERVO_2_DRIVER
Definition: board.h:251
#define PWM_SERVO_1_DRIVER
Definition: board.h:238
#define PWM_SERVO_2_CHANNEL
Definition: board.h:252
#define PWM_SERVO_5_CONF
Definition: board.h:292
#define PWM_SERVO_4_DRIVER
Definition: board.h:277
#define PWM_SERVO_0_DRIVER
Definition: board.h:225
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af, bool is_output)
Setup a gpio for input or output with alternate function.
Definition: gpio_arch.c:65
int32_t actuators_pwm_values[ACTUATORS_PWM_NB]
#define TIM3_SERVO_HZ
#define TIM5_SERVO_HZ
#define PWM_CMD_TO_US(_t)
Print the configuration variables from the header.
static void pwmpcb(PWMDriver *pwmp)
PWM callback function.
#define TIM4_SERVO_HZ
#define SERVO_HZ
#define TIM8_SERVO_HZ
#define TIM9_SERVO_HZ
#define TIM12_SERVO_HZ
#define PWM_FREQUENCY
#define TIM1_SERVO_HZ
#define TIM2_SERVO_HZ
void actuators_pwm_arch_init(void)
Arch dependent init file.
void actuators_pwm_commit(void)
#define ACTUATORS_PWM_NB
#define PWM_SERVO_6
Definition: elle0_common.h:392
#define PWM_SERVO_6_AF
Definition: elle0_common.h:396
#define PWM_SERVO_6_GPIO
Definition: elle0_common.h:394
#define PWM_SERVO_6_PIN
Definition: elle0_common.h:395
Some architecture independent helper functions for GPIOs.
#define PWM_SERVO_6_DRIVER
#define PWM_SERVO_6_CHANNEL
#define PWM_SERVO_6_CONF
#define PWM_SERVO_8_GPIO
Definition: krooz_sd.h:318
#define PWM_SERVO_9_PIN
Definition: krooz_sd.h:331
#define PWM_SERVO_7_PIN
Definition: krooz_sd.h:307
#define PWM_SERVO_7_AF
Definition: krooz_sd.h:308
#define PWM_SERVO_9_GPIO
Definition: krooz_sd.h:330
#define PWM_SERVO_9_AF
Definition: krooz_sd.h:332
#define PWM_SERVO_7
Definition: krooz_sd.h:304
#define PWM_SERVO_8
Definition: krooz_sd.h:316
#define PWM_SERVO_7_GPIO
Definition: krooz_sd.h:306
#define PWM_SERVO_9
Definition: krooz_sd.h:328
#define PWM_SERVO_8_AF
Definition: krooz_sd.h:320
#define PWM_SERVO_8_PIN
Definition: krooz_sd.h:319
#define PWM_SERVO_10_GPIO
#define PWM_SERVO_9_CONF
#define PWM_SERVO_7_CHANNEL
#define PWM_SERVO_10_DRIVER
#define PWM_SERVO_10_CONF
#define PWM_SERVO_7_CONF
#define PWM_SERVO_8_CHANNEL
#define PWM_SERVO_7_DRIVER
#define PWM_SERVO_8_CONF
#define PWM_SERVO_10_PIN
#define PWM_SERVO_10
#define PWM_SERVO_8_DRIVER
#define PWM_SERVO_10_AF
#define PWM_SERVO_9_DRIVER
#define PWM_SERVO_10_CHANNEL
#define PWM_SERVO_9_CHANNEL
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83