Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EEPROM Checksum is either last char or last char+1 #158

Open
drf5n opened this issue Apr 11, 2022 · 2 comments
Open

EEPROM Checksum is either last char or last char+1 #158

drf5n opened this issue Apr 11, 2022 · 2 comments

Comments

@drf5n
Copy link

drf5n commented Apr 11, 2022

grbl/eeprom.c: In function 'memcpy_to_eeprom_with_checksum': grbl/eeprom.c:133:26: warning: '<<' in boolean context, did you mean '' ? [-Wint-in-bool-context] 133 | checksum = (checksum << 1) || (checksum >> 7); | ~~~~~~~~~~^~~~~ grbl/eeprom.c: In function 'memcpy_from_eeprom_with_checksum': grbl/eeprom.c:144:26: warning: '<<' in boolean context, did you mean '' ? [-Wint-in-bool-context] 144 | checksum = (checksum << 1) || (checksum >> 7); | ~~~~~~~~~~^~~~~

That looks like it was a typo -- it squashes the checksum down to a single bit of information, when it looks like it intended to roll it right one bit, so the final checksum ends up either zero or one larger than the last character in the data:

grbl-Mega/grbl/eeprom.c

Lines 130 to 149 in df87b36

void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size) {
unsigned char checksum = 0;
for(; size > 0; size--) {
checksum = (checksum << 1) || (checksum >> 7);
checksum += *source;
eeprom_put_char(destination++, *(source++));
}
eeprom_put_char(destination, checksum);
}
int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size) {
unsigned char data, checksum = 0;
for(; size > 0; size--) {
data = eeprom_get_char(source++);
checksum = (checksum << 1) || (checksum >> 7);
checksum += data;
*(destination++) = data;
}
return(checksum == eeprom_get_char(source));
}

Maybe it was supposed to be

checksum = (checksum << 1) | (checksum >> 7);

Originally posted by @drf5n in #157 (comment)

drf5n added a commit to drf5n/grbl-Mega that referenced this issue Apr 11, 2022
 Per gnea#158  The logical OR squashes the previous checksum down to 1 bit of information, resulting in the final checksum being either the last character written, or one plus the last character written.

This change switches to the bitwise-or to convert the squashing into a 1-bit roll to the left.
@drf5n
Copy link
Author

drf5n commented Apr 11, 2022

drf5n added a commit to drf5n/grbl-Mega that referenced this issue Apr 11, 2022
Per gnea#158 the logical OR discards most of the checksum information, resulting in the final chekcsum being either the last character, or the last character +1.

This fix brings the checksum in-line with grblHAL's implementation:  https://github.com/grblHAL/core/blob/3a84b58d301f04279268b4ef1045fd6bc0961be5/nuts_bolts.c#L267-L278
@drf5n
Copy link
Author

drf5n commented Apr 1, 2023

See also GRBL issue # grbl#1249

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant