crcmod


  • Pybytes Beta

    Does somebody know if there is an implementation of crcmod available for mircopython?
    I need to calculate crc for 16bits values.



  • @gertjanvanhethof

    I have a DALLAS 1Wire implementation.

    I'm sure you can find look up values to change it to the CRC8 you require.

    def crc8Nib (data):
    #'''
    CRC8_TABLE_SML1 = bytes([0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41])
    CRC8_TABLE_SML2 = bytes([0x00, 0x9d, 0x23, 0xbe, 0x46, 0xdb, 0x65, 0xf8, 0x8c, 0x11, 0xaf, 0x32, 0xca, 0x57, 0xe9, 0x74])
    #'''
    try:
    crc = 0xFF # 0xFF seeded
    for x in data:
    t = (x ^ crc) & 0xFF
    crc = CRC8_TABLE_SML1[t & 0x0f] ^ CRC8_TABLE_SML2[t >> 4]
    return crc;
    except ValueError:
    pass


  • Pybytes Beta

    Thxs for you answer.

    Do you also hava a CRC8 function which is able to do the following?

    Name: CRC-8
    Width: 8 bit
    Polynomial 0x31 (x8 + x5 + x4 + 1)
    Initialization 0xFF
    Final XOR: 0x00
    Examples: CRC (0xBEEF) = 0x92



  • @gertjanvanhethof
    This is my CRC16 function based on a very common CRC16 method.
    Perhaps it will help you.
    You may need to change the seed value to 0x0000, depending on what CRC16 you are hoping to implement

    def crc16Nib (data):
    CRC16_TBL_SML = ([0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400,])
    crc = 0xFFFF # 0xFFFF seeded
    for x in data:
    crc = CRC16_TBL_SML[(x ^ crc) & 0x0F] ^ (crc >> 4) # because using the reflected algorithm, the low nibble is processed first
    crc = CRC16_TBL_SML[((x >> 4) ^ crc) & 0x0F] ^ (crc >> 4)
    return crc;


Log in to reply
 

Pycom on Twitter