1.2.5.2 Using The Library

The SE CRC library is called to compute the CRC of a given length (8, 16, 32 bits) to append to a data buffer on transmission, and to verify the correctness of a received frame and its CRC. The following example illustrates how the USI service computes the CRC of a received frame to verify frame integrity.

Example implementation of SE CRC library usage for frame validation

static void _SRV_USI_Callback_Handle ( uint8_t *pData, uint16_t length, uintptr_t context )
{
    uint32_t crcGetValue;
    uint32_t crcRcvValue;
    SRV_USI_PROTOCOL_ID protocol;
    PCRC_CRC_TYPE crcType;

    if (length)
    {
        /* New received message */
        /* Extract Protocol */
        protocol = (SRV_USI_PROTOCOL_ID)USI_TYPE_PROTOCOL(*(pData + 1));

        /* Get CRC type from Protocol */
        crcType = _SRV_USI_GetCRCTypeFromProtocol(protocol);

        /* Check CRC */
        crcGetValue = SRV_PCRC_GetValue(pData, length - (1<<crcType),
                PCRC_HT_USI, crcType, 0);

        if (crcType == PCRC_CRC8)
        {
            crcRcvValue = (uint32_t)(*(pData + length - 1));
        }
        else if (crcType == PCRC_CRC16)
        {
            crcRcvValue = (((uint32_t)(*(pData + length - 2))) << 8) +
                          (uint32_t)(*(pData + length - 1));
        }
        else
        {
            crcRcvValue = (((uint32_t)(*(pData + length - 4))) << 24) +
                          (((uint32_t)(*(pData + length - 3))) << 16) +
                          (((uint32_t)(*(pData + length - 2))) << 8) +
                          (uint32_t)(*(pData + length - 1));
        }

        if (crcGetValue != crcRcvValue)
        {
            /* Discard message */
            return;
        }

        /* Correct frame, process it */
    }
}