Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LT_SMBus.cpp
Go to the documentation of this file.
1 /*!
2 LTC SMBus Support: API for a shared SMBus layer
3 
4 @verbatim
5 
6 This API is shared with Linduino and RTOS code. End users should code to this
7 API to enable use of the PMBus code without modifications.
8 
9 @endverbatim
10 
11 
12 Copyright 2018(c) Analog Devices, Inc.
13 
14 All rights reserved.
15 
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18  - Redistributions of source code must retain the above copyright
19  notice, this list of conditions and the following disclaimer.
20  - Redistributions in binary form must reproduce the above copyright
21  notice, this list of conditions and the following disclaimer in
22  the documentation and/or other materials provided with the
23  distribution.
24  - Neither the name of Analog Devices, Inc. nor the names of its
25  contributors may be used to endorse or promote products derived
26  from this software without specific prior written permission.
27  - The use of this software may or may not infringe the patent rights
28  of one or more patent holders. This license does not release you
29  from the requirement that you obtain separate licenses from these
30  patent holders to use this software.
31  - Use of the software either in source or binary form, must be run
32  on or directly connected to an Analog Devices Inc. component.
33 
34 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
35 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
36 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
38 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
40 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45 
46 //! @ingroup PMBus_SMBus
47 //! @{
48 //! @defgroup LT_SMBus LT_SMBus: Base class of all SMBus with utility functions
49 //! @}
50 
51 /*! @file
52  @ingroup LT_SMBus
53  Library Header File for LT_SMBus
54 */
55 
56 #include "LT_SMBus.h"
57 const uint8_t table_[256] PROGMEM = { 0, 7, 14, 9, 28, 27, 18, 21,
58  56, 63, 54, 49, 36, 35, 42, 45,
59  112, 119, 126, 121, 108, 107, 98, 101,
60  72, 79, 70, 65, 84, 83, 90, 93,
61  224, 231, 238, 233, 252, 251, 242, 245,
62  216, 223, 214, 209, 196, 195, 202, 205,
63  144, 151, 158, 153, 140, 139, 130, 133,
64  168, 175, 166, 161, 180, 179, 186, 189,
65  199, 192, 201, 206, 219, 220, 213, 210,
66  255, 248, 241, 246, 227, 228, 237, 234,
67  183, 176, 185, 190, 171, 172, 165, 162,
68  143, 136, 129, 134, 147, 148, 157, 154,
69  39, 32, 41, 46, 59, 60, 53, 50,
70  31, 24, 17, 22, 3, 4, 13, 10,
71  87, 80, 89, 94, 75, 76, 69, 66,
72  111, 104, 97, 102, 115, 116, 125, 122,
73  137, 142, 135, 128, 149, 146, 155, 156,
74  177, 182, 191, 184, 173, 170, 163, 164,
75  249, 254, 247, 240, 229, 226, 235, 236,
76  193, 198, 207, 200, 221, 218, 211, 212,
77  105, 110, 103, 96, 117, 114, 123, 124,
78  81, 86, 95, 88, 77, 74, 67, 68,
79  25, 30, 23, 16, 5, 2, 11, 12,
80  33, 38, 47, 40, 61, 58, 51, 52,
81  78, 73, 64, 71, 82, 85, 92, 91,
82  118, 113, 120, 127, 106, 109, 100, 99,
83  62, 57, 48, 55, 34, 37, 44, 43,
84  6, 1, 8, 15, 26, 29, 20, 19,
85  174, 169, 160, 167, 178, 181, 188, 187,
86  150, 145, 152, 159, 138, 141, 132, 131,
87  222, 217, 208, 215, 194, 197, 204, 203,
88  230, 225, 232, 239, 250, 253, 244, 243
89  };
90 
92 {
93  poly_ = 0x07;
94  crc_polynomial_ = 0x0107;
95  pec_enabled_ = false;
96 
97  // Set up the PEC table.
99 }
100 
101 
102 /*
103  * Construct a table for PEC calculation
104  */
105 void LT_SMBus::constructTable(uint16_t user_polynomial)
106 {
107 // uint8_t temp;
108 // uint8_t a;
109 // uint8_t poly = (uint8_t)(user_polynomial & 0x0FF);
110 
111 // if ((user_polynomial & 0xFFFFFF01) != 0x00000101)
112 // throw new ApplicationException("Invalid userPolynomial.");
113 
114  crc_polynomial_ = user_polynomial;
115  // for (i = 0; i < 256; i++)
116  // {
117  // temp = 0;
118  // a = (uint8_t)i;
119  // for (j = 0; j < 8; j++)
120  // {
121  // if (((temp ^ a) & 0x80) != 0)
122  // {
123  // temp = (uint8_t)((temp << 1) ^ poly);
124  // }
125  // else
126  // {
127  // temp <<= 1;
128  // }
129  // a <<= 1;
130  // }
131  // table_[i] = temp;
132  // }
133 }
134 
135 /*
136  * Helper function for calculating PEC
137  */
138 uint8_t LT_SMBus::doCalculate(uint8_t data, uint8_t begining_value)
139 {
140  return table_[(0xFF & (begining_value ^ data))];
141 }
142 
143 /*
144  * Helper function for calculating PEC
145  */
146 uint8_t LT_SMBus::calculate(uint8_t *data, uint8_t begining_value, uint8_t start_index, uint8_t length)
147 {
148  uint8_t i;
149  uint8_t ret_value = begining_value;
150 
151  for (i = start_index; i < (start_index + length); i++)
152  {
153  ret_value = doCalculate(data[i], ret_value);
154  }
155 
156  return ret_value;
157 }
158 
159 /*
160  * Clear the PEC value and get ready to calculate.
161  */
163 {
164  running_pec_ = 0;
165 }
166 
167 /*
168  * Add a value to the PEC
169  */
170 void LT_SMBus::pecAdd(uint8_t byte_value)
171 {
172  uint8_t i;
173  running_pec_ = running_pec_ ^ byte_value;
174 
175  for (i=0; i<8; i++)
176  {
177  if ((running_pec_ & 0x80) > 0x00)
178  {
179  running_pec_ = (running_pec_ << 1) ^ poly_;
180  }
181  else
182  {
183  running_pec_ = running_pec_ << 1;
184  }
185 
186  }
187 }
188 
189 /*
190  * Get the final PEC value
191  */
192 uint8_t LT_SMBus::pecGet(void)
193 {
194  return running_pec_;
195 }
196 
197 /*
198  * Check CRC of block data organized as 31 data bytes plus CRC
199 */
200 bool LT_SMBus::checkCRC (uint8_t *data)
201 {
202  uint8_t pos;
203  bool nok;
204 
205  pecClear();
206 
207  for (pos = 0; pos < 31; pos++)
208  pecAdd(data[pos]);
209 
210  nok = pecGet() != data[pos];
211 
212  pecClear();
213 
214  return nok;
215 }
216 
217 /*
218  * Get CRC of block data organized as 31 data bytes plus CRC
219 */
220 uint8_t LT_SMBus::getCRC (uint8_t *data)
221 {
222  uint8_t pos;
223  bool nok;
224  uint8_t pec;
225 
226  pecClear();
227 
228  for (pos = 0; pos < 31; pos++)
229  pecAdd(data[pos]);
230 
231  pec = pecGet();
232 
233  pecClear();
234 
235  return pec;
236 }
LTC SMBus Support: API for a shared SMBus layer.
uint8_t getCRC(uint8_t *data)
Get CRC of block data organized as 31 bytes pluse CRC.
Definition: LT_SMBus.cpp:220
bool pec_enabled_
Definition: LT_SMBus.h:64
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
uint8_t doCalculate(uint8_t data, uint8_t begining_value)
Helper function for pec.
Definition: LT_SMBus.cpp:138
static int16_t pos
void constructTable(uint16_t user_polynomial)
Initialize the table used to speed up pec calculations.
Definition: LT_SMBus.cpp:105
bool checkCRC(uint8_t *data)
Check CRC of block data organized as 31 data bytes plus CRC.
Definition: LT_SMBus.cpp:200
uint8_t pecGet(void)
Get the current pec result.
Definition: LT_SMBus.cpp:192
unsigned char poly_
The poly used in the calc.
Definition: LT_SMBus.h:67
uint16_t crc_polynomial_
The crc poly used in the calc.
Definition: LT_SMBus.h:68
static bool pec
Definition: program.ino:88
uint8_t running_pec_
Temporary pec calc value.
Definition: LT_SMBus.h:66
LT_SMBus()
Definition: LT_SMBus.cpp:91
void pecClear(void)
Clear the pec value so it can start a new calculation.
Definition: LT_SMBus.cpp:162
uint8_t calculate(uint8_t *data, uint8_t begining_value, uint8_t start_index, uint8_t length)
Helper function for pec.
Definition: LT_SMBus.cpp:146
static int i
Definition: DC2430A.ino:184
void pecAdd(uint8_t byte_value)
Add a byte to the pec calculation.
Definition: LT_SMBus.cpp:170
const uint8_t table_ [256] PROGMEM
Definition: LT_SMBus.cpp:57