00001 /* CRC7 implementation */ 00002 /* Copyright (C) 2009 Holger Dietze 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) version 3. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License along 00015 * with this program; if not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef CRC7_H 00019 #define CRC7_H 00020 00021 #include <stdint.h> 00022 00039 inline uint8_t _crc7_update (uint8_t crc, uint8_t data) 00040 { 00041 #if 0 00042 /* bit-wise algorithm */ 00043 int i; 00044 for (i = 8; i > 0; i--) { 00045 uint8_t b = (data >> 7) ^ ((crc & 0x80) >> 7); 00046 crc = (crc ^ b ^ (b << 3)) << 1; 00047 crc &= 0xfe; 00048 data <<= 1; 00049 } 00050 return crc; 00051 #else 00052 /* no need to do &= 0xfe, as that bit will not be used anyway */ 00053 /*crc &= 0x7f;*/ 00054 data ^= crc; 00055 crc = data >> 4; 00056 data ^= crc ^ (crc >> 3); 00057 return /*0xfe & */((uint8_t)(data<<1) ^ (uint8_t)(data << 4)); 00058 #endif 00059 } 00060 00061 00062 #endif