Mbed OS 文档翻译 之 参考(API(驱动(MbedCRC)))

MbedCRC

MbedCRC 类支持循环冗余校验(CRC)算法。MbedCRC 是一个模板类,其多项式值和多项式宽度作为参数。

您可以使用计算 API 计算所选多项式的 CRC。如果数据在部分中可用,则必须按正确的顺序调用 compute_partial_start,compute_partial 和 compute_partial_stop API 以获取正确的 CRC 值。您可以使用 get_polynomial 和 get_width API 来了解当前对象的多项式和宽度值。

ROM 多项式表用于支持的 8/16 位 CCITT,16 位 IBM 和 32 位 ANSI 多项式。默认情况下,ROM 表用于 CRC 计算。如果 ROM 表不可用,则在运行时逐位计算所有数据输入的 CRC。

对于支持硬件 CRC 的平台,MbedCRC API 取代了 CRC 的软件实现,以利用平台提供的硬件加速。

MbedCRC 类参考

mbed::MbedCRC< polynomial, width > 类模板参考

公共类型
enum  CrcMode { HARDWARE = 0, TABLE = 1, BITWISE }
typedef uint64_t crc_data_size_t
公共成员函数
 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
int32_t compute (void *buffer, crc_data_size_t size, uint32_t *crc)
int32_t compute_partial (void *buffer, crc_data_size_t size, uint32_t *crc)
int32_t compute_partial_start (uint32_t *crc)
int32_t compute_partial_stop (uint32_t *crc)
uint32_t get_polynomial (void) const
uint8_t get_width (void) const
template<>
 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
template<>
 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
template<>
 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
template<>
 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)
template<>
 MbedCRC (uint32_t initial_xor, uint32_t final_xor, bool reflect_data, bool reflect_remainder)

MbedCRC 示例

示例一

下面是计算 32 位 CRC 的 CRC 示例。

main.cpp                                                                                                                                             导入到 Mbed IDE

#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;
 }

示例二

下面是使用 compute_partial API 的 32 位 CRC 示例。

main.cpp                                                                                                                                                导入到 Mbed IDE

#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%x \"123456789\" is \"0xCBF43926\" Result: 0x%x\n", 
                            ct.get_polynomial(), crc);
    
    return 0;
}

示例三

以下是 SD 驱动程序的 CRC 示例。

main.cpp                                                                                                                                                 导入到 Mbed IDE

#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%x \"CMD0\" is \"0x95\" Result: 0x%x\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%x \"CMD8\" is \"0x87\" Result: 0x%x\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%x \"CMD17\" is \"0x55\" Result: 0x%x\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%x\n", crc);
    return 0;
}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值