/* CRC-8: the poly is 0x31 (x^8 + x^5 + x^4 + 1)  */
static uint8_t calc_crc8(uint8_t *buf, uint16_t length)
/* CRC-8: the poly is 0x31 (x^8 + x^5 + x^4 + 1)  */
static uint8_t calc_crc8(uint8_t *buf, uint16_t length)
{
    uint8_t crc = 0x00;
    uint8_t i = 0;
    while (length--) {
        crc ^= *buf++;
        for (i = 8; i > 0; i--) {
            if (crc & 0x80) {
                crc = (crc << 1) ^ 0x31;
            } else {
                crc <<= 1;
            }
        }
    }
    return crc;
}