Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
actuators_dualpwm_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 
26 //VALID TIMERS IS TIM5 ON THE LISA/M
27 
29 #include "modules/actuators/actuators_dualpwm_arch.h"
31 
32 #include <libopencm3/stm32/gpio.h>
33 #include <libopencm3/stm32/rcc.h>
34 #include <libopencm3/stm32/timer.h>
35 #include <libopencm3/cm3/nvic.h>
36 
37 #include "mcu_periph/gpio_arch.h"
38 
40 
41 
43 
47 {
48 
49  /*-----------------------------------
50  * Configure timer peripheral clocks
51  *-----------------------------------*/
52 #if PWM_USE_TIM1
53  rcc_periph_clock_enable(RCC_TIM1);
54 #endif
55 #if PWM_USE_TIM2
56  rcc_periph_clock_enable(RCC_TIM2);
57 #endif
58 #if PWM_USE_TIM3
59  rcc_periph_clock_enable(RCC_TIM3);
60 #endif
61 #if PWM_USE_TIM4
62  rcc_periph_clock_enable(RCC_TIM4);
63 #endif
64 #if PWM_USE_TIM5
65  rcc_periph_clock_enable(RCC_TIM5);
66 #endif
67 #if PWM_USE_TIM8
68  rcc_periph_clock_enable(RCC_TIM8);
69 #endif
70 #if PWM_USE_TIM9
71  rcc_periph_clock_enable(RCC_TIM9);
72 #endif
73 #if PWM_USE_TIM12
74  rcc_periph_clock_enable(RCC_TIM12);
75 #endif
76 
77  /*----------------
78  * Configure GPIO
79  *----------------*/
80 #ifdef DUAL_PWM_SERVO_5
81  gpio_setup_pin_af(DUAL_PWM_SERVO_5_GPIO, DUAL_PWM_SERVO_5_PIN, DUAL_PWM_SERVO_5_AF, TRUE);
82 #endif
83 #ifdef DUAL_PWM_SERVO_6
84  gpio_setup_pin_af(DUAL_PWM_SERVO_6_GPIO, DUAL_PWM_SERVO_6_PIN, DUAL_PWM_SERVO_6_AF, TRUE);
85 #endif
86 
87 #if DUAL_PWM_USE_TIM5
88  rcc_periph_reset_pulse(RST_TIM5);
90 
91  nvic_set_priority(NVIC_TIM5_IRQ, 2);
92  nvic_enable_irq(NVIC_TIM5_IRQ);
93  timer_enable_irq(TIM5, TIM_DIER_CC1IE);
94 #endif
95 
96  //calculation the values to put into the timer registers to generate pulses every 4ms and 16ms.
97  ratio_4ms = (ONE_MHZ_CLK / 250) - 1;
98  ratio_16ms = (ONE_MHZ_CLK / 62.5) - 1;
99 
100 }
101 
102 
105 #if DUAL_PWM_USE_TIM5
106 void tim5_isr(void)
107 {
108 
109  dual_pwm_isr();
110 }
111 #endif
112 
113 
114 
115 
119 {
120 
121 #if DUAL_PWM_USE_TIM5
122  timer_clear_flag(TIM5, TIM_SR_CC1IF);
123 #endif
124 }
125 
126 
128 {
129 
130 #if DUAL_PWM_USE_TIM5
131  timer_set_period(TIM5, period);
132 #endif
133 }
134 
135 
136 void set_dual_pwm_timer_s_oc(uint32_t oc_value, uint32_t oc_value2)
137 {
138 
139 #if DUAL_PWM_USE_TIM5
140  timer_set_oc_value(DUAL_PWM_SERVO_5_TIMER, DUAL_PWM_SERVO_5_OC, oc_value);
141  timer_set_oc_value(DUAL_PWM_SERVO_6_TIMER, DUAL_PWM_SERVO_6_OC, oc_value2);
142 #endif
143 }
144 
145 
146 
147 
148 void dual_pwm_isr(void)
149 {
150 
151  static int num_pulse = 0; //status of the timer. Are we controling the first or the second servo
152 
154 
155  if (num_pulse == 1) {
156 
158  set_dual_pwm_timer_s_oc(actuators_dualpwm_values[DUAL_PWM_SERVO_5_P1],actuators_dualpwm_values[DUAL_PWM_SERVO_5_P2]);
159 
160  num_pulse = 0;
161  } else {
162 
164  set_dual_pwm_timer_s_oc(actuators_dualpwm_values[DUAL_PWM_SERVO_6_P1],actuators_dualpwm_values[DUAL_PWM_SERVO_6_P2]);
165 
166  num_pulse = 1;
167  }
168 }
169 
170 
174 {
175 
176  //we don't need to commit the values into this function as far as it's done in the interuption
177  //(wich is called every 4ms and 16ms alternatively (twice every 20ms))
178 
179 }
void set_servo_timer(uint32_t timer, uint32_t freq, uint8_t channels_mask)
Set Timer configuration.
STM32 PWM and dualPWM servos shared functions.
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
#define ONE_MHZ_CLK
#define TIM5_SERVO_HZ
#define PWM_TIM5_CHAN_MASK
Definition: elle0_common.h:430
void actuators_dualpwm_arch_init(void)
Arch dependent init file.
#define TRUE
Definition: std.h:4
uint32_t ratio_4ms
uint32_t ratio_16ms
void set_dual_pwm_timer_s_oc(uint32_t oc_value, uint32_t oc_value2)
void set_dual_pwm_timer_s_period(uint32_t period)
void actuators_dualpwm_commit(void)
Set pulse widths from actuator values, assumed to be in us.
void dual_pwm_isr(void)
void clear_timer_flag(void)
Interuption called at the end of the timer.
uint32_t actuators_dualpwm_values[ACTUATORS_DUALPWM_NB]
#define ACTUATORS_DUALPWM_NB
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78