Page 1 of 1

Consolidate all the CRC functions and LUTs in one place

Posted: Wed Oct 11, 2017 2:00 pm
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.

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

Posted: Sun Oct 22, 2017 11:37 am
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

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

Posted: Sun Oct 22, 2017 2:14 pm
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.

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

Posted: Mon Oct 23, 2017 1:43 pm
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);

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

Posted: Wed Jan 17, 2018 7:41 pm
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 ...).

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

Posted: Mon Dec 10, 2018 10:27 pm
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

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

Posted: Sat Jan 05, 2019 8:27 pm
by Giovanni
bump