Mistake on this page?
Report an issue in GitHub or email us

MbedCRC

The MbedCRC Class provides support for Cyclic Redundancy Check (CRC) algorithms. MbedCRC is a template class with polynomial value and polynomial width as arguments.

You can use the compute API to calculate CRC for the selected polynomial. If data is available in parts, you must call the compute_partial_start, compute_partial and compute_partial_stop APIs in the proper order to get the correct CRC value. You can use the get_polynomial and get_width APIs to learn the current object's polynomial and width values.

ROM polynomial tables are for supported 8/16-bit CCITT, 16-bit IBM and 32-bit ANSI polynomials. By default, ROM tables are used for CRC computation. If ROM tables are not available, then CRC is computed at runtime bit by bit for all data input.

For boards that support hardware CRC, the MbedCRC API replaces the software implementation of CRC to take advantage of the hardware acceleration the board provides.

MbedCRC class reference

MbedCRC examples

Example 1

Below is a CRC example to compute 32-bit CRC.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

int main()
{
    MbedCRC<POLY_32BIT_ANSI, 32> ct;

    char  test[] = "123456789";
    uint32_t crc = 0;

    printf("\nPolynomial = 0x%lx  Width = %d \n", ct.get_polynomial(), ct.get_width());

    ct.compute((void *)test, strlen((const char *)test), &crc);
    printf("The CRC of data \"123456789\" is : 0x%lx\n", crc);
    return 0;
}

Example 2

Below is a 32-bit CRC example using compute_partial APIs.

/*
 * Copyright (c) 2017-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

int main()
{
    MbedCRC<POLY_32BIT_ANSI, 32> ct;

    char  test[] = "123456789";
    uint32_t crc;

    ct.compute_partial_start(&crc);
    ct.compute_partial((void *)&test, 4, &crc);
    ct.compute_partial((void *)&test[4], 5, &crc);
    ct.compute_partial_stop(&crc);

    printf("The CRC of 0x%lx \"123456789\" is \"0xCBF43926\" Result: 0x%lx\n",
           ct.get_polynomial(), crc);

    return 0;
}

Example 3

Below is a CRC example for the SD driver.

/*
 * Copyright (c) 2018-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */

#include "mbed.h"

int crc_sd_7bit()
{
    MbedCRC<POLY_7BIT_SD, 7> ct;
    char test[5];
    uint32_t crc;

    test[0] = 0x40;
    test[1] = 0x00;
    test[2] = 0x00;
    test[3] = 0x00;
    test[4] = 0x00;

    ct.compute((void *)test, 5, &crc);
    // CRC 7-bit as 8-bit data
    crc = (crc | 0x01) & 0xFF;
    printf("The CRC of 0x%lx \"CMD0\" is \"0x95\" Result: 0x%lx\n",
           ct.get_polynomial(), crc);

    test[0] = 0x48;
    test[1] = 0x00;
    test[2] = 0x00;
    test[3] = 0x01;
    test[4] = 0xAA;

    ct.compute((void *)test, 5, &crc);
    // CRC 7-bit as 8-bit data
    crc = (crc | 0x01) & 0xFF;
    printf("The CRC of 0x%lx \"CMD8\" is \"0x87\" Result: 0x%lx\n",
           ct.get_polynomial(), crc);

    test[0] = 0x51;
    test[1] = 0x00;
    test[2] = 0x00;
    test[3] = 0x00;
    test[4] = 0x00;

    ct.compute((void *)test, 5, &crc);
    // CRC 7-bit as 8-bit data
    crc = (crc | 0x01) & 0xFF;
    printf("The CRC of 0x%lx \"CMD17\" is \"0x55\" Result: 0x%lx\n",
           ct.get_polynomial(), crc);

    return 0;
}

int crc_sd_16bit()
{
    char test[512];
    uint32_t crc;
    MbedCRC<POLY_16BIT_CCITT, 16> sd(0, 0, false, false);

    memset(test, 0xFF, 512);
    // 512 bytes with 0xFF data --> CRC16 = 0x7FA1
    sd.compute((void *)test, 512, &crc);
    printf("16BIT SD CRC (512 bytes 0xFF) is \"0x7FA1\" Result: 0x%lx\n", crc);
    return 0;
}

int main()
{
    crc_sd_16bit();
    crc_sd_7bit();
    return 0;
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.