void DAS(void){ unsigned long input; unsigned static long LastTime; // time at previous ADC sample unsigned long thisTime; // time at current ADC sample long jitter; // time between measured and expected, in us if(NumSamples < RUNLENGTH){ // finite time run PE0 ^= 0x01; input = ADC_In(); // channel set when calling ADC_Init PE0 ^= 0x01; thisTime = OS_Time(); // current time, 12.5 ns DASoutput = Filter(input); FilterWork++; // calculation finished if(FilterWork>1){ // ignore timing of first interrupt unsigned long diff = OS_TimeDifference(LastTime,thisTime); if(diff>PERIOD){ jitter = (diff-PERIOD+4)/8; // in 0.1 usec }else{ jitter = (PERIOD-diff+4)/8; // in 0.1 usec } if(jitter > MaxJitter){ MaxJitter = jitter; // in usec } // jitter should be 0 if(jitter >= JitterSize){ jitter = JITTERSIZE-1; } JitterHistogram[jitter]++; } LastTime = thisTime; PE0 ^= 0x01; } }
void DAS(void){ int index; unsigned short input; unsigned static long LastTime; // time at previous ADC sample unsigned long thisTime; // time at current ADC sample long jitter; // time between measured and expected if(NumSamples < RUNLENGTH){ // finite time run input = ADC_In(1); thisTime = OS_Time(); // current time, 20 ns DASoutput = Filter(input); FilterWork++; // calculation finished if(FilterWork>1){ // ignore timing of first interrupt jitter = OS_TimeDifference(LastTime,thisTime) - 100000/50; // in usec if(jitter > MaxJitter){ MaxJitter = jitter; } if(jitter < MinJitter){ MinJitter = jitter; } // jitter should be 0 index = jitter+JITTERSIZE/2; // us units if(index<0)index = 0; if(index>=JitterSize) index = JITTERSIZE-1; JitterHistogram[index]++; } LastTime = thisTime; } }
// ******** Jitter2 *************** // records the jitter for the second periodic background thread void Jitter2(void) { int index; unsigned static long LastTime; static char First = TRUE; unsigned long thisTime; long jitter; thisTime = OS_Time(); if(First == FALSE) { jitter = (OS_TimeDifference(thisTime, LastTime) / 10) - (gThread2Period / 50); // in usec if(jitter > MaxJitter2) { MaxJitter2 = jitter; } if(jitter < MinJitter2) { MinJitter2 = jitter; } index = jitter + JITTERSIZE / 2; // us units if(index < 0) index = 0; if(index >= JitterSize) index = JITTERSIZE - 1; JitterHistogram2[index]++; } else { First = FALSE; } LastTime = thisTime; }
int main(void) { unsigned int id; unsigned long time; id = OS_Id(); PF2 ^= 0x04; Display_Message(0,line++, "Hello world: ", id); OS_AddThread(thread, 128, 1); time = OS_Time(); OS_Sleep(1000); time = (((OS_TimeDifference(time, OS_Time()))/1000ul)*125ul)/10000ul; Display_Message(0,line++, "Sleep time: ", time); PF2 ^= 0x04; OS_Kill(); }
void JitterFinish(long thisTime){ if(LastTime!=0){ jitter = OS_TimeDifference(thisTime,LastTime)/50-PERIOD/50; // in usec if(jitter > MaxJitter){ MaxJitter = jitter; } if(jitter < MinJitter){ MinJitter = jitter; } // jitter should be 0 jindex = jitter+JITTERSIZE/2; // us units if(jindex<0)jindex = 0; if(jindex>=JitterSize) jindex = JITTERSIZE-1; JitterHistogram[jindex]++; } LastTime = thisTime; }
// ******* OS_UpdateTimeInfoOnDisable ********** // Used in OS_DisableInterrupts and OS_StartCritical // Used to keep track of how much time is spent when interrupts are enabled. void OS_UpdateTimeInfoOnDisable(void) { if (continueMeasuring && OS_IsRunning) { unsigned long thisTime = OS_Time(); if (interruptsEnabled) { // interrupts were enabled. Calculate time spent in disabled mode if (lastEnableTime != 0) { if (thisTime >= lastEnableTime) { unsigned long diff = OS_TimeDifference(thisTime, lastEnableTime); if (enableTime > UINT32_T_MAX*80000 - diff) { continueMeasuring = 0; } // overflowed enableTime += diff; } // else there was an overflow in the timer } // else this was the first time disabling interrupts. Don't calculate anything //lastDisableTime = thisTime; } lastDisableTime = thisTime; } interruptsEnabled = 0; }
static void pingHandler(ping_config_t* ping) { int peripheral = ping->peripheral; int portBase = ping->portBase; int pin = ping->pin; int pulseStarted = ping->pulseStarted; UINT reload = OS_RawTimerReload(); GPIOPinIntClear(portBase, pin); // this is the rising edge if(!pulseStarted && GPIOPinRead(portBase, pin)) { ping->pulseStartTime = reload - OS_RawTimerTime(); ping->pulseStarted = TRUE; } else if(pulseStarted) { ping->pulseStarted = FALSE; ping->pulseEndTime = reload - OS_RawTimerTime(); GPIOPinIntDisable(portBase, pin); ping->sampleHandler(OS_TimeDifference(ping->pulseEndTime, ping->pulseStartTime, reload)); } }
// ******* OS_UpdateTimeInfoOnEnable ********** // Used in OS_EnableInterrupts and OS_EndCritical // Used to keep track of how much time is spent when interrupts are disabled. void OS_UpdateTimeInfoOnEnable(void) { if (continueMeasuring && OS_IsRunning) { unsigned long thisTime = OS_Time(); // this is called when interrupts are going to be enabled // keep track of disabled time if (!interruptsEnabled) { // interrupts were disabled, calculate time spent in disabled mode if (lastDisableTime != 0) { if (thisTime >= lastDisableTime) { unsigned long diff = OS_TimeDifference(thisTime, lastDisableTime); if (disableTime > UINT32_T_MAX*80000 - diff) { continueMeasuring = 0; } disableTime += diff; if (diff > maxDisableTime) { maxDisableTime = diff; } } // else there was an overflow in the timer } // else this is the first time enabling interrupts. Don't calculate anything //lastEnableTime = thisTime; } // else do nothing lastEnableTime = thisTime; } interruptsEnabled = 1; }
//*******PseudoWork************* // simple time delay, simulates user program doing real work // Input: amount of work in 100ns units (free free to change units // Output: none void PseudoWork(unsigned short work){ unsigned short startTime; startTime = OS_Time(); // time in 100ns units while(OS_TimeDifference(startTime,OS_Time()) <= work){} }