Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 
106 {
107  sys_time.cpu_ticks_per_sec = PCLK / T0_PCLK_DIV;
108  /* cpu ticks per desired sys_time timer step */
110 
111  /* setup Timer 0 to count forever */
112  /* reset & disable timer 0 */
113  T0TCR = TCR_RESET;
114  /* set the prescale divider */
115  T0PR = T0_PCLK_DIV - 1;
116  /* enable interrupt on match0 for sys_ticks */
117  T0MCR = TMCR_MR0_I;
118  /* disable capture registers */
119  T0CCR = 0;
120  /* disable external match register */
121  T0EMR = 0;
122 
123  /* set first sys tick interrupt */
124  /* We need to wait long enough to be sure
125  * that all the init part is finished before
126  * the first interrupt. Since the global
127  * interrupts are enable at the end of the init
128  * phase, if we miss the first one, the
129  * sys_tick_handler is not called afterward
130  */
132 
133  /* enable timer 0 */
134  T0TCR = TCR_ENABLE;
135 
136  /* select TIMER0 as IRQ */
138  /* enable TIMER0 interrupt */
140  /* on slot vic slot 1 */
142  /* address of the ISR */
144 }
145 
146 
147 // FIXME : nb_tick rollover ???
148 //
149 // 97 days at 512hz
150 // 12 hours at 100khz
151 //
152 static inline void sys_tick_irq_handler(void)
153 {
154 
155  /* set match register for next interrupt */
157 
158  sys_time.nb_tick++;
162  sys_time.nb_sec++;
163 #ifdef SYS_TIME_LED
164  LED_TOGGLE(SYS_TIME_LED);
165 #endif
166  }
167  for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
168  if (sys_time.timer[i].in_use &&
171  sys_time.timer[i].elapsed = true;
172  if (sys_time.timer[i].cb) {
173  sys_time.timer[i].cb(i);
174  }
175  }
176  }
177 }
178 
179 void TIMER0_ISR(void)
180 {
181  ISR_ENTRY();
182 
183  while (T0IR & TIMER0_IT_MASK) {
184 
185  if (T0IR & SYS_TICK_IT) {
187  T0IR = SYS_TICK_IT;
188  }
189 
190 #if defined ACTUATORS && ( defined SERVOS_4017 || defined SERVOS_4015_MAT || defined SERVOS_PPM_MAT)
191  if (T0IR & ACTUATORS_IT) {
192 #ifdef SERVOS_4017
193  SERVOS_4017_ISR();
194 #endif
195 #ifdef SERVOS_4015_MAT
197 #endif
198 #ifdef SERVOS_PPM_MAT
200 #endif
201  T0IR = ACTUATORS_IT;
202  }
203 #endif /* ACTUATORS && (SERVOS_4017 || SERVOS_4015_MAT || SERVOS_PPM_MAT) */
204 
205 #if defined RADIO_CONTROL && defined RADIO_CONTROL_TYPE_PPM
206  if (T0IR & PPM_IT) {
207  PPM_ISR();
208  T0IR = PPM_IT;
209  }
210 #endif
211 #ifdef TRIGGER_EXT
212  if (T0IR & TRIGGER_IT) {
213  TRIG_ISR();
214  T0IR = TRIGGER_IT;
215  LED_TOGGLE(3);
216  }
217 #endif
218 #ifdef MB_SCALE
219  if (T0IR & MB_SCALE_IT) {
220  MB_SCALE_ICP_ISR();
221  T0IR = MB_SCALE_IT;
222  }
223 #endif
224 #ifdef MB_TACHO
225  if (T0IR & MB_TACHO_IT) {
226  MB_TACHO_ISR();
227  T0IR = MB_TACHO_IT;
228  }
229 #endif
230 #ifdef USE_PWM_INPUT1
231  if (T0IR & PWM_INPUT_IT1) {
232  PWM_INPUT_ISR_1();
234  }
235 #endif
236 #ifdef USE_PWM_INPUT2
237  if (T0IR & PWM_INPUT_IT2) {
238  PWM_INPUT_ISR_2();
240  }
241 #endif
242 #ifdef USE_AMI601
243  if (T0IR & AMI601_IT) {
244  AMI601_ISR();
245  T0IR = AMI601_IT;
246  }
247 #endif
248  }
249  VICVectAddr = 0x00000000;
250  ISR_EXIT();
251 }
sys_time::cpu_ticks_per_sec
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:80
sys_time::timer
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:75
radio_control.h
TIMER0_VIC_SLOT
#define TIMER0_VIC_SLOT
Definition: sys_time_arch.h:59
PPM_ISR
#define PPM_ISR()
Definition: ppm_arch.h:49
SYS_TICK_IT
#define SYS_TICK_IT
Definition: sys_time_arch.c:38
T0CCR
#define T0CCR
Definition: LPC21xx.h:60
sys_time_timer::elapsed
volatile bool elapsed
Definition: sys_time.h:66
sys_time::resolution_cpu_ticks
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:79
sys_time_timer::duration
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:68
TIMER0_ISR
void TIMER0_ISR(void)
Definition: sys_time_arch.c:179
armVIC.h
VICIntSelect
#define VICIntSelect
Definition: LPC21xx.h:430
T0MCR
#define T0MCR
Definition: LPC21xx.h:55
uint32_t
unsigned long uint32_t
Definition: types.h:18
AMI601_ISR
#define AMI601_ISR()
Definition: ami601.h:30
TMCR_MR0_I
#define TMCR_MR0_I
Definition: lpcTMR.h:59
LED_TOGGLE
#define LED_TOGGLE(i)
Definition: led_hw.h:53
MB_SCALE_IT
#define MB_SCALE_IT
Definition: sys_time_arch.c:63
_VIC_ADDR
#define _VIC_ADDR(idx)
Definition: armVIC.h:20
PCLK
#define PCLK
Definition: booz_1.0.h:18
TCR_ENABLE
#define TCR_ENABLE
Definition: lpcTMR.h:54
VIC_BIT
#define VIC_BIT(chan)
Definition: lpcVIC.h:105
SERVOS_4017_ISR
#define SERVOS_4017_ISR()
Definition: servos_4017_hw.h:55
PWM_INPUT_IT2
#define PWM_INPUT_IT2
Definition: sys_time_arch.c:79
sys_tick_irq_handler
static void sys_tick_irq_handler(void)
Definition: sys_time_arch.c:152
TRIG_ISR
void TRIG_ISR()
Definition: trigger_ext_hw.c:30
sys_time::nb_tick
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:74
T0TCR
#define T0TCR
Definition: LPC21xx.h:51
T0PR
#define T0PR
Definition: LPC21xx.h:53
sys_time_timer::in_use
bool in_use
Definition: sys_time.h:64
ServosPPMMat_ISR
#define ServosPPMMat_ISR()
Definition: servos_ppm_hw.h:60
sys_time.h
Architecture independent timing functions.
T0IR
#define T0IR
Definition: LPC21xx.h:50
Servos4015Mat_ISR
#define Servos4015Mat_ISR()
Definition: servos_4015_MAT_hw.h:54
VICVectAddr
#define VICVectAddr
Definition: LPC21xx.h:436
PPM_IT
#define PPM_IT
Definition: sys_time_arch.c:57
led.h
arch independent LED (Light Emitting Diodes) API
ami601.h
TIMER0_IT_MASK
#define TIMER0_IT_MASK
Definition: sys_time_arch.c:94
pwm_input.h
arch independent PWM input capture API
PWM_INPUT_IT1
#define PWM_INPUT_IT1
Definition: sys_time_arch.c:76
sys_time_timer::cb
sys_time_cb cb
Definition: sys_time.h:65
T0EMR
#define T0EMR
Definition: LPC21xx.h:65
TCR_RESET
#define TCR_RESET
Definition: lpcTMR.h:55
MB_TACHO_IT
#define MB_TACHO_IT
Definition: sys_time_arch.c:69
ACTUATORS_IT
#define ACTUATORS_IT
Definition: sys_time_arch.c:51
ISR_ENTRY
#define ISR_ENTRY()
Definition: armVIC.h:40
ISR_EXIT
#define ISR_EXIT()
Definition: armVIC.h:61
sys_time_arch_init
void sys_time_arch_init(void)
Definition: sys_time_arch.c:49
sys_time
Definition: sys_time.h:71
SYS_TIME_NB_TIMER
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:40
T0MR0
#define T0MR0
Definition: LPC21xx.h:56
sys_time::nb_sec_rem
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:73
sys_time_timer::end_time
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:67
VIC_ENABLE
#define VIC_ENABLE
Definition: lpcVIC.h:102
VICIntEnable
#define VICIntEnable
Definition: LPC21xx.h:431
VIC_TIMER0
#define VIC_TIMER0
Definition: lpcVIC.h:72
sys_time::nb_sec
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
_VIC_CNTL
#define _VIC_CNTL(idx)
Definition: armVIC.h:19
sys_time::resolution
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:77
AMI601_IT
#define AMI601_IT
Definition: sys_time_arch.c:85
TRIGGER_IT
#define TRIGGER_IT
Definition: sys_time_arch.c:91