Consolidate all the CRC functions and LUTs in one place

Use this forum for requesting small changes in ChibiOS. Large changes should be discussed in the development forum. This forum is NOT for support.
Post Reply
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Consolidate all the CRC functions and LUTs in one place

Post by faisal »

At the title says ... There are a couple CRC functions and LUTs scatter throughout the source. I think they should be in one place. I use pycrc to generate my crc library, and a little script to automate it (it renames some constants that were otherwise common). The license is permissive too (MIT). See attached.

For example, the crc in os/ex/subsystems/mfs/mfs.c, is actually crc_16ccitt. It also support arbitrary polynomial generation, so the crc7 used in os/hal/src/hal_mmc_spi.c could just as easily be generated.
Attachments
crc.zip
(177.79 KiB) Downloaded 595 times
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Consolidate all the CRC functions and LUTs in one place

Post by Giovanni »

Hi,

I am adding a crypto driver to the HAL, all such algorithms will have a place there (maybe I will add a separate one for just CRCs).

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Consolidate all the CRC functions and LUTs in one place

Post by steved »

Giovanni wrote:maybe I will add a separate one for just CRCs

Good idea - I suspect CRCs are more commonly used than specific crypto functions.

If you envisage pre-generating LUTs for the more popular CRCs, I mostly need CCITT-16, and occasionally CRC-16. Also worth remembering the 8-bit CRCs; there's one used with the Dallas OWB which could be handy.
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: Consolidate all the CRC functions and LUTs in one place

Post by steved »

Looking at the crypto interface, a separate CRC interface is definitely going to be simpler.

Three parameters usually suffice for me:

Code: Select all

uint16_t crcCCITT16(unsigned char *data, uint16_t length, uint16_t seed);
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: Consolidate all the CRC functions and LUTs in one place

Post by faisal »

A separate CRC directory makes more sense. The library of CRC functions I included in my first post need not (actually shouldn't ..) be part of HAL drivers. It probably deserves to be in HAL/lib. Hope we can get all those included (the one missing is crc7 whic is used in hal_mmc_spi.c. PyCRC can easily generate that as well ...).
faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: Consolidate all the CRC functions and LUTs in one place

Post by faisal »

*Bump*

Here is an example of a script to generate a bunch of CRC functions using pyCRC and an associated makefile. Something like this could be included in hal/lib, with the pyCRC dependency in os/ext

Code: Select all

#!/bin/bash

declare -a model_names=\
(
    "crc-5"
    "crc-8"
    "dallas-1-wire"
    "crc-12-3gpp"
    "crc-15"
    "crc-16"
    "crc-16-usb"
    "crc-16-modbus"
    "crc-16-genibus"
    "crc-16-ccitt"
    "r-crc-16"
    "kermit"
    "x-25"
    "xmodem"
    "zmodem"
    "crc-24"
    "crc-32"
    "crc-32c"
    "crc-32-mpeg"
    "crc-32-bzip2"
    "posix"
    "jam"
    "xfer"
    "crc-64"
    "crc-64-jones"
    "crc-64-xz"
)

declare -a source_names=\
(
    "crc_5"
    "crc_8"
    "crc_ow"
    "crc_12_3g"
    "crc_15"
    "crc_16"
    "crc_16usb"
    "crc_16mb"
    "crc_16gb"
    "crc_16ccitt"
    "crc_r16"
    "crc_kmt"
    "crc_x25"
    "crc_xm"
    "crc_zm"
    "crc_24"
    "crc_32"
    "crc_32c"
    "crc_32mpg"
    "crc_32bzip2"
    "crc_psx"
    "crc_jam"
    "crc_xfer"
    "crc_64"
    "crc_64jones"
    "crc_64xz"
)

arr_len=${#model_names[@]}
arr_len_test=${#source_names[@]}

if [ $arr_len -ne $arr_len_test ];
then
    echo "ERROR: Length mismatch between model_names and source_names."
    exit 1
fi

PYCRC_PATH="./pycrc-0.9.1"

for ((i=1;i<${arr_len}+1; i++));
do
    echo "Generating model: ${model_names[$i-1]}"
    $PYCRC_PATH/pycrc.py --model ${model_names[$i-1]} --algorithm table-driven --generate h -o ${source_names[$i-1]}.h --symbol-prefix ${source_names[$i-1]}
    $PYCRC_PATH/pycrc.py --model ${model_names[$i-1]} --algorithm table-driven --generate c -o ${source_names[$i-1]}.c --symbol-prefix ${source_names[$i-1]}
    sed -i "s/CRC_ALGO_/${source_names[$i-1]^^}_ALGO_/" ${source_names[$i-1]}.h
done

# Autogenerate crc.mk
rm crc.mk
{   echo -e "\n#Generated using gen_src.sh. Do not modify manually. \n"
    echo -e "CRCPATH := \$(UTILS)/crc"
    echo -e "CRCSRC :="
    echo -e "UTILSINC += \$(CRCPATH)/"
    echo -e "CRCSRC += \\"

    for srcn in "${source_names[@]::${#source_names[@]}-1}"
    do
        echo -e "    \$(CRCPATH)/$srcn.c \\"
    done
    echo -e "    \$(CRCPATH)/${source_names[-1]}.c"
    echo
    echo -e "UTILSSRC += \$(CRCSRC)"
    echo
} >> crc.mk
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Consolidate all the CRC functions and LUTs in one place

Post by Giovanni »

bump
Post Reply