void sendATCommand( char * command, int waitTime, char CRout){ char frame2[50]; char done = 0; char count = 0; int j = 0; int size; int commandLen = strlen2(command); for (j = 0; j < 50; j++) frame2[j] = 0; frame2[0] = 0; frame2[1] = 0; do{ UART_OutString(command); if (CRout) UART_OutChar(CR); Delay(500000*waitTime); j = 0; size = RxFifo_Size(); while (size>0){ frame2[j++] = UART_InChar(); size = RxFifo_Size(); // Delay(500000); } j = 0; while (frame2[j] != 'O') j++; if (frame2[j] == 'O' && frame2[j+1] == 'K' && frame2[j+2] == CR) done = 1; count++; } while (!done && count < 10); }
// copy from hardware RX FIFO to software RX FIFO // stop when hardware RX FIFO is empty or software RX FIFO is full void static copyHardwareToSoftware(void){ char letter; while(((UART0_FR_R&UART_FR_RXFE) == 0) && (RxFifo_Size() < (FIFOSIZE - 1))){ letter = UART0_DR_R; RxFifo_Put(letter); } }
// Parses a JSON schedule into the global variable // schedule and sorts it void scheduleParse(void) { char key[11]; enum key_t i; unsigned long value, current_zone; while(RxFifo_Size() > 0) { if(!getNextString(key)) break; for(i = ZONE; i < (sizeof(KEYS)/sizeof(char*)); i++) { if(!strcmp(key, KEYS[i])) { if(!getNextNum(&value)) break; switch(i) { case ZONE: current_zone = value; break; case START: schedule[schedule_idx].zone = current_zone; schedule[schedule_idx].start_time = value; break; case END: schedule[schedule_idx].end_time = value; schedule_idx++; break; default: break; } } } } }
unsigned char * receiveData(void){ if (RxFifo_Size()==0) return 0; else { while(XBee_RecieveRxFrame() != 7); XBee_RecieveRxFrame(); return messageBuffer; } }
void XBeeInit(){ char * commands [] = {"ATDL66", "ATDH0", "ATMY6D", "ATAP1", "ATCN", ""}; int i = 0; int j; UART_Init(); while (RxFifo_Size()>0){ //flush FIFO UART_InChar(); } ID = 1; UART_OutString("x"); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); //SysTick_Wait10ms(110); //wait waitTime number of ms; sendATCommand("+++", 110, 0); //UART_InString(response, 5); //RIT128x96x4StringDraw(response, 10, 10 , 15); for (i=0;i<5;i++){ sendATCommand(commands[i], 20, 1); } SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); SysTick_Wait10ms(10); }
// RDRF set on new receive data // TDRE set on an empty transmit data register interrupt 21 void SCI1Handler(void){ char data; //PTT_PTT3 = 1; if(SCI1SR1 & RDRF){ #if HISTOGRAM RxHistogram[1][RxFifo_Size(1)]++; #endif RxFifo_Put(1,SCI1DRL); // clears RDRF } if((SCI1CR2&0x80)&&(SCI1SR1&TDRE)){ if(TxFifo_Get(1,&data)){ SCI1DRL = data; // clears TDRE } else{ SCI1CR2 = 0x2c; // disarm TDRE } } //PTT_PTT3 = 0; }
void XBeeInit(){ char * commands [] = {"ATDL66", "ATDH0", "ATMY6D", "ATAP1", "ATCN", ""}; int i = 0; int j; unsigned long i44 = 0; unsigned long j44 = 0; unsigned long delay = 110; // SysTick_Init(); UART_Init(); while (RxFifo_Size()>0){ //flush FIFO UART_InChar(); } ID = 1; UART_OutString("x"); // volatile unsigned long dummy = 0; for (i44 = 0; i44 < delay; i44++) for (j44 = 0; j44 < 150000; j44++); // dummy ++ ; //wait10ms(110); // Delay(5500000); //SysTick_Wait10ms(110); //wait waitTime number of ms; sendATCommand("+++", 110, 0); //UART_InString(response, 5); //RIT128x96x4StringDraw(response, 10, 10 , 15); for (i=0;i<5;i++){ sendATCommand(commands[i], 20, 1); } for (i44 = 0; i44 < delay; i44++) for (j44 = 0; j44 < 150000; j44++); // Delay(55000000); }
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); }
unsigned int XBee_RecieveRxFrame(void){ unsigned char byte; unsigned short tempNum = 0; unsigned short messageLength = 0; unsigned short bufferIdx = 0; if (RxFifo_Size()>0) byte = UART_InChar(); else return 0; if(decryptionStage == 0){ if(byte == 0x7E){ decryptionStage += 1; RxCheckSum = 0; //printf("%c",byte); } } else if(decryptionStage == 1){ tempNum = byte; tempNum = tempNum << 8; byte = UART_InChar(); tempNum = tempNum + byte; RxFrameLength = tempNum; decryptionStage += 1; } else if(decryptionStage == 2){ RxFrameType = byte; RxCheckSum += byte; decryptionStage += 1; } else if(decryptionStage == 3){ RxFrameId = byte; RxCheckSum += byte; decryptionStage += 1; } else if(decryptionStage == 4){ tempNum = byte; RxCheckSum += byte; tempNum = tempNum << 8; byte = UART_InChar(); RxCheckSum += byte; tempNum = tempNum + byte; TxAdrs= tempNum; decryptionStage += 1; } else if(decryptionStage == 5){ RxCheckSum += byte; decryptionStage += 1; } else if(decryptionStage == 6){ messageLength = RxFrameLength - 5; // messageBuffer[bufferIdx] = byte; // bufferIdx += 1; // messageLength -= 1; RxCheckSum += byte; messageBuffer[bufferIdx] = byte; for(bufferIdx = 1; bufferIdx <messageLength; bufferIdx++){ messageBuffer[bufferIdx] = UART_InChar(); RxCheckSum += messageBuffer[bufferIdx]; } newMessage = 1; messageBuffer[bufferIdx] = 0; // printf("%s",messageBuffer); decryptionStage += 1; } else if(decryptionStage == 7){ decryptionStage = 0; frameCheckSum = byte; if((RxCheckSum + frameCheckSum) == 0xFF){ successfulDecryption = 1; } else{ successfulDecryption = 0; } } return decryptionStage; }