cancel
Showing results for 
Search instead for 
Did you mean: 

calculating checksum of struct

VYoun
Associate III

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

1 ACCEPTED SOLUTION

Accepted Solutions

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);

}

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

View solution in original post

10 REPLIES 10

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

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

Thanks a lot, should I write the crc32 function?

Piranha
Chief II

> 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.

thank you very much for your help, the problem is that the function is to calculate CRC and I am looking for checksum

Piranha
Chief II

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!

thank you for your reply

> I need to calculate checksum over a struct I have defined.

Why?

JW

Integrity, establish it has been written completely? Usual stuff I'd imagine.

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

That or some other summing algorithm, wasn't clear what part of the problem was posing the challenge here.

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