Home > Back-end >  How to generate CRC7 based on lookup table?
How to generate CRC7 based on lookup table?

Time:05-11

I am trying to implement CRC-7/MMC checksum with pregenerated lookup table. This is the code so far:

#include <iostream>
#include <string>
#include <cstdint>

using namespace std;

/* CRC-7/MMC
Poly: 0x09
Xorout: NO
Init: 0x00
Check value: 0x75 for "123456789"
*/

uint16_t CRC7_table[256];

void generate_crc7_table() {
    uint16_t byte;
    for (uint16_t i = 0; i < 256; i  ) {
        byte = i;
        for (uint16_t bit = 0; bit < 8; bit  ) {
            if (byte & 1) { //if lsb is 1 
                byte >>= 1;
                byte ^= 0x09;
            }
            else
                byte >>= 1;
        }
        CRC7_table[i] = byte >> 1; //to drop off MSB
    }
}

uint16_t crc7(string input) {
    uint16_t reg = 0;
    uint16_t b;

    for (uint16_t i = 0; i < input.length(); i  ) {
        b = (input[i] ^ reg) & 0xFF;
        reg = (reg >> 1) ^ CRC7_table[b];
    }
    return reg;
}


int main()
{
    generate_crc7_table();
    cout << hex << crc7("123456789") << endl;
    return 0;
}

But it gives wrong output. I should get 0x75 but I get 0x07. I used this website to checkout the outputs. Any suggestion or idea is highly appreciated. Thanks.

CodePudding user response:

Note that the CRC definition you pointed to includes refin=false refout=false. That CRC is not reflected, so it is computed with left shifts, not right shifts.

Given that, and the fact that the CRC is less than eight bits in length, you will also want to keep the seven bits at the top of the byte being used for calculation, as opposed to the bottom. I.e. bits 1 to 7, as opposed to bits 0 to 6. The polynomial is then also shifted up by one for the table calculation.

This allows the table-driven, byte-wise calculation to simply exclusive-or each message byte into the byte being used for the calculations. If you want to return the CRC in the low seven bits, you can shift it down one at the end.

Example (0x12 is 0x09 shifted up one):

#include <iostream>
#include <string>

uint8_t table[256];

void make_table() {
    uint8_t octet = 0;
    do {
        uint8_t crc = octet;
        for (int k = 0; k < 8; k  )
            crc = crc & 0x80 ? (crc << 1) ^ 0x12 : crc << 1;
        table[octet  ] = crc;
    } while (octet);
}

uint8_t crc7(std::string const& input) {
    uint8_t crc = 0;
    for (auto octet : input)
        crc = table[crc ^ octet];
    return crc >> 1;
}

int main() {
    make_table();
    std::cout << std::hex << (unsigned)crc7("123456789") << '\n';
}

crcany will generate CRC code for you, given the definition. Since your CRC-7/MMC is in Greg's catalogue, crcany will generate that code out of the box, along with the other 100 CRCs defined there. The generated code includes bit-wise, byte-wise, and word-wise calculations.

  • Related