system-timer.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 #include PLATFORM_HEADER
00018 #include "error.h"
00019 #include "hal/micro/micro-common.h"
00020 #include "hal/micro/cortexm3/micro-common.h"
00021 #include "micro/system-timer.h"
00022
00023
00024
00025
00026 static boolean sleepTimerInterruptOccurred = FALSE;
00027
00028
00029
00030
00031
00032
00033
00034
00035 int16u halCommonGetInt16uMillisecondTick(void)
00036 {
00037 return (int16u)halCommonGetInt32uMillisecondTick();
00038 }
00039
00040 int16u halCommonGetInt16uQuarterSecondTick(void)
00041 {
00042 return (int16u)(halCommonGetInt32uMillisecondTick() >> 8);
00043 }
00044
00045
00046
00047
00048
00049
00050 int32u halCommonGetInt32uMillisecondTick(void)
00051 {
00052 int32u time;
00053
00054 time = SLEEPTMR_CNTH<<16;
00055 time |= SLEEPTMR_CNTL;
00056
00057 return time;
00058 }
00059
00060
00061 void halSleepTimerIsr(void)
00062 {
00063
00064 INT_SLEEPTMRFLAG = INT_SLEEPTMRWRAP | INT_SLEEPTMRCMPA | INT_SLEEPTMRCMPB;
00065
00066
00067 sleepTimerInterruptOccurred = TRUE;
00068 }
00069
00070 #define CONVERT_QS_TO_TICKS(x) (x << 8)
00071 #define CONVERT_TICKS_TO_QS(x) (x >> 8)
00072 #define TIMER_MAX_QS 0x1000000 // = 4194304 seconds * 4 = 16777216
00073 static StStatus internalSleepForQs(boolean useGpioWakeMask,
00074 int32u *duration,
00075 int32u gpioWakeBitMask)
00076 {
00077 StStatus status = ST_SUCCESS;
00078 int32u sleepOverflowCount;
00079 int32u remainder;
00080 int32u startCount;
00081
00082
00083 if(*duration==0) {
00084 INTERRUPTS_ON();
00085 return status;
00086 }
00087
00088 ATOMIC(
00089
00090 INT_CFGCLR = INT_SLEEPTMR;
00091
00092
00093
00094
00095 sleepOverflowCount = (*duration)/TIMER_MAX_QS;
00096
00097 remainder = CONVERT_QS_TO_TICKS((*duration)%TIMER_MAX_QS);
00098
00099 startCount = halCommonGetInt32uMillisecondTick();
00100
00101 sleepTimerInterruptOccurred = FALSE;
00102
00103 if(remainder) {
00104
00105 SLEEPTMR_CMPAL = (startCount+remainder)&0xFFFF;
00106 SLEEPTMR_CMPAH = ((startCount+remainder)>>16)&0xFFFF;
00107
00108 INT_SLEEPTMRFLAG = INT_SLEEPTMRCMPA;
00109 INT_SLEEPTMRCFG = INT_SLEEPTMRCMPA;
00110 }
00111 if(sleepOverflowCount) {
00112
00113 SLEEPTMR_CMPBL = startCount&0xFFFF;
00114 SLEEPTMR_CMPBH = (startCount>>16)&0xFFFF;
00115
00116
00117
00118 INT_SLEEPTMRFLAG = INT_SLEEPTMRCMPB;
00119 INT_SLEEPTMRCFG = INT_SLEEPTMRCMPB;
00120 }
00121
00122
00123 INT_CFGSET = INT_SLEEPTMR;
00124 )
00125
00126 while(*duration > 0) {
00127 {
00128 halSleepWithOptions(SLEEPMODE_WAKETIMER, gpioWakeBitMask);
00129 }
00130
00131 INT_SLEEPTMRCFG = INT_SLEEPTMRCFG_RESET;
00132
00133
00134
00135 if(!sleepTimerInterruptOccurred) {
00136 status = ST_SLEEP_INTERRUPTED;
00137
00138
00139
00140
00141
00142 *duration -= CONVERT_TICKS_TO_QS(halCommonGetInt32uMillisecondTick() -
00143 startCount);
00144 break;
00145 } else {
00146 if(sleepOverflowCount) {
00147 sleepOverflowCount--;
00148 *duration -= TIMER_MAX_QS;
00149 } else {
00150 *duration -= CONVERT_TICKS_TO_QS(remainder);
00151 }
00152 sleepTimerInterruptOccurred = FALSE;
00153 if(sleepOverflowCount) {
00154
00155 INT_SLEEPTMRFLAG = INT_SLEEPTMRCMPB;
00156 INT_SLEEPTMRCFG = INT_SLEEPTMRCMPB;
00157 } else if(!sleepOverflowCount && (*duration>0)){
00158
00159 INT_SLEEPTMRFLAG = INT_SLEEPTMRCMPA;
00160 INT_SLEEPTMRCFG = INT_SLEEPTMRCMPA;
00161 }
00162 }
00163 }
00164
00165 return status;
00166 }
00167
00168 StStatus halSleepForQsWithOptions(int32u *duration, int32u gpioWakeBitMask)
00169 {
00170 return internalSleepForQs(TRUE, duration, gpioWakeBitMask);
00171 }
00172