//************SW2Push************* // Called when SW2 Button pushed, Lab 3 only // Adds another foreground task // background threads execute once and return void SW2Push(void){ if(OS_MsTime() > 20){ // debounce if(OS_AddThread(&ButtonWork,100,4)){ NumCreated++; } OS_ClearMsTime(); // at least 20ms between touches } }
//******** OS_AddPeriodicThread *************** // add a background periodic task // typically this function receives the highest priority // Inputs: pointer to a void/void background function // thread number to make the code simple // period given in system time units // priority 0 is highest, 5 is lowest // Outputs: 1 if successful, 0 if this thread can not be added // It is assumed that the user task will run to completion and return // This task can not spin, block, loop, sleep, or kill // This task can call OS_Signal OS_bSignal OS_AddThread // You are free to select the time resolution for this function // This task does not have a Thread ID // In lab 2, this command will be called 0 or 1 times // In lab 2, the priority field can be ignored // In lab 3, this command will be called 0 1 or 2 times // In lab 3, there will be up to four background threads, and this priority field // determines the relative priority of these four threads int OS_AddPeriodicThread(void(*task)(void), unsigned long threadnumber, unsigned long period, unsigned long priority) { long status; status = StartCritical(); // Clear periodic counter OS_ClearMsTime(threadnumber); if(threadnumber == 1) { // Set the global function pointer to the address of the provided function gThread1p = task; gThread1Valid = VALID; gThread1Period = period; TimerDisable(TIMER2_BASE, TIMER_A); IntPrioritySet(INT_TIMER2A, (priority << 5)); // Sets new TIMER2 period TimerLoadSet(TIMER2_BASE, TIMER_A, period); TimerIntClear(TIMER2_BASE, TIMER_TIMA_TIMEOUT); TimerIntEnable(TIMER2_BASE, TIMER_TIMA_TIMEOUT); IntEnable(INT_TIMER2A); TimerEnable(TIMER2_BASE, TIMER_A); } else { // Set the global function pointer to the address of the provided function gThread2p = task; gThread2Valid = VALID; gThread2Period = period; TimerDisable(TIMER2_BASE, TIMER_B); IntPrioritySet(INT_TIMER2B, (priority << 5)); // Sets new TIMER2 period TimerLoadSet(TIMER2_BASE, TIMER_B, period); TimerIntClear(TIMER2_BASE, TIMER_TIMB_TIMEOUT); TimerIntEnable(TIMER2_BASE, TIMER_TIMB_TIMEOUT); IntEnable(INT_TIMER2B); TimerEnable(TIMER2_BASE, TIMER_B); } EndCritical(status); return 1; }
//******** Robot *************** // foreground thread, accepts data from producer // inputs: none // outputs: none void Robot(void){ unsigned long data; // ADC sample, 0 to 1023 unsigned long voltage; // in mV, 0 to 3000 unsigned long time; // in 10msec, 0 to 1000 unsigned long t=0; OS_ClearMsTime(); DataLost = 0; // new run with no lost data printf("Robot running..."); eFile_RedirectToFile("Robot"); printf("time(sec)\tdata(volts)\n\r"); do{ t++; time=OS_MsTime(); // 10ms resolution in this OS data = OS_Fifo_Get(); // 1000 Hz sampling get from producer voltage = (300*data)/1024; // in mV printf("%0u.%02u\t%0u.%03u\n\r",time/100,time%100,voltage/1000,voltage%1000); } while(time < 1000); // change this to mean 10 seconds eFile_EndRedirectToFile(); printf("done.\n\r"); Running = 0; // robot no longer running OS_Kill(); }
static int _SH_ClearTime(void) { OS_ClearMsTime(); return 0; }