Skip to main content
matic
Associate III
February 16, 2016
Question

Wrong CRC calculation with optimized code

  • February 16, 2016
  • 12 replies
  • 2335 views
Posted on February 16, 2016 at 20:00

Hi.

I encountered a problem where I use CRC hardware with optimized code in Keil. I use CRC hardware inside of interrupt routine and without optimization I get correct result. But if I turn optimization on (level 1 is enough) I don't get correct result anymore. Do you have any idea what could be wrong here?

Before calculation (already inside of ISR) I reset CRC and then do 6 consecutive writes to DR register. First I write a half-word and after that five writes of a byte variable from an array. Then I store a result to new 8-bit variable:

*(uint16_t *)&CRC->DR = half_word_var;

*(uint8_t *)&CRC->DR = byte_array[0];

*(uint8_t *)&CRC->DR = byte_array[1];

*(uint8_t *)&CRC->DR = byte_array[2];

*(uint8_t *)&CRC->DR = byte_array[3];

*(uint8_t *)&CRC->DR = byte_array[4];

*(uint8_t *)&CRC->DR = byte_array[5];

byte_result = (uint8_t)CRC->DR;
    This topic has been closed for replies.

    12 replies

    Tesla DeLorean
    Guru
    February 24, 2016
    Posted on February 24, 2016 at 13:34

    Aha, I misunderstood the situation then, sorry. In that case the casting is completely unnecessary, isn't it?

    While it's not conveyed here, the OP has an F3 or F0 part which has a more advanced CRC peripheral.

    In this case, as I recall, it's being set in an 8-bit mode and can then be feed bytes, half-words, and words.

    If the forum was not so awful you'd be able to click on the avatar and see the post history.

    I'd prefer it if people would keep one thread to discuss their project than keep forking threads and creating new ones about materially the same topic or progression of implementation and losing all the context.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    matic
    maticAuthor
    Associate III
    February 24, 2016
    Posted on February 24, 2016 at 22:18

    Yes, I use F3 MCU and if I use CRC without casting, I don't get correct result. Actually it is correct, but not in the way that I want.

    Example:

    CRC->DR  =  0xAAAA;   // Half-word

    CRC->DR  =  0xBBBB;    // Half-word

    In that case the CRC would be as if it is calculated over 0xAAAA0000BBBB.

    If I want to get CRC over 0xAAAABBBB, I have to write:

    CRC->DR  =  0xAAAA;     // For the first write, casting is not required

    *(uint16_t *)&CRC->DR  =  0xBBBB;