Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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  /* Timer global mode:
62  * - No divider.
63  * - Alignement edge.
64  * - Direction up.
65  */
66  if ((timer == TIM9) || (timer == TIM12))
67  //There are no EDGE and DIR settings in TIM9 and TIM12
68  {
69  timer_set_mode(timer, TIM_CR1_CKD_CK_INT, 0, 0);
70  } else {
71  timer_set_mode(timer, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
72  }
73 
74 
75  // By default the PWM_BASE_FREQ is set to 1MHz thus the timer tick period is 1uS
76  uint32_t timer_clk = timer_get_frequency(timer);
77  timer_set_prescaler(timer, (timer_clk / PWM_BASE_FREQ) - 1);
78 
79  timer_disable_preload(timer);
80 
81  timer_continuous_mode(timer);
82 
83  timer_set_period(timer, (PWM_BASE_FREQ / freq) - 1);
84 
85  /* Disable outputs and configure channel if needed. */
86  if (bit_is_set(channels_mask, 0)) {
87  actuators_pwm_arch_channel_init(timer, TIM_OC1);
88  }
89  if (bit_is_set(channels_mask, 1)) {
90  actuators_pwm_arch_channel_init(timer, TIM_OC2);
91  }
92  if (bit_is_set(channels_mask, 2)) {
93  actuators_pwm_arch_channel_init(timer, TIM_OC3);
94  }
95  if (bit_is_set(channels_mask, 3)) {
96  actuators_pwm_arch_channel_init(timer, TIM_OC4);
97  }
98 
99  /*
100  * Set initial output compare values.
101  * Note: Maybe we should preload the compare registers with some sensible
102  * values before we enable the timer?
103  */
104  //timer_set_oc_value(timer, TIM_OC1, 1000);
105  //timer_set_oc_value(timer, TIM_OC2, 1000);
106  //timer_set_oc_value(timer, TIM_OC3, 1000);
107  //timer_set_oc_value(timer, TIM_OC4, 1000);
108 
109  /* -- Enable timer -- */
110  /*
111  * ARR reload enable.
112  * Note: In our case it does not matter much if we do preload or not. As it
113  * is unlikely we will want to change the frequency of the timer during
114  * runtime anyways.
115  */
116  timer_enable_preload(timer);
117 
118  /* Counter enable. */
119  timer_enable_counter(timer);
120 
121 }
122 
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 freq, uint8_t channels_mask)
Set Timer configuration.
STM32 PWM and dualPWM servos shared functions.
#define PWM_BASE_FREQ
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:311
stm32 arch dependant microcontroller initialisation functions.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98