00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00018 #include "sceptre.h"
00019
00020
00021 volatile uint32_t timer0_counter = 0;
00022 volatile uint32_t timer1_counter = 0;
00023 static uint8_t timer_10ms_prescaler = 10;
00024 static uint32_t timer_10ms_counter = 0;
00025
00026 timer_10ms_hook_t timer_10ms_hook = NULL;
00027 static void timer_tick_10ms(void);
00028
00029
00034 static void timer_tick_10ms(void)
00035 {
00036 timer_10ms_prescaler -= 1;
00037 if (timer_10ms_prescaler==0)
00038 {
00039 timer_10ms_prescaler = 10;
00040 timer_10ms_counter += 1;
00041 if (timer_10ms_hook!=NULL) timer_10ms_hook();
00042 }
00043 }
00044
00045
00050 static void timer0_handler(void) __irq
00051 {
00052 T0IR = 1;
00053 IENABLE;
00054
00055 #if SYSTEM_TIMER==TIMER_ZERO
00056 timer_tick_10ms();
00057 #endif
00058
00059 timer0_counter++;
00060
00061 IDISABLE;
00062 VICVectAddr = 0;
00063 }
00064
00065
00070 static void timer1_handler(void) __irq
00071 {
00072 T1IR = 1;
00073 IENABLE;
00074
00075 #if SYSTEM_TIMER==TIMER_ONE
00076 timer_tick_10ms();
00077 #endif
00078
00079 timer1_counter++;
00080
00081 IDISABLE;
00082 VICVectAddr = 0;
00083 }
00084
00085
00091 void timer_enable(uint8_t timer_num)
00092 {
00093 if (timer_num==TIMER_ZERO)
00094 {
00095 T0TCR = 1;
00096 }
00097 else
00098 {
00099 T1TCR = 1;
00100 }
00101 }
00102
00103
00109 void timer_disable(uint8_t timer_num)
00110 {
00111 if (timer_num==TIMER_ZERO)
00112 {
00113 T0TCR = 0;
00114 }
00115 else
00116 {
00117 T1TCR = 0;
00118 }
00119 }
00120
00121
00127 void timer_reset(uint8_t timer_num)
00128 {
00129 uint32_t regVal;
00130
00131 if (timer_num==TIMER_ZERO)
00132 {
00133 regVal = T0TCR;
00134 regVal |= 0x02;
00135 T0TCR = regVal;
00136 }
00137 else
00138 {
00139 regVal = T1TCR;
00140 regVal |= 0x02;
00141 T1TCR = regVal;
00142 }
00143 return;
00144 }
00145
00146
00154 bool_t timer_init(uint8_t timer_num, uint32_t interval)
00155 {
00156 if (timer_num==SYSTEM_TIMER)
00157 {
00158 timer_10ms_prescaler = 10;
00159 timer_10ms_counter = 0;
00160 }
00161
00162 if (timer_num==TIMER_ZERO)
00163 {
00164 timer0_counter = 0;
00165 T0MR0 = interval;
00166 T0MCR = 3;
00167 return irq_install(TIMER0_INT,(void *)timer0_handler);
00168 }
00169 else
00170 {
00171 timer1_counter = 0;
00172 T1MR0 = interval;
00173 T1MCR = 3;
00174 return irq_install(TIMER1_INT,(void *)timer1_handler);
00175 }
00176 }
00177
00178
00184 uint32_t timer_get_count(uint8_t timer_num)
00185 {
00186 if (timer_num==TIMER_ZERO) return timer0_counter;
00187 else return timer1_counter;
00188 }
00189
00190
00197 void timer_set_count(uint8_t timer_num, uint32_t val)
00198 {
00199 if (timer_num==TIMER_ZERO) timer0_counter = val;
00200 else timer1_counter = val;
00201 }
00202
00203
00209 inline uint32_t timer_get_system_count(void)
00210 {
00211 #if SYSTEM_TIMER==TIMER_ZERO
00212 return timer0_counter;
00213 #elif SYSTEM_TIMER==TIMER_ONE
00214 return timer1_counter;
00215 #endif
00216 }
00217
00218
00224 inline void timer_set_system_count(uint32_t val)
00225 {
00226 #if SYSTEM_TIMER==TIMER_ZERO
00227 timer0_counter = val;
00228 #elif SYSTEM_TIMER==TIMER_ONE
00229 timer1_counter = val;
00230 #endif
00231 }
00232
00233
00239 inline uint32_t timer_get_10ms_counter(void)
00240 {
00241 return timer_10ms_counter;
00242 }
00243
00244
00250 void timer_set_10ms_hook(timer_10ms_hook_t f)
00251 {
00252 timer_10ms_hook = f;
00253 }
00254
00255
00261 void timer_wait_ms(uint32_t delay_ms)
00262 {
00263 uint32_t last_count = timer_get_system_count();
00264 while (timer_get_system_count()<last_count+delay_ms);
00265 }