Exemplo n.º 1
0
//*****************************************************************************
//
//! 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());
}
Exemplo n.º 2
0
//*****************************************************************************
//
//! 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());
}
Exemplo n.º 3
0
//****************************************************************************
//
//! AutoBaud() performs Automatic baud rate detection.
//!
//! This function will send the sync pattern to the board and establish basic
//! communication with the device.  The call to OpenUART() in the routine
//! main() set the baud rate that will be used.
//!
//! \return If any part of the function fails, the function will return a
//!     negative error code. The function will return 0 to indicate success.
//
//****************************************************************************
int32_t
AutoBaud(void)
{
    static uint8_t const pui8SyncPattern[]={0x55, 0x55};
    uint8_t ui8Command;
    uint8_t ui8Ack;

    //
    // Send out the sync pattern and wait for an ack from the board.
    //
    if(UARTSendData(pui8SyncPattern, 2))
    {
        return(-1);
    }

    //
    // Wait for the ACK to be received, if something besides an ACK or a zero
    // is received then something went wrong.
    //
    do
    {
        UARTReceiveData(&ui8Ack, 1);
    } while(ui8Ack == 0);

    if (ui8Ack != COMMAND_ACK)
    {
        return(-1);
    }

    //
    // Make sure we can at least communicate with the board.
    //
    ui8Command = COMMAND_PING;
    if(SendCommand(&ui8Command, 1) < 0)
    {
        return(-1);
    }
    return(0);
}
Exemplo n.º 4
0
//*****************************************************************************
//
//! SendPacket() sends a data packet.
//!
//! \param pucData is the location of the data to be sent to the device.
//! \param ucSize is the number of bytes to send from puData.
//! \param bAck is a boolean that is true if an ACK/NAK packet should be
//! received in response to this packet.
//!
//! This function sends a packet of data to the device.
//!
//! \returns The function returns zero to indicated success while any non-zero
//!     value indicates a failure.
//
//*****************************************************************************
int
SendPacket(unsigned char *pucData, unsigned char ucSize, unsigned long bAck)
{
    unsigned char ucCheckSum;
    unsigned char ucAck;

    ucCheckSum = CheckSum(pucData, ucSize);

    //
    // Make sure that we add the bytes for the size and checksum to the total.
    //
    ucSize += 2;

    //
    // Send the Size in bytes.
    //
    if(UARTSendData(&ucSize, 1))
    {
        return(-1);
    }

    //
    // Send the CheckSum
    //
    if(UARTSendData(&ucCheckSum, 1))
    {
        return(-1);
    }

    //
    // Now send the remaining bytes out.
    //
    ucSize -= 2;

    //
    // Send the Data
    //
    if(UARTSendData(pucData, ucSize))
    {
        return(-1);
    }

    //
    // Return immediately if no ACK/NAK is expected.
    //
    if(!bAck)
    {
        return(0);
    }

    //
    // Wait for the acknoledge from the device.
    //
    do
    {
        if(UARTReceiveData(&ucAck, 1))
        {
            return(-1);
        }
    }
    while(ucAck == 0);

    if(ucAck != COMMAND_ACK)
    {
        return(-1);
    }
    return(0);
}
Exemplo n.º 5
0
//*****************************************************************************
//
//! SendPacket() sends a data packet.
//!
//! \param pui8Data is the location of the data to be sent to the device.
//! \param ui8Size is the number of bytes to send from puData.
//! \param bAck is a boolean that is true if an ACK/NAK packet should be
//! received in response to this packet.
//!
//! This function sends a packet of data to the device.
//!
//! \returns The function returns zero to indicated success while any non-zero
//!     value indicates a failure.
//
//*****************************************************************************
int32_t
SendPacket(uint8_t *pui8Data, uint8_t ui8Size, bool bAck)
{
    uint8_t ui8CheckSum;
    uint8_t ui8Ack;

    ui8CheckSum = CheckSum(pui8Data, ui8Size);

    //
    // Make sure that we add the bytes for the size and checksum to the total.
    //
    ui8Size += 2;

    //
    // Send the Size in bytes.
    //
    if(UARTSendData(&ui8Size, 1))
    {
        return(-1);
    }

    //
    // Send the CheckSum
    //
    if(UARTSendData(&ui8CheckSum, 1))
    {
        return(-1);
    }

    //
    // Now send the remaining bytes out.
    //
    ui8Size -= 2;

    //
    // Send the Data
    //
    if(UARTSendData(pui8Data, ui8Size))
    {
        return(-1);
    }

    //
    // Return immediately if no ACK/NAK is expected.
    //
    if(!bAck)
    {
        return(0);
    }

    //
    // Wait for the acknowledge from the device.
    //
    do
    {
        if(UARTReceiveData(&ui8Ack, 1))
        {
            return(-1);
        }
    }
    while(ui8Ack == 0);

    if(ui8Ack != COMMAND_ACK)
    {
        return(-1);
    }
    return(0);
}