00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <aversive.h>
00025 #include <aversive/error.h>
00026 #include <aversive/wait.h>
00027
00028 #include <time_ext.h>
00029 #include <time_ext_config.h>
00030
00031
00032 static volatile time_ext_t g_time;
00033
00034
00035
00036 static volatile uint32_t g_timer1_cnt;
00037 static volatile uint16_t g_timer1_val;
00038 static volatile uint32_t g_inst_number;
00039 static volatile uint32_t g_frequency;
00040 static volatile uint16_t g_nano_per_inst;
00041
00042 #if 0
00043
00044 static volatile void (*g_timer1_ovf_interrupt)(void);
00045 static volatile void (*g_timer2_ovf_interrupt)(void);
00046
00047
00048
00049
00050
00051 void SIG_OVERFLOW1(void) __attribute__ ((signal));
00052 void SIG_OVERFLOW1(void)
00053 {
00054 g_timer1_cnt++;
00055 }
00056 #endif
00057
00058
00059
00060
00061
00062 void SIG_OVERFLOW2(void) __attribute__ ((signal));
00063 void SIG_OVERFLOW2(void)
00064 {
00065 #if 0
00066 TCNT1 = 0;
00067 g_timer1_cnt = 0;
00068 #endif
00069
00070 g_time.nano += (NANO_PER_QUARTZ_TICK << 8) / time_ext_get_prescaler();
00071
00072 if(g_time.nano >= NANO_PER_S)
00073 {
00074 g_time.nano -= NANO_PER_S;
00075 g_time.sec++;
00076 }
00077 }
00078
00079
00080 #if 0
00081
00082
00083
00084
00085 void timer1_ovf_basic(void)
00086 {
00087 g_timer1_cnt++;
00088 }
00089
00090
00091
00092
00093
00094 void timer1_ovf_calib(void)
00095 {
00096
00097 g_timer1_cnt++;
00098 }
00099
00100
00101
00102
00103
00104 void timer2_ovf_basic(void)
00105 {
00106 TCNT1 = 0;
00107 g_timer1_cnt = 0;
00108
00109 g_time.nano += (NANO_PER_QUARTZ_TICK << 8) / time_ext_get_prescaler();
00110
00111 if(g_time.nano >= NANO_PER_S)
00112 {
00113 g_time.nano -= NANO_PER_S;
00114 g_time.sec++;
00115 }
00116 }
00117
00118
00119
00120
00121
00122 void timer2_ovf_calib(void)
00123 {
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 TCCR1B = 0x00;
00139 TIFR1 = 0xFF;
00140 TCCR2B = 0x00;
00141 TIFR2 = 0xFF;
00142
00143
00144 g_timer1_val = TCNT1;
00145
00146
00147 g_inst_number = (((uint32_t)g_timer1_cnt) << 16) + g_timer1_val;
00148
00149
00150 NOTICE(E_TIME_EXT, "%ld instructions in 8 second !", g_inst_number);
00151 NOTICE(E_TIME_EXT, "timer_val = %d, timer_cnt = %ld", g_timer1_val, g_timer1_cnt);
00152 g_frequency = g_inst_number >> 3;
00153 NOTICE(E_TIME_EXT, "Main crystal frequency = %ld Hz", g_frequency);
00154 g_nano_per_inst = (1000000000UL / g_frequency);
00155 NOTICE(E_TIME_EXT, "Instruction every %d nanosecond", g_nano_per_inst);
00156
00157
00158 g_timer1_ovf_interrupt = timer1_ovf_basic;
00159 g_timer2_ovf_interrupt = timer2_ovf_basic;
00160
00161 g_timer1_cnt = 0;
00162 TCNT2 = 0;
00163
00164 TCCR1B = 0x01;
00165 TCCR2B = 0x01;
00166 }
00167 #endif
00168
00169
00170
00171
00172
00173
00174
00175 void time_ext_init(void)
00176 {
00177
00178
00179
00180
00181
00182
00183 NOTICE(E_TIME_EXT, "Initialization");
00184
00185 #if 0
00186
00187 g_timer1_ovf_interrupt = timer1_ovf_basic;
00188 g_timer2_ovf_interrupt = timer2_ovf_basic;
00189
00190
00191 TIMSK1 = 0x00;
00192 TCCR1A = 0x00;
00193 TCCR1B = 0x00;
00194 TCNT1 = 0x00;
00195 g_timer1_val = 0;
00196 g_timer1_cnt = 0;
00197 #endif
00198
00199
00200
00201
00202
00203
00204 ASSR = _BV(AS2);
00205
00206 TCCR2A = 0x00;
00207
00208 TCCR2B = 0x01;
00209
00210 TCNT2 = 0x00;
00211
00212 TIMSK2 = _BV(TOIE2);
00213
00214
00215 g_time.sec = 0;
00216 g_time.nano = 0;
00217 }
00218
00219
00220
00221
00222
00223 inline void time_ext_set_prescaler(uint8_t p)
00224 {
00225
00226 TCCR2B = p & 0x07;
00227 }
00228
00229
00230
00231
00232 inline uint8_t time_ext_get_prescaler(void)
00233 {
00234 return TCCR2B & 0x07;
00235 }
00236
00237 #if 0
00238
00239
00240
00241 void time_ext_calib(void)
00242 {
00243
00244
00245
00246
00247 TCCR1A = 0x00;
00248 TCCR1B = 0x00;
00249 TIMSK1 = _BV(TOIE1);
00250 TIFR1 = 0xFF;
00251
00252
00253 time_ext_set_prescaler(TIMER2_PRESCALER_OFF);
00254
00255
00256 g_timer1_ovf_interrupt = timer1_ovf_calib;
00257 g_timer2_ovf_interrupt = timer2_ovf_calib;
00258
00259 g_frequency = 0;
00260 g_nano_per_inst = 0;
00261 g_inst_number = 0;
00262
00263 g_timer1_cnt = 0;
00264 g_timer1_val = 0;
00265 TCNT1 = 0;
00266 TCNT2 = 0;
00267
00268
00269 TCCR2B = TIMER2_PRESCALER_1024;
00270 TCCR1B = 0x01;
00271 }
00272 #endif
00273
00274
00275
00276
00277
00278
00279
00280 inline uint32_t time_ext_get_s(void)
00281 {
00282 return g_time.sec;
00283 }
00284
00285 inline uint32_t time_ext_get_ns(void)
00286 {
00287 uint32_t tmp, tmp1;
00288
00289
00290
00291 tmp = ((uint32_t)TCNT2) * NANO_PER_QUARTZ_TICK / time_ext_get_prescaler();
00292
00293
00294 return g_time.nano + tmp;
00295 }
00296
00297 time_ext_t time_ext_get(void)
00298 {
00299 time_ext_t t;
00300 t.nano = time_ext_get_ns();
00301 t.sec = time_ext_get_s();
00302 if(t.nano >= NANO_PER_S) {
00303 t.sec++;
00304 t.nano -= NANO_PER_S;
00305 }
00306 return t;
00307 }
00308
00309
00310
00311
00312
00313
00314 inline void time_ext_set(uint32_t sec, uint32_t nano)
00315 {
00316 TCNT2 = 0;
00317 g_time.nano = nano;
00318 g_time.sec = sec;
00319 NOTICE(E_TIME_EXT, "Time set to %ld %ld", sec, nano);
00320 }
00321
00322
00323
00324
00325
00326
00327