void COM_task() { volatile UINT8 uartData = 0,i; communication.curAppTime = GetAppTime(); if(RCSTA1bits.OERR == 1) { RCSTA1bits.CREN = 0; Delay10us(1); RCSTA1bits.CREN = 1; } if( communication.prevAppTime != communication.curAppTime) { if( communication.prevState == communication.state && (communication.state == COM_IN_PACKET_COLLECTION)) { --communication.timeout ; if( communication.timeout == 0) { //COM_txStr("RESTART"); COM_restart(); return; } } communication.prevAppTime = communication.curAppTime; } switch( communication.state) { case COM_START: #if(defined __18F8722_H) ||(defined __18F46K22_H) if( UART1_hasData() == FALSE ) return; uartData = UART1_read(); #else if( UART1_hasData() == FALSE ) return; uartData = UART_read(); #endif if( uartData == communication.rx_sop ) { communication.rxPacketIndex = 0; communication.state = COM_IN_PACKET_COLLECTION; communication.timeout = TIMEOUT; } break; case COM_IN_PACKET_COLLECTION: #if(defined __18F8722_H) ||(defined __18F46K22_H) if( UART1_hasData()==FALSE ) return; uartData = UART1_read(); #else if( UART_hasData()==FALSE ) return; uartData = UART_read(); #endif if(uartData == communication.rx_eop ) { UINT8 parseResult = 0; COM_RESP_CODE txCode = COM_RESP_NONE; UINT8 *txData ; parseResult = parsePacket(&txCode); //parse packet switch( parseResult) { case IGNORE: COM_reset(); return; case PARSE_SUCCESS: if( communication.callBack != 0 ) { communication.txPacketLength = communication.callBack(&communication.rxPacketBuffer[COM_RX_DATA_START_INDEX], &communication.txCode, &txData); communication.txPacketBuffer[COM_DEVICE_ADDRESS_INDEX] = BROADCAST_ADDRESS; //store BROADCAST_ADDRESS ++communication.txPacketLength; communication.txPacketBuffer[COM_TX_CODE_INDEX] = communication.txCode; //store tx code ++communication.txPacketLength; for( i = COM_TX_DATA_START_INDEX ; i < communication.txPacketLength ; i++) //store data { communication.txPacketBuffer[i] = *txData; txData++; } } else { COM_reset(); } break; case PARSE_FAILURE: { communication.txPacketBuffer[COM_DEVICE_ADDRESS_INDEX] = DEVICE_ADDRESS; //store device address ++communication.txPacketLength; communication.txPacketBuffer[COM_TX_CODE_INDEX] = communication.txCode; //store tx code ++communication.txPacketLength; } break; default: break; } communication.state = COM_IN_TX_DATA; } else { communication.rxPacketBuffer[communication.rxPacketIndex++]=uartData; if( communication.rxPacketIndex >= RX_PACKET_SIZE) { communication.txPacketBuffer[COM_DEVICE_ADDRESS_INDEX] = DEVICE_ADDRESS; //store device address ++communication.txPacketLength; communication.txPacketBuffer[COM_TX_CODE_INDEX] = COM_RESP_OVERRUN; //store tx code ++communication.txPacketLength; communication.state = COM_IN_TX_DATA; } } break; case COM_IN_TX_DATA: COM_txData(); COM_reset(); break; default: COM_reset(); break; } communication.prevState = communication.state; }
void COM1_task(void) { volatile UINT8 uartData = 0,i; communication[0].curAppTime = GetAppTime(); if(RCSTA1bits.OERR == 1) { RCSTA1bits.CREN = 0; Delay10us(1); RCSTA1bits.CREN = 1; } #ifdef UART2_ACTIVE if(RCSTA2bits.OERR == 1) { RCSTA2bits.CREN = 0; Delay10us(1); RCSTA2bits.CREN = 1; } #endif for(i = 0 ; i < ACTIVE_USARTS ; i++ ) { if( communication[i].prevAppTime != communication[i].curAppTime) { if( communication[i].prevState == communication[i].state && (communication[i].state == COM_IN_PACKET_COLLECTION)) { --communication[i].timeout ; if( communication[i].timeout == 0) { COM1_restart(); return; } } communication[i].prevAppTime = communication[i].curAppTime; } switch( communication[i].state) { case COM_START: if( i == UART1) { if( UART1_hasData() == FALSE ) return; uartData = UART1_read(); #ifdef PASS_THROUGH UART2_write(uartData); UART2_transmit(); #endif } else if( i == UART2) { if( UART2_hasData() == FALSE ) return; uartData = UART2_read(); } if( uartData == communication[i].rx_sop ) { communication[i].rxPacketIndex = 0; communication[i].state = COM_IN_PACKET_COLLECTION; } break; case COM_IN_PACKET_COLLECTION: if( i == UART1) { if( UART1_hasData() == FALSE ) return; uartData = UART1_read(); #ifdef PASS_THROUGH UART2_write(uartData); UART2_transmit(); #endif } else if( i == UART2) { if( UART2_hasData() == FALSE ) return; uartData = UART2_read(); } if(uartData == communication[i].rx_eop ) { UINT8 parseResult = 0; COM_RESP_CODE txCode = COM_RESP_NONE; UINT8 *txData ; #ifdef __NO_CHECKSUM__ parseResult = PARSE_SUCCESS; #else parseResult = parse1Packet((&txCode), i); //parse packet #endif switch( parseResult) { case IGNORE: COM1_reset(); return; case PARSE_SUCCESS: if( communication[i].callBack != 0 ) { communication[i].txPacketLength = communication[i].callBack(&communication[i].rxPacketBuffer[COM_RX_DATA_START_INDEX], &communication[i].txCode, &txData); if(communication[i].txCode == COM_RESP_NONE ) { COM1_reset(); return; } communication[i].txPacketBuffer[COM_DEVICE_ADDRESS_INDEX] = DEVICE_ADDRESS; //store device address ++communication[i].txPacketLength; communication[i].txPacketBuffer[COM_TX_CODE_INDEX] = communication[i].txCode; //store tx code ++communication[i].txPacketLength; for( i = COM_TX_DATA_START_INDEX ; i < communication[i].txPacketLength ; i++) //store data { communication[i].txPacketBuffer[i] = *txData; txData++; } } else { COM1_reset(); } break; case PARSE_FAILURE: { communication[i].txPacketBuffer[COM_DEVICE_ADDRESS_INDEX] = DEVICE_ADDRESS; //store device address ++communication[i].txPacketLength; communication[i].txPacketBuffer[COM_TX_CODE_INDEX] = txCode; //store tx code ++communication[i].txPacketLength; } break; default: break; } communication[i].state = COM_IN_TX_DATA; } else { communication[i].rxPacketBuffer[communication[0].rxPacketIndex++]=uartData; communication[i].timeout = 0; if( communication[i].rxPacketIndex >= RX_PACKET_SIZE) { communication[i].txPacketBuffer[COM_DEVICE_ADDRESS_INDEX] = DEVICE_ADDRESS; //store device address ++communication[i].txPacketLength; communication[i].txPacketBuffer[COM_TX_CODE_INDEX] = COM_RESP_OVERRUN; //store tx code ++communication[i].txPacketLength; communication[i].state = COM_IN_TX_DATA; } } break; case COM_IN_TX_DATA: COM_txData(); COM1_reset(); break; default: COM1_reset(); break; } communication[i].prevState = communication[i].state; } }