void main(void){ PLL_Init(); // running at 24MHz DDRT |= 0x1F; // debugging outputs PTT &= ~0x1F; Debug_Profile(0); // 0 means initialization phase Fifo_Init(); // Initialize fifo OC0_Init(); // variable rate interrupt ForeExpected = 0; // expected data for(;;){ Debug_Profile(1); // 1 means start of foreground waiting PTT_PTT1 = 0; // falling edge of PT1 means start of foreground waiting while(Fifo_Get(&ForeData)==FIFOFAIL){ } Debug_Profile(2); // 2 means foreground has new data PTT_PTT1 = 1; // rising edge of PT1 means start of foreground processing if(ForeExpected != ForeData){ Errors++; PTT ^= 0x10; // critical section found ForeExpected = ForeData+1; // resych to lost/bad data } else{ ForeExpected++; // sequence is 0,1,2,3,...,254,255,0,1,... } if((ForeData%10)==0){ Debug_Profile(3); // 3 means foreground has 10th data } } }
//------------sort_insertion------------ // Straight insertion sort from "The Art of Computer Programming", // Donald E. Knuth, Volume 3 Sorting and Searching, Second Edition, // 1998, pages 80-82. The input array is copied, sorted, and // stored in the output array. The input array is not modified. // Input: input pointer to an array of 32-bit numbers to be sorted // output pointer to an array of 32-bit numbers to store sorted output // length number of elements in input and output arrays // Output: none void sort_insertion(uint32_t *input, uint32_t *output, uint32_t length){ uint32_t temp; int i, j; output[0] = input[0]; #if (DEBUGTYPE == SWDUMP) Debug_Profile(0); #endif #if (DEBUGTYPE == HWPORT) PROFILE = 1; #endif for(j=1; j<length; j=j+1){ output[j] = input[j]; #if (DEBUGTYPE == SWDUMP) Debug_Profile(1); #endif #if (DEBUGTYPE == HWPORT) PROFILE = 2; #endif for(i=j; i>0; i=i-1){ if(output[i] < output[i-1]){ // an entry is smaller than the one before it // swap them, causing it to sink toward the beginning #if (DEBUGTYPE == SWDUMP) Debug_Profile(2); #endif #if (DEBUGTYPE == HWPORT) PROFILE = 4; #endif temp = output[i-1]; output[i-1] = output[i]; output[i] = temp; } else{ // an entry is greater or equal to the one before it // it will be greater or equal to all before it #if (DEBUGTYPE == SWDUMP) Debug_Profile(3); // NOTE: The Debug_place array may skip 3 and go from 2 // to 1 in the case where index 'i' counts to 0 and the // latest entry sinks to the beginning of the list. In // other words, an entry is smaller than all of the // ones before it; a change in the left-to-right // minimum. // Knuth describes the running time of this algorithm: // 9*B + 10*N - 3*A - 9 units // where, // N is the number of records sorted ('length') // A is the number of times 'i' decreases to 0 (number of 2's immediately followed by 1's without a 3 in between) // B is the number of moves (number of 2's in Debug_place) #endif #if (DEBUGTYPE == HWPORT) PROFILE = 8; #endif break; } } } #if (DEBUGTYPE == HWPORT) PROFILE = 0; #endif }
interrupt 8 void OC0Han(void){ // periodic interrupt Debug_Profile(4); // 4 means background thread active PTT_PTT0 = 1; // rising edge of PT0 means start of interrupt TFLG1 = 0x01; // acknowledge OC0 TC0 = TC0 +BackPeriod; // varies from 10us to 1ms if(Fifo_Put(BackData)==FIFOFAIL){ NumLost++; } BackData++; // sequence is 0,1,2,3,...,254,255,0,1,... if(BackPeriod > 500){ BackPeriod = 200; } else{ BackPeriod = BackPeriod+23; } NumInterrupts++; PTT_PTT0 = 0; // falling edge of PT0 means end of interrupt }