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