pflib v3.9.0-rc3-11-g2537d8f
Pretty Fine HGCROC Interaction Library
Loading...
Searching...
No Matches
ad5593r.h
1#ifndef AD5593R_H_INCLUDED
2#define AD5593R_H_INCLUDED
3
4// using this transport as a general I2C interface
5#include "pflib/lpgbt/lpGBT_ConfigTransport_I2C.h"
6
7namespace pflib {
8
12class AD5593R {
13 static constexpr uint8_t REG_DAC_PIN = 0x05;
14 static constexpr uint8_t REG_ADC_PIN = 0x04;
15 static constexpr uint8_t REG_OPENDRAIN = 0x0C;
16 static constexpr uint8_t REG_GPO_ENABLE = 0x08;
17 static constexpr uint8_t REG_GPI_ENABLE = 0x0A;
18 static constexpr uint8_t REG_ADC_SEQ = 0x02;
19 static constexpr uint8_t REG_GENERAL = 0x03;
20 static constexpr uint8_t REG_POWERDOWN = 0x0B;
21 static constexpr uint8_t REG_PULLDOWN = 0x06;
22 static constexpr uint8_t REG_GPO_WRITE = 0x09;
23 static constexpr uint8_t REG_GPI_READ = 0x60;
24 static constexpr uint8_t REG_RESET = 0x0F;
25
26 public:
27 AD5593R(const std::string& bus, uint8_t addr) : i2c_{addr, bus} {
28 // turn on the reference
29 i2c_.write_raw(REG_POWERDOWN, 0x2, 0x0);
30 // turn on the ADC buffer, setup range, etc
31 i2c_.write_raw(REG_GENERAL, 0x1, 0x0);
32 }
33
34 void setup_gpi(int pin, bool pull_down = false) {
35 if (pin < 0 || pin > 7) return;
36 clear_bit(REG_GPO_ENABLE, pin);
37 clear_bit(REG_ADC_PIN, pin);
38 clear_bit(REG_DAC_PIN, pin);
39 set_bit(REG_GPI_ENABLE, pin);
40 if (pull_down) {
41 set_bit(REG_PULLDOWN, pin);
42 } else {
43 clear_bit(REG_PULLDOWN, pin);
44 }
45 }
46
47 void setup_gpo(int pin, int value = -1) {
48 if (pin < 0 || pin > 7) return;
49 clear_bit(REG_GPI_ENABLE, pin);
50 clear_bit(REG_ADC_PIN, pin);
51 clear_bit(REG_DAC_PIN, pin);
52 if (value == 0) {
53 clear_bit(REG_GPO_WRITE, pin);
54 } else {
55 set_bit(REG_GPO_WRITE, pin);
56 }
57 set_bit(REG_GPO_ENABLE, pin);
58 }
59
60 bool is_gpo(int pin) { return read_reg(REG_GPO_ENABLE) & (1 << pin); }
61
62 bool gpo_get_value(int pin) { return read_reg(REG_GPO_WRITE) & (1 << pin); }
63
64 void setup_dac(int pin, bool zero = true) {
65 if (pin < 0 || pin > 7) return;
66 clear_bit(REG_GPO_ENABLE, pin);
67 clear_bit(REG_GPI_ENABLE, pin);
68 set_bit(REG_ADC_PIN, pin);
69 set_bit(REG_PULLDOWN, pin);
70 // set DAC to zero by default
71 if (zero) {
72 dac_write(pin, 0);
73 }
74 set_bit(REG_DAC_PIN, pin);
75 }
76
77 void setup_adc(int pin) {
78 if (pin < 0 || pin > 7) return;
79 // turn off DAC on pin
80 clear_bit(REG_DAC_PIN, pin);
81 // turn off gpi on pin
82 clear_bit(REG_GPI_ENABLE, pin);
83 // turn off gpo on pin
84 clear_bit(REG_GPO_ENABLE, pin);
85 // turn off pull-down on pin
86 clear_bit(REG_PULLDOWN, pin);
87 // enable ADC
88 set_bit(REG_ADC_PIN, pin);
89 }
90
91 int adc_read(int pin) {
92 //""" selects a pin (must be configured already for ADC operation), performs
93 // ADC conversion sequence and reads back the raw 12 bit result from the ADC
94 // data register for that pin """
95 // first byte sets repetition and temperature indicator readback, second
96 // byte selects ADC channel (all off by default)
97 i2c_.write_raw(REG_ADC_SEQ, 0x0, (1 << pin));
98 // select ADC address
99 i2c_.write_raw(0x40);
100 std::vector<uint8_t> result = i2c_.read_raw(2);
101 return int(result[1]) + (int(result[0] & 0xF) << 8);
102 }
103
104 float adc_volts(int pin, int ave = 10) {
105 int sum = 0;
106 for (int i = 0; i < ave; i++) sum += adc_read(pin);
107 return (sum * 1.0 / ave) * 2.5 / 4096;
108 }
109
110 void dac_write(int pin, uint16_t value) {
111 if (pin < 0 || pin > 7) return;
112 i2c_.write_raw(0x10 | pin, ((0x08 | pin) << 4) | ((value & 0xF00) >> 8),
113 value & 0xFF);
114 }
115
116 void clear_pin(int pin) {
117 clear_bit(REG_GPO_ENABLE, pin);
118 clear_bit(REG_GPI_ENABLE, pin);
119 clear_bit(REG_ADC_PIN, pin);
120 clear_bit(REG_DAC_PIN, pin);
121 }
122
123 private:
124 uint16_t read_reg(uint8_t reg) {
125 i2c_.write_raw(0x70 | (reg & 0xF));
126 std::vector<uint8_t> rv = i2c_.read_raw(2);
127 return (uint16_t(rv[0]) << 8) | (rv[1]);
128 }
129 void write_reg(uint8_t reg, uint16_t value) {
130 uint8_t b = uint8_t(value >> 8);
131 uint8_t c = uint8_t(value & 0xFF);
132 i2c_.write_raw((reg & 0xF), b, c);
133 }
134 void set_bit(uint8_t reg, int bit) {
135 uint16_t val = read_reg(reg);
136 val |= (1 << bit);
137 write_reg(reg, val);
138 }
139 void clear_bit(uint8_t reg, int bit) {
140 uint16_t val = read_reg(reg);
141 val |= (1 << bit);
142 val ^= (1 << bit);
143 write_reg(reg, val);
144 }
145
147};
148
149} // namespace pflib
150
151#endif // AD5593R_H_INCLUDED
Partial clone from python code, with just needed functionalities for testing for now.
Definition ad5593r.h:12
Definition lpGBT_ConfigTransport_I2C.h:10
This version of the fast control code interfaces with the CMS Fast control library which can be contr...
Definition Backend.cxx:3