Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pwm_input_arch.c
Go to the documentation of this file.
1 /* $Id$
2  *
3  * Copyright (C) 2011 The Paparazzi Team
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
29 
30 #include "LPC21xx.h"
31 #include "interrupt_hw.h"
32 
33 //UPDATE THESE TO BE MORE ACCESSIBLE AND BE WARY OF EXISTING USAGE
34 //POSSIBLY MAKE MORE INPUTS ACCESSIBLE
35 #ifdef USE_PWM_INPUT1
36 //INPUT CAPTURE CAP0.3 on P0.29
37 #define PWM_INPUT1_PINSEL PINSEL1
38 #define PWM_INPUT1_PINSEL_BIT 26
39 #define PWM_INPUT1_PINSEL_VAL (0x2 << PWM_INPUT1_PINSEL_BIT)
40 #define PWM_INPUT1_PINSEL_MASK (0x3 <<PWM_INPUT1_PINSEL_BIT)
41 #endif
42 #ifdef USE_PWM_INPUT2
43 //INPUT CAPTURE CAP0.0 on P0.30
44 #define PWM_INPUT2_PINSEL PINSEL1
45 #define PWM_INPUT2_PINSEL_BIT 28
46 #define PWM_INPUT2_PINSEL_VAL (0x3 << PWM_INPUT2_PINSEL_BIT)
47 #define PWM_INPUT2_PINSEL_MASK (0x3 <<PWM_INPUT2_PINSEL_BIT)
48 #endif
49 
50 void pwm_input_init ( void )
51 {
52  // initialize the arrays to 0 to avoid junk
53  for (int i=0; i < PWM_INPUT_NB; i++)
54  {
55  pwm_input_duty_tics[i] = 0;
56  pwm_input_duty_valid[i] = 0;
57  pwm_input_period_tics[i] = 0;
59  }
60  /* select pin for capture */
61 #ifdef USE_PWM_INPUT1
62  PWM_INPUT1_PINSEL = (PWM_INPUT1_PINSEL & ~PWM_INPUT1_PINSEL_MASK) | PWM_INPUT1_PINSEL_VAL;
63  //enable capture 0.3 on rising edge + trigger interrupt
65 #endif
66 #ifdef USE_PWM_INPUT2
67  PWM_INPUT2_PINSEL = (PWM_INPUT2_PINSEL & ~PWM_INPUT2_PINSEL_MASK) | PWM_INPUT2_PINSEL_VAL;
68  //enable capture 0.0 on rising edge + trigger interrupt
70 #endif
71 }
72 
73 //FIXME what about clock time overflow???
74 #ifdef USE_PWM_INPUT1
75 void pwm_input_isr1(void)
76 {
77  static uint32_t t_rise;
78  static uint32_t t_fall;
79 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
80  static uint32_t t_oldrise = 0;
81 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
82  static uint32_t t_oldfall = 0;
83 #endif
84 
85  if (T0CCR & TCCR_CR3_F) {
86  t_fall = T0CR3;
87  T0CCR |= TCCR_CR3_R;
88  T0CCR &= ~TCCR_CR3_F;
89 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
90  pwm_input_duty_tics[0] = t_fall - t_rise;
92 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
93  pwm_input_period_tics[0] = t_fall - t_oldfall;
95  t_oldfall = t_fall;
96 #endif //ACTIVE_HIGH
97  } else if (T0CCR & TCCR_CR3_R) {
98  t_rise = T0CR3;
99  T0CCR |= TCCR_CR3_F;
100  T0CCR &= ~TCCR_CR3_R;
101 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
102  pwm_input_duty_tics[0] = t_rise - t_fall;
104 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
105  pwm_input_period_tics[0] = t_rise - t_oldrise;
107  t_oldrise = t_rise;
108 #endif //ACTIVE_LOW
109  }
110 }
111 #endif //USE_PWM_INPUT1
112 
113 #ifdef USE_PWM_INPUT2
114 void pwm_input_isr2(void)
115 {
116  static uint32_t t_rise;
117  static uint32_t t_fall;
118 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
119  static uint32_t t_oldrise = 0;
120 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
121  static uint32_t t_oldfall = 0;
122 #endif
123 
124  if (T0CCR & TCCR_CR0_F) {
125  t_fall = T0CR0;
126  T0CCR |= TCCR_CR0_R;
127  T0CCR &= ~TCCR_CR0_F;
128 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
129  pwm_input_duty_tics[1] = t_fall - t_rise;
131 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
132  pwm_input_period_tics[1] = t_fall - t_oldfall;
134  t_oldfall = t_fall;
135 #endif //ACTIVE_HIGH
136  } else if (T0CCR & TCCR_CR0_R) {
137  t_rise = T0CR0;
138  T0CCR |= TCCR_CR0_F;
139  T0CCR &= ~TCCR_CR0_R;
140 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
141  pwm_input_duty_tics[1] = t_rise - t_fall;
143 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
144  pwm_input_period_tics[1] = t_rise - t_oldrise;
146  t_oldrise = t_rise;
147 #endif //ACTIVE_LOW
148  }
149 }
150 #endif //USE_PWM_INPUT2
#define T0CCR
Definition: LPC21xx.h:60
#define TCCR_CR0_F
Definition: lpcTMR.h:135
#define T0CR0
Definition: LPC21xx.h:61
volatile uint32_t pwm_input_duty_tics[PWM_INPUT_NB]
Definition: pwm_input.c:31
#define TCCR_CR3_I
Definition: lpcTMR.h:145
#define TCCR_CR0_R
Definition: lpcTMR.h:134
#define TCCR_CR3_F
Definition: lpcTMR.h:144
volatile uint8_t pwm_input_period_valid[PWM_INPUT_NB]
Definition: pwm_input.c:34
#define TCCR_CR0_I
Definition: lpcTMR.h:136
#define PWM_INPUT_NB
handling of arm7 PWM input using a timer with capture
volatile uint8_t pwm_input_duty_valid[PWM_INPUT_NB]
Definition: pwm_input.c:32
#define T0CR3
Definition: LPC21xx.h:64
unsigned long uint32_t
Definition: types.h:18
volatile uint32_t pwm_input_period_tics[PWM_INPUT_NB]
Definition: pwm_input.c:33
#define TRUE
Definition: imu_chimu.h:144
#define TCCR_CR3_R
Definition: lpcTMR.h:143
void pwm_input_init(void)
handling of arm7 PWM input using a timer with capture