Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LT_2978FaultLog.cpp
Go to the documentation of this file.
1 /*!
2 LTC SMBus Support: API for a LTC2978 Fault Log
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_2978FaultLog LT_2978FaultLog
49 //! @}
50 
51 /*! @file
52  @ingroup LT_2978FaultLog
53  Library Header File for LT_2978FaultLog
54 */
55 
56 #include <Arduino.h>
57 #include "LT_2978FaultLog.h"
58 
59 
61 {
62  faultLog2978 = NULL;
63 
64  buffer = NULL;
65  voutPeaks = NULL;
66  voutDatas = NULL;
67 }
68 
69 
71 {
72  return (uint8_t *)pos >= (uint8_t *)this + firstValidByte && (uint8_t *)pos + (size - 1) <= (uint8_t *)this + lastValidByte;
73 }
74 
75 /*
76  * Read LTC2978 fault log
77  *
78  * address: PMBUS address
79  */
80 void
82 {
83  uint16_t size = sizeof(struct LT_2978FaultLog::FaultLogLtc2978);
84  uint8_t *data = (uint8_t *) malloc(size);
85  if (data == 0)
86  Serial.print(F("bad malloc."));
87  data[0] = 0x00;
88 
89 
90  // Copy to RAM
92  // MFR_FAULT_LOG_RESTORE requires a delay (see datasheet), typical delay is 2ms, multiplying by 10 for safety
93  delay(20);
94  // Read block data with log
95  pmbus_->smbus()->readBlock(address, MFR_FAULT_LOG, data, 255);
96 
98 
99  log->firstValidByte = 47;
100 
101  log->lastValidByte = 237;
102 
103  uint8_t cycle_start = log->preamble.position_last;
104 
105  log->loops = (LT_2978FaultLog::FaultLogReadLoopLtc2978 *) (log->telemetryData - 39 + cycle_start);
106 
107  faultLog2978 = log;
108 }
109 
110 
112 {
113  free(faultLog2978);
114  faultLog2978 = 0;
115 }
116 
118 {
119  return (uint8_t *)faultLog2978;
120 }
121 
123 {
124  return 255;
125 }
126 
127 void LT_2978FaultLog::dumpBinary(Print *printer)
128 {
129  dumpBin(printer, (uint8_t *)faultLog2978, 255);
130 }
131 
132 void LT_2978FaultLog::print(Print *printer)
133 {
134  if (printer == 0)
135  printer = &Serial;
136  buffer = new char[FILE_TEXT_LINE_MAX];
137 
138  printTitle(printer);
139 
140  printTime(printer);
141 
142  printPeaks(printer);
143 
144  printAllLoops(printer);
145 
146 
147  delete [] buffer;
148 }
149 
150 
151 void LT_2978FaultLog::printTitle(Print *printer)
152 {
153  printer->print(F("LTC2978 Log Data\n"));
154 }
155 
156 void LT_2978FaultLog::printTime(Print *printer)
157 {
158  uint8_t *time = (uint8_t *)&faultLog2978->preamble.shared_time;
159  snprintf_P(buffer, FILE_TEXT_LINE_MAX, PSTR("Fault Time 0x%02x%02x%02x%02x%02x%02x\n"), time[5], time[4], time[3], time[2], time[1], time[0]);
160  printer->print(buffer);
161  printer->print((long) getSharedTime200us(faultLog2978->preamble.shared_time));
162  printer->println(F(" Ticks (200us each)"));
163 }
164 
165 void LT_2978FaultLog::printPeaks(Print *printer)
166 {
167  voutPeaks = new Peak16Words*[8];
168 
169  voutPeaks[0] = &faultLog2978->preamble.peaks.vout0_peaks;
170  voutPeaks[1] = &faultLog2978->preamble.peaks.vout1_peaks;
171  voutPeaks[2] = &faultLog2978->preamble.peaks.vout2_peaks;
172  voutPeaks[3] = &faultLog2978->preamble.peaks.vout3_peaks;
173  voutPeaks[4] = &faultLog2978->preamble.peaks.vout4_peaks;
174  voutPeaks[5] = &faultLog2978->preamble.peaks.vout5_peaks;
175  voutPeaks[6] = &faultLog2978->preamble.peaks.vout6_peaks;
176  voutPeaks[7] = &faultLog2978->preamble.peaks.vout7_peaks;
177 
178 
179  printer->println(F("\nPeak Values:"));
180  printer->println(F("--------"));
181 
182  printFastChannel(0, printer);
183  printFastChannel(1, printer);
184 
185  float vin_max, vin_min;
188  printer->print(F("Vin: Min: "));
189  printer->print(vin_min, 6);
190  printer->print(F(", Peak: "));
191  printer->print(vin_max, 6);
192  printer->println();
193 
194  printFastChannel(2, printer);
195  printFastChannel(3, printer);
196 
197  float temp_max, temp_min;
200  printer->print(F("Temp: Min: "));
201  printer->print(temp_min, 6);
202  printer->print(F(", Peak: "));
203  printer->print(temp_max, 6);
204  printer->println();
205 
206  printFastChannel(4, printer);
207  printFastChannel(5, printer);
208  printFastChannel(6, printer);
209  printFastChannel(7, printer);
210 
211  delete [] voutPeaks;
212 }
213 
214 void LT_2978FaultLog::printFastChannel(uint8_t index, Print *printer)
215 {
216  float vout_peak, vout_min;
217  vout_peak = math_.lin16_to_float(getLin16WordVal(voutPeaks[index]->peak), 0x13);
218  vout_min = math_.lin16_to_float(getLin16WordVal(voutPeaks[index]->min), 0x13);
219  printer->print(F("Vout"));
220  printer->print(index);
221  printer->print(F(": Min: "));
222  printer->print(vout_min, 6);
223  printer->print(F(", Peak: "));
224  printer->println(vout_peak, 6);
225 }
226 
227 void LT_2978FaultLog::printAllLoops(Print *printer)
228 {
229  voutDatas = new VoutData*[8];
230 
231  printer->println(F("Fault Log Loops Follow:"));
232  printer->println(F("(most recent data first)"));
233 
234  for (int index = 0; index <= 5 && (index < 5 || faultLog2978->isValidData(&faultLog2978->loops[index])); index++)
235  {
236  printLoop(index, printer);
237  }
238 
239  delete [] voutDatas;
240 }
241 
242 void LT_2978FaultLog::printLoop(uint8_t index, Print *printer)
243 {
244  printer->println(F("-------"));
245  printer->print(F("Loop: "));
246  printer->println(index);
247  printer->println(F("-------"));
248 
249  voutDatas[0] = &faultLog2978->loops[index].vout_data0;
250  voutDatas[1] = &faultLog2978->loops[index].vout_data1;
251  voutDatas[2] = &faultLog2978->loops[index].vout_data2;
252  voutDatas[3] = &faultLog2978->loops[index].vout_data3;
253  voutDatas[4] = &faultLog2978->loops[index].vout_data4;
254  voutDatas[5] = &faultLog2978->loops[index].vout_data5;
255  voutDatas[6] = &faultLog2978->loops[index].vout_data6;
256  voutDatas[7] = &faultLog2978->loops[index].vout_data7;
257 
258  uint8_t stat;
259  float val;
260 
261  printLoopChannel(7, printer);
262  printLoopChannel(6, printer);
263  printLoopChannel(5, printer);
264  printLoopChannel(4, printer);
265 
267  {
268  printer->println(F("TEMPERATURE:"));
270  snprintf_P(buffer, FILE_TEXT_LINE_MAX, PSTR(" STATUS_TEMP: 0x%02x\n"), stat);
271  printer->print(buffer);
272  }
274  {
276  printer->print(F(" READ_TEMP: "));
277  printer->print(val, 6);
278  printer->println(F(" C"));
279  }
280 
281  printLoopChannel(3, printer);
282  printLoopChannel(2, printer);
283 
285  {
286  printer->println(F("VIN:"));
288  snprintf_P(buffer, FILE_TEXT_LINE_MAX, PSTR(" STATUS_INPUT: 0x%02x\n"), stat);
289  printer->print(buffer);
290  }
292  {
294  printer->print(F(" READ_VIN: "));
295  printer->print(val, 6);
296  printer->println(F(" V"));
297  }
298 
299  printLoopChannel(1, printer);
300  printLoopChannel(0, printer);
301  printer->println();
302 
303 }
304 
305 void LT_2978FaultLog::printLoopChannel(uint8_t index, Print *printer)
306 {
307  uint8_t stat;
308  float val;
309 
310  if (faultLog2978->isValidData(&voutDatas[index]->status_mfr, 1))
311  {
312  printer->print(F("CHAN"));
313  printer->print(index);
314  printer->println(F(":"));
315  stat = getRawByteVal(voutDatas[index]->status_mfr);
316  snprintf_P(buffer, FILE_TEXT_LINE_MAX, PSTR(" STATUS_MFR: 0x%02x\n"), stat);
317  printer->print(buffer);
318  }
319  if (faultLog2978->isValidData(&voutDatas[index]->status_vout, 1))
320  {
321  stat = getRawByteVal(voutDatas[index]->status_vout);
322  snprintf_P(buffer, FILE_TEXT_LINE_MAX, PSTR(" STATUS_VOUT: 0x%02x\n"), stat);
323  printer->print(buffer);
324  }
325  if (faultLog2978->isValidData(&voutDatas[index]->read_vout))
326  {
327  val = math_.lin16_to_float(getLin16WordReverseVal(voutDatas[index]->read_vout), 0x13);
328  printer->print(F(" READ_VOUT: "));
329  printer->print(val, 6);
330  printer->println(F(" V"));
331  }
332 }
333 
334 
335 
struct Lin16WordReverse read_vout
uint16_t getLin5_11WordReverseVal(Lin5_11WordReverse value)
void dumpBin(Print *printer, uint8_t *log, uint8_t size)
#define MFR_FAULT_LOG
Definition: LT_PMBus.h:140
struct Lin5_11WordReverse vin
void read(uint8_t address)
Reads the fault log from the specified address, reserves memory to hold the data. ...
float lin16_to_float(lin16_t lin16_mant, lin16m_t vout_mode)
virtual void sendByte(uint8_t address, uint8_t command)=0
SMBus send byte command.
void dumpBinary(Print *printer=0)
Dumps binary of the fault log to a Print inheriting object, or Serial if none specified.
uint16_t getLin5_11WordVal(Lin5_11Word value)
void print(Print *printer=0)
Pretty prints this part&#39;s fault log to a Print inheriting object, or Serial if none specified...
void release()
Frees the memory reserved for the fault log.
LT_PMBusMath math_
static LT_PMBus * pmbus
Definition: DC2875A.ino:82
float lin11_to_float(lin11_t xin)
#define FILE_TEXT_LINE_MAX
Definition: LT_FaultLog.h:60
#define min(a, b)
LT_PMBus * pmbus_
Definition: LT_FaultLog.h:129
uint8_t * getBinary()
Get binary of the fault log or NULL if no log.
static uint8_t address
Definition: DC2091A.ino:83
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
struct FaultLogPreambleLtc2978 preamble
static int16_t pos
uint16_t getLin16WordVal(Lin16Word value)
LT_2978FaultLog(LT_PMBus *pmbus)
Constructor.
LT_SMBus * smbus()
Definition: LT_PMBus.h:401
LTC SMBus Support: Implementation for a LTC2978 Fault Log.
uint64_t getSharedTime200us(FaultLogTimeStamp time_stamp)
bool isValidData(void *pos, uint8_t size=2)
static int index
virtual uint8_t readBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)=0
SMBus read block command.
struct FaultLogReadLoopLtc2978 * loops
FaultLogLtc2978 * faultLog2978
struct Lin5_11WordReverse read_temp1
uint8_t getRawByteVal(RawByte value)
uint16_t getLin16WordReverseVal(Lin16WordReverse value)
#define MFR_FAULT_LOG_RESTORE
Definition: LT_PMBus.h:136
uint16_t getBinarySize()
Get size of binary data.
PMBus communication.
Definition: LT_PMBus.h:370