throttle speed before ejection
[aversive.git] / modules / base / time_ext / time_ext.h
1 /*  
2  *  Copyright Droids Corporation (2008)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  */
19
20 /*
21  * Author : Julien LE GUEN - jlg@jleguen.info
22  */
23 #ifndef _TIME_EXT_H_
24 #define _TIME_EXT_H
25
26
27 #include <time_ext_config.h>
28
29 /*
30  *      Since we want the maximum resolution available,
31  *      the prescaler of timer2 is set to 1.
32  *      Thus counter2 is incremented every positive edge of the external quartz.
33  *      You can define the frequency of your quartz in time_ext_config.h
34  */
35
36
37 /* Number of NANOSECONDS */
38 #define NANO_PER_US     1000UL
39 #define NANO_PER_MS     1000000UL
40 #define NANO_PER_S      1000000000UL
41
42
43 /* Prescaler */
44 #define TIMER2_PRESCALER_OFF    0
45 #define TIMER2_PRESCALER_1              1
46 #define TIMER2_PRESCALER_8              2
47 #define TIMER2_PRESCALER_32             3
48 #define TIMER2_PRESCALER_64             4
49 #define TIMER2_PRESCALER_128    5
50 #define TIMER2_PRESCALER_256    6
51 #define TIMER2_PRESCALER_1024   7
52
53
54 /* Useful defines */
55 #define NANO_PER_QUARTZ_TICK    TIME_EXT_QUARTZ_PERIOD
56
57
58
59
60 /*
61  *      Time structure.
62  *      We want to have the best resolution possible.
63  *      Thus, we store nano-seconds and seconds.
64  */
65 typedef struct _time_ext
66 {
67         uint32_t sec;
68         uint32_t nano;
69 } time_ext_t;
70
71
72 /* Initialize the module */
73 void time_ext_init(void);
74
75 /* 
76  * Setter and getter for the prescaler 
77  * By default, it is set to 1
78  */
79 inline void time_ext_set_prescaler(uint8_t p);
80 inline uint8_t time_ext_get_prescaler(void);
81
82 /*
83  *      Get time (seconds only, nanos only, or both)
84  */
85 inline uint32_t time_ext_get_s(void);
86 inline uint32_t time_ext_get_ns(void);
87 time_ext_t time_ext_get(void);
88
89 /*
90  *      Sets the global time.
91  *      This resets TCNT2 as well
92  */
93 inline void time_ext_set(uint32_t sec, uint32_t nano);
94
95 #endif /* _TIME_EXT_H_ */