This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] MSPM0L1117:无法在主机 MCU 上复制 CRC-16 8 月 CCIT 结果

Guru**** 2667725 points

Other Parts Discussed in Thread: SYSCONFIG, MSPM0L1117

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1594924/mspm0l1117-cannot-replicate-crc-16-aug-ccit-results-on-a-host-mcu

器件型号: MSPM0L1117
主题中讨论的其他器件: SysConfig

尊敬的 E2E 团队:

我尝试从 MSPM0L1117 SysConfig 复制以下 CRCP 设置:

image.png

如下所示、当将 DL_CRCP_calculateBlock16 与数据 0x0002 并且大小为 0x02 时、最终结果为 0x6070:

image.png

我需要在主机 MCU 上复制这些结果、但到目前为止、我尚未找到正确的功能和设置来实现这一目标

#include <stdint.h>
#include <stdio.h>

/**
 * Calculates the CRC-16/AUG-CCITT checksum for a given data buffer.
 *
 * Parameters:
 *   data: Pointer to the input data array.
 *   len: Length of the input data in bytes.
 *
 * Returns:
 *   The calculated 16-bit CRC checksum.
 */
uint16_t crc16_aug_ccitt(const uint8_t *data, size_t len) {
    // Parameters for CRC-16/AUG-CCITT:
    // Polynomial: 0x1021
    // Initial value (seed): 0x0F1D
    // Input data not reflected, output CRC not reflected, no final XOR.
    uint16_t crc = 0x0F1D; 
    uint16_t polynomial = 0x1021;

    for (size_t i = 0; i < len; ++i) {
        crc ^= (uint16_t)(data[i] << 8); // XOR current byte with the high byte of CRC

        for (int j = 0; j < 8; ++j) {
            if (crc & 0x8000) {
                crc = (crc << 1) ^ polynomial;
            } else {
                crc <<= 1;
            }
        }
    }

    return crc; // No final XOR operation
}

int main() {
    //...
    uint8_t input_data[] = {0x00, 0x02};
    size_t data_len = sizeof(input_data);

    uint16_t result = crc16_aug_ccitt(input_data, data_len);

    //...
}

我已经为 INPUT_DATA 和 CRC 尝试了不同的字节顺序、但到目前为止、我无法复制 0x6070 的结果。  您能否帮助确定我的算法有哪些不正确或缺失?

谢谢、
Ryan

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Ryan、

    首先、 DL_CRCP_calculateBlock16() 的输入大小是 16 位数据的数量、因此对于{0x00,0x02}、API 的输入需要为 1。

    由于字节序设置为大端顺序、因此您需要更改输入数据顺序和 CRC 种子顺序、例如{0x02、0x00}和 0x1D0F。

    实际上、我建议将从器件侧 CRC 配置直接更改为小端、以便您可以在当前代码和配置中获得预期的结果。  

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    感谢 Pengfei 的帮助!  我目前不太适应更改外设端 CRC 配置。  休息一下并查看您的回复后、我修改了 c 代码、以便在原始 MSPM0L1117 应用中获得预期输出。

    #include <stdint.h>
    #include <stdio.h>
    
    /**
     * Calculates the CRC-16/AUG-CCITT checksum for a given data buffer.
     *
     * Parameters:
     *   data: Pointer to the input data array.
     *   len: Length of the input data in bytes.
     *
     * Returns:
     *   The calculated 16-bit CRC checksum.
     */
    uint16_t crc16_aug_ccitt(const uint16_t *data, size_t len) {
        // Parameters for CRC-16/AUG-CCITT:
        // Polynomial: 0x1021
        // Initial value (seed): 0x0F1D (from user input 0xf1d)
        // Input data not reflected, output CRC not reflected, no final XOR.
        uint16_t crc = 0x1D0F; 
        uint16_t polynomial = 0x1021;
    
        for (size_t i = 0; i < len; ++i) {
            crc ^= (uint16_t)(data[i] << 8); // XOR current byte with the high byte of CRC
    
            for (int j = 0; j < 8; ++j) {
                if (crc & 0x8000) {
                    crc = (crc << 1) ^ polynomial;
                } else {
                    crc <<= 1;
                }
            }
        }
    
        return crc; // No final XOR operation
    }
    
    int main() {
        // Input data 0x00000002 represented as a byte array (Big Endian)
        // The most significant byte of the input is processed first.
        uint16_t input_data[] = {0x0000, 0x0002};
        size_t data_len = sizeof(input_data);
    
        uint16_t result = crc16_aug_ccitt(input_data, data_len);
    
        printf("Calculated CRC-16/AUG-CCITT for 0x00000002: 0x%04X\n", result);
    
        return 0;
    }

    这将返回 0x6070、这是我期望收到的结果。  再次感谢您的支持!

    此致、
    Ryan