-
Notifications
You must be signed in to change notification settings - Fork 11
/
pmbus.cpp
474 lines (312 loc) · 8.22 KB
/
pmbus.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* -*- tab-width: 2; mode: c; -*-
*
* Minimal C++ class for interfacing with a PMBus PSU.
*
* Copyright (c) 2020, Steve Jack.
*
* Use at your own risk.
*
* Notes
*
* Tested with a Dell DPS-750TB.
*
*/
#pragma GCC diagnostic warning "-Wunused-variable"
#define DIAGNOSTICS 1
#include "pmbus.h"
/*
*
*/
PMBus::PMBus() {
int i;
I2C = &Wire;
memset(mfr_id, 0,sizeof(mfr_id));
memset(mfr_model, 0,sizeof(mfr_model));
memset(mfr_revision,0,sizeof(mfr_revision));
memset(mfr_location,0,sizeof(mfr_location));
memset(mfr_date, 0,sizeof(mfr_date));
memset(mfr_serial, 0,sizeof(mfr_serial));
i = 0;
status_reg[i] = 0x7a; status_byte[i] = &status_vout;
status_reg[++i] = 0x7b; status_byte[i] = &status_iout;
status_reg[++i] = 0x7c; status_byte[i] = &status_input;
status_reg[++i] = 0x7d; status_byte[i] = &status_temperature;
status_reg[++i] = 0x7e; status_byte[i] = &status_cml;
status_reg[++i] = 0x7f; status_byte[i] = &status_other;
status_reg[++i] = 0x80; status_byte[i] = &status_mfr_specific;
status_reg[++i] = 0x81; status_byte[i] = &status_fans;
while (++i < STATUS_REGISTERS) {
status_reg[i] = 0;
}
return;
}
/*
*
*/
int PMBus::init(int pson,int i2c_enable,short int direction,uint8_t address,Stream *Debug_Serial) {
return init(pson,i2c_enable,direction,address,Debug_Serial,NULL);
}
//
int PMBus::init(int pson,int i2c_enable,short int direction,uint8_t address,Stream *Debug_Serial,TwoWire *i2c) {
int i;
char text[128];
i = text[0] = 0;
pmbus_address = address;
PSON_do = pson;
I2C_enable_do = i2c_enable;
output_direction = direction;
Debug = Debug_Serial;
if (i2c) {
I2C = i2c;
}
pinMode(PSON_do,OUTPUT);
pinMode(I2C_enable_do,OUTPUT);
digitalWrite(I2C_enable_do,0 ^ output_direction);
digitalWrite(PSON_do,0 ^ output_direction);
//
delay(500);
//
read_string(0x99,sizeof(mfr_id) - 1,(uint8_t *) mfr_id); delay(1);
read_string(0x9c,sizeof(mfr_location) - 1,(uint8_t *) mfr_location); delay(1);
read_string(0x9d,sizeof(mfr_date) - 1,(uint8_t *) mfr_date); delay(1);
read_string(0x9e,sizeof(mfr_serial) - 1,(uint8_t *) mfr_serial); delay(1);
check_model();
//
clear_faults();
#if DIAGNOSTICS
if (Debug) {
sprintf(text,"manf.: \'%s\'\r\n",&mfr_id[1]);
Debug->print(text);
sprintf(text,"model: \'%s\'\r\n",&mfr_model[1]);
Debug->print(text);
sprintf(text,"revision: \'%s\'\r\n",&mfr_revision[1]);
Debug->print(text);
sprintf(text,"date: \'%s\'\r\n",&mfr_date[1]);
Debug->print(text);
sprintf(text,"serial: \'%s\'\r\n",&mfr_serial[1]);
Debug->print(text);
if (total_power_on) {
int days, years;
days = (int) ((long int) total_power_on / 86400l);
years = days / 365;
sprintf(text,"on time: %lu s (%08lx) ",total_power_on,total_power_on);
Debug->print(text);
sprintf(text,"%d days, %d years\r\n",days,years);
Debug->print(text);
}
Debug->print("\r\nPMBus::init() complete\r\n");
}
#endif
return 0;
}
/*
*
*/
void PMBus::check_model() {
read_string(0x9a,sizeof(mfr_model) - 1,(uint8_t *) mfr_model); delay(1);
read_string(0x9b,sizeof(mfr_revision) - 1,(uint8_t *) mfr_revision); delay(1);
pmbus_revision = read_byte(0x98); delay(1);
vout_mode = read_byte(0x20); delay(1);
vout_command = read_word(0x21); delay(1);
// Model specific setup.
if (strncmp(&mfr_model[1],"DPS750TB1",9) == 0) {
status_reg[5] = 0;
status_reg[6] = 0;
temperatures = 2;
fans = 1;
if (pmbus_revision == 0) {
vout_scale = 1.0 / (float) 0x200;
}
} else if (strncmp(&mfr_model[1],"D1U86T-W-800-12-HB4C",20) == 0) {
// muRata, unchecked, 0xe5 returns garbage on the DPS-750.
int i;
uint8_t buffer[4];
read_block(0xe5, 4,buffer); delay(1);
for (i = 0; i < 4; ++i) {
total_power_on <<= 8;
total_power_on |= buffer[i];
}
} else {
status_reg[7] = 0;
}
//
return;
}
/*
*
*/
void PMBus::standby() {
digitalWrite(PSON_do,1 ^ output_direction);
return;
}
/*
*
*/
void PMBus::on() {
digitalWrite(PSON_do,0 ^ output_direction);
return;
}
/*
*
*/
int PMBus::scan() {
int i;
float tmp_f;
uint32_t msecs;
static uint32_t last_scan = 0;
//
msecs = millis();
if ((msecs - last_scan) < 1000) {
return 0;
}
last_scan = msecs;
//
if (mfr_model[0] == 0) {
check_model();
}
//
status_u8 = read_word(0x78); delay(1);
status_word = read_word(0x79);
for (i = 0; i < STATUS_REGISTERS; ++i) {
if (status_reg[i]) {
delay(1);
*status_byte[i] = read_byte(status_reg[i]);
}
}
//
delay(1); read_linear(0x88,&V_in,90.0,264.0);
delay(1); read_linear(0x89,&I_in,0.0,16.0);
delay(1); read_linear(0x97,&W_in,0.0,750.0);
delay(1);
if (vout_scale) {
tmp_f = vout_scale * (float) read_word(0x8b);
if (tmp_f < 16.0) {
V_out = tmp_f;
}
} else {
read_linear(0x8b,&V_out,0.0,9999.0);
}
delay(1); read_linear(0x8c,&I_out,0.0,70.0);
delay(1); read_linear(0x96,&W_out,0.0,750.0);
for (i = 0; i < temperatures; ++i) {
delay(1); read_linear(0x8d + i,&T[i],-10.0,100.0);
}
//
for (i = 0; i < fans; ++i) {
delay(1); read_linear(0x90 + i,&fan[i],0.0,3000.0);
}
//
return 1;
}
/*
*
*/
void PMBus::clear_faults() {
I2C->beginTransmission(pmbus_address);
I2C->write(0x03);
I2C->endTransmission(true);
status_u8 =
status_word = 0;
status_vout =
status_iout =
status_input =
status_temperature =
status_cml =
status_other =
status_mfr_specific =
status_fans = 0;
return;
}
/*
*
*/
void PMBus::write_byte(uint8_t reg,uint8_t value) {
I2C->beginTransmission(pmbus_address);
I2C->write(reg);
I2C->write(value);
I2C->endTransmission(true);
return;
}
/*
*
*/
uint8_t PMBus::read_byte(uint8_t reg) {
I2C->beginTransmission(pmbus_address);
I2C->write(reg);
I2C->endTransmission(false);
I2C->requestFrom(pmbus_address,(uint8_t) 1,(uint8_t) true);
return I2C->read();
}
/*
*
*/
uint16_t PMBus::read_word(uint8_t reg) {
uint16_t value;
I2C->beginTransmission(pmbus_address);
I2C->write(reg);
I2C->endTransmission(false);
I2C->requestFrom(pmbus_address,(uint8_t) 2,(uint8_t) true);
value = I2C->read();
value |= I2C->read() << 8;
return value;
}
/*
*
*/
int PMBus::read_string(uint8_t reg,int bytes,uint8_t *buffer) {
int len = 0;
len = (int) read_byte(reg);
if ((len)&&(len < bytes)) {
read_block(reg,len,buffer);
}
return len;
}
/*
*
*/
void PMBus::read_block(uint8_t reg,int bytes, uint8_t *buffer) {
int i;
I2C->beginTransmission(pmbus_address);
I2C->write(reg);
I2C->endTransmission(false);
I2C->requestFrom(pmbus_address,(uint8_t) bytes,(uint8_t) true);
for (i = 0; i < bytes; ++i) {
buffer[i] = I2C->read();
}
return;
}
/*
*
*/
void PMBus::read_linear(uint8_t reg,float *value,float min_f,float max_f) {
float tmp_f;
uint16_t linear;
linear = read_word(reg);
tmp_f = linear2float(linear);
if ((tmp_f >= min_f)&&(tmp_f <= max_f)) {
*value = tmp_f;
}
#if 0
if (Debug) {
char text[64], text2[16];
dtostrf(*value,6,1,text2);
sprintf(text,"%02x %04x (%d,%d,%d) %s\n",
reg,linear,(int) linear,(int) (linear >> 11),(int) (linear & 0x7ff),text2);
Debug->print(text);
}
#endif
return;
}
/*
*
*/
float PMBus::linear2float(uint16_t u16) {
float value = 0;
union {linear_t linear; uint16_t u16;} conv;
conv.u16 = u16;
value = (float) conv.linear.y * pow(2.0,(float) conv.linear.n);
return value;
}
/*
*
*/