Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pwm_input_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 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  */
22 
31 
32 #include "LPC21xx.h"
33 #include "interrupt_hw.h"
34 
35 //UPDATE THESE TO BE MORE ACCESSIBLE AND BE WARY OF EXISTING USAGE
36 //POSSIBLY MAKE MORE INPUTS ACCESSIBLE
37 #ifdef USE_PWM_INPUT1
38 //INPUT CAPTURE CAP0.3 on P0.29
39 #define PWM_INPUT1_PINSEL PINSEL1
40 #define PWM_INPUT1_PINSEL_BIT 26
41 #define PWM_INPUT1_PINSEL_VAL (0x2 << PWM_INPUT1_PINSEL_BIT)
42 #define PWM_INPUT1_PINSEL_MASK (0x3 <<PWM_INPUT1_PINSEL_BIT)
43 #endif
44 #ifdef USE_PWM_INPUT2
45 //INPUT CAPTURE CAP0.0 on P0.30
46 #define PWM_INPUT2_PINSEL PINSEL1
47 #define PWM_INPUT2_PINSEL_BIT 28
48 #define PWM_INPUT2_PINSEL_VAL (0x3 << PWM_INPUT2_PINSEL_BIT)
49 #define PWM_INPUT2_PINSEL_MASK (0x3 <<PWM_INPUT2_PINSEL_BIT)
50 #endif
51 
52 void pwm_input_init ( void )
53 {
54  // initialize the arrays to 0 to avoid junk
55  for (int i=0; i < PWM_INPUT_NB; i++)
56  {
57  pwm_input_duty_tics[i] = 0;
58  pwm_input_duty_valid[i] = 0;
59  pwm_input_period_tics[i] = 0;
61  }
62  /* select pin for capture */
63 #ifdef USE_PWM_INPUT1
64  PWM_INPUT1_PINSEL = (PWM_INPUT1_PINSEL & ~PWM_INPUT1_PINSEL_MASK) | PWM_INPUT1_PINSEL_VAL;
65  //enable capture 0.3 on rising edge + trigger interrupt
67 #endif
68 #ifdef USE_PWM_INPUT2
69  PWM_INPUT2_PINSEL = (PWM_INPUT2_PINSEL & ~PWM_INPUT2_PINSEL_MASK) | PWM_INPUT2_PINSEL_VAL;
70  //enable capture 0.0 on rising edge + trigger interrupt
72 #endif
73 }
74 
75 //FIXME what about clock time overflow???
76 #ifdef USE_PWM_INPUT1
77 void pwm_input_isr1(void)
78 {
79  static uint32_t t_rise;
80  static uint32_t t_fall;
81 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
82  static uint32_t t_oldrise = 0;
83 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
84  static uint32_t t_oldfall = 0;
85 #endif
86 
87  if (T0CCR & TCCR_CR3_F) {
88  t_fall = T0CR3;
89  T0CCR |= TCCR_CR3_R;
90  T0CCR &= ~TCCR_CR3_F;
91 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
92  pwm_input_duty_tics[0] = t_fall - t_rise;
94 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
95  pwm_input_period_tics[0] = t_fall - t_oldfall;
97  t_oldfall = t_fall;
98 #endif //ACTIVE_HIGH
99  } else if (T0CCR & TCCR_CR3_R) {
100  t_rise = T0CR3;
101  T0CCR |= TCCR_CR3_F;
102  T0CCR &= ~TCCR_CR3_R;
103 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
104  pwm_input_duty_tics[0] = t_rise - t_fall;
106 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
107  pwm_input_period_tics[0] = t_rise - t_oldrise;
109  t_oldrise = t_rise;
110 #endif //ACTIVE_LOW
111  }
112 }
113 #endif //USE_PWM_INPUT1
114 
115 #ifdef USE_PWM_INPUT2
116 void pwm_input_isr2(void)
117 {
118  static uint32_t t_rise;
119  static uint32_t t_fall;
120 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
121  static uint32_t t_oldrise = 0;
122 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
123  static uint32_t t_oldfall = 0;
124 #endif
125 
126  if (T0CCR & TCCR_CR0_F) {
127  t_fall = T0CR0;
128  T0CCR |= TCCR_CR0_R;
129  T0CCR &= ~TCCR_CR0_F;
130 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
131  pwm_input_duty_tics[1] = t_fall - t_rise;
133 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
134  pwm_input_period_tics[1] = t_fall - t_oldfall;
136  t_oldfall = t_fall;
137 #endif //ACTIVE_HIGH
138  } else if (T0CCR & TCCR_CR0_R) {
139  t_rise = T0CR0;
140  T0CCR |= TCCR_CR0_F;
141  T0CCR &= ~TCCR_CR0_R;
142 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
143  pwm_input_duty_tics[1] = t_rise - t_fall;
145 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
146  pwm_input_period_tics[1] = t_rise - t_oldrise;
148  t_oldrise = t_rise;
149 #endif //ACTIVE_LOW
150  }
151 }
152 #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:29
#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:32
#define TCCR_CR0_I
Definition: lpcTMR.h:136
#define PWM_INPUT_NB
volatile uint8_t pwm_input_duty_valid[PWM_INPUT_NB]
Definition: pwm_input.c:30
#define T0CR3
Definition: LPC21xx.h:64
unsigned long uint32_t
Definition: types.h:18
handling of arm7 PWM input using a timer with capture.
volatile uint32_t pwm_input_period_tics[PWM_INPUT_NB]
Definition: pwm_input.c:31
#define TRUE
Definition: imu_chimu.h:144
#define TCCR_CR3_R
Definition: lpcTMR.h:143
void pwm_input_init(void)