cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong CRC calculation with optimized code

matic
Associate III
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;
12 REPLIES 12
qwer.asdf
Senior
Posted on February 24, 2016 at 08:54

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

Some quotes from the RM0090 reference manual:

''Each write operation into the data register creates a combination of the previous CRC value and the new one (CRC computation is done on the whole 32-bit data word, and not byte per byte).''

 

 

''The CRC registers have to be accessed by words (32 bits).''

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
Up vote any posts that you find helpful, it shows what's working..
matic
Associate III
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;