Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces 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 
30 #include "mcu_periph/pwm_input_arch.h"
31 
32 #include "LPC21xx.h"
33 #include "armVIC.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  pwm_input_duty_tics[i] = 0;
57  pwm_input_duty_valid[i] = 0;
58  pwm_input_period_tics[i] = 0;
60  }
61  /* select pin for capture */
62 #ifdef USE_PWM_INPUT1
63  PWM_INPUT1_PINSEL = (PWM_INPUT1_PINSEL & ~PWM_INPUT1_PINSEL_MASK) | PWM_INPUT1_PINSEL_VAL;
64  //enable capture 0.3 on rising edge + trigger interrupt
66 #endif
67 #ifdef USE_PWM_INPUT2
68  PWM_INPUT2_PINSEL = (PWM_INPUT2_PINSEL & ~PWM_INPUT2_PINSEL_MASK) | PWM_INPUT2_PINSEL_VAL;
69  //enable capture 0.0 on rising edge + trigger interrupt
71 #endif
72 }
73 
74 //FIXME what about clock time overflow???
75 #ifdef USE_PWM_INPUT1
76 void pwm_input_isr1(void)
77 {
78  static uint32_t t_rise;
79  static uint32_t t_fall;
80 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
81  static uint32_t t_oldrise = 0;
82 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
83  static uint32_t t_oldfall = 0;
84 #endif
85 
86  if (T0CCR & TCCR_CR3_F) {
87  t_fall = T0CR3;
88  T0CCR |= TCCR_CR3_R;
89  T0CCR &= ~TCCR_CR3_F;
90 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
91  pwm_input_duty_tics[0] = t_fall - t_rise;
92  pwm_input_duty_valid[0] = true;
93 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
94  pwm_input_period_tics[0] = t_fall - t_oldfall;
95  pwm_input_period_valid[0] = true;
96  t_oldfall = t_fall;
97 #endif //ACTIVE_HIGH
98  } else if (T0CCR & TCCR_CR3_R) {
99  t_rise = T0CR3;
100  T0CCR |= TCCR_CR3_F;
101  T0CCR &= ~TCCR_CR3_R;
102 #if USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_LOW
103  pwm_input_duty_tics[0] = t_rise - t_fall;
104  pwm_input_duty_valid[0] = true;
105 #elif USE_PWM_INPUT1 == PWM_PULSE_TYPE_ACTIVE_HIGH
106  pwm_input_period_tics[0] = t_rise - t_oldrise;
107  pwm_input_period_valid[0] = true;
108  t_oldrise = t_rise;
109 #endif //ACTIVE_LOW
110  }
111 }
112 #endif //USE_PWM_INPUT1
113 
114 #ifdef USE_PWM_INPUT2
115 void pwm_input_isr2(void)
116 {
117  static uint32_t t_rise;
118  static uint32_t t_fall;
119 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
120  static uint32_t t_oldrise = 0;
121 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
122  static uint32_t t_oldfall = 0;
123 #endif
124 
125  if (T0CCR & TCCR_CR0_F) {
126  t_fall = T0CR0;
127  T0CCR |= TCCR_CR0_R;
128  T0CCR &= ~TCCR_CR0_F;
129 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
130  pwm_input_duty_tics[1] = t_fall - t_rise;
131  pwm_input_duty_valid[1] = true;
132 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
133  pwm_input_period_tics[1] = t_fall - t_oldfall;
134  pwm_input_period_valid[1] = true;
135  t_oldfall = t_fall;
136 #endif //ACTIVE_HIGH
137  } else if (T0CCR & TCCR_CR0_R) {
138  t_rise = T0CR0;
139  T0CCR |= TCCR_CR0_F;
140  T0CCR &= ~TCCR_CR0_R;
141 #if USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_LOW
142  pwm_input_duty_tics[1] = t_rise - t_fall;
143  pwm_input_duty_valid[1] = true;
144 #elif USE_PWM_INPUT2 == PWM_PULSE_TYPE_ACTIVE_HIGH
145  pwm_input_period_tics[1] = t_rise - t_oldrise;
146  pwm_input_period_valid[1] = true;
147  t_oldrise = t_rise;
148 #endif //ACTIVE_LOW
149  }
150 }
151 #endif //USE_PWM_INPUT2
#define T0CCR
Definition: LPC21xx.h:60
#define TCCR_CR0_F
Definition: lpcTMR.h:135
void pwm_input_init(void)
#define T0CR0
Definition: LPC21xx.h:61
volatile uint32_t pwm_input_duty_tics[PWM_INPUT_NB]
Definition: pwm_input.c:34
#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:37
#define TCCR_CR0_I
Definition: lpcTMR.h:136
volatile uint8_t pwm_input_duty_valid[PWM_INPUT_NB]
Definition: pwm_input.c:35
#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:36
#define TCCR_CR3_R
Definition: lpcTMR.h:143