|
| 1 | +// CRC compatibility, adapted from the C-only comments here: |
| 2 | +// http://svn.savannah.nongnu.org/viewvc/trunk/avr-libc/include/util/crc16.h?revision=933&root=avr-libc&view=markup |
| 3 | + |
| 4 | +/* Copyright (c) 2002, 2003, 2004 Marek Michalkiewicz |
| 5 | + All rights reserved. |
| 6 | +
|
| 7 | + Redistribution and use in source and binary forms, with or without |
| 8 | + modification, are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | + * Redistributions of source code must retain the above copyright |
| 11 | + notice, this list of conditions and the following disclaimer. |
| 12 | +
|
| 13 | + * Redistributions in binary form must reproduce the above copyright |
| 14 | + notice, this list of conditions and the following disclaimer in |
| 15 | + the documentation and/or other materials provided with the |
| 16 | + distribution. |
| 17 | +
|
| 18 | + * Neither the name of the copyright holders nor the names of |
| 19 | + contributors may be used to endorse or promote products derived |
| 20 | + from this software without specific prior written permission. |
| 21 | +
|
| 22 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 23 | + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 24 | + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 26 | + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 29 | + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 30 | + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 31 | + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 32 | + POSSIBILITY OF SUCH DAMAGE. */ |
| 33 | + |
| 34 | +#ifndef _UTIL_CRC16_H_ |
| 35 | +#define _UTIL_CRC16_H_ |
| 36 | + |
| 37 | +#include <stdint.h> |
| 38 | + |
| 39 | +static inline uint16_t _crc16_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused)); |
| 40 | +static inline uint16_t _crc16_update(uint16_t crc, uint8_t data) |
| 41 | +{ |
| 42 | + unsigned int i; |
| 43 | + |
| 44 | + crc ^= data; |
| 45 | + for (i = 0; i < 8; ++i) { |
| 46 | + if (crc & 1) { |
| 47 | + crc = (crc >> 1) ^ 0xA001; |
| 48 | + } else { |
| 49 | + crc = (crc >> 1); |
| 50 | + } |
| 51 | + } |
| 52 | + return crc; |
| 53 | +} |
| 54 | + |
| 55 | +static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data) __attribute__((always_inline, unused)); |
| 56 | +static inline uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data) |
| 57 | +{ |
| 58 | + unsigned int i; |
| 59 | + |
| 60 | + crc = crc ^ ((uint16_t)data << 8); |
| 61 | + for (i=0; i<8; i++) { |
| 62 | + if (crc & 0x8000) { |
| 63 | + crc = (crc << 1) ^ 0x1021; |
| 64 | + } else { |
| 65 | + crc <<= 1; |
| 66 | + } |
| 67 | + } |
| 68 | + return crc; |
| 69 | +} |
| 70 | + |
| 71 | +static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data) __attribute__((always_inline, unused)); |
| 72 | +static inline uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data) |
| 73 | +{ |
| 74 | + data ^= (crc & 255); |
| 75 | + data ^= data << 4; |
| 76 | + |
| 77 | + return ((((uint16_t)data << 8) | (crc >> 8)) ^ (uint8_t)(data >> 4) |
| 78 | + ^ ((uint16_t)data << 3)); |
| 79 | +} |
| 80 | + |
| 81 | +static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data) __attribute__((always_inline, unused)); |
| 82 | +static inline uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data) |
| 83 | +{ |
| 84 | + unsigned int i; |
| 85 | + |
| 86 | + crc = crc ^ data; |
| 87 | + for (i = 0; i < 8; i++) { |
| 88 | + if (crc & 0x01) { |
| 89 | + crc = (crc >> 1) ^ 0x8C; |
| 90 | + } else { |
| 91 | + crc >>= 1; |
| 92 | + } |
| 93 | + } |
| 94 | + return crc; |
| 95 | +} |
| 96 | + |
| 97 | +#endif |
0 commit comments