AP_Math: added crc_sum_of_bytes_16()

This commit is contained in:
Andrew Tridgell 2023-12-01 09:00:05 +11:00
parent bdab1054d6
commit 7102205be3
2 changed files with 14 additions and 5 deletions

View File

@ -599,13 +599,19 @@ uint8_t parity(uint8_t byte)
return p; return p;
} }
// sums the bytes in the supplied buffer, returns that sum mod 256 // sums the bytes in the supplied buffer, returns that sum mod 0xFFFF
// (i.e. shoved into a uint8_t) uint16_t crc_sum_of_bytes_16(const uint8_t *data, uint16_t count)
uint8_t crc_sum_of_bytes(uint8_t *data, uint16_t count)
{ {
uint8_t ret = 0; uint16_t ret = 0;
for (uint32_t i=0; i<count; i++) { for (uint32_t i=0; i<count; i++) {
ret += data[i]; ret += data[i];
} }
return ret; return ret;
} }
// sums the bytes in the supplied buffer, returns that sum mod 256
// (i.e. shoved into a uint8_t)
uint8_t crc_sum_of_bytes(const uint8_t *data, uint16_t count)
{
return crc_sum_of_bytes_16(data, count) & 0xFF;
}

View File

@ -66,4 +66,7 @@ uint8_t parity(uint8_t byte);
// sums the bytes in the supplied buffer, returns that sum mod 256 // sums the bytes in the supplied buffer, returns that sum mod 256
// (i.e. shoved into a uint8_t) // (i.e. shoved into a uint8_t)
uint8_t crc_sum_of_bytes(uint8_t *data, uint16_t count); uint8_t crc_sum_of_bytes(const uint8_t *data, uint16_t count);
// sums the bytes in the supplied buffer, returns that sum mod 0xFFFF
uint16_t crc_sum_of_bytes_16(const uint8_t *data, uint16_t count);