Skip to main content
infoinfo991
Associate III
January 4, 2014
Question

need to change CRC peripheral's polynomial

  • January 4, 2014
  • 6 replies
  • 2220 views
Posted on January 04, 2014 at 03:13

The reference manual for the STM32F4 states that the polynomial for the CRC generator is 0x4C11DB7

I must change it to 0x0B5D0A7D or else my application will not work, as I am spoofing another device

Is there a trick to calculating an input such that it seems as if my CRC used 0x0B5D0A7D?

Is there anyway to change it? I have searched, the FAQ does not help at all.
    This topic has been closed for replies.

    6 replies

    lowpowermcu
    Associate III
    January 4, 2014
    Posted on January 04, 2014 at 11:49

    Hi Franck,

    The polynomial is not configurable in STM32F4 family so you have to do the calculation by software if you have another polynom.

    PS: The CRC in STM32F3 family has the polynomial configurable.

    Tesla DeLorean
    Guru
    January 4, 2014
    Posted on January 04, 2014 at 15:43

    What are the data and crc shift directions?

    Can you provide a couple of example data sets?
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    infoinfo991
    Associate III
    January 4, 2014
    Posted on January 04, 2014 at 20:46

    I have the example data at the bottom of this page

    http://eleccelerator.com/wiki/index.php?title=DualShock_4

    infoinfo991
    Associate III
    January 4, 2014
    Posted on January 04, 2014 at 21:09

    Oops I made a mistake, it seems like 0x4C11DB7 will work after all

    Tesla DeLorean
    Guru
    January 5, 2014
    Posted on January 05, 2014 at 02:36

    Yeah, your talking about the result/remainder, not the polynomial. They claim it's the same although I haven't run the math on the STM32, which is kind of backward for a little endian processor.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Tesla DeLorean
    Guru
    January 7, 2014
    Posted on January 07, 2014 at 21:24

    Ok, so a couple of issues with the STM32 F1/F2/F4 hardware

    It shifts in the wrong direction, and consumes 32-bits at a time. You'd need to bit reverse (RBIT) the value before passing it to the hardware, and special case the last 0-3 bytes. There was a

    /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/CRC%20computation&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1617

    about using the hardware for PKZIP/CRC32 computations. Here is a worked case for a software solution.

    /*
    Dual Shock 4 CRC Demo - Clive - sourcer32@gmail.com
    Data set for example pulled from
    http://eleccelerator.com/wiki/index.php?title=DualShock_4
    STM32 Thread
    [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/need%20to%20change%20CRC%20peripheral%27s%20polynomial&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx?Paged%3DTRUE%26p_StickyPost%3D%26p_DiscussionLastUpdated%3D20140105%252020%253a55%253a31%26p_ID%3D36700%26View%3D%257bF47A9ED8%252dE726%252d42BE%252dACED%252d732F13B66581%257d%26FolderCTID%3D0x012001%26PageFirstRow%3D41&currentviews=48]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fcortex%5fmx%5fstm32%2fneed%20to%20change%20CRC%20peripheral%27s%20polynomial&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https%3A%2F%2Fmy%2Est%2Ecom%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex%5Fmx%5Fstm32%2FAllItems%2Easpx%3FPaged%3DTRUE%26p%5FStickyPost%3D%26p%5FDiscussionLastUpdated%3D20140105%252020%253a55%253a31%26p%5FID%3D36700%26View%3D%257bF47A9ED8%252dE726%252d42BE%252dACED%252d732F13B66581%257d%26FolderCTID%3D0x012001%26PageFirstRow%3D41¤tviews=48
    cl -Ox dualshk.c
    >dualshk
    88 Total
    75 Payload
    0B5D0A7D CRC Encoded
    0B5D0A7D
    0B5D0A7D
    */
    #include <
    windows.h
    >
    #include <
    stdio.h
    >
    //****************************************************************************
    DWORD Crc32Byte(DWORD Crc, BYTE Data)
    {
    int i;
    Crc = Crc ^ (DWORD)Data;
    for(i=0; i<
    8
    ; i++)
    if (Crc & 1)
    Crc = (Crc >> 1) ^ 0xEDB88320; // Polynomial used in PKZIP, CRC-32 Right Shifting
    else
    Crc = (Crc >> 1);
    return(Crc);
    }
    //****************************************************************************
    DWORD Crc32ByteBlock(DWORD Crc, DWORD Size, BYTE *Buffer)
    {
    while(Size--)
    Crc = Crc32Byte(Crc, *Buffer++);
    return(Crc);
    }
    //****************************************************************************
    DWORD Crc32FastBlock(DWORD Crc, DWORD Size, BYTE *Buffer)
    {
    static const DWORD CrcTable[] = { // For 0xEDB88320 Polynomial
    0x00000000,0x1DB71064,0x3B6E20C8,0x26D930AC,0x76DC4190,0x6B6B51F4,0x4DB26158,0x5005713C,
    0xEDB88320,0xF00F9344,0xD6D6A3E8,0xCB61B38C,0x9B64C2B0,0x86D3D2D4,0xA00AE278,0xBDBDF21C };
    while(Size--)
    {
    Crc = Crc ^ (DWORD)*Buffer++;
    Crc = (Crc >> 4) ^ CrcTable[Crc & 0x0F]; // Process 8-bits, two rounds of 4-bits
    Crc = (Crc >> 4) ^ CrcTable[Crc & 0x0F];
    }
    return(Crc);
    }
    //****************************************************************************
    // http://eleccelerator.com/wiki/index.php?title=DualShock_4
    BYTE TestVector[] = { 0x02,0x15,0x20,0x53,0x00,0x4F,0x00,0x42,0x00, // Header
    0xA1,0x11,0xC0,0x00,0x83,0x81,0x7E,0x7E,0x08,0x00,0x3C,0x00,0x00,0x83,0xA2, // Payload
    0x07,0xF1,0xFF,0xF9,0xFF,0x04,0x00,0x21,0x03,0x17,0x1F,0x29,0xF9,0x00,0x00,
    0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,
    0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
    0x80,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,
    0x7D,0x0A,0x5D,0x0B }; // CRC32
    //****************************************************************************
    int main(int argc, char **argv)
    {
    printf(''%d Total
    
    '', sizeof(TestVector));
    printf(''%d Payload
    
    '', sizeof(TestVector) - 4 - 9); // Less 9 byte header, and 4 byte CRC
    printf(''%08X CRC Encoded
    
    '', *((DWORD *)&TestVector[sizeof(TestVector) - 4]) );
    // Initial value 0xFFFFFFFF, then inverted
    printf(''%08
    
    X'',~Crc32ByteBlock(0xFFFFFFFF, sizeof(TestVector) - 4 - 9, &TestVector[9]) );
    printf(''%08
    
    X'',~Crc32FastBlock(0xFFFFFFFF, sizeof(TestVector) - 4 - 9, &TestVector[9]) );
    return(0);
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..