Skip to main content
VYoun
Associate III
July 6, 2020
Solved

calculating checksum of struct

  • July 6, 2020
  • 4 replies
  • 3823 views

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

    This topic has been closed for replies.
    Best answer by Tesla DeLorean

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

    }

    4 replies

    Tesla DeLorean
    Guru
    July 6, 2020

    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    VYoun
    VYounAuthor
    Associate III
    July 6, 2020

    Thanks a lot, should I write the crc32 function?

    Tesla DeLorean
    Guru
    July 8, 2020

    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Piranha
    Principal III
    July 6, 2020

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

    VYoun
    VYounAuthor
    Associate III
    July 7, 2020

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

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    July 8, 2020

    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Piranha
    Principal III
    July 7, 2020

    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!

    VYoun
    VYounAuthor
    Associate III
    July 7, 2020

    thank you for your reply

    waclawek.jan
    Super User
    July 8, 2020

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

    Why?

    JW

    Tesla DeLorean
    Guru
    July 8, 2020

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

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