void LCDWrite(unsigned char inputData) { int tempo; GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, (inputData & 0xf0) ); GPIOPinWrite(LCDPORT, RS, 0x01); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, (inputData & 0x0f) << 4 ); GPIOPinWrite(LCDPORT, RS, 0x01); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); }
void LCDCommand(unsigned char command) { GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, (command & 0xf0) ); GPIOPinWrite(LCDPORT, RS, 0x00); GPIOPinWrite(LCDPORT, E, 0x02); int tempo; tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, (command & 0x0f) << 4 ); GPIOPinWrite(LCDPORT, RS, 0x00); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); }
/* * ======== Adaptor_setPeriod ======== */ Void Adaptor_setPeriod(Int id, UInt32 periodInMs) { UInt key; UInt adjPeriod; /* * Adjust as need: * - 0 stays 0 (not going to send events) * - less than this module's period, set to this module's period * - make sure it is a multiple of this module's period */ if (periodInMs == 0) { adjPeriod = periodInMs; } else if (periodInMs < ServiceMgr_periodInMs) { adjPeriod = ServiceMgr_periodInMs; } else { adjPeriod = (periodInMs / ServiceMgr_periodInMs) * ServiceMgr_periodInMs; } /* Must protect against the Adaptor Thread */ key = Hwi_disable(); /* Set the new period and scheduled time */ Adaptor_period[id] = adjPeriod * 1000 / Clock_tickPeriod; Adaptor_scheduled[id] = Clock_getTicks() + Adaptor_period[id]; /* Leave the gate */ Hwi_restore(key); }
/* * ======== clk0Fxn ======= */ Void clk0Fxn(UArg arg0) { UInt32 time; time = Clock_getTicks(); System_printf("System time in clk0Fxn = %lu\n", (ULong)time); }
/* * ======== TimestampProvider_get32 ======== * The 32-bit timestamp can be retrieved more efficiently than the 64-bit one, * so it has a different implementation. */ Bits32 TimestampProvider_get32() { /* * If we're sharing the Clock timer, get the timestamp by using the Clock * tick count. */ if (TimestampProvider_useClockTimer) { UInt key; UInt32 timestamp; /* * Disable interrupts so that the Clock tick count doesn't change if * the timer expires while we are reading it. */ key = Hwi_disable(); /* * timestamp = (clock ticks) x (tick period) + (current timer count) * The 'getExpiredCounts' API retrieves the current Timer count and * also accounts for timer rollover. */ timestamp = Clock_getTicks() * Timer_getPeriod(MOD->timer) + Timer_getExpiredCounts(MOD->timer); Hwi_restore(key); return (timestamp); } /* If we have a dedicated timer, just read the timer count. */ else { return (Timer_getCount(MOD->timer)); } }
/* * ======== clk1Fxn ======= */ Void clk1Fxn(UArg arg0) { UInt32 time; time = Clock_getTicks(); System_printf("System time in clk1Fxn = %lu\n", (ULong)time); System_printf("Calling BIOS_exit() from clk1Fxn\n"); BIOS_exit(0); }
/* * ======== Clock_getTimeout ======== */ UInt32 Clock_getTimeout(Clock_Object *obj) { if (obj->active == TRUE) { return (obj->currTimeout - Clock_getTicks()); } else { return (obj->timeout); } }
systime_t osGetSystemTime(void) { systime_t time; //Get current tick count time = Clock_getTicks(); //Convert system ticks to milliseconds return OS_SYSTICKS_TO_MS(time); }
/********************************************************************* * @fn SimpleBLEBroadcaster_processEvent * * @brief Application task entry point for the Simple BLE Broadcaster. * * @param none * * @return none */ static void SimpleBLEBroadcaster_taskFxn(UArg a0, UArg a1) { // Initialize application SimpleBLEBroadcaster_init(); // Application main loop for (;;) { // Get the ticks since startup uint32_t tickStart = Clock_getTicks(); // Waits for a signal to the semaphore associated with the calling thread. // Note that the semaphore associated with a thread is signaled when a // message is queued to the message receive queue of the thread or when // ICall_signal() function is called onto the semaphore. ICall_Errno errno = ICall_wait(ICALL_TIMEOUT_FOREVER); if (errno == ICALL_ERRNO_SUCCESS) { ICall_EntityID dest; ICall_ServiceEnum src; ICall_HciExtEvt *pMsg = NULL; if (ICall_fetchServiceMsg(&src, &dest, (void **)&pMsg) == ICALL_ERRNO_SUCCESS) { if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity)) { // Process inter-task message SimpleBLEBroadcaster_processStackMsg((ICall_Hdr *)pMsg); } if (pMsg) { ICall_freeMsg(pMsg); } } // If RTOS queue is not empty, process app message. while (!Queue_empty(appMsgQueue)) { sbbEvt_t *pMsg = (sbbEvt_t *)Util_dequeueMsg(appMsgQueue); if (pMsg) { // Process message. SimpleBLEBroadcaster_processAppMsg(pMsg); // Free the space from the message. ICall_free(pMsg); } } } } }
/*! * ======== VirtQueue_cacheWb ======== * * Used for flushing SysMin trace buffer. */ Void VirtQueue_cacheWb() { static UInt32 oldticks; if (Clock_getTicks() >= (oldticks + CACHE_WB_TICK_PERIOD)) { /* Don't keep flushing cache */ return; } /* Flush the cache */ Cache_wbAll(); }
/* * ======== idl0Fxn ======== */ Void idl0Fxn() { /* check for command input from serial port */ if (cmd != 0) { switch (cmd) { /* toggle automatic status dumping */ case 's': if (statusEnabled == TRUE) { statusEnabled = FALSE; System_printf("Status reporting disabled.\n"); } else { statusEnabled = TRUE; System_printf("Status reporting enabled.\n"); } break; default: break; } cmd = 0; } /* dump counts every 100 Clock ticks */ if ((statusEnabled == TRUE) && Clock_getTicks() && (Clock_getTicks() % 100 == 0)) { System_printf("status:\n\tswi0Count: %u, swi1Count: %u\n", swi0Count, swi1Count); System_printf("\tclk0Count: %u, clk1Count: %u\n", clk0Count, clk1Count); } /* * Flush characters in SysMin buffer */ System_flush(); }
/*! * ======== VirtQueue_cacheWb ======== * * Used for flushing SysMin trace buffer. */ Void VirtQueue_cacheWb() { static UInt32 oldticks = 0; UInt32 newticks; newticks = Clock_getTicks(); if (newticks - oldticks < (UInt32)CACHE_WB_TICK_PERIOD) { /* Don't keep flushing cache */ return; } oldticks = newticks; /* Flush the cache */ Cache_wbAll(); }
/* * ======== VirtQueue_cacheWb ======== * * Used for flushing SysMin trace buffer. */ Void traceBuf_cacheWb() { static UInt32 oldticks = 0; UInt32 newticks; newticks = Clock_getTicks(); if (newticks - oldticks < (UInt32)CACHE_WB_TICK_PERIOD) { /* Don't keep flushing cache */ return; } oldticks = newticks; Cache_wbAll(); }
/* * ======== TimestampProvider_get32 ======== */ Bits32 TimestampProvider_get32() { if (TimestampProvider_useClockTimer) { UInt key; UInt32 timestamp; key = Hwi_disable(); timestamp = Clock_getTicks() * Timer_getPeriod(MOD->timer) + Timer_getExpiredCounts(MOD->timer); Hwi_restore(key); return (timestamp); } else { return (Timer_getCount(MOD->timer)); } }
/* * ======== TimestampProvider_get64 ======== */ Void TimestampProvider_get64(Types_Timestamp64 *result) { UInt key; UInt64 timestamp; key = Hwi_disable(); if (TimestampProvider_useClockTimer) { timestamp = Clock_getTicks() * Timer_getPeriod(MOD->timer) + Timer_getExpiredCounts(MOD->timer); } else { timestamp = ((UInt64)MOD->hi << 32) + Timer_getExpiredCounts(MOD->timer); } Hwi_restore(key); result->hi = timestamp >> 32; result->lo = timestamp; }
/* * ======== Adaptor_runScheduledServices ======== */ Void Adaptor_runScheduledServices(Void) { UInt id; UInt key; UInt32 currentTime; /* Get the current time to determine who should be called */ currentTime = Clock_getTicks(); /* Query each service */ for (id = 0; id < ServiceMgr_getNumServices(); id++) { /* To protect against a change via Adaptor_setPeriod */ key = Hwi_disable(); /* * The service must have a non-zero period to be processed. * If scheduled time has past, call the process function. * Also handle the case where the clock wraps. */ if ((Adaptor_period[id] != 0) && EXPIRED(Adaptor_scheduled[id], currentTime)) { /* Set the new scheduled time */ Adaptor_scheduled[id] = currentTime + Adaptor_period[id]; Hwi_restore(key); /* Call the service's function to let it send events */ ServiceMgr_processCallback(id, ServiceMgr_Reason_PERIODEXPIRED, NULL); } else { Hwi_restore(key); } } }
/* * ======== Power_notify ======== * * Note: when this function is called, Swi and Task scheduling are disabled, * but interrupts are enabled. */ Power_Status Power_notify(Power_Event eventType, UInt timeout, Power_SigType sigType, UArg extArg1, UArg extArg2) { Power_NotifyResponse clientStatus; UInt32 notifyStartTime; Queue_Handle notifyQ; Queue_Elem * elem; Arg eventArg1; Arg eventArg2; UInt clients = 0; Fxn notifyFxn; Arg clientArg; UInt key; /* determine the appropriate notification queue */ notifyQ = (Queue_Handle)((UInt8 *)(Power_module->notifyQ) + (UInt)(eventType * (2 * sizeof(Ptr)))); /* if queue is empty, return immediately */ if (Queue_empty(notifyQ)) { return (Power_SOK); } /* reset the count of clients doing delayed completion */ ti_sysbios_family_c674_Power_notifyWaitCount[(UInt)eventType] = 0; /* grab notification start time (# ticks) */ notifyStartTime = Clock_getTicks(); /* point to first client notify object */ elem = Queue_head(notifyQ); /* walk the queue and notify each registered client of the event */ do { clients++; /* count each registered client being notified */ /* pull params from notify object */ notifyFxn = ((Power_NotifyObj *)elem)->notifyFxn; clientArg = ((Power_NotifyObj *)elem)->clientArg; /* determine the event arguments... */ /* if event triggered internally then Power determines event args: */ if (sigType == Power_SigType_INTERNAL) { if (eventType == Power_PENDING_CPU_SETPOINTCHANGE) { eventArg1 = (Arg) Power_module->currentSetpointCPU; eventArg2 = (Arg) Power_module->nextSP; } else if (eventType == Power_DONE_CPU_SETPOINTCHANGE) { eventArg1 = (Arg) Power_module->previousSP; eventArg2 = (Arg) Power_module->currentSetpointCPU; } else if (eventType == Power_PENDING_PER_SETPOINTCHANGE) { eventArg1 = (Arg) Power_module->currentSetpointPER; eventArg2 = (Arg) Power_module->nextSPPER; } else if (eventType == Power_DONE_PER_SETPOINTCHANGE) { eventArg1 = (Arg) Power_module->previousSPPER; eventArg2 = (Arg) Power_module->currentSetpointPER; } else { eventArg1 = NULL; eventArg2 = NULL; } } /* else for externally triggered events use client-specified args: */ else { eventArg1 = (Arg) extArg1; eventArg2 = (Arg) extArg2; } asm(" .global _Power_ntfy"); asm("_Power_ntfy:"); clientStatus = (Power_NotifyResponse) (*(Fxn)notifyFxn)(eventType, eventArg1, eventArg2, clientArg); /* if client said not done, increment count of clients to wait for */ if (clientStatus == Power_NOTIFYNOTDONE) { key = Hwi_disable(); ti_sysbios_family_c674_Power_notifyWaitCount[(UInt)eventType] += 1; Hwi_restore(key); } else if (clientStatus == Power_NOTIFYERROR) { return (Power_EFAIL); } /* get next element in this notify queue */ elem = Queue_next(elem); } while (elem != (Queue_Elem *) notifyQ); /* if no timout and a client said not done, return immediately */ if ((timeout == 0) && (ti_sysbios_family_c674_Power_notifyWaitCount[(UInt)eventType] != 0)) { return (Power_ETIMEOUT); } /* if any client said not done: wait until they signal completion */ while (ti_sysbios_family_c674_Power_notifyWaitCount[(UInt)eventType] != 0) { if ((Clock_getTicks() - notifyStartTime) > timeout) { return (Power_ETIMEOUT); } } return (Power_SOK); }
Int32 AVSYNC_Audio_ISR() { #if 0 void AVSYNC_AudioISR (AVSYNC_INFO *pAVSyncInfo) { U32 delta; U8 i; U8 mediaTimeUpdated = 0; #ifdef LOG_AVSYNC U8 str; U16 idx; #endif static int log_cnt = 0; #ifdef LOG_AVSYNC log_index++; if (log_index >= MAX_STRINGS) log_index = 0; str = log_string[log_index]; sprintf (str, "%lld] AudioISR ", Clock_getTicks() ); idx = strlen (str); str = log_string[log_index] + (idx - 1); #endif if (pAVSyncInfo->clockState.eState == TIME_ClockStateRunning) { S64 mediaTimeInc; U64 oldMediaTime, audioRefTime; /* timeInfo.mediaTime & RefClk might have started with different start times * For skew correction to timeInfo.mediaTime <if required>, compute the difference between RefClk & timeInfo.mediaTime * and add that to timeInfo.mediaTime */ if (pAVSyncInfo->timeInfo.lastWallTimeInTicks > pAVSyncInfo->timeInfo.wallTimeInTicks) pAVSyncInfo->stat.wallClkRollOverCnt++; mediaTimeInc = (pAVSyncInfo->timeInfo.wallTimeInTicks - pAVSyncInfo->timeInfo.lastWallTimeInTicks); pAVSyncInfo->timeInfo.mediaTimeInTicks += mediaTimeInc; mediaTimeInc = (mediaTimeInc / WALL_TIMER_FREQ_MS); #ifdef LOG_AVSYNC sprintf (str, "OrgWall %lld OrgLastWall %lld OrgMedia %lld OrgInc %lld ", pAVSyncInfo->timeInfo.wallTimeInTicks, pAVSyncInfo->timeInfo.lastWallTimeInTicks, pAVSyncInfo->timeInfo.mediaTime, mediaTimeInc); idx = strlen (str); str = log_string[log_index] + (idx - 1); #endif // TODO: Add some intelligence to check this audio PTS is valid */ #ifdef ENABLE_AUDIO_REFCLK_CORRECTION if (pAVSyncInfo->timeInfo.mediaTimeBase[pAVSyncInfo->timeInfo.activeRefClk.strmId] != AVSYNC_INVALID_TIMESTAMP && pAVSyncInfo->timeInfo.activeRefClk.nTimeStamp != AVSYNC_INVALID_TIMESTAMP) { log_cnt++; /* Check Ref Audio stream has started really & do skew correction */ if (pAVSyncInfo->timeInfo.activeRefClk.clkType == TIME_ClkTypeAudio) { if ( (pAVSyncInfo->streamInfo.nAudioStrmActiveMask & (1 << pAVSyncInfo->timeInfo.activeRefClk.strmId)) && (pAVSyncInfo->stat.streamStats[pAVSyncInfo->timeInfo.activeRefClk.strmId].nAudFramesRendered > 0)) { trigger_log = 1; // wait__(); // wait__(); ref_clk_var[ref_clk_debug_idx].orgWallTime = pAVSyncInfo->timeInfo.wallTimeInTicks; ref_clk_var[ref_clk_debug_idx].orgLastWallTime = pAVSyncInfo->timeInfo.lastWallTimeInTicks; ref_clk_var[ref_clk_debug_idx].orgMediaTime = pAVSyncInfo->timeInfo.mediaTime; ref_clk_var[ref_clk_debug_idx].orgMediaInc = mediaTimeInc; ref_clk_var[ref_clk_debug_idx].newMediaInc = 0; ref_clk_var[ref_clk_debug_idx].newMediaTime = 0; ref_clk_var[ref_clk_debug_idx].newWallTime = 0; ref_clk_var[ref_clk_debug_idx].refClk = pAVSyncInfo->timeInfo.activeRefClk.nTimeStamp; ref_clk_var[ref_clk_debug_idx].refClkOffset = pAVSyncInfo->timeInfo.activeRefClk.nOffsetToMediaTime; #ifdef __ti__ ref_clk_var[ref_clk_debug_idx].currentTimeStamp = Clock_getTicks(); #else ref_clk_var[ref_clk_debug_idx].currentTimeStamp = pAVSyncInfo->timeInfo.currentTimeStamp; #endif oldMediaTime = pAVSyncInfo->timeInfo.mediaTime; audioRefTime = pAVSyncInfo->timeInfo.activeRefClk.nTimeStamp + pAVSyncInfo->timeInfo.activeRefClk.nOffsetToMediaTime; delta = (pAVSyncInfo->timeInfo.mediaTime + mediaTimeInc) - audioRefTime + pAVSyncInfo->audioBackendDelay; if (delta > pAVSyncInfo->maxAudioLagTime) { // Update WallClock logWallTimeResetInfo(pAVSyncInfo->timeInfo.wallTimeInTicks, audioRefTime * (WALL_TIMER_FREQ_MS), mediaTimeInc, (pAVSyncInfo->timeInfo.mediaTime - oldMediaTime)); pAVSyncInfo->timeInfo.wallTimeInTicks = audioRefTime * (WALL_TIMER_FREQ_MS); pAVSyncInfo->timeInfo.mediaTime = audioRefTime; mediaTimeInc = (pAVSyncInfo->timeInfo.mediaTime - oldMediaTime); // Important. After refclk correction, reset refclk to INVALID_TIMESTAMP. // Required - If refclk <audio> update at audio rendering end doesnt at regular intervals, mediastamp will get reset to a older // refClk value several times within the next refclk update period at audio renderer. pAVSyncInfo->timeInfo.activeRefClk.nTimeStamp = AVSYNC_INVALID_TIMESTAMP; ref_clk_var[ref_clk_debug_idx].newMediaInc = mediaTimeInc; ref_clk_var[ref_clk_debug_idx].newMediaTime = pAVSyncInfo->timeInfo.mediaTime; ref_clk_var[ref_clk_debug_idx].newWallTime = pAVSyncInfo->timeInfo.wallTimeInTicks; #ifdef LOG_AVSYNC sprintf (str, "delta %lld newInc %lld refClk %lld refClkOffset %lld newMedia %lld newWallTime %lld\n", delta, mediaTimeInc, pAVSyncInfo->timeInfo.activeRefClk.nTimeStamp, pAVSyncInfo->timeInfo.activeRefClk.nOffsetToMediaTime, pAVSyncInfo->timeInfo.mediaTime, pAVSyncInfo->timeInfo.wallTimeInTicks ); idx = strlen (str); str = log_string[log_index] + (idx - 1); #endif pAVSyncInfo->stat.wallClkResetCnt++; mediaTimeUpdated = 1; } if ((delta < 0) && (delta < -(pAVSyncInfo->maxAudioLeadTime))) { // Update WallClock logWallTimeResetInfo(pAVSyncInfo->timeInfo.wallTimeInTicks, audioRefTime * (WALL_TIMER_FREQ_MS), mediaTimeInc, (pAVSyncInfo->timeInfo.mediaTime - oldMediaTime)); pAVSyncInfo->timeInfo.wallTimeInTicks = audioRefTime * (WALL_TIMER_FREQ_MS); pAVSyncInfo->timeInfo.mediaTime = audioRefTime; mediaTimeInc = (pAVSyncInfo->timeInfo.mediaTime - oldMediaTime); // Important. After refclk correction, reset refclk to INVALID_TIMESTAMP. // Required - If refclk <audio> update at audio rendering end doesnt at regular intervals, mediastamp will get reset to a older // refClk value several times within the next refclk update period at audio renderer. pAVSyncInfo->timeInfo.activeRefClk.nTimeStamp = AVSYNC_INVALID_TIMESTAMP; ref_clk_var[ref_clk_debug_idx].newMediaInc = mediaTimeInc; ref_clk_var[ref_clk_debug_idx].newMediaTime = pAVSyncInfo->timeInfo.mediaTime; ref_clk_var[ref_clk_debug_idx].newWallTime = pAVSyncInfo->timeInfo.wallTimeInTicks; pAVSyncInfo->stat.wallClkResetCnt++; mediaTimeUpdated = 1; #ifdef LOG_AVSYNC sprintf (str, "delta %lld newInc %lld refClk %lld newMedia %lld newWallTime %lld\n", delta, mediaTimeInc, audioRefTime, pAVSyncInfo->timeInfo.mediaTime, pAVSyncInfo->timeInfo.wallTimeInTicks ); idx = strlen (str); str = log_string[log_index] + (idx - 1); #endif } } } } #endif if (mediaTimeUpdated == 0) pAVSyncInfo->timeInfo.mediaTime += mediaTimeInc; /* Update all running counters */ for (i=0; i<AVSYNC_MAX_STREAMS_SUPPORTED; i++) { if (pAVSyncInfo->timeInfo.mediaTimeBase[i] != AVSYNC_INVALID_TIMESTAMP) { if ( pAVSyncInfo->timeInfo.mediaTimeRunning[i] == 0) pAVSyncInfo->timeInfo.wallTimeBase[i] = pAVSyncInfo->timeInfo.wallTimeInTicks; pAVSyncInfo->timeInfo.mediaTimeRunning[i] += mediaTimeInc; } } pAVSyncInfo->timeInfo.lastWallTimeInTicks = pAVSyncInfo->timeInfo.wallTimeInTicks; } if (log_cnt % 30) { ref_clk_debug_idx++; if (ref_clk_debug_idx >= MAX_STORAGE) ref_clk_debug_idx = 0; } }
/* * ======== Clock_startI ======== * Set the Clock object's currTimeout value and set its active flag * to TRUE. */ Void Clock_startI(Clock_Object *obj) { if (CLOCK_TICK_MODE == Clock_TickMode_DYNAMIC) { UInt32 nowTick, nowDelta; UInt32 scheduledTick, scheduledDelta; UInt32 remainingTicks; Bool objectServiced = FALSE; /* now see if need this new timeout before next scheduled tick ... */ /* wait till after first tick */ if ((Clock_module->ticking == TRUE) && /* if Clock is NOT currently processing its Q */ (Clock_module->inWorkFunc == FALSE)) { /* * get virtual current tick count, * signal Timer to save corresponding NOW info */ nowTick = Clock_TimerProxy_getCurrentTick(Clock_module->timer, TRUE); nowDelta = nowTick - Clock_module->ticks; scheduledDelta = Clock_module->nextScheduledTick - Clock_module->ticks; if (nowDelta <= scheduledDelta) { objectServiced = TRUE; /* start new Clock object */ obj->currTimeout = nowTick + obj->timeout; obj->active = TRUE; /* get the next scheduled tick */ scheduledTick = Clock_module->nextScheduledTick; /* how many ticks until scheduled tick? */ remainingTicks = scheduledTick - nowTick; if (obj->timeout < remainingTicks) { Clock_scheduleNextTick(obj->timeout, obj->currTimeout); } } } if (objectServiced == FALSE) { /* * get virtual current tick count, * DO NOT (!) signal Timer to save corresponding NOW info */ nowTick = Clock_getTicks(); /* start new Clock object */ obj->currTimeout = nowTick + obj->timeout; obj->active = TRUE; if (Clock_module->inWorkFunc == TRUE) { Clock_module->startDuringWorkFunc = TRUE; } } } /* CLOCK_TICK_MODE == Clock_TickMode_PERIODIC */ else { obj->currTimeout = (Clock_module->ticks + obj->timeout); obj->active = TRUE; } }
/* * ======== SysMin_putch ======== * Custom implementation for using circular * buffer without using flush */ Void SysMin_putch(Char ch) { IArg key; UInt i; #ifndef SMP static UInt coreId = 0; #else UInt coreId; #endif UInt lineIdx; Char *lineBuf; Int index; UInt64 uSec; static Bool configure = FALSE; static UInt startIdx; static UInt endIdx; static UInt timeStampSecCharLen; const UInt minSecCharLen = 4; /* for 1 us tick period */ const UInt maxuSecCharLen = 6; /* for 1 us tick period */ /* Max characters for seconds would be 10 assuming 1 sec tick period, * so decimal point index will be 11, and maximum time stamp buffer * length would be 18 accounting maxuSecCharLen and a trailing NULL */ const UInt decPtIdx = 11; const UInt timeStampBufLen = 18; const UInt leftSpaceIdx = 10; Char timeStamp[18] = {" \0"}; /* Configure the trace timestamp format */ if (!configure) { Int i = 0, mod = 10; /* Find number of characters needes for seconds and sub-seconds, * tick periods are specified in microseconds */ for (; i < maxuSecCharLen; i++) { if (Clock_tickPeriod % mod) { break; } mod = mod * 10; } timeStampSecCharLen = minSecCharLen + i; startIdx = decPtIdx - timeStampSecCharLen; endIdx = timeStampBufLen - (i + 1); /* Account for null character too */ configure = TRUE; } if (SysMin_bufSize != 0) { #ifndef SMP key = Gate_enterSystem(); #else /* Disable only local interrupts to place chars in local line buffer */ key = (IArg)Core_hwiDisable(); coreId = Core_getId(); #endif lineIdx = module->lineBuffers[coreId].lineidx; lineBuf = module->lineBuffers[coreId].linebuf; lineBuf[lineIdx++] = ch; module->lineBuffers[coreId].lineidx = lineIdx; #ifdef SMP /* Copy line buffer to shared output buffer at EOL or when filled up */ if ((ch == '\n') || (lineIdx >= SysMin_LINEBUFSIZE)) { Gate_enterSystem(); /* Tag core number */ SysMin_output('['); SysMin_output(0x30 + coreId); SysMin_output(']'); #else if (module->getTime == TRUE) { #endif uSec = Clock_getTicks() * (UInt64)Clock_tickPeriod; SysMin_output('['); if (uSec) { sprintf(timeStamp, "%17llu\0", uSec); } for (index = startIdx; index < endIdx; index++) { if (index == decPtIdx) { SysMin_output('.'); } if (timeStamp[index] == ' ' && index >= leftSpaceIdx) { SysMin_output('0'); } else { SysMin_output(timeStamp[index]); } } SysMin_output(']'); SysMin_output(' '); #ifdef SMP for (i = 0; i < lineIdx; i++) { SysMin_output(lineBuf[i]); } module->writeidx[0] = module->outidx; module->lineBuffers[coreId].lineidx = 0; Gate_leaveSystem(key); } else { /* restore local interrupts */ Core_hwiRestore((UInt)key); } #else module->getTime = FALSE; } /* Copy line buffer to shared output buffer at EOL or when filled up */ if ((ch == '\n') || (lineIdx >= SysMin_LINEBUFSIZE)) { for (i = 0; i < lineIdx; i++) { SysMin_output(lineBuf[i]); } module->lineBuffers[coreId].lineidx = 0; module->getTime = TRUE; module->writeidx[0] = module->outidx; } Gate_leaveSystem(key); #endif } }
/* * ======== TimestampProvider_get64 ======== * This API has different implementations based on whether we're using a * dedicated timer or sharing the Clock timer. * If we're sharing the Clock timer, we have to multiply the Clock tick count * by the Clock tick period. If we have a dedicate timer, the timer period is * 2^32, so we can just use the MOD->hi tick count as the upper 32-bits of the * timestamp. */ Void TimestampProvider_get64(Types_Timestamp64 *result) { UInt key; /* * If we're sharing the Clock timer, get the timestamp by using the Clock * tick count. */ if (TimestampProvider_useClockTimer) { UInt64 timestamp; /* * Disable interrupts so that the Clock tick count doesn't change if * the timer expires while we are reading it. */ key = Hwi_disable(); /* * timestamp = (clock ticks) x (tick period) + (current timer count) * The 'getExpiredCounts' API retrieves the current Timer count and * also accounts for timer rollover. */ timestamp = (UInt64) Clock_getTicks() * Timer_getPeriod(MOD->timer) + Timer_getExpiredCounts(MOD->timer); Hwi_restore(key); /* * Copy the value into the result structure. * * The 28x is little endian, so it stores the 16-bit words * in reverse order. For example, the value * 0xFEDCBA9876543210 * Is stored as: * Address Value * 0x0 0x3210 * 0x1 0x7654 * 0x2 0xBA98 * 0x3 0xFEDC * * To retrieve the lower 32 bits, simply cast the value as a UInt32. * To retrieve the upper 32 bits, cast the address of 'timestamp' as a * pointer to a UInt32, increment the pointer by 1, then retrieve the * value at that address. */ result->lo = (UInt32) timestamp; result->hi = *(((UInt32 *) ×tamp) + 1); } /* If we have a dedicated timer... */ else { /* Disable interrupts while reading the tick count and timer value. */ key = Hwi_disable(); /* Use the tick counter as the upper 32-bits. */ result->hi = MOD->hi; /* * Get the timer value as the lower 32-bits. This API will also take * timer rollover into account and add 1 to result->hi if necessary. */ Timer_getExpiredCounts64(MOD->timer, result); Hwi_restore(key); } }
/* * ======== millis ======== */ unsigned long millis(void) { return (Clock_getTicks()); }
//***************************************************************************** // //! Festo Station Task. //! //! This task function do all the work related to controlling the Festo //! Station. The task will first initialize the Driver and then run a infinite //! loop, waiting and processing external events. //! //! \return None. // //***************************************************************************** Void _task_FESTO(UArg arg0, UArg arg1) { // initialize driver FestoStationDriver DriverInstance; FestoStationDriver* Driver = &DriverInstance; Festo_Driver_Init(Driver); uint32_t EventPosted; DisplayMessage MessageObject; MessageObject.ScreenID = 0; MessageObject.uptimeSeconds = 0; MessageObject.piecesProcessed = 0; MessageObject.blackAccepted = 0; MessageObject.blackRejected = 0; MessageObject.plasticAccepted = 0; MessageObject.plasticRejected = 0; MessageObject.orangeAccepted = 0; MessageObject.orangeRejected = 0; MessageObject.piecesProcessedPerSecond = 0; MessageObject.heightCalibrated = 230; MessageObject.upperHeightCalibrated = 245; MessageObject.lowerHeightCalibrated = 227; time_t t1 = time(NULL); strcpy((char*) MessageObject.timeString, asctime(localtime(&t1))); uint32_t uptimeSeconds = 0; uint32_t piecesProcessed = 0; uint32_t blackAccepted = 0; uint32_t blackRejected = 0; uint32_t plasticAccepted = 0; uint32_t plasticRejected = 0; uint32_t orangeAccepted = 0; uint32_t orangeRejected = 0; uint32_t metallicAccepted = 0; uint32_t metallicRejected = 0; uint32_t piecesProcessedPerSecond = 0; uint32_t *ColourAccepted; uint32_t *ColourRejected; uint32_t *MaterialAccepted; uint32_t *MaterialRejected; uint8_t colour = 0; uint8_t material = 0; uint32_t i = 0; uint32_t FestoState = 0; // 0 = stopped // 1 = idle // 2 = initial state // 10 = cal int32_t LowerLimit = 1200; int32_t UpperLimit = 1500; int32_t Uptime = 0; int32_t Time0 = 0; int32_t Time1 = 0; uint8_t Running = 0; Clock_Params clockParams; Clock_Handle myClock; Clock_Params_init(&clockParams); clockParams.arg = (UArg) Board_ACTUATOR_EJECTOR; myClock = Clock_create(_Festo_Deactivate_Ejector, 200, &clockParams, NULL); uint32_t heightMeasured = 0; uint32_t heightCalibratedADC = 1380; uint32_t heightCalibrated10mm = 225; //float ConvertFactor = 0.1*MessageObject.heightCalibrated/1200; GPIO_write(Board_LED0, Board_LED_OFF); GPIO_write(Board_LED1, Board_LED_OFF); GPIO_write(Board_LED2, Board_LED_ON); Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); Clock_start(Clock_1_sec); while(1) { EventPosted = Event_pend(FestoEvents, Event_Id_NONE, FESTO_EVENT_BUTTON_UP + FESTO_EVENT_BUTTON_DOWN + FESTO_EVENT_BUTTON_SELECT + FESTO_EVENT_RISER_DOWN + FESTO_EVENT_RISER_UP + FESTO_EVENT_ADC_FINISH + FESTO_EVENT_PIECE_IN_PLACE + FESTO_EVENT_EJECTOR_FINISHED + FESTO_EVENT_TICK + FESTO_EVENT_COOLDOWN + FESTO_EVENT_PIECE_NOT_IN_PLACE, FESTO_TIMEOUT); if (EventPosted & FESTO_EVENT_BUTTON_UP) { if (FestoState == 0) { Festo_Control_Driver(Driver, FESTO_ENABLED); FestoState = 1; Running = 1; GPIO_write(Board_LED0, Board_LED_ON); GPIO_write(Board_LED1, Board_LED_OFF); GPIO_write(Board_LED2, Board_LED_OFF); MessageObject.ScreenID = 1; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); Time0 = Clock_getTicks(); } else if (FestoState == 12) { MessageObject.heightCalibrated++; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 13) { MessageObject.upperHeightCalibrated++; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 14) { MessageObject.lowerHeightCalibrated++; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } } else if (EventPosted & FESTO_EVENT_BUTTON_DOWN) { if (FestoState == 1) { Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); Festo_Control_Driver(Driver, FESTO_DISABLED); FestoState = 0; Running = 0; GPIO_write(Board_LED0, Board_LED_OFF); GPIO_write(Board_LED1, Board_LED_OFF); GPIO_write(Board_LED2, Board_LED_ON); MessageObject.ScreenID = 0; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 12) { MessageObject.heightCalibrated--; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 13) { MessageObject.upperHeightCalibrated--; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 14) { MessageObject.lowerHeightCalibrated--; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } } else if (EventPosted & FESTO_EVENT_BUTTON_SELECT) { if (FestoState == 0) { GPIO_write(Board_LED0, Board_LED_OFF); GPIO_write(Board_LED1, Board_LED_ON); GPIO_write(Board_LED2, Board_LED_OFF); Festo_Control_Driver(Driver, FESTO_ENABLED); FestoState = 10; MessageObject.ScreenID = 2; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); if (Festo_Sense_Riser_Down(Driver) != 1) { Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); } else { FestoState = 11; MessageObject.ScreenID = 3; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } } else if (FestoState == 11) { FestoState = 12; MessageObject.ScreenID = 4; MessageObject.heightCalibrated = 230; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 12) { FestoState = 13; Festo_Control_Platform(Driver, FESTO_PLATFORM_RAISE); Event_post(FestoEvents, FESTO_EVENT_ADC_START); } else if (FestoState == 13) { FestoState = 14; MessageObject.ScreenID = 6; MessageObject.lowerHeightCalibrated = 227; UpperLimit = MessageObject.upperHeightCalibrated * (1.0 * heightCalibratedADC)/(1.0 * heightCalibrated10mm); System_printf("Upper Limit Cal: %d for %d *0.1 mm\n", UpperLimit, MessageObject.upperHeightCalibrated); System_flush(); Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 14) { FestoState = 15; MessageObject.ScreenID = 7; LowerLimit = MessageObject.lowerHeightCalibrated * (1.0 * heightCalibratedADC)/(1.0 * heightCalibrated10mm); System_printf("Lower Limit Cal: %d for %d *0.1 mm\n", LowerLimit, MessageObject.lowerHeightCalibrated); System_flush(); Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (FestoState == 15) { FestoState = 0; MessageObject.ScreenID = 0; GPIO_write(Board_LED0, Board_LED_OFF); GPIO_write(Board_LED1, Board_LED_OFF); GPIO_write(Board_LED2, Board_LED_ON); Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else { } } else if (EventPosted & FESTO_EVENT_RISER_UP) { if (FestoState == 3) { if (Festo_Sense_Piece_Placed(Driver) == 1) { FestoState = 4; // wait estabilize for (i = 0; i < 1000000; i++); Event_post(FestoEvents, FESTO_EVENT_ADC_START); } else { FestoState = 1; Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); } } else if (FestoState == 13) { Event_post(FestoEvents, FESTO_EVENT_ADC_START); } else { } } else if (EventPosted & FESTO_EVENT_RISER_DOWN) { if (FestoState == 2) { if (Festo_Sense_Piece_Placed(Driver) == 1) { // get a lot of samples for (i = 0; i < 1200; i++) { // this waits to get a reliable reading. If the reading is different from before, reset. if (colour != Festo_Sense_Piece_Colour(Driver) || material != Festo_Sense_Piece_Material(Driver)) { i = 0; } colour = Festo_Sense_Piece_Colour(Driver); material = Festo_Sense_Piece_Material(Driver); } FestoState = 3; Festo_Control_Platform(Driver, FESTO_PLATFORM_RAISE); } else { FestoState = 1; } } if (FestoState == 5) { Festo_Control_Ejector(Driver, FESTO_EJECTOR_EXTEND); Clock_start(myClock); } if (FestoState == 6) { Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); Clock_start(myClock); FestoState = 7; } if (FestoState == 10) { FestoState = 11; MessageObject.ScreenID = 3; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } } else if (EventPosted & FESTO_EVENT_ADC_FINISH) { if (Mailbox_pend(ADCMailbox, &heightMeasured, BIOS_NO_WAIT)) { Festo_Sense_Set_Piece_Height(Driver, heightMeasured); } else { Event_post(FestoEvents, FESTO_EVENT_ADC_START); } if (FestoState == 4) { if (colour == FESTO_COLOR_ORANGE && material == FESTO_PIECE_OTHER) { ColourAccepted = &orangeAccepted; ColourRejected = &orangeRejected; MaterialAccepted = &plasticAccepted; MaterialRejected = &plasticRejected; } else if (colour == FESTO_COLOR_OTHER && material == FESTO_PIECE_OTHER) { ColourAccepted = &blackAccepted; ColourRejected = &blackRejected; MaterialAccepted = &plasticAccepted; MaterialRejected = &plasticRejected; } else if (material == FESTO_PIECE_METALLIC) { ColourAccepted = NULL; ColourRejected = NULL; MaterialAccepted = &metallicAccepted; MaterialRejected = &metallicRejected; } else { ColourAccepted = NULL; ColourRejected = NULL; MaterialAccepted = NULL; MaterialRejected = NULL; } if (heightMeasured < UpperLimit && heightMeasured > LowerLimit)//withn range { if (ColourAccepted != NULL) { (*ColourAccepted)++; } if (MaterialAccepted != NULL) { (*MaterialAccepted)++; piecesProcessed++; } FestoState = 6; System_printf("Piece is acceptable\n"); System_flush(); Festo_Control_Ejector(Driver, FESTO_EJECTOR_EXTEND); Clock_start(myClock); } else // out of range { if (ColourAccepted != NULL) { (*ColourRejected)++; } if (MaterialAccepted != NULL) { (*MaterialRejected)++; piecesProcessed++; } System_printf("Piece is NOT acceptable\n"); System_flush(); FestoState = 5; Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); } } else if (FestoState == 13) { heightCalibratedADC = heightMeasured; heightCalibrated10mm = MessageObject.heightCalibrated; System_printf("ADC Cal: %d for %d *0.1 mm\n", heightCalibratedADC, heightCalibrated10mm); System_flush(); MessageObject.ScreenID = 5; MessageObject.upperHeightCalibrated = 245; Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } } else if (EventPosted & FESTO_EVENT_EJECTOR_FINISHED) { if (FestoState == 6) { Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); Clock_start(myClock); } else if (FestoState == 7) { FestoState = 1; } else { Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); Clock_start(myClock); FestoState = 7; } } else if (EventPosted & FESTO_EVENT_TICK) { if (Running) { Time1 = Clock_getTicks(); Uptime += (Time1 - Time0); Time0 = Time1; uptimeSeconds = Uptime * 0.001; piecesProcessedPerSecond = 100 * piecesProcessed/(0.016667*uptimeSeconds); MessageObject.piecesProcessed = piecesProcessed; MessageObject.blackAccepted = blackAccepted; MessageObject.blackRejected = blackRejected; MessageObject.plasticAccepted = plasticAccepted; MessageObject.plasticRejected = plasticRejected; MessageObject.orangeAccepted = orangeAccepted; MessageObject.orangeRejected = orangeRejected; MessageObject.piecesProcessedPerSecond = piecesProcessedPerSecond; MessageObject.uptimeSeconds = uptimeSeconds; MessageObject.metalAccepted = metallicAccepted; MessageObject.metalRejected = metallicRejected; } Seconds_set(Seconds_get()+1); t1 = time(NULL); strcpy((char*) MessageObject.timeString, asctime(localtime(&t1))); Mailbox_post(DisplayMailbox, &MessageObject, BIOS_NO_WAIT); } else if (EventPosted & FESTO_EVENT_PIECE_NOT_IN_PLACE) { if (FestoState <= 4) { FestoState = 1; Festo_Control_Ejector(Driver, FESTO_EJECTOR_RETRACT); Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); } } else { if (FestoState == 1) { if (Festo_Sense_Piece_Placed(Driver) == 1) { FestoState = 2; if (Festo_Sense_Riser_Down(Driver) == 0) { Festo_Control_Platform(Driver, FESTO_PLATFORM_LOWER); } else { Event_post(FestoEvents, FESTO_EVENT_RISER_DOWN); } } } } Task_sleep(100); } }
int gettimeofday(struct timeval *tp, void *tzp) { uint32_t ticks = Clock_getTicks(); tp->tv_sec = ticks / 1000; tp->tv_usec = (ticks % 1000) * 1000; return 0; }
void initLCD() { //SysCtlPeripheralEnable(LCDPORTENABLE); GPIOPinTypeGPIOOutput(LCDPORT, 0xff); // Please refer to the HD44780 datasheet for how these initializations work! int tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 30){ } //SysCtlDelay((500e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, RS, 0x00 ); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x30 ); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 30){ } //SysCtlDelay((50e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x30 ); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 30){ } //SysCtlDelay((50e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x30 ); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 30){ } //SysCtlDelay((10e-3)*CLKSPEED/3); GPIOPinWrite(LCDPORT, D4 | D5 | D6 | D7, 0x20 ); GPIOPinWrite(LCDPORT, E, 0x02); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 1){ } //SysCtlDelay((20e-6)*CLKSPEED/3); GPIOPinWrite(LCDPORT, E, 0x00); tempo = Clock_getTicks(); while (Clock_getTicks() - tempo < 30){ } //SysCtlDelay((10e-3)*CLKSPEED/3); LCDCommand(0x01); // Clear the screen. LCDCommand(0x06); // Cursor moves right. LCDCommand(0x0f); // Cursor blinking, turn on LCD. }