Paparazzi UAS  v5.18.0_stable
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 "subsystems/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
89 
90  nvic_set_priority(NVIC_TIM5_IRQ, 2);
91  nvic_enable_irq(NVIC_TIM5_IRQ);
92  timer_enable_irq(TIM5, TIM_DIER_CC1IE);
93 #endif
94 
95  //calculation the values to put into the timer registers to generate pulses every 4ms and 16ms.
96  ratio_4ms = (ONE_MHZ_CLK / 250) - 1;
97  ratio_16ms = (ONE_MHZ_CLK / 62.5) - 1;
98 
99 }
100 
101 
104 #if DUAL_PWM_USE_TIM5
105 void tim5_isr(void)
106 {
107 
108  dual_pwm_isr();
109 }
110 #endif
111 
112 
113 
114 
118 {
119 
120 #if DUAL_PWM_USE_TIM5
121  timer_clear_flag(TIM5, TIM_SR_CC1IF);
122 #endif
123 }
124 
125 
127 {
128 
129 #if DUAL_PWM_USE_TIM5
130  timer_set_period(TIM5, period);
131 #endif
132 }
133 
134 
135 void set_dual_pwm_timer_s_oc(uint32_t oc_value, uint32_t oc_value2)
136 {
137 
138 #if DUAL_PWM_USE_TIM5
139  timer_set_oc_value(DUAL_PWM_SERVO_5_TIMER, DUAL_PWM_SERVO_5_OC, oc_value);
140  timer_set_oc_value(DUAL_PWM_SERVO_6_TIMER, DUAL_PWM_SERVO_6_OC, oc_value2);
141 #endif
142 }
143 
144 
145 
146 
147 void dual_pwm_isr(void)
148 {
149 
150  static int num_pulse = 0; //status of the timer. Are we controling the first or the second servo
151 
153 
154  if (num_pulse == 1) {
155 
157  set_dual_pwm_timer_s_oc(actuators_dualpwm_values[DUAL_PWM_SERVO_5_P1],actuators_dualpwm_values[DUAL_PWM_SERVO_5_P2]);
158 
159  num_pulse = 0;
160  } else {
161 
163  set_dual_pwm_timer_s_oc(actuators_dualpwm_values[DUAL_PWM_SERVO_6_P1],actuators_dualpwm_values[DUAL_PWM_SERVO_6_P2]);
164 
165  num_pulse = 1;
166  }
167 }
168 
169 
173 {
174 
175  //we don't need to commit the values into this function as far as it's done in the interuption
176  //(wich is called every 4ms and 16ms alternatively (twice every 20ms))
177 
178 }
ratio_4ms
uint32_t ratio_4ms
Definition: actuators_dualpwm_arch.c:39
TIM5_SERVO_HZ
#define TIM5_SERVO_HZ
Definition: actuators_pwm_arch.h:69
actuators_dualpwm_arch_init
void actuators_dualpwm_arch_init(void)
Arch dependent init file.
Definition: actuators_dualpwm_arch.c:28
actuators_shared_arch.h
uint32_t
unsigned long uint32_t
Definition: types.h:18
ONE_MHZ_CLK
#define ONE_MHZ_CLK
Definition: pwm_input_arch.c:34
set_servo_timer
void set_servo_timer(uint32_t timer, uint32_t freq, uint8_t channels_mask)
Set Timer configuration.
Definition: actuators_shared_arch.c:59
clear_timer_flag
void clear_timer_flag(void)
Interuption called at the end of the timer.
Definition: actuators_dualpwm_arch.c:117
actuators_dualpwm_values
uint32_t actuators_dualpwm_values[ACTUATORS_DUALPWM_NB]
Definition: actuators_dualpwm_arch.c:42
set_dual_pwm_timer_s_period
void set_dual_pwm_timer_s_period(uint32_t period)
Definition: actuators_dualpwm_arch.c:126
ratio_16ms
uint32_t ratio_16ms
Definition: actuators_dualpwm_arch.c:39
gpio_setup_pin_af
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:61
set_dual_pwm_timer_s_oc
void set_dual_pwm_timer_s_oc(uint32_t oc_value, uint32_t oc_value2)
Definition: actuators_dualpwm_arch.c:135
actuators_dualpwm_commit
void actuators_dualpwm_commit(void)
Set pulse widths from actuator values, assumed to be in us.
Definition: actuators_dualpwm_arch.c:172
dual_pwm_isr
void dual_pwm_isr(void)
Definition: actuators_dualpwm_arch.c:147
ACTUATORS_DUALPWM_NB
#define ACTUATORS_DUALPWM_NB
Definition: actuators_dualpwm_arch.h:35
PWM_TIM5_CHAN_MASK
#define PWM_TIM5_CHAN_MASK
Definition: elle0_common.h:430
TRUE
#define TRUE
Definition: std.h:4
actuators_dualpwm.h