forked from zzzbatmand/AD5593R_PI_cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAD5593R.cpp
520 lines (425 loc) · 15.1 KB
/
AD5593R.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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
extern "C" {
#include <linux/i2c-dev.h>
}
#include <sys/ioctl.h>
#include <wiringPi.h>
#include "AD5593R.h"
//Definitions
#define _ADAC_NULL 0b00000000
#define _ADAC_ADC_SEQUENCE 0b00000010 // ADC sequence register - Selects ADCs for conversion
#define _ADAC_GP_CONTROL 0b00000011 // General-purpose control register - DAC and ADC control register
#define _ADAC_ADC_CONFIG 0b00000100 // ADC pin configuration - Selects which pins are ADC inputs
#define _ADAC_DAC_CONFIG 0b00000101 // DAC pin configuration - Selects which pins are DAC outputs
#define _ADAC_PULL_DOWN 0b00000110 // Pull-down configuration - Selects which pins have an 85 kO pull-down resistor to GND
#define _ADAC_LDAC_MODE 0b00000111 // LDAC mode - Selects the operation of the load DAC
#define _ADAC_GPIO_WR_CONFIG 0b00001000 // GPIO write configuration - Selects which pins are general-purpose outputs
#define _ADAC_GPIO_WR_DATA 0b00001001 // GPIO write data - Writes data to general-purpose outputs
#define _ADAC_GPIO_RD_CONFIG 0b00001010 // GPIO read configuration - Selects which pins are general-purpose inputs
#define _ADAC_POWER_REF_CTRL 0b00001011 // Power-down/reference control - Powers down the DACs and enables/disables the reference
#define _ADAC_OPEN_DRAIN_CFG 0b00001100 // Open-drain configuration - Selects open-drain or push-pull for general-purpose outputs
#define _ADAC_THREE_STATE 0b00001101 // Three-state pins - Selects which pins are three-stated
#define _ADAC_RESERVED 0b00001110 // Reserved
#define _ADAC_SOFT_RESET 0b00001111 // Software reset - Resets the AD5593R
/**
* @name ADAC Configuration Data Bytes
******************************************************************************/
///@{
//write into MSB after _ADAC_POWER_REF_CTRL command to enable VREF
#define _ADAC_VREF_ON 0b00000010
#define _ADAC_SEQUENCE_ON 0b00000010
/**
* @name ADAC Write / Read Pointer Bytes
******************************************************************************/
///@{
#define _ADAC_DAC_WRITE 0b00010000
#define _ADAC_ADC_READ 0b01000000
#define _ADAC_DAC_READ 0b01010000
#define _ADAC_GPIO_READ 0b01100000
#define _ADAC_REG_READ 0b01110000
//Class constructor
AD5593R::AD5593R(const char* i2c_device_filepath, uint8_t i2c_addr, int a0): i2c_bus_addr(i2c_addr), i2c_file(0), i2c_filepath(i2c_device_filepath)
{
_a0 = a0;
_GPRC_msbs = 0x00;
_GPRC_lsbs = 0x00;
_PCR_msbs = 0x00;
_PCR_lsbs = 0x00;
_GPO_states = 0x00;
//intializing the configuration struct.
for (unsigned int i = 0; i < _num_of_channels; i++) {
config.ADCs[i] = 0;
config.DACs[i] = 0;
config.GPIs[i] = 0;
config.GPOs[i] = 0;
}
for (unsigned int i = 0; i < _num_of_channels; i++) {
values.ADCs[i] = -1;
values.DACs[i] = -1;
values.GPI_reads[i] = -1;
values.GPO_writes[i] = -1;
}
//this allows for multiple devices on the same bus, see header.
if (_a0 > -1) {
pinMode(_a0, OUTPUT);
digitalWrite(_a0, HIGH);
}
}
// Initiate the i2c device.
bool AD5593R::init() {
//Open i2c device
int file;
if((file = open(i2c_filepath.data(), O_RDWR)) < 0) {
AD5593R_PRINTLN("Can't open i2c channel");
return false;
}
i2c_file = file;
//Set address
if(ioctl(file, I2C_SLAVE, i2c_bus_addr) < 0) {
AD5593R_PRINTLN("IO_CTRL failed on the i2c");
return false;
}
//Check its answering
uint8_t check = ad5593r_write(_ADAC_ADC_SEQUENCE, 0x02, uint8_t(1 << 1));
if (check != 0) {
AD5593R_PRINTLN("Device Unresponsive Communication Error");
return false;
}
return true;
}
// Custom i2c Read/Write, shamelessly stolen and modifyed from "https://github.com/wwanc/Si5351Pi_cpp"
uint8_t AD5593R::ad5593r_write(uint8_t addr, uint8_t msb, uint8_t lsb) {
uint8_t buff[3];
buff[0] = addr;
buff[1] = msb;
buff[2] = lsb;
int result = write(i2c_file, buff, 3);
if(result < 0){
return result;
}
return 0;
}
uint16_t AD5593R::ad5593r_read(uint8_t addr) {
int result = write(i2c_file, &addr, 1);
if(result < 0){
return result;
}
uint16_t data;
result = read(i2c_file, &data, 2);
if(result < 0){
return result;
}
// LSB and MSB is switched, so switch it back here.
uint8_t msb = data;
uint8_t lsb = data >> 8;
uint16_t reg_val = (msb << 8) + lsb;
return reg_val;
}
// The print function is used in different ways.
void AD5593R::ad5593r_print(std::string data) {
std::cout << data;
}
void AD5593R::ad5593r_print(uint8_t data) {
printf("%02X", data);
}
void AD5593R::ad5593r_print(int data) {
printf("%u", data);
}
void AD5593R::ad5593r_print(float data) {
std::cout << data;
}
void AD5593R::ad5593r_println(std::string data) {
std::cout << data << std::endl;
}
void AD5593R::enable_internal_Vref() {
//Enable selected device for writing
_Vref = 2.5;
_ADC_max = _Vref;
_DAC_max = _Vref;
if (_a0 > -1) digitalWrite(_a0, LOW);
//check if the on bit is already fliped on
if ((_PCR_msbs & 0x02) != 0x02) {
_PCR_msbs = _PCR_msbs ^ 0x02;
}
ad5593r_write(_ADAC_POWER_REF_CTRL, _PCR_msbs, _PCR_lsbs);
//Disable selected device for writing
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINTLN("Internal Reference on.");
}
void AD5593R::disable_internal_Vref() {
//Enable selected device for writing
_Vref = -1;
_ADC_max = _Vref;
_DAC_max = _Vref;
if (_a0 > -1) digitalWrite(_a0, LOW);
//check if the on bit is already fliped off
if ((_PCR_msbs & 0x02) == 0x02) {
_PCR_msbs = _PCR_msbs ^ 0x02;
}
ad5593r_write(_ADAC_POWER_REF_CTRL, _PCR_msbs, _PCR_lsbs);
//Disable selected device for writing
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINTLN("Internal Reference off.");
}
void AD5593R::set_ADC_max_2x_Vref() {
//Enable selected device for writing
_ADC_max = 2 * _Vref;
if (_a0 > -1) digitalWrite(_a0, LOW);
//check if 2x bit is on in the general purpose register
if ((_GPRC_lsbs & 0x20) != 0x20) {
_GPRC_lsbs = _GPRC_lsbs ^ 0x20;
}
ad5593r_write(_ADAC_GP_CONTROL, _GPRC_msbs, _GPRC_lsbs);
//Disable selected device for writing
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINTLN("ADC max voltage = 2xVref");
_ADC_2x_mode = 1;
}
void AD5593R::set_ADC_max_1x_Vref() {
//Enable selected device for writing
_ADC_max = _Vref;
if (_a0 > -1) digitalWrite(_a0, LOW);
if ((_GPRC_lsbs & 0x20) == 0x20) {
_GPRC_lsbs = _GPRC_lsbs ^ 0x20;
}
ad5593r_write(_ADAC_GP_CONTROL, _GPRC_msbs, _GPRC_lsbs);
//Disable selected device for writing
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINTLN("ADC max voltage = 1xVref");
_ADC_2x_mode = 0;
}
void AD5593R::set_DAC_max_2x_Vref() {
//Enable selected device for writing
_DAC_max = 2 * _Vref;
if (_a0 > -1) digitalWrite(_a0, LOW);
if ((_GPRC_lsbs & 0x10) != 0x10) {
_GPRC_lsbs = _GPRC_lsbs ^ 0x10;
}
ad5593r_write(_ADAC_GP_CONTROL, _GPRC_msbs, _GPRC_lsbs);
//Disable selected device for writing
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINTLN("DAC max voltage = 2xVref");
_DAC_2x_mode = 1;
}
void AD5593R::set_DAC_max_1x_Vref() {
//Enable selected device for writing
_DAC_max = _Vref;
if (_a0 > -1) digitalWrite(_a0, LOW);
if ((_GPRC_lsbs & 0x10) == 0x10) {
_GPRC_lsbs = _GPRC_lsbs ^ 0x10;
}
ad5593r_write(_ADAC_GP_CONTROL, _GPRC_msbs, _GPRC_lsbs);
//Disable selected device for writing
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINTLN("ADC max voltage = 1xVref");
_DAC_2x_mode = 0;
}
void AD5593R::set_Vref(float Vref) {
_Vref = Vref;
if (_ADC_2x_mode == 0) {
_ADC_max = Vref;
}
else {
_ADC_max = 2 * Vref;
}
if (_DAC_2x_mode == 0) {
_DAC_max = Vref;
}
else {
_DAC_max = 2 * Vref;
}
}
void AD5593R::configure_DAC(uint8_t channel) {
if (_a0 > -1) digitalWrite(_a0, LOW);
config.DACs[channel] = 1;
uint8_t channel_byte = 1 << channel;
//check to see if the channel is a DAC already
if ((_DAC_config & channel_byte) != channel_byte) {
_DAC_config = _DAC_config ^ channel_byte;
}
ad5593r_write(_ADAC_DAC_CONFIG, 0x00, _DAC_config);
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is configured as a DAC");
}
void AD5593R::configure_DACs(bool* channels) {
for (size_t i = 0; i < _num_of_channels; i++) {
if (channels[i] == 1) {
configure_DAC(i);
}
}
}
int AD5593R::write_DAC(uint8_t channel, float voltage) {
//error checking
if (config.DACs[channel] == 0) {
AD5593R_PRINT("ERROR! Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is not a DAC");
return -1;
}
if (_DAC_max == -1) {
AD5593R_PRINTLN("Vref, or DAC_max is not defined");
return -2;
}
if (voltage > _DAC_max) {
AD5593R_PRINTLN("Vref, or DAC_max is lower than set voltage");
return -3;
}
if (_a0 > -1) digitalWrite(_a0, LOW);
unsigned int data_bits = (voltage / _DAC_max) * 4095;
//extract the 4 most signifigant bits, and move them down to the bottom
uint8_t data_msbs = (data_bits & 0xf00) >> 8;
uint8_t lsbs = (data_bits & 0x0ff);
//place the channel data in the most signifigant bits
uint8_t msbs = (0b10000000 | (channel << 4)) | data_msbs;
ad5593r_write((_ADAC_DAC_WRITE | channel), msbs, lsbs);
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINT(" is set to ");
AD5593R_PRINT(voltage);
AD5593R_PRINTLN(" Volts");
if (_a0 > -1) digitalWrite(_a0, HIGH);
values.DACs[channel] = voltage;
return 1;
}
void AD5593R::configure_ADC(uint8_t channel) {
if (_a0 > -1) digitalWrite(_a0, LOW);
config.ADCs[channel] = 1;
uint8_t channel_byte = 1 << channel;
//check to see if the channel is a ADC already
if ((_ADC_config & channel_byte) != channel_byte) {
_ADC_config = _ADC_config ^ channel_byte;
}
ad5593r_write(_ADAC_ADC_CONFIG, 0x00, _ADC_config);
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is configured as a ADC");
}
void AD5593R::configure_ADCs(bool* channels) {
for (size_t i = 0; i < _num_of_channels; i++) {
if (channels[i] == 1) {
configure_ADC(i);
}
}
}
float AD5593R::read_ADC(uint8_t channel) {
if (config.ADCs[channel] == 0) {
AD5593R_PRINT("ERROR! Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is not an ADC");
return -1;
}
if (_ADC_max == -1) {
AD5593R_PRINTLN("Vref, or ADC_max is not defined");
return -2;
}
if (_a0 > -1) digitalWrite(_a0, LOW);
// Select what IO we want to read from.
ad5593r_write(_ADAC_ADC_SEQUENCE, 0x02, uint8_t(1 << channel));
uint16_t data_bits = ad5593r_read(_ADAC_ADC_READ);
// We don't need the first bit, as it just informs us what IO/pin, the voltage is read from. (We read them one at a time anyway, that is why)
data_bits &= 0x0fff;
if (_a0 > -1) digitalWrite(_a0, HIGH);
float data = _ADC_max * (data_bits) / 4095;
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINT(" reads ");
AD5593R_PRINT(data);
AD5593R_PRINTLN(" Volts");
return data;
}
float* AD5593R::read_ADCs() {
for (size_t i = 0; i < _num_of_channels; i++) {
if (config.ADCs[i] == 1) {
read_ADC(i);
}
}
return values.ADCs;
}
void AD5593R::configure_GPI(uint8_t channel) {
if (_a0 > -1) digitalWrite(_a0, LOW);
config.GPIs[channel] = 1;
uint8_t channel_byte = 1 << channel;
//check to see if the channel is a gpi already
if ((_GPI_config & channel_byte) != channel_byte) {
_GPI_config = _GPI_config ^ channel_byte;
}
ad5593r_write(_ADAC_GPIO_RD_CONFIG, 0x00, _GPI_config);
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is configured as a GPI");
}
void AD5593R::configure_GPIs(bool* channels) {
for (size_t i = 0; i < _num_of_channels; i++) {
if (channels[i] == 1) {
configure_GPI(i);
}
}
}
void AD5593R::configure_GPO(uint8_t channel) {
if (_a0 > -1) digitalWrite(_a0, LOW);
config.GPOs[channel] = 1;
uint8_t channel_byte = 1 << channel;
//check to see if the channel is a gpo already
if ((_GPO_config & channel_byte) != channel_byte) {
_GPO_config = _GPO_config ^ channel_byte;
}
ad5593r_write(_ADAC_GPIO_WR_CONFIG, 0x00, _GPO_config);
if (_a0 > -1) digitalWrite(_a0, HIGH);
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is configured as a GPO");
}
void AD5593R::configure_GPOs(bool* channels) {
for (size_t i = 0; i < _num_of_channels; i++) {
if (channels[i] == 1) {
configure_GPO(i);
}
}
}
bool* AD5593R::read_GPIs() {
if (_a0 > -1) digitalWrite(_a0, LOW);
uint16_t data_bits = ad5593r_read(_ADAC_GPIO_READ);
// We don't need the first 2 bytes, as it just informs us of the pin that returned a voltage.
data_bits &= 0x00ff;
if (_a0 > -1) digitalWrite(_a0, HIGH);
for (uint8_t i = 0; i < _num_of_channels; i++) {
if (config.GPIs[i] == 1) {
values.GPI_reads[i] = bool(data_bits & 0x01);
AD5593R_PRINT("Channel ");
AD5593R_PRINT(i);
AD5593R_PRINT(" GPI reads ");
AD5593R_PRINT(data_bits >> i & 0x01);
AD5593R_PRINTLN("");
}
data_bits >> 1;
}
return values.GPI_reads;
}
int AD5593R::write_GPO(uint8_t channel, bool state) {
//error checking
if (config.GPOs[channel] == 0) {
AD5593R_PRINT("ERROR! Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINTLN(" is not a GPO");
return -1;
}
if (_a0 > -1) digitalWrite(_a0, LOW);
uint8_t data_bits = state << channel;
if ((_GPO_states & data_bits) != data_bits) {
_GPO_states = _GPO_states ^ data_bits;
}
AD5593R_PRINT("Channel ");
AD5593R_PRINT(channel);
AD5593R_PRINT(" GPO switched to ");
AD5593R_PRINT(state);
AD5593R_PRINTLN("");
uint8_t reply = ad5593r_write(_ADAC_GPIO_WR_DATA, 0x00, _GPO_states);
if (_a0 > -1) digitalWrite(_a0, HIGH);
return 1;
}