Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
actuators_shared_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 // for timer_get_frequency
29 #include "mcu_arch.h"
30 
31 
35  enum tim_oc_id oc_id) {
36 
37  timer_disable_oc_output(timer_peripheral, oc_id);
38  //There is no such register in TIM9 and 12.
39  if (timer_peripheral != TIM9 && timer_peripheral != TIM12)
40  timer_disable_oc_clear(timer_peripheral, oc_id);
41  timer_enable_oc_preload(timer_peripheral, oc_id);
42  timer_set_oc_slow_mode(timer_peripheral, oc_id);
43  timer_set_oc_mode(timer_peripheral, oc_id, TIM_OCM_PWM1);
44  timer_set_oc_polarity_high(timer_peripheral, oc_id);
45  timer_enable_oc_output(timer_peripheral, oc_id);
46  // Used for TIM1 and TIM8, the function does nothing if other timer is specified.
47  timer_enable_break_main_output(timer_peripheral);
48 }
49 
50 
56 void set_servo_timer(uint32_t timer, uint32_t period, uint8_t channels_mask) {
57  // WARNING, this reset is only implemented for TIM1-8 in libopencm3!!
58  timer_reset(timer);
59 
60  /* Timer global mode:
61  * - No divider.
62  * - Alignement edge.
63  * - Direction up.
64  */
65  if ((timer == TIM9) || (timer == TIM12))
66  //There are no EDGE and DIR settings in TIM9 and TIM12
67  timer_set_mode(timer, TIM_CR1_CKD_CK_INT, 0, 0);
68  else
69  timer_set_mode(timer, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
70 
71 
72  // By default the PWM_BASE_FREQ is set to 1MHz thus the timer tick period is 1uS
73  uint32_t timer_clk = timer_get_frequency(timer);
74  timer_set_prescaler(timer, (timer_clk / PWM_BASE_FREQ) -1);
75 
76  timer_disable_preload(timer);
77 
78  timer_continuous_mode(timer);
79 
80  timer_set_period(timer, (PWM_BASE_FREQ / period) - 1);
81 
82  /* Disable outputs and configure channel if needed. */
83  if (bit_is_set(channels_mask, 0)) {
84  actuators_pwm_arch_channel_init(timer, TIM_OC1);
85  }
86  if (bit_is_set(channels_mask, 1)) {
87  actuators_pwm_arch_channel_init(timer, TIM_OC2);
88  }
89  if (bit_is_set(channels_mask, 2)) {
90  actuators_pwm_arch_channel_init(timer, TIM_OC3);
91  }
92  if (bit_is_set(channels_mask, 3)) {
93  actuators_pwm_arch_channel_init(timer, TIM_OC4);
94  }
95 
96  /*
97  * Set initial output compare values.
98  * Note: Maybe we should preload the compare registers with some sensible
99  * values before we enable the timer?
100  */
101  //timer_set_oc_value(timer, TIM_OC1, 1000);
102  //timer_set_oc_value(timer, TIM_OC2, 1000);
103  //timer_set_oc_value(timer, TIM_OC3, 1000);
104  //timer_set_oc_value(timer, TIM_OC4, 1000);
105 
106  /* -- Enable timer -- */
107  /*
108  * ARR reload enable.
109  * Note: In our case it does not matter much if we do preload or not. As it
110  * is unlikely we will want to change the frequency of the timer during
111  * runtime anyways.
112  */
113  timer_enable_preload(timer);
114 
115  /* Counter enable. */
116  timer_enable_counter(timer);
117 
118 }
119 
uint32_t timer_get_frequency(uint32_t timer_peripheral)
Get Timer clock frequency (before prescaling) Only valid if using the internal clock for the timer...
Definition: mcu_arch.c:139
void actuators_pwm_arch_channel_init(uint32_t timer_peripheral, enum tim_oc_id oc_id)
Set PWM channel configuration.
void set_servo_timer(uint32_t timer, uint32_t period, uint8_t channels_mask)
Set Timer configuration.
#define PWM_BASE_FREQ
unsigned long uint32_t
Definition: types.h:18
STM32 PWM and dualPWM servos shared functions.
unsigned char uint8_t
Definition: types.h:14