2 * Copyright Droids Corporation (2008)
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.
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.
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
21 * Author : Julien LE GUEN - jlg@jleguen.info
25 #include <aversive/error.h>
26 #include <aversive/wait.h>
29 #include <time_ext_config.h>
31 /* Global time variable */
32 static volatile time_ext_t g_time;
36 static volatile uint32_t g_timer1_cnt;
37 static volatile uint16_t g_timer1_val;
38 static volatile uint32_t g_inst_number;
39 static volatile uint32_t g_frequency;
40 static volatile uint16_t g_nano_per_inst;
43 /* Function pointer (points to the current interrupt) */
44 static volatile void (*g_timer1_ovf_interrupt)(void);
45 static volatile void (*g_timer2_ovf_interrupt)(void);
49 * Interrupt vector: Timer1 OVerFlow
51 void SIG_OVERFLOW1(void) __attribute__ ((signal));
52 void SIG_OVERFLOW1(void)
60 * Interrupt vector: Timer2 OVerFlow
62 void SIG_OVERFLOW2(void) __attribute__ ((signal));
63 void SIG_OVERFLOW2(void)
69 /* 256 times NANO_PER_QUARTZ_TICK / prescaler since last overflow */
70 g_time.nano += (NANO_PER_QUARTZ_TICK << 8) / time_ext_get_prescaler();
72 if(g_time.nano >= NANO_PER_S)
74 g_time.nano -= NANO_PER_S;
82 * Timer1 overflow interrupt
83 * Basic one, used in production
85 void timer1_ovf_basic(void)
91 * Timer1 overflow interrupt
94 void timer1_ovf_calib(void)
96 /* Remember how many times we were here */
101 * Timer2 overflow interrupt
102 * Basic one, used in production
104 void timer2_ovf_basic(void)
108 /* 256 times NANO_PER_QUARTZ_TICK / prescaler since last overflow */
109 g_time.nano += (NANO_PER_QUARTZ_TICK << 8) / time_ext_get_prescaler();
111 if(g_time.nano >= NANO_PER_S)
113 g_time.nano -= NANO_PER_S;
119 * Timer2 overflow interrupt
120 * Used for crystal calibration
122 void timer2_ovf_calib(void)
124 /* XXX nothing for now */
127 * When calibrating the main crystal, this
128 * interrupt is triggered exactly one second
129 * after the start of the calibration.
130 * This then reads the number of instructions
131 * executed during this second, and deduces the
132 * main crystal frequency.
133 * It then configures the module back to its normal
134 * function (that is, couting time).
143 /* Fetch the value of TCNT1 */
144 g_timer1_val = TCNT1; //(((uint16_t)TCNT1H) << 8) + TCNT1L;
146 /* Total number of instructions */
147 g_inst_number = (((uint32_t)g_timer1_cnt) << 16) + g_timer1_val;
150 NOTICE(E_TIME_EXT, "%ld instructions in 8 second !", g_inst_number);
151 NOTICE(E_TIME_EXT, "timer_val = %d, timer_cnt = %ld", g_timer1_val, g_timer1_cnt);
152 g_frequency = g_inst_number >> 3;
153 NOTICE(E_TIME_EXT, "Main crystal frequency = %ld Hz", g_frequency);
154 g_nano_per_inst = (1000000000UL / g_frequency);
155 NOTICE(E_TIME_EXT, "Instruction every %d nanosecond", g_nano_per_inst);
157 /* Come back to normal state */
158 g_timer1_ovf_interrupt = timer1_ovf_basic;
159 g_timer2_ovf_interrupt = timer2_ovf_basic;
175 void time_ext_init(void)
178 * Asynchronous operation of Timer2
179 * Some considerations must be taken.
180 * See DataSheet for additional information.
183 NOTICE(E_TIME_EXT, "Initialization");
187 g_timer1_ovf_interrupt = timer1_ovf_basic;
188 g_timer2_ovf_interrupt = timer2_ovf_basic;
190 /* Deactivate TIMER1 */
199 /* Asynchronous mode:
200 * EXCLK = 0 (we have an oscillator)
202 * other bits are read only
205 /* We want a 'normal' operation mode */
207 /* Clock Select: no prescaling */
209 /* Reset the counter */
211 /* We want an interrupt when TCNT2 overflows */
221 * Change the prescaler
223 inline void time_ext_set_prescaler(uint8_t p)
225 /* Prevent fools to pass an incorrect value */
230 * Get the prescaler value
232 inline uint8_t time_ext_get_prescaler(void)
234 return TCCR2B & 0x07;
239 * Calibration of main crystal
241 void time_ext_calib(void)
244 * Configure TIMER1 and all the stuff
245 * No prescaler, normal mode, interrupt on OVF
250 TIFR1 = 0xFF; /* clear flags */
252 /* Configure TIMER2 */
253 time_ext_set_prescaler(TIMER2_PRESCALER_OFF);
255 /* Change the interrupt handlers */
256 g_timer1_ovf_interrupt = timer1_ovf_calib;
257 g_timer2_ovf_interrupt = timer2_ovf_calib;
268 /* Launch the prescalers -> timers ON */
269 TCCR2B = TIMER2_PRESCALER_1024;
277 * Since we only update g_time when TCNT2 overflows,
278 * we add the value of TCNT2 to the nano field.
280 inline uint32_t time_ext_get_s(void)
285 inline uint32_t time_ext_get_ns(void)
288 /* Fetch timer1 value */
291 tmp = ((uint32_t)TCNT2) * NANO_PER_QUARTZ_TICK / time_ext_get_prescaler();
292 //tmp += tmp1 * g_nano_per_inst;
294 return g_time.nano + tmp;
297 time_ext_t time_ext_get(void)
300 t.nano = time_ext_get_ns();
301 t.sec = time_ext_get_s();
302 if(t.nano >= NANO_PER_S) {
304 t.nano -= NANO_PER_S;
312 * Resets TCNT2 as well
314 inline void time_ext_set(uint32_t sec, uint32_t nano)
319 NOTICE(E_TIME_EXT, "Time set to %ld %ld", sec, nano);