mirror of
https://github.com/Telecominfraproject/OpenCellular.git
synced 2025-12-30 18:41:11 +00:00
Building a single buffer for crc calculation is often inefficient, so add a new function that calculates crc8 from an existing crc8. BUG=chromium:576911 BRANCH=None TEST=Manual on sentry with subsequent commit. Verify that smbus communication with battery is functional. Change-Id: I05ffedb81ffcf0c126acda5f6212b3147b1580a1 Signed-off-by: Shawn Nematbakhsh <shawnn@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/333786 Commit-Ready: Shawn N <shawnn@chromium.org> Tested-by: Shawn N <shawnn@chromium.org> Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
29 lines
600 B
C
29 lines
600 B
C
/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
* found in the LICENSE file.
|
|
*/
|
|
#include "common.h"
|
|
#include "crc8.h"
|
|
|
|
inline uint8_t crc8(const uint8_t *data, int len)
|
|
{
|
|
return crc8_arg(data, len, 0);
|
|
}
|
|
|
|
uint8_t crc8_arg(const uint8_t *data, int len, uint8_t previous_crc)
|
|
{
|
|
unsigned crc = previous_crc << 8;
|
|
int i, j;
|
|
|
|
for (j = len; j; j--, data++) {
|
|
crc ^= (*data << 8);
|
|
for (i = 8; i; i--) {
|
|
if (crc & 0x8000)
|
|
crc ^= (0x1070 << 3);
|
|
crc <<= 1;
|
|
}
|
|
}
|
|
|
|
return (uint8_t)(crc >> 8);
|
|
}
|