ostimer_lpc2xxx.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include <cfg/arch.h>
00045 #include <arch/arm/lpc2xxx.h>
00046 #include <dev/irqreg.h>
00047
00048 #ifndef NUT_TICK_FREQ
00049 #define NUT_TICK_FREQ 1000UL
00050 #endif
00051
00052
00053
00054
00055
00056 #define CLK_IN 14745600UL
00057
00058
00059
00060
00061 typedef void (*PFNCT)(void);
00062 static PFNCT NutOSTimerTickHandler = NULL;
00063
00064
00065
00066
00067 static uint32_t GetPeripheralClk (void)
00068 {
00069 uint32_t msel;
00070 volatile uint32_t vpbdiv;
00071 uint32_t CPUClkFreq;
00072 uint32_t PeripheralClkFreq;
00073
00074 msel = (uint32_t)(PLLCFG & 0x1F);
00075 CPUClkFreq = CLK_IN * (msel + 1);
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 vpbdiv = (uint32_t)(VPBDIV & 0x03);
00086 vpbdiv = (uint32_t)(VPBDIV & 0x03);
00087 switch (vpbdiv) {
00088 case 0: PeripheralClkFreq = CPUClkFreq / 4; break;
00089 case 1: PeripheralClkFreq = CPUClkFreq; break;
00090 case 2: PeripheralClkFreq = CPUClkFreq / 2; break;
00091 default: PeripheralClkFreq = CPUClkFreq / 4; break;
00092 }
00093
00094 return (PeripheralClkFreq);
00095 }
00096
00097
00098
00099
00100 static void TimerTickISRHandler (void)
00101 {
00102
00103
00104
00105 T0IR = 0xFF;
00106
00107 if (NutOSTimerTickHandler != NULL) {
00108 (*NutOSTimerTickHandler)();
00109 }
00110
00111
00112
00113
00114 VICVectAddr = 0;
00115 }
00116
00128 void NutRegisterTimer(void (*handler) (void *))
00129 {
00130
00131
00132
00133 NutOSTimerTickHandler = (PFNCT)handler;
00134
00135
00136
00137
00138 T0TCR = 0;
00139
00140
00141
00142
00143 T0PC = 0;
00144
00145
00146
00147
00148 T0MR0 = (GetPeripheralClk() / NUT_TICK_FREQ);
00149
00150
00151
00152
00153
00154 T0MCR = 3;
00155
00156
00157
00158
00159 T0CCR = 0;
00160
00161
00162
00163
00164 T0EMR = 0;
00165
00166
00167
00168
00169 T0TCR = 1;
00170
00171
00172
00173
00174 VICVectAddr2 = (uint32_t)TimerTickISRHandler;
00175 VICVectCntl2 = 0x20 | VIC_TIMER0;
00176 }
00177
00178
00184 uint32_t NutArchClockGet(int idx)
00185 {
00186 uint32_t msel;
00187 uint32_t CPUClkFreq;
00188
00189 msel = (uint32_t)(PLLCFG & 0x1F);
00190 CPUClkFreq = CLK_IN * (msel + 1);
00191
00192 return CPUClkFreq;
00193 }
00194
00200 uint32_t NutGetTickClock(void)
00201 {
00202 return NUT_TICK_FREQ;
00203 }
00204
00208 uint32_t NutTimerMillisToTicks(uint32_t ms)
00209 {
00210 #if (NUT_TICK_FREQ % 1000)
00211 if (ms >= 0x3E8000UL)
00212 return (ms / 1000UL) * NUT_TICK_FREQ;
00213 return (ms * NUT_TICK_FREQ + 999UL) / 1000UL;
00214 #else
00215 return ms * (NUT_TICK_FREQ / 1000UL);
00216 #endif
00217 }