summaryrefslogtreecommitdiff
path: root/packages/i2c-tools/picodlp-control/Crc8.c
blob: 87dcf5c2f493af846b8029312adb77d4e40f2bb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/****************************************************************************
*
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
*   See README and COPYING for more details.
*
****************************************************************************/
/**
*
*   @file   Crc8.c 
*
*   @brief  This file contains the definition of the CRC-8 algorithim
*           used by SMBus
*           
*
*****************************************************************************/

/* ---- Include Files ----------------------------------------------------- */

#include "Config.h"

#include "Crc8.h"

#include "Log.h"

/* ---- Public Variables -------------------------------------------------- */
/* ---- Private Constants and Types --------------------------------------- */
/* ---- Private Variables ------------------------------------------------- */
/* ---- Private Function Prototypes --------------------------------------- */
/* ---- Functions --------------------------------------------------------- */

/****************************************************************************/
/**
*   Calculates the CRC-8 used as part of SMBus.
*
*   CRC-8 is defined to be x^8 + x^2 + x + 1
*
*   To use this function use the following template:
*
*       crc = Crc8( crc, data );
*/

#if 0   // Traditional implementation

#define POLYNOMIAL    (0x1070U << 3) 

unsigned char Crc8( unsigned char inCrc, unsigned char inData )
{
        int i;
    unsigned short  data;

    data = inCrc ^ inData;
    data <<= 8;
  
        for ( i = 0; i < 8; i++ ) 
    {
                if (( data & 0x8000 ) != 0 )
        {
            data = data ^ POLYNOMIAL;
        }
                data = data << 1;
        }

#if 0
#if defined( LogBuf2 )
    LogBuf2( "Crc8: data:0x%02x crc:0x%02x\n", inData, (unsigned char)( data >> 8 ));
#else

    Log( "Crc8: data:0x%02x crc:0x%02x\n", inData, (unsigned char)( data >> 8 ));
#endif

#endif


        return (unsigned char)( data >> 8 );

} // Crc8

#else   // Optimized for 8 bit CPUs (0x22 bytes on ATMega128 versus 0x30 for above version)

unsigned char Crc8( unsigned char inCrc, unsigned char inData )
{
        unsigned char   i;
    unsigned char   data;

    data = inCrc ^ inData;
  
        for ( i = 0; i < 8; i++ ) 
    {
        if (( data & 0x80 ) != 0 )
        {
            data <<= 1;
            data ^= 0x07;
        }
        else
        {
            data <<= 1;
        }
        }

#if 0
#if defined( LogBuf2 )
    LogBuf2( "Crc8: data:0x%02x crc:0x%02x\n", inData, data );
#else

    Log( "Crc8: data:0x%02x crc:0x%02x\n", inData, data );
#endif

#endif


        return data;

} // Crc8

#endif


#if defined( CFG_CRC8BLOCK )

/****************************************************************************/
/**
*   Calculates the CRC-8 used as part of SMBus over a block of memory.
*/

uint8_t Crc8Block( uint8_t crc, uint8_t *data, uint8_t len )
{
    while ( len > 0 )
    {
        crc = Crc8( crc, *data++ );
        len--;
    }

    return crc;

} // Crc8Block

#endif  // CFG_CRC8BLOCK

/** @} */