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