//***************************************************************************** // //! GetPacket() receives a data packet. //! //! \param pui8Data is the location to store the data received from the device. //! \param pui8Size is the number of bytes returned in the pui8Data buffer that //! was provided. //! //! This function receives a packet of data from UART port. //! //! \returns The function returns zero to indicated success while any non-zero //! value indicates a failure. // //***************************************************************************** int32_t GetPacket(uint8_t *pui8Data, uint8_t *pui8Size) { uint8_t ui8CheckSum; uint8_t ui8Size; // // Get the size and the checksum. // do { if(UARTReceiveData(&ui8Size, 1)) { return(-1); } } while(ui8Size == 0); if(UARTReceiveData(&ui8CheckSum, 1)) { return(-1); } *pui8Size = ui8Size - 2; if(UARTReceiveData(pui8Data, *pui8Size)) { *pui8Size = 0; return(-1); } // // Calculate the checksum from the data. // if(CheckSum(pui8Data, *pui8Size) != ui8CheckSum) { *pui8Size = 0; return(NakPacket()); } return(AckPacket()); }
//***************************************************************************** // //! GetPacket() receives a data packet. //! //! \param pucData is the location to store the data received from the device. //! \param pucSize is the number of bytes returned in the pucData buffer that //! was provided. //! //! This function receives a packet of data from UART port. //! //! \returns The function returns zero to indicated success while any non-zero //! value indicates a failure. // //***************************************************************************** int GetPacket(unsigned char *pucData, unsigned char *pucSize) { unsigned char ucCheckSum; unsigned char ucSize; // // Get the size waht and the checksum. // do { if(UARTReceiveData(&ucSize, 1)) { return(-1); } } while(ucSize == 0); if(UARTReceiveData(&ucCheckSum, 1)) { return(-1); } *pucSize = ucSize - 2; if(UARTReceiveData(pucData, *pucSize)) { *pucSize = 0; return(-1); } // // Calculate the checksum from the data. // if(CheckSum(pucData, *pucSize) != ucCheckSum) { *pucSize = 0; return(NakPacket()); } return(AckPacket()); }
//***************************************************************************** // //! Receives a data packet. //! //! \param pui8Data is the location to store the data that is sent to the boot //! loader. //! \param pui32Size is the number of bytes returned in the pui8Data buffer //! that was provided. //! //! This function receives a packet of data from specified transfer function. //! //! \return Returns zero to indicate success while any non-zero value indicates //! a failure. // //***************************************************************************** int ReceivePacket(uint8_t *pui8Data, uint32_t *pui32Size) { uint32_t ui32Size, ui32CheckSum; // // Wait for non-zero data before getting the first byte that holds the // size of the packet we are receiving. // ui32Size = 0; while(ui32Size == 0) { ReceiveData((uint8_t *)&ui32Size, 1); } // // Subtract off the size and checksum bytes. // ui32Size -= 2; // // Receive the checksum followed by the actual data. // ReceiveData((uint8_t *)&ui32CheckSum, 1); // // If there is room in the buffer then receive the requested data. // if(*pui32Size >= ui32Size) { // // Receive the actual data in the packet. // ReceiveData(pui8Data, ui32Size); // // Send a no acknowledge if the checksum does not match, otherwise send // an acknowledge to the packet later. // if(CheckSum(pui8Data, ui32Size) != (ui32CheckSum & 0xff)) { // // Indicate tha the packet was not received correctly. // NakPacket(); // // Packet was not received, there is no valid data in the buffer. // return(-1); } } else { // // If the caller allocated a buffer that was too small for the received // data packet, receive it but don't fill the buffer. // Then inform the caller that the packet was not received correctly. // while(ui32Size--) { ReceiveData(pui8Data, 1); } // // Packet was not received, there is no valid data in the buffer. // return(-1); } // // Make sure to return the number of bytes received. // *pui32Size = ui32Size; // // Packet was received successfully. // return(0); }