NetBurner 3.5.6
PDF Version
PAC193x.h
1
2/***********************************************************
3
4 This is a library for Microchip PAC193x
5
6 © 2020 Microchip Technology Inc. and its subsidiaries.
7
8 Subject to your compliance with these terms, you may use Microchip
9 software and any derivatives of this software. You must retain the above
10 copyright notice with any redistribution of this software and the
11 following disclaimers.
12 It is your responsibility to comply with third party license terms
13 applicable to your use of third party software (including open source
14 software) that may accompany this Microchip software.
15
16 THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
17 EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING
18 ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
19 FOR A PARTICULAR PURPOSE.
20
21 IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
22 INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
23 WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
24 HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.
25 TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
26 CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF
27 FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
28
29***********************************************************/
30
31/***********************************************************
32
33 Version 1.0.0
34
35***********************************************************/
36
37#ifndef PAC193x_h
38#define PAC193x_h
39
40#include <Wire.h>
41
42#define RSENSE 24900000 //microohm
43#define I2C_ADDRESS 0x10
44#define CHANNEL 1
45
46// PAC193x register addresses
47
48#define PAC1934_REFRESH_CMD_ADDR 0x00
49#define PAC1934_CTRL_ADDR 0x01
50#define PAC1934_ACC_COUNT_ADDR 0x02
51#define PAC1934_VPOWER1_ACC_ADDR 0x03
52#define PAC1934_VPOWER2_ACC_ADDR 0x04
53#define PAC1934_VPOWER3_ACC_ADDR 0X05
54#define PAC1934_VPOWER4_ACC_ADDR 0X06
55#define PAC1934_VBUS1_ADDR 0x07
56#define PAC1934_VBUS2_ADDR 0x08
57#define PAC1934_VBUS3_ADDR 0x09
58#define PAC1934_VBUS4_ADDR 0x0A
59#define PAC1934_VSENSE1_ADDR 0x0B
60#define PAC1934_VSENSE2_ADDR 0x0C
61#define PAC1934_VSENSE3_ADDR 0X0D
62#define PAC1934_VSENSE4_ADDR 0X0E
63#define PAC1934_VBUS1_AVG_ADDR 0X0F
64#define PAC1934_VBUS2_AVG_ADDR 0X10
65#define PAC1934_VBUS3_AVG_ADDR 0X11
66#define PAC1934_VBUS4_AVG_ADDR 0X12
67#define PAC1934_VSENSE1_AVG_ADDR 0X13
68#define PAC1934_VSENSE2_AVG_ADDR 0X14
69#define PAC1934_VSENSE3_AVG_ADDR 0X15
70#define PAC1934_VSENSE4_AVG_ADDR 0X16
71#define PAC1934_VPOWER1_ADDR 0X17
72#define PAC1934_VPOWER2_ADDR 0X18
73#define PAC1934_VPOWER3_ADDR 0X19
74#define PAC1934_VPOWER4_ADDR 0X1A
75#define PAC1934_CHANNEL_DIS_ADDR 0X1C
76#define PAC1934_NEG_PWR_ADDR 0X1D
77#define PAC1934_REFRESH_G_CMD_ADDR 0x1E
78#define PAC1934_REFRESH_V_CMD_ADDR 0x1F
79#define PAC1934_SLOW_ADDR 0X20
80#define PAC1934_CTRL_ACT_ADDR 0X21
81#define PAC1934_CHANNEL_DIS_ACT_ADDR 0X22
82#define PAC1934_NEG_PWR_ACT_ADDR 0X23
83#define PAC1934_CTRL_LAT_ADDR 0X24
84#define PAC1934_CHANNEL_DIS_LAT_ADDR 0X25
85#define PAC1934_NEG_PWR_LAT_ADDR 0x26
86
87#define PAC1934_PRODUCT_ID_ADDR 0xFD
88#define PAC1934_MANUFACTURER_ID_ADDR 0xFE
89#define PAC1934_REVISION_ID_ADDR 0xFF
90
91class PAC193x {
92 public:
93//class constructors:
94 PAC193x();
95 PAC193x(uint32_t resistorValue);
96 PAC193x(TwoWire &wire);
97 PAC193x(TwoWire &wire, uint32_t resistorValue);
98
99//class public functions:
100 void begin();
101/*
102 Function
103 Refresh()
104 Summary
105 Executes a 'REFRESH' command.
106 Description
107 This method executes a 'REFRESH' command. In this case, the accumulator data,
108 accumulator count, Vbus, Vsense measurements are all refreshed
109 and the accumulators are reset.
110 The updated data is stable and can be read after 1ms.
111 Input
112 None.
113 Output
114 None.
115 Returns
116 None.
117*/
118 void Refresh();
119
120
121/*
122 Function
123 UpdateVoltageRaw()
124 Summary
125 Obtains the current Vbus register value.
126 Description
127 This method obtains the most recent register value of a bus voltage sample.
128 Input
129 None.
130 Output
131 The bus voltage register value is assigned to VoltageRaw property of PAC193x class.
132 Returns
133 In case of execution error, the returned value is an error code.
134*/
135 int16_t UpdateVoltageRaw(); //vbus
136
137
138/*
139 Function
140 UpdateVoltage()
141 Summary
142 Calculates the current real Vbus value.
143 Description
144 This method obtains the calculated value of a bus voltage sample.
145 The value unit is milli-Volt.
146 Input
147 None.
148 Output
149 The bus voltage value is assigned to Voltage property of PAC193x class.
150 Returns
151 In case of execution error, the returned value is an error code.
152*/
153 int16_t UpdateVoltage();
154
155
156/*
157 Function
158 UpdateVsenseRaw()
159 Summary
160 Obtains the current Vsense register value.
161 Description
162 This method obtains the most recent register value of the sense voltage samples.
163 Input
164 None.
165 Output
166 The register value of current sense voltage is assigned to VsenseRaw property of PAC193x class.
167 Returns
168 In case of execution error, the returned value is an error code.
169*/
170 int16_t UpdateVsenseRaw(); //vsense
171
172
173/*
174 Function
175 UpdateVsense()
176 Summary
177 Calculates the current real Vsense value.
178 Description
179 This method obtains the most recent real value of the sense voltage samples.
180 The value unit is milli-Volt.
181 Input
182 None.
183 Output
184 The calculated sense voltage value is assigned to Vsense property of PAC193x class.
185 Returns
186 In case of execution error, the returned value is an error code.
187*/
188 int16_t UpdateVsense();
189
190
191/*
192 Function
193 UpdateCurrent()
194 Summary
195 Calculates the current Isense value.
196 Description
197 This method obtains the most recent calculated value of the sense current samples.
198 The value unit is milli-Amp.
199 Input
200 None.
201 Output
202 The current value is assigned to Current property of PAC193x class.
203 Returns
204 In case of execution error, the returned value is an error code.
205*/
206 int16_t UpdateCurrent(); //isense
207
208
209/*
210 Function
211 UpdatePowerRaw()
212 Summary
213 Obtains the current VPower register value.
214 Description
215 This method obtains the register value of the proportional power.
216 Input
217 None.
218 Output
219 The register value of power is assigned to PowerRaw property of PAC193x class.
220 Returns
221 In case of execution error, the returned value is an error code.
222*/
223 int16_t UpdatePowerRaw();
224
225
226/*
227 Function
228 UpdatePower()
229 Summary
230 Calculates the current real VPower value.
231 Description
232 This method obtains the real value of the power.
233 The value unit is milli-Watt.
234 Input
235 None.
236 Output
237 The power value is assigned to Power property of PAC193x class.
238 Returns
239 In case of execution error, the returned value is an error code.
240*/
241 int16_t UpdatePower();
242
243
244/*
245 Function
246 UpdatePowerAccRaw()
247 Summary
248 Obtains the current register accumulated power.
249 Description
250 This method obtains the register value of the accumulator sum of Vpower samples.
251 Input
252 None.
253 Output
254 The power accumulator register value is assigned to PowerAccRaw property of PAC193x class.
255 Returns
256 In case of execution error, the returned value is an error code.
257*/
258 int16_t UpdatePowerAccRaw();
259
260
261/*
262 Function
263 UpdatePowerAcc()
264 Summary
265 Calculates the current real accumulated power.
266 Description
267 This method obtains the calculated value of the accumulator sum of Vpower samples.
268 The value unit is milli-Watt.
269 Input
270 None.
271 Output
272 The power accumulated value is assigned to PowerAcc property of PAC193x class.
273 Returns
274 In case of execution error, the returned value is an error code.
275*/
276 int16_t UpdatePowerAcc();
277
278
279/*
280 Function
281 UpdateAccCount()
282 Summary
283 Obtains the current register count of accumulated power measurements.
284 Description
285 This method obtains the register value of the accumulator sample count of Vpower.
286 Input
287 None.
288 Output
289 The accumulator sample count register value is assigned to AccCount property of PAC193x class.
290 Returns
291 In case of execution error, the returned value is an error code.
292*/
293 int16_t UpdateAccCount();
294
295
296/*
297 Function
298 UpdateAvgPower()
299 Summary
300 Calculates the current real average power.
301 Description
302 This method obtains the calculated average value of the Vpower samples.
303 The value unit is milli-Watt.
304 Input
305 None.
306 Output
307 The average power value is assigned to AvgPower property of PAC193x class.
308 Returns
309 In case of execution error, the returned value is an error code.
310*/
311 int16_t UpdateAvgPower();
312
313
314
315/*
316 Function
317 UpdateEnergy()
318 Summary
319 Calculates the current energy value.
320 Description
321 This method obtains the calculated energy value that corresponds with the accumulated power.
322 The value unit is milli-Watt-hour.
323 Input
324 None.
325 Output
326 The energy value is assigned to Energy property of PAC193x class.
327 Returns
328 In case of execution error, the returned value is an error code.
329*/
330 int16_t UpdateEnergy();
331
332
333/*
334 Function
335 UpdateOverflowAlert()
336 Summary
337 Obtains the 'Overflow' bit value.
338 Description
339 This method obtains the 'Overflow' indication status bit value. This bit will be set to '1' if
340 any of the accumulators or the accumulator counter overflows. This bit is cleared by Refresh.
341 Input
342 None.
343 Output
344 The 'Overflow' bit value is assigned to OverflowAlert property of PAC193x class.
345 Possible values are:
346 0 - no accumulator or accumulator counter overflow has occured
347 1 - accumulator or accumulator counter has overflowed
348 Returns
349 In case of execution error, the returned value is an error code.
350*/
351 int16_t UpdateOverflowAlert();
352
353
354/*
355 Function
356 UpdateSlowStatus()
357 Summary
358 Obtains the 'Slow' bit value.
359 Description
360 This method obtains the 'Slow' bit value of the Slow register.
361 Input
362 None.
363 Output
364 The 'Slow' bit value is assigned to SlowStatus property of PAC193x class.
365 Possible values are:
366 0 - The Slow pin is pulled low externally
367 1 - The Slow pin is pulled high externally
368 Returns
369 In case of execution error, the returned value is an error code.
370*/
371 int16_t UpdateSlowStatus();
372
373
374/*
375 Function
376 UpdatePowerOnStatus()
377 Summary
378 Obtains the 'POR' bit value.
379 Description
380 This method obtains the 'POR' bit value.
381 Input
382 None.
383 Output
384 The 'POR' bit value is assigned to PowerOnStatus property of PAC193x class.
385 Possible values are:
386 0 - This bit has been creared over i2C since the last Power On occured
387 1 - This bit has the POR default value of 1 and it hasn't been cleared since the last reset occured.
388 Returns
389 In case of execution error, the returned value is an error code.
390*/
391 int16_t UpdatePowerOnStatus();
392
393
394/*
395 Function
396 UpdateSampleRateLat()
397 Summary
398 Obtains the 'SAMPLE_RATE_LAT' value
399 Description
400 This method obtains the sample rate value that was active before the
401 most recent REFRESH command.
402 Input
403 None.
404 Output
405 The sample rate value that was active before the most recent REFRESH command is assigned to SampleRateLat property of PAC193x class.
406 Possible values are:
407 1024 samples/s
408 256 samples/s
409 64 samples/s
410 8 samples/s
411 Returns
412 In case of execution error, the returned value is an error code.
413*/
414 int16_t UpdateSampleRateLat();
415
416
417/*
418 Function
419 setSampleRate()
420 Summary
421 Sets the sample rate value.
422 Description
423 This method sets the current value of the sample rate.
424 Input
425 value - the sample rate value to be set. Accepted values are:
426 1024 samples/s
427 256 samples/s
428 64 samples/s
429 8 samples/s
430 Output
431 None.
432 Returns
433 In case of execution error, the returned value is an error code.
434*/
435 int16_t setSampleRate(uint16_t value);
436
437
438/*
439 Function
440 UpdateProductID()
441 Summary
442 Obtains the Product ID value for the PAC193x.
443 Description
444 This method obtains the Product ID value for the PAC193x. This register is read-only.
445 Input
446 None
447 Output
448 The Product ID register value is assigned to ProductID property of PAC193x class.
449 Returns
450 In case of execution error, the returned value is an error code.
451*/
452 int16_t UpdateProductID();
453
454
455/*
456 Function
457 UpdateManufacturerID()
458 Summary
459 Obtains the Manufacturer ID value for the PAC193x.
460 Description
461 This method obtains the Manufacturer ID that identifies Microchip as
462 the manufacturer of the PAC193x. This register is read-only.
463 Input
464 None.
465 Output
466 The Manufacturer ID register value is assigned to ManufacturerID property of PAC193x class.
467 Returns
468 In case of execution error, the returned value is an error code.
469*/
470 int16_t UpdateManufacturerID();
471
472
473/*
474 Function
475 UpdateRevisionID()
476 Summary
477 Obtains the Revision ID value for the PAC193x.
478 Description
479 This method Obtains the die revision value. This register is read-only.
480 Input
481 None.
482 Output
483 The Revision ID register value is assigned to RevisionID property of PAC193x class.
484 Returns
485 In case of execution error, the returned value is an error code.
486*/
487 int16_t UpdateRevisionID();
488
489//class public properties:
490 uint16_t VoltageRaw; //vbus
491 float Voltage;
492 uint16_t VsenseRaw; //vsense
493 float Vsense;
494 float Current; //isense
495 uint32_t PowerRaw;
496 double Power;
497 uint64_t PowerAccRaw;
498 double PowerAcc;
499 uint32_t AccCount;
500 double AvgPower;
501 float Energy;
502 uint8_t OverflowAlert;
503 uint8_t SlowStatus;
504 uint8_t PowerOnStatus;
505 uint16_t SampleRateLat;
506 uint8_t ProductID;
507 uint8_t ManufacturerID;
508 uint8_t RevisionID;
509
510
511 private:
512//class private functions:
513 void Read(uint8_t reg_address, int Nbytes, uint8_t *pBuffer);
514 uint8_t Read8(uint8_t reg_address);
515 uint16_t Read16(uint8_t reg_address);
516 uint32_t Read32(uint8_t reg_address);
517 uint64_t Read64(uint8_t reg_address);
518 void Write8(uint8_t reg_address, uint8_t data);
519//class private properties:
520 uint32_t rsense;
521 TwoWire &wire;
522
523/* Property
524 errorCode
525 Description
526 In case of execution error of one of the functions called,
527 the value of errorCode indicates wich type of error has occured.
528 Possible values: 0 - No error
529 -1 - Read from device i2C communication error
530 -2 - Write to device i2C communication error
531 -3 - rsense resitor value choice error (0 or negative value)
532 -4 - Sample Rate choice error (only the following values are possible: 8, 64, 256, 1024)
533*/
534 int16_t errorCode;
535
536};
537
538#endif
Wire interface class.
Definition Wire.h:43