fp@1126: /*****************************************************************************
fp@1126:  *
fp@1363:  *  $Id$
fp@1363:  *
fp@1363:  *  Copyright (C) 2006-2009  Florian Pose, Ingenieurgemeinschaft IgH
fp@1363:  *
fp@1363:  *  This file is part of the IgH EtherCAT Master.
fp@1363:  *
fp@1363:  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
fp@1363:  *  modify it under the terms of the GNU General Public License version 2, as
fp@1363:  *  published by the Free Software Foundation.
fp@1363:  *
fp@1363:  *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
fp@1363:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
fp@1363:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
fp@1363:  *  Public License for more details.
fp@1363:  *
fp@1363:  *  You should have received a copy of the GNU General Public License along
fp@1363:  *  with the IgH EtherCAT Master; if not, write to the Free Software
fp@1363:  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
fp@1363:  *
fp@1363:  *  ---
fp@1363:  *
fp@1363:  *  The license mentioned above concerns the source code only. Using the
fp@1363:  *  EtherCAT technology and brand is only permitted in compliance with the
fp@1363:  *  industrial property and similar rights of Beckhoff Automation GmbH.
fp@1126:  *
fp@1126:  ****************************************************************************/
fp@1126: 
fp@1126: #include "sii_crc.h"
fp@1126: 
fp@1126: /*****************************************************************************/
fp@1126: 
fp@1126: /** Calculates the SII checksum field.
fp@1126:  *
fp@1126:  * The checksum is generated with the polynom x^8+x^2+x+1 (0x07) and an
fp@1126:  * initial value of 0xff (see IEC 61158-6-12 ch. 5.4).
fp@1126:  *
fp@1126:  * The below code was originally generated with PYCRC
fp@1126:  * http://www.tty1.net/pycrc
fp@1126:  *
fp@1126:  * ./pycrc.py --width=8 --poly=0x07 --reflect-in=0 --xor-in=0xff
fp@1126:  *   --reflect-out=0 --xor-out=0 --generate c --algorithm=bit-by-bit
fp@1126:  *
fp@1126:  * \return CRC8
fp@1126:  */
fp@1126: uint8_t calcSiiCrc(
fp@1126:         const uint8_t *data, /**< pointer to data */
fp@1126:         size_t length /**< number of bytes in \a data */
fp@1126:         )
fp@1126: {
fp@1126:     unsigned int i;
fp@1126:     uint8_t bit, byte, crc = 0x48;
fp@1126: 
fp@1126:     while (length--) {
fp@1126:         byte = *data++;
fp@1126:         for (i = 0; i < 8; i++) {
fp@1126:             bit = crc & 0x80;
fp@1126:             crc = (crc << 1) | ((byte >> (7 - i)) & 0x01);
fp@1126:             if (bit) crc ^= 0x07;
fp@1126:         }
fp@1126:     }
fp@1126: 
fp@1126:     for (i = 0; i < 8; i++) {
fp@1126:         bit = crc & 0x80;
fp@1126:         crc <<= 1;
fp@1126:         if (bit) crc ^= 0x07;
fp@1126:     }
fp@1126: 
fp@1126:     return crc;
fp@1126: }
fp@1126: 
fp@1126: /*****************************************************************************/