00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "at91sam7s64.h"
00024
00025 #include "processor.h"
00026 #include "timer.h"
00027
00028 #include "global.h"
00029
00030 typedef void (*voidFuncPtr)(void);
00031 volatile static voidFuncPtr TimerIntFunc[TIMER_NUM_INTERRUPTS];
00032
00033
00034 volatile DWORD TimerPauseReg;
00035 volatile DWORD PitCounter;
00036
00037
00038
00039 void delay_us(unsigned long t)
00040 {
00041
00042 while(t--)
00043 {
00044 asm volatile ("nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n");
00045 asm volatile ("nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n");
00046 asm volatile ("nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n");
00047 asm volatile ("nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n");
00048 asm volatile ("nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n");
00049 asm volatile ("nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n nop\r\n");
00050 }
00051 }
00052
00053 void timerInit(void)
00054 {
00055 int i;
00056
00057 for(i=0; i<TIMER_NUM_INTERRUPTS; i++)
00058 timerAttach(i,0);
00059
00060
00061
00062
00063
00064
00065 timerInitPit(1000);
00066 }
00067
00068 void timerInitPit(int rate)
00069 {
00070
00071
00072
00073
00074
00075 AT91C_BASE_PITC->PITC_PIMR = AT91C_PITC_PITEN|AT91C_PITC_PITIEN|(F_CPU/(16*rate)-1);
00076
00077 AT91C_BASE_PITC->PITC_PIVR;
00078
00079
00080 TimerPauseReg = 0;
00081 PitCounter = 0;
00082
00083
00084 processorAicAttachSys(SYSPID_PITC, timerServicePit);
00085 }
00086
00087 void timerAttach(u08 interruptNum, void (*userFunc)(void) )
00088 {
00089
00090 if(interruptNum < TIMER_NUM_INTERRUPTS)
00091 {
00092
00093
00094 TimerIntFunc[interruptNum] = userFunc;
00095 }
00096 }
00097
00098 void timerPause(int pause)
00099 {
00100
00101 TimerPauseReg = pause;
00102 while(TimerPauseReg);
00103 }
00104
00105
00106
00107
00108
00109 void timerServicePit(void)
00110 {
00111
00112
00113 PitCounter+=AT91C_BASE_PITC->PITC_PIVR>>20;
00114
00115
00116 if(TimerPauseReg)
00117 TimerPauseReg--;
00118
00119
00120 if(TimerIntFunc[TIMER_PITOVERFLOW_INT])
00121 TimerIntFunc[TIMER_PITOVERFLOW_INT]();
00122 }
00123