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 #include "board.h"
00040 #include <assert.h>
00041
00042
00043
00044 #define MAX_TIMER 4
00045
00046 static volatile uint32_t _dwTickCount = 0 ;
00047 static uint8_t SysTickConfigured = 0 ;
00048
00049 static volatile uint32_t _dwTickTimer = 0 ;
00050 static TimeEvent *pTimeEventList = 0;
00051
00052 SyTickDelayCounter_t DelayTimer;
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 void SysTick_Handler( void )
00066 {
00067 TimeEvent *pEvent;
00068 pEvent = pTimeEventList;
00069 _dwTickCount ++;
00070 if(_dwTickTimer)
00071 _dwTickTimer --;
00072 while(pEvent) {
00073 if(pEvent->time_start && pEvent->occur == 0) {
00074 pEvent->time_tick--;
00075 if(pEvent->time_tick == 0) {
00076 pEvent->time_start = 0;
00077 pEvent->occur = 1;
00078 }
00079 }
00080 pEvent = pEvent->pNextEvent;
00081 }
00082 }
00083
00084 void SetTimeEvent(TimeEvent* pEvent)
00085 {
00086 pTimeEventList = pEvent;
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 uint32_t TimeTick_Configure( void )
00096 {
00097 uint8_t Mdiv_Val;
00098 uint32_t Pck;
00099 _dwTickCount = 0 ;
00100
00101 TRACE_INFO( "Configure system tick to get 1ms tick period.\n\r" ) ;
00102
00103 Mdiv_Val = ( (PMC->PMC_MCKR & PMC_MCKR_MDIV_Msk) >> PMC_MCKR_MDIV_Pos);
00104
00105 if(Mdiv_Val == 0) {
00106 Pck = BOARD_MCK;
00107 } else if(Mdiv_Val == 3 ) {
00108 Pck = BOARD_MCK * Mdiv_Val;
00109 } else {
00110 Pck = BOARD_MCK * (Mdiv_Val*2);
00111 }
00112
00113 DelayTimer.pTimer1 = NULL; DelayTimer.pTimer1=NULL;
00114
00115 if ( SysTick_Config( Pck/1000 ) ) {
00116 TRACE_ERROR("SysTick configuration error\n\r" ) ;
00117 SysTickConfigured = 0;
00118 return 1;
00119 }
00120 SysTickConfigured = 1;
00121 return 0;
00122 }
00123
00124
00125
00126
00127
00128
00129 uint32_t GetDelayInTicks(uint32_t startTick, uint32_t endTick)
00130 {
00131 assert(SysTickConfigured);
00132
00133 if (endTick >= startTick) return (endTick - startTick);
00134 return (endTick + (0xFFFFFFFF - startTick) + 1);
00135
00136 }
00137
00138
00139
00140
00141
00142
00143 uint32_t GetTicks(void)
00144 {
00145 assert(SysTickConfigured);
00146
00147 return _dwTickCount;
00148 }
00149
00150
00151
00152
00153
00154 void Wait( volatile uint32_t dwMs )
00155 {
00156 uint32_t dwStart , dwEnd;
00157
00158 assert(SysTickConfigured);
00159
00160 dwStart = _dwTickCount ;
00161 dwEnd = _dwTickCount;
00162 while(GetDelayInTicks(dwStart, dwEnd) < dwMs ){
00163 dwEnd = _dwTickCount;
00164 }
00165 }
00166
00167
00168
00169
00170
00171 void Sleep( volatile uint32_t dwMs )
00172 {
00173 uint32_t dwStart , dwEnd;
00174
00175 assert(SysTickConfigured);
00176
00177 __ASM("CPSIE I");
00178 dwStart = _dwTickCount ;
00179 dwEnd = _dwTickCount;
00180 do {
00181 if (GetDelayInTicks(dwStart, dwEnd) < dwMs ) {
00182 break ;
00183 }
00184 dwEnd = _dwTickCount;
00185 __ASM("WFI");
00186 } while( 1 ) ;
00187 }