//------------UART_InCharNonBlock------------ // input ASCII character from UART // output: 0 if RxFifo is empty // character if char UART_InCharNonBlock(void){ char letter; if(RxFifo_Get(&letter) == FIFOFAIL){ return 0; // empty }; return(letter); }
// input ASCII character from UART // block if RxFifo is empty char UART_InChar(void){ char letter; while(RxFifo_Get(&letter) == FIFOFAIL){ OS_Wait(&RxDataAvailable); } return(letter); }
// advance the fifo to the next alphabetic letter long nextAlpha(void) { long status = 0; char ch; while(RxFifo_Peek(&ch)) { if(isalpha(ch)) { status = 1; break; } RxFifo_Get(&ch); } return status; }
// Searchs for a desired string by reading // through the recieved characters. This will remove // items from the buffer. tBoolean WiFly_Match(const char * match) { char ch; long idx = 0; long len = strlen(match); tBoolean status = true; while((len != idx) && status) { status = RxFifo_Get(&ch); idx = (ch == match[idx]) ? (idx+1) : 0; } if(len == idx) status = true; return status; }
// get the next key long getNextString(char* string) { long status; char ch; if(nextAlpha()) { while(RxFifo_Get(&ch)) { if(isalpha(ch)) *string++ = ch; else { *string = '\0'; status = 1; break; } } } return status; }
// Opens a network connection to the server tBoolean WiFly_Open(char *resp) { tBoolean status = false; char* resp_pt = resp; RxFifo_Flush(); // Flux the FIFO status = WiFly_Send(CMD_CMD, CMD_RSP); if(status){ status = WiFly_Send(OPEN_CMD, NO_RSP); if(status){ SysCtlDelay(SysCtlClockGet()*3); // Blocking wait while(RxFifo_Get(resp_pt)) resp_pt++; *resp_pt = NULL; } } WiFly_Send(EXIT_CMD, EXIT_RSP); return status; }
// get the next number long getNextNum(unsigned long* num) { long status = 0; char num_s[11], ch; size_t i = 0; if(nextDigit()) { while(RxFifo_Get(&ch)) { if(isdigit(ch)) { num_s[i++] = ch; } else { num_s[i] = '\0'; *num = strtoul(num_s, NULL, 10); status = 1; break; } } } return status; }
// Gets posix time from NIST servers // TODO: Use the NIST DNS for reliability unsigned long WiFly_Time(void){ tBoolean status = false; unsigned char i = 0; unsigned long time = 0; char time_str[11]; // A string formatted long can only be 10 characters RxFifo_Flush(); status = WiFly_Send(CMD_CMD, CMD_RSP); if(status) { status = WiFly_Send(TIME_CMD, TIME_RSP); // Get time if(status) { for(i = 0; i < 10; i++) RxFifo_Get(&time_str[i]); time_str[10] = 0x0; RIT128x96x4StringDraw(time_str, 0, 0, 15); time = strtoul(time_str, NULL, 0); // Convert to unsigned long } } WiFly_Send(EXIT_CMD, EXIT_RSP); return time; }
// input ASCII character from UART // spin if RxFifo is empty char UART_InChar(void){ char letter; while(RxFifo_Get(&letter) == FIFOFAIL){}; return(letter); }
int main2(void){ int i; // *************** Test #1: test transmit (index) FIFO ************** TxFifo_Init(); result = TxFifo_Get(&letter); // letter = ??, result = 0 result = TxFifo_Put('A'); // result = 1 result = TxFifo_Put('B'); // result = 1 result = TxFifo_Put('C'); // result = 1 result = TxFifo_Get(&letter); // letter = 0x41, result = 1 result = TxFifo_Get(&letter); // letter = 0x42, result = 1 result = TxFifo_Put('D'); // result = 1 result = TxFifo_Size(); // result = 2 result = TxFifo_Get(&letter); // letter = 0x43, result = 1 result = TxFifo_Get(&letter); // letter = 0x44, result = 1 result = TxFifo_Size(); // result = 0 result = TxFifo_Get(&letter); // letter = ??, result = 0 for(i='A'; i<'A'+TXFIFOSIZE; i=i+1){ result = TxFifo_Put(i); // result = 1 } result = TxFifo_Size(); // result = 16 result = TxFifo_Get(&letter); // letter = 0x41, result = 1 result = TxFifo_Size(); // result = 15 result = TxFifo_Get(&letter); // letter = 0x42, result = 1 result = TxFifo_Size(); // result = 14 result = TxFifo_Put(' '); // result = 1 result = TxFifo_Size(); // result = 15 // *************** Test #2: test receive (pointer) FIFO ************* RxFifo_Init(); result = RxFifo_Get(&letter); // letter = ??, result = 0 result = RxFifo_Put('A'); // result = 1 result = RxFifo_Put('B'); // result = 1 result = RxFifo_Put('C'); // result = 1 result = RxFifo_Get(&letter); // letter = 0x41, result = 1 result = RxFifo_Get(&letter); // letter = 0x42, result = 1 result = RxFifo_Put('D'); // result = 1 result = RxFifo_Size(); // result = 2 result = RxFifo_Get(&letter); // letter = 0x43, result = 1 result = RxFifo_Get(&letter); // letter = 0x44, result = 1 result = RxFifo_Size(); // result = 0 result = RxFifo_Get(&letter); // letter = ??, result = 0 for(i='A'; i<'A'+RXFIFOSIZE-1; i=i+1){ result = RxFifo_Put(i); // result = 1 } result = RxFifo_Size(); // result = 9 result = RxFifo_Get(&letter); // letter = 0x41, result = 1 result = RxFifo_Size(); // result = 8 result = RxFifo_Get(&letter); // letter = 0x42, result = 1 result = RxFifo_Size(); // result = 7 result = RxFifo_Put(' '); // result = 1 result = RxFifo_Size(); // result = 8 // *********** Test #3: test transmit (index) FIFO creator ********** Tx2Fifo_Init(); result = Tx2Fifo_Get(&letter);// letter = ??, result = 0 result = Tx2Fifo_Put('A'); // result = 1 result = Tx2Fifo_Put('B'); // result = 1 result = Tx2Fifo_Put('C'); // result = 1 result = Tx2Fifo_Get(&letter);// letter = 0x41, result = 1 result = Tx2Fifo_Get(&letter);// letter = 0x42, result = 1 result = Tx2Fifo_Put('D'); // result = 1 result = Tx2Fifo_Size(); // result = 2 result = Tx2Fifo_Get(&letter);// letter = 0x43, result = 1 result = Tx2Fifo_Get(&letter);// letter = 0x44, result = 1 result = Tx2Fifo_Size(); // result = 0 result = Tx2Fifo_Get(&letter);// letter = ??, result = 0 for(i='A'; i<'A'+TX2FIFOSIZE; i=i+1){ result = Tx2Fifo_Put(i); // result = 1 } result = Tx2Fifo_Size(); // result = 32 result = Tx2Fifo_Get(&letter);// letter = 0x41, result = 1 result = Tx2Fifo_Size(); // result = 31 result = Tx2Fifo_Get(&letter);// letter = 0x42, result = 1 result = Tx2Fifo_Size(); // result = 30 result = Tx2Fifo_Put(' '); // result = 1 result = Tx2Fifo_Size(); // result = 31 // *********** Test #4: test receive (pointer) FIFO creator ********* Rx2Fifo_Init(); result = Rx2Fifo_Get(&letter);// letter = ??, result = 0 result = Rx2Fifo_Put('A'); // result = 1 result = Rx2Fifo_Put('B'); // result = 1 result = Rx2Fifo_Put('C'); // result = 1 result = Rx2Fifo_Get(&letter);// letter = 0x41, result = 1 result = Rx2Fifo_Get(&letter);// letter = 0x42, result = 1 result = Rx2Fifo_Put('D'); // result = 1 result = Rx2Fifo_Size(); // result = 2 result = Rx2Fifo_Get(&letter);// letter = 0x43, result = 1 result = Rx2Fifo_Get(&letter);// letter = 0x44, result = 1 result = Rx2Fifo_Size(); // result = 0 result = Rx2Fifo_Get(&letter);// letter = ??, result = 0 for(i='A'; i<'A'+RX2FIFOSIZE-1; i=i+1){ result = Rx2Fifo_Put(i); // result = 1 } result = Rx2Fifo_Size(); // result = 26 result = Rx2Fifo_Get(&letter);// letter = 0x41, result = 1 result = Rx2Fifo_Size(); // result = 25 result = Rx2Fifo_Get(&letter);// letter = 0x42, result = 1 result = Rx2Fifo_Size(); // result = 24 result = Rx2Fifo_Put(' '); // result = 1 result = Rx2Fifo_Size(); // result = 25 while(1); }
int main(void){ int i; SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ); // 50 MHz // **** general initialization **** DisableInterrupts(); SYSCTL_RCGC1_R |= SYSCTL_RCGC1_TIMER0;// activate timer0 SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF+SYSCTL_RCGC2_GPIOG; // activate ports F and G BackData = 0; // allow time to finish activating BackCounter = 0; EnteredCount = 0; ForeExpected = 0; Errors = 0; EnterGet = 0; GPIO_PORTF_DIR_R |= 0x0F; // make PF3-PF0 out (connect to scope/logic analyzer) GPIO_PORTF_DEN_R |= 0x0F; // enable digital I/O on PF3-PF0 GPIO_PORTG_DIR_R |= 0x04; // make PG2 out (PG2 built-in LED) GPIO_PORTG_DEN_R |= 0x04; // enable digital I/O on PG2 GPIO_PORTG2 = 0x00; // clear PG2 GPIO_PORTF0 = 0x00; GPIO_PORTF1 = 0x00; GPIO_PORTF2 = 0x00; TIMER0_CTL_R &= ~TIMER_CTL_TAEN; // disable timer0A during setup TIMER0_CFG_R = TIMER_CFG_16_BIT; // configure for 16-bit timer mode // **** timer0A initialization **** TIMER0_TAMR_R = TIMER_TAMR_TAMR_PERIOD;// configure for periodic mode TIMER0_TAILR_R = INTPERIOD - 1; // start value to count down from TIMER0_IMR_R |= TIMER_IMR_TATOIM;// enable timeout (rollover) interrupt TIMER0_ICR_R = TIMER_ICR_TATOCINT;// clear timer0A timeout flag // **** interrupt initialization **** // Timer0A=priority 2 NVIC_PRI4_R = (NVIC_PRI4_R&0x00FFFFFF)|0x40000000; // top 3 bits NVIC_EN0_R |= NVIC_EN0_INT19; // enable interrupt 19 in NVIC // *************** Test #5: test interrupt vulnerability ************ RxFifo_Init(); for(i=0; i<HISTOGRAMSIZE; i=i+1){ unsigned long returnaddress; LineHistogram[i] = 0; returnaddress = ((unsigned long)&RxFifo_Get + 2*i)&0xFFFFFFFE; LineHistogramAddress[i] = returnaddress; // possible places in Get that could be interrupted } TIMER0_ICR_R = TIMER_ICR_TATOCINT;// clear timer0A timeout flag TIMER0_CTL_R |= TIMER_CTL_TAEN; // enable timer0A 16-b, periodic, interrupts EnableInterrupts(); while(1){ do{ GPIO_PORTF0 = 1; // profile of main program EnterGet = 1; i = RxFifo_Get(&ForeActual); // i = 0 (FIFOFAIL) if error EnterGet = 0; GPIO_PORTF0 = 0; } while(!i); GPIO_PORTF1 = 2; // profile of main program if(ForeExpected != ForeActual){ Errors = Errors + 1; // critical section found ForeExpected = ForeActual + 1; // resych to lost/bad data GPIO_PORTG2 = 0x04; // set PG2, means error } else{ ForeExpected = ForeExpected + 1;// sequence is 0,1,2,3,...,254,255,0,1,... } GPIO_PORTF1 = 0; } }
//-------------------------SCI_InChar------------------------ // Wait for new serial port input, interrupt synchronization // The input is not echoed // Input: SCI port // Output: ASCII code for key typed char SCI_InChar(unsigned char port){ char letter; while (RxFifo_Get(port, &letter) == 0){}; return(letter); }