/*************************************************************************************************** * @fn MT_UartProcessZToolData * * @brief | SOP | Data Length | CMD | Data | FCS | * | 1 | 1 | 2 | 0-Len | 1 | * * Parses the data and determine either is SPI or just simply serial data * then send the data to correct place (MT or APP) * * @param port - UART port * event - Event that causes the callback * * * @return None ***************************************************************************************************/ void MT_UartProcessZToolData ( uint8 port, uint8 event ) { uint8 ch; uint8 bytesInRxBuffer; (void)event; // Intentionally unreferenced parameter while (Hal_UART_RxBufLen(port)) { HalUARTRead (port, &ch, 1); switch (state) { case SOP_STATE: if (ch == MT_UART_SOF) state = LEN_STATE; break; case LEN_STATE: LEN_Token = ch; tempDataLen = 0; /* Allocate memory for the data */ pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof ( mtOSALSerialData_t ) + MT_RPC_FRAME_HDR_SZ + LEN_Token ); if (pMsg) { /* Fill up what we can */ pMsg->hdr.event = CMD_SERIAL_MSG; pMsg->msg = (uint8*)(pMsg+1); pMsg->msg[MT_RPC_POS_LEN] = LEN_Token; state = CMD_STATE1; } else { state = SOP_STATE; return; } break; case CMD_STATE1: pMsg->msg[MT_RPC_POS_CMD0] = ch; state = CMD_STATE2; break; case CMD_STATE2: pMsg->msg[MT_RPC_POS_CMD1] = ch; /* If there is no data, skip to FCS state */ if (LEN_Token) { state = DATA_STATE; } else { state = FCS_STATE; } break; case DATA_STATE: /* Fill in the buffer the first byte of the data */ pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen++] = ch; /* Check number of bytes left in the Rx buffer */ bytesInRxBuffer = Hal_UART_RxBufLen(port); /* If the remain of the data is there, read them all, otherwise, just read enough */ if (bytesInRxBuffer <= LEN_Token - tempDataLen) { HalUARTRead (port, &pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen], bytesInRxBuffer); tempDataLen += bytesInRxBuffer; } else { HalUARTRead (port, &pMsg->msg[MT_RPC_FRAME_HDR_SZ + tempDataLen], LEN_Token - tempDataLen); tempDataLen += (LEN_Token - tempDataLen); } /* If number of bytes read is equal to data length, time to move on to FCS */ if ( tempDataLen == LEN_Token ) state = FCS_STATE; break; case FCS_STATE: FSC_Token = ch; /* Make sure it's correct */ if ((MT_UartCalcFCS ((uint8*)&pMsg->msg[0], MT_RPC_FRAME_HDR_SZ + LEN_Token) == FSC_Token)) { osal_msg_send( App_TaskID, (byte *)pMsg ); } else { /* deallocate the msg */ osal_msg_deallocate ( (uint8 *)pMsg ); } /* Reset the state, send or discard the buffers at this point */ state = SOP_STATE; break; default: break; } } }