2020-07-06 04:57 AM
Hello,
I need to calculate checksum over a struct I have defined. Can anyone please help me with this?
The data is structured as below:
typedef struct eeprom_struct {
// char serial_number[5];
float detector_comp_ch0;
float detector_comp_ch1;
float detector_comp_ch2;
float detector_comp_ch3;
float detector_comp_ch4;
float detector_comp_ch5;
float detector_comp_ch6;
float detector_comp_ch7;
char serial_number[14];
uint checksum;
} eeprom_struct;
I know this is not directly related to STM32 MCUs, but I really need to solve this, and could not get an answer anywhere else.
Thank you in advance for your help.
Best regards,
Vouria
Solved! Go to Solution.
2020-07-08 02:11 PM
This all seems like mundane C coding,
uint sum32(int size, uint8_t *ptr)
{
uint sum = 0;
while(size--) sum += *ptr++; // sum bytes over buffer
return(sum);
}
2020-07-06 06:32 AM
eeprom_struct foo;
...
foo.checksum = crc32(0xFFFFFFFF, sizeof(eeprom_struct) - sizeof(uint), (void *)&foo); // A crc with initial value, length in bytes, byte pointer to buffer
2020-07-06 06:48 AM
Thanks a lot, should I write the crc32 function?
2020-07-06 01:41 PM
> could not get an answer anywhere else
O, you poor one! There are not a single example... https://lmgtfy.com/?q=CRC+example+C&iie=1
> should I write the crc32 function?
No, it will write itself, you just sit and wait for a it to happen.
2020-07-07 12:39 AM
thank you very much for your help, the problem is that the function is to calculate CRC and I am looking for checksum
2020-07-07 01:42 AM
CRC is also a checksum. Oh, but you need The Checksum - the one without a name/algoritm. Here is the code for it:
foo.checksum = 0xF001;
It's also superior compared to other checksums - it never fails!
2020-07-07 02:32 AM
thank you for your reply
2020-07-08 01:45 PM
> I need to calculate checksum over a struct I have defined.
Why?
JW
2020-07-08 02:06 PM
Integrity, establish it has been written completely? Usual stuff I'd imagine.
2020-07-08 02:07 PM
That or some other summing algorithm, wasn't clear what part of the problem was posing the challenge here.