Example #1
0
//*****************************************************************************
//
//! Calculates the CRC-8-CCITT of an array of bytes.
//!
//! \param ucCrc is the starting CRC-8-CCITT value.
//! \param pucData is a pointer to the data buffer.
//! \param ulCount is the number of bytes in the data buffer.
//!
//! This function is used to calculate the CRC-8-CCITT of the input buffer.
//! The CRC-8-CCITT is computed in a running fashion, meaning that the entire
//! data block that is to have its CRC-8-CCITT computed does not need to be
//! supplied all at once.  If the input buffer contains the entire block of
//! data, then \b ucCrc should be set to 0.  If, however, the entire block of
//! data is not available, then \b ucCrc should be set to 0 for the first
//! portion of the data, and then the returned value should be passed back in
//! as \b ucCrc for the next portion of the data.
//!
//! For example, to compute the CRC-8-CCITT of a block that has been split into
//! three pieces, use the following:
//!
//! \verbatim
//!     ucCrc = Crc8CCITT(0, pucData1, ulLen1);
//!     ucCrc = Crc8CCITT(ucCrc, pucData2, ulLen2);
//!     ucCrc = Crc8CCITT(ucCrc, pucData3, ulLen3);
//! \endverbatim
//!
//! Computing a CRC-8-CCITT in a running fashion is useful in cases where the
//! data is arriving via a serial link (for example) and is therefore not all
//! available at one time.
//!
//! \return The CRC-8-CCITT of the input data.
//
//*****************************************************************************
unsigned char
Crc8CCITT(unsigned char ucCrc, const unsigned char *pucData,
          unsigned long ulCount)
{
    unsigned long ulTemp;

    //
    // If the data buffer is not short-aligned, then perform a single step of
    // the CRC to make it short-aligned.
    //
    if((unsigned long)pucData & 1)
    {
        //
        // Perform the CRC on this input byte.
        //
        ucCrc = CRC8_ITER(ucCrc, *pucData);

        //
        // Skip this input byte.
        //
        pucData++;
        ulCount--;
    }

    //
    // If the data buffer is not word-aligned and there are at least two bytes
    // of data left, then perform two steps of the CRC to make it word-aligned.
    //
    if(((unsigned long)pucData & 2) && (ulCount > 1))
    {
        //
        // Read the next short.
        //
        ulTemp = *(unsigned short *)pucData;

        //
        // Perform the CRC on these two bytes.
        //
        ucCrc = CRC8_ITER(ucCrc, ulTemp);
        ucCrc = CRC8_ITER(ucCrc, ulTemp >> 8);

        //
        // Skip these input bytes.
        //
        pucData += 2;
        ulCount -= 2;
    }
Example #2
0
//*****************************************************************************
//
//! Calculates the CRC-8-CCITT of an array of bytes.
//!
//! \param ui8Crc is the starting CRC-8-CCITT value.
//! \param pui8Data is a pointer to the data buffer.
//! \param ui32Count is the number of bytes in the data buffer.
//!
//! This function is used to calculate the CRC-8-CCITT of the input buffer.
//! The CRC-8-CCITT is computed in a running fashion, meaning that the entire
//! data block that is to have its CRC-8-CCITT computed does not need to be
//! supplied all at once.  If the input buffer contains the entire block of
//! data, then \b ui8Crc should be set to 0.  If, however, the entire block of
//! data is not available, then \b ui8Crc should be set to 0 for the first
//! portion of the data, and then the returned value should be passed back in
//! as \b ui8Crc for the next portion of the data.
//!
//! For example, to compute the CRC-8-CCITT of a block that has been split into
//! three pieces, use the following:
//!
//! \verbatim
//!     ui8Crc = Crc8CCITT(0, pui8Data1, ui32Len1);
//!     ui8Crc = Crc8CCITT(ui8Crc, pui8Data2, ui32Len2);
//!     ui8Crc = Crc8CCITT(ui8Crc, pui8Data3, ui32Len3);
//! \endverbatim
//!
//! Computing a CRC-8-CCITT in a running fashion is useful in cases where the
//! data is arriving via a serial link (for example) and is therefore not all
//! available at one time.
//!
//! \return The CRC-8-CCITT of the input data.
//
//*****************************************************************************
uint8_t
Crc8CCITT(uint8_t ui8Crc, const uint8_t *pui8Data, uint32_t ui32Count)
{
    uint32_t ui32Temp;

    //
    // If the data buffer is not 16 bit-aligned, then perform a single step of
    // the CRC to make it 16 bit-aligned.
    //
    if((uint32_t)pui8Data & 1)
    {
        //
        // Perform the CRC on this input byte.
        //
        ui8Crc = CRC8_ITER(ui8Crc, *pui8Data);

        //
        // Skip this input byte.
        //
        pui8Data++;
        ui32Count--;
    }

    //
    // If the data buffer is not word-aligned and there are at least two bytes
    // of data left, then perform two steps of the CRC to make it word-aligned.
    //
    if(((uint32_t)pui8Data & 2) && (ui32Count > 1))
    {
        //
        // Read the next 16 bits.
        //
        ui32Temp = *(uint16_t *)pui8Data;

        //
        // Perform the CRC on these two bytes.
        //
        ui8Crc = CRC8_ITER(ui8Crc, ui32Temp);
        ui8Crc = CRC8_ITER(ui8Crc, ui32Temp >> 8);

        //
        // Skip these input bytes.
        //
        pui8Data += 2;
        ui32Count -= 2;
    }