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
sys_time_arch.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (C) 2009-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 
30 #include "mcu_periph/sys_time.h"
31 
32 #include "armVIC.h"
33 
34 #ifdef SYS_TIME_LED
35 #include "led.h"
36 #endif
37 
38 #define SYS_TICK_IT TIR_MR0I
39 
40 #if defined ACTUATORS && ( defined SERVOS_4017 || defined SERVOS_4015_MAT || defined SERVOS_PPM_MAT)
41 #ifdef SERVOS_4015_MAT
42 #include "subsystems/actuators/servos_4015_MAT_hw.h"
43 #endif
44 #ifdef SERVOS_4017
45 #include "subsystems/actuators/servos_4017_hw.h"
46 #endif
47 #ifdef SERVOS_PPM_MAT
48 #include "subsystems/actuators/servos_ppm_hw.h"
49 #endif
50 #else
51 #define ACTUATORS_IT 0x00
52 #endif /* ACTUATORS */
53 
54 #if defined RADIO_CONTROL && defined RADIO_CONTROL_TYPE_PPM
56 #else
57 #define PPM_IT 0x00
58 #endif
59 
60 #ifdef MB_SCALE
61 #include "mb_scale.h"
62 #else
63 #define MB_SCALE_IT 0x00
64 #endif
65 
66 #ifdef MB_TACHO
67 #include "mb_tacho.h"
68 #else
69 #define MB_TACHO_IT 0x00
70 #endif
71 
72 #ifdef USE_PWM_INPUT
73 #include "mcu_periph/pwm_input.h"
74 #endif
75 #ifndef USE_PWM_INPUT1
76 #define PWM_INPUT_IT1 0x00
77 #endif
78 #ifndef USE_PWM_INPUT2
79 #define PWM_INPUT_IT2 0x00
80 #endif
81 
82 #ifdef USE_AMI601
83 #include "peripherals/ami601.h"
84 #else
85 #define AMI601_IT 0x00
86 #endif
87 
88 #ifdef TRIGGER_EXT
89 #include "core/trigger_ext_hw.h"
90 #else
91 #define TRIGGER_IT 0x00
92 #endif
93 
94 #define TIMER0_IT_MASK (SYS_TICK_IT | \
95  ACTUATORS_IT | \
96  PPM_IT | \
97  TRIGGER_IT | \
98  MB_SCALE_IT | \
99  MB_TACHO_IT | \
100  PWM_INPUT_IT1 | \
101  PWM_INPUT_IT2 | \
102  AMI601_IT)
103 
104 
105 void sys_time_arch_init( void ) {
106  sys_time.cpu_ticks_per_sec = PCLK / T0_PCLK_DIV;
107  /* cpu ticks per desired sys_time timer step */
109 
110  /* setup Timer 0 to count forever */
111  /* reset & disable timer 0 */
112  T0TCR = TCR_RESET;
113  /* set the prescale divider */
114  T0PR = T0_PCLK_DIV - 1;
115  /* enable interrupt on match0 for sys_ticks */
116  T0MCR = TMCR_MR0_I;
117  /* disable capture registers */
118  T0CCR = 0;
119  /* disable external match register */
120  T0EMR = 0;
121 
122  /* set first sys tick interrupt */
124 
125  /* enable timer 0 */
126  T0TCR = TCR_ENABLE;
127 
128  /* select TIMER0 as IRQ */
130  /* enable TIMER0 interrupt */
132  /* on slot vic slot 1 */
134  /* address of the ISR */
136 }
137 
138 
139 // FIXME : nb_tick rollover ???
140 //
141 // 97 days at 512hz
142 // 12 hours at 100khz
143 //
144 static inline void sys_tick_irq_handler(void) {
145 
146  /* set match register for next interrupt */
148 
149  sys_time.nb_tick++;
153  sys_time.nb_sec++;
154 #ifdef SYS_TIME_LED
155  LED_TOGGLE(SYS_TIME_LED);
156 #endif
157  }
158  for (unsigned int i=0; i<SYS_TIME_NB_TIMER; i++) {
159  if (sys_time.timer[i].in_use &&
162  sys_time.timer[i].elapsed = TRUE;
163  if (sys_time.timer[i].cb) {
164  sys_time.timer[i].cb(i);
165  }
166  }
167  }
168 }
169 
170 void TIMER0_ISR ( void ) {
171  ISR_ENTRY();
172 
173  while (T0IR & TIMER0_IT_MASK) {
174 
175  if (T0IR&SYS_TICK_IT) {
177  T0IR = SYS_TICK_IT;
178  }
179 
180 #if defined ACTUATORS && ( defined SERVOS_4017 || defined SERVOS_4015_MAT || defined SERVOS_PPM_MAT)
181  if (T0IR&ACTUATORS_IT) {
182 #ifdef SERVOS_4017
183  SERVOS_4017_ISR();
184 #endif
185 #ifdef SERVOS_4015_MAT
187 #endif
188 #ifdef SERVOS_PPM_MAT
190 #endif
191  T0IR = ACTUATORS_IT;
192  }
193 #endif /* ACTUATORS && (SERVOS_4017 || SERVOS_4015_MAT || SERVOS_PPM_MAT) */
194 
195 #if defined RADIO_CONTROL && defined RADIO_CONTROL_TYPE_PPM
196  if (T0IR&PPM_IT) {
197  PPM_ISR();
198  T0IR = PPM_IT;
199  }
200 #endif
201 #ifdef TRIGGER_EXT
202  if (T0IR&TRIGGER_IT) {
203  TRIG_ISR();
204  T0IR = TRIGGER_IT;
205  LED_TOGGLE(3);
206  }
207 #endif
208 #ifdef MB_SCALE
209  if (T0IR&MB_SCALE_IT) {
210  MB_SCALE_ICP_ISR();
211  T0IR = MB_SCALE_IT;
212  }
213 #endif
214 #ifdef MB_TACHO
215  if (T0IR&MB_TACHO_IT) {
216  MB_TACHO_ISR();
217  T0IR = MB_TACHO_IT;
218  }
219 #endif
220 #ifdef USE_PWM_INPUT1
221  if (T0IR&PWM_INPUT_IT1) {
222  PWM_INPUT_ISR_1();
224  }
225 #endif
226 #ifdef USE_PWM_INPUT2
227  if (T0IR&PWM_INPUT_IT2) {
228  PWM_INPUT_ISR_2();
230  }
231 #endif
232 #ifdef USE_AMI601
233  if (T0IR&AMI601_IT) {
234  AMI601_ISR();
235  T0IR = AMI601_IT;
236  }
237 #endif
238  }
239  VICVectAddr = 0x00000000;
240  ISR_EXIT();
241 }
#define VICIntSelect
Definition: LPC21xx.h:430
#define T0CCR
Definition: LPC21xx.h:60
volatile bool_t elapsed
Definition: sys_time.h:63
sys_time_cb cb
Definition: sys_time.h:62
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:77
#define T0MCR
Definition: LPC21xx.h:55
#define TCR_RESET
Definition: lpcTMR.h:55
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
arch independent PWM input capture API
#define PCLK
Definition: booz_1.0.h:18
#define T0TCR
Definition: LPC21xx.h:51
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:64
#define _VIC_ADDR(idx)
Definition: armVIC.h:20
#define TIMER0_IT_MASK
Definition: sys_time_arch.c:94
void TIMER0_ISR(void)
#define SERVOS_4017_ISR()
#define T0PR
Definition: LPC21xx.h:53
Architecture independent timing functions.
#define T0IR
Definition: LPC21xx.h:50
#define VIC_TIMER0
Definition: lpcVIC.h:72
#define VICVectAddr
Definition: LPC21xx.h:436
unsigned long uint32_t
Definition: types.h:18
#define TCR_ENABLE
Definition: lpcTMR.h:54
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:74
#define PPM_IT
Definition: sys_time_arch.c:57
#define MB_SCALE_IT
Definition: sys_time_arch.c:63
#define PWM_INPUT_IT2
Definition: sys_time_arch.c:79
#define T0MR0
Definition: LPC21xx.h:56
#define T0EMR
Definition: LPC21xx.h:65
#define AMI601_ISR()
Definition: ami601.h:30
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:70
#define LED_TOGGLE(i)
Definition: led_hw.h:30
#define Servos4015Mat_ISR()
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:71
void sys_time_arch_init(void)
#define PPM_ISR()
Definition: ppm_arch.h:45
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
#define TRIGGER_IT
Definition: sys_time_arch.c:91
#define TRUE
Definition: imu_chimu.h:144
#define ACTUATORS_IT
Definition: sys_time_arch.c:51
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:65
#define ISR_EXIT()
Definition: armVIC.h:61
#define TIMER0_VIC_SLOT
Definition: sys_time_arch.h:59
static void sys_tick_irq_handler(void)
#define VICIntEnable
Definition: LPC21xx.h:431
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
void TRIG_ISR()
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:76
arch independent LED (Light Emitting Diodes) API
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:40
#define TMCR_MR0_I
Definition: lpcTMR.h:59
#define SYS_TICK_IT
Definition: sys_time_arch.c:38
#define MB_TACHO_IT
Definition: sys_time_arch.c:69
#define AMI601_IT
Definition: sys_time_arch.c:85
bool_t in_use
Definition: sys_time.h:61
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:72
#define PWM_INPUT_IT1
Definition: sys_time_arch.c:76
#define ServosPPMMat_ISR()
Definition: servos_ppm_hw.h:60
#define ISR_ENTRY()
Definition: armVIC.h:40
#define VIC_ENABLE
Definition: lpcVIC.h:102