lolxnano
Code to control the LoLX SiPM biasing board from the connected nanoPi
Loading...
Searching...
No Matches
ad56x6.h
1#ifndef AD56X6_H
2#define AD56X6_H
3
4#define DAC_RB_ENABLE 0b1001
5#define DAC_WRITESET 0b0011
6#define DAC_POWUPDN 0b0100
7#define DAC_RST 0b0110
8#define DAC_NOP 0
9#define DAC_SLOPE_QP -1257.86
10#define DAC_OFFSET_QP 116289.0
11#define DAC_SLOPE_HV -641.931
12#define DAC_OFFSET_HV 50730.5
13
14#ifdef REV0
15#define DAC_VREF 4.096
16#else
17#define DAC_VREF 2.5
18#endif
19
20#define DAC_VREF_HV 2.5
21#define DAC_SLOPE34_HV double(1<<16)/2.5
22
23#define DAC_ENHV_CH 3
24#define DAC_ENHV_EN 0xfefe
25
26#include <iostream>
27#include <vector>
28
29#include "nanopi.h"
30
37class AD56x6
38{
39 public:
46 AD56x6(NanoPi &np, int cs): nanoPi(&np), cselect(cs){
47 if(!np.HasSPI() || !np.HasGPIO())
48 std::cout << "SPI/GPIO not yet initialized!" << std::endl;
49 if(gverbose)
50 std::cout << "AD56x6::cselect = " << cselect << ", nanoPi = " << nanoPi << std::endl;
51 };
52 virtual ~AD56x6(){};
53 unsigned char *AssembleWord(uint8_t cmd, uint8_t chan, uint16_t *data = NULL);
54 bool Set(unsigned short chan, uint16_t setting, bool confirm = true);
55 virtual uint16_t GetSetting(double v, unsigned short chan = 0) = 0;
56 virtual double GetVoltage(uint16_t s, unsigned short chan = 0) = 0;
66 bool RampVoltage(unsigned short chan, uint16_t vset = -1, unsigned int steps = 16, unsigned int tdelay = 500);
68 void PowerOn() { nanoPi->SendWord(cselect, AssembleWord(DAC_POWUPDN, 1), nbytes); };
69 uint16_t GetData();
75 void Readback(short ch = -1);
76 const uint16_t vfull = 0xffff;
77 const uint8_t nbytes = 3;
78 std::vector<uint16_t> readback;
79 protected:
80 uint32_t bits = 0;
82 virtual uint8_t EncChan(uint8_t chan) { return 0xF & chan; };
83 NanoPi *nanoPi = NULL;
84 const int cselect;
85#ifdef DEBUG
86 bool verbose = true;
87#else
88 bool verbose = false;
89#endif
90};
91
97class AD5676: public AD56x6{
98 public:
99 AD5676(NanoPi &np, int cs): AD56x6(np, cs)
100 {
101 readback.resize(8);
102 };
103 ~AD5676(){};
105 uint16_t GetSetting(double v, unsigned short chan = 0);
107 double GetVoltage(uint16_t s, unsigned short chan = 0);
109 void SetVref(double v)
110 {
111 vref = v;
112 }
113 private:
114 double vref = DAC_VREF;
115};
116/* class AD5666: public AD56x6 */
117/* { */
118/* public: */
119/* bool SetInternalRef(bool on = true); */
120/* private: */
121/* const uint8_t dac_rst = 0b0111; */
122/* } */
123
129class AD5686R: public AD56x6
130{
131 public:
132 AD5686R(NanoPi &np, int cs): AD56x6(np, cs)
133 {
134 readback.resize(4);
135 };
136 ~AD5686R(){};
137 bool SetInternalRef(bool on = true);
138 uint16_t GetSetting(double v, unsigned short chan);
139 double GetVoltage(uint16_t s, unsigned short chan);
140 void SetCalib(std::vector<double> sl, std::vector<double> offs)
141 {
142 slope = sl; offset = offs;
143 };
144
152 bool EnableHV(bool on = true);
153 bool DisableHV(){ return EnableHV(false); };
154 bool IsHVEnabled();
155 private:
157 uint8_t EncChan(uint8_t chan)
158 {
159 return 0xF & (1 << chan);
160 };
161 std::vector<double> slope = { DAC_SLOPE_QP, DAC_SLOPE_HV, DAC_SLOPE34_HV, DAC_SLOPE34_HV };
162 std::vector<double> offset = { DAC_OFFSET_QP, DAC_OFFSET_HV, 0., 0. };
163};
164#endif
165
166/* emacs
167 * Local Variables:
168 * tab-width: 8
169 * c-basic-offset: 3
170 * indent-tabs-mode: nil
171 * End:
172 */
uint16_t GetSetting(double v, unsigned short chan=0)
Calibration specific to the bias tuning DACs.
Definition ad56x6.cc:92
void SetVref(double v)
Method to change the DAC reference voltage value for calibration calculations.
Definition ad56x6.h:109
double GetVoltage(uint16_t s, unsigned short chan=0)
Calibration specific to the bias tuning DACs.
Definition ad56x6.cc:98
uint16_t GetSetting(double v, unsigned short chan)
Calibration specific to the HV DAC.
Definition ad56x6.cc:102
bool EnableHV(bool on=true)
Turn HV amplifier on/off.
Definition ad56x6.cc:127
double GetVoltage(uint16_t s, unsigned short chan)
Calibration specific to the HV DAC.
Definition ad56x6.cc:112
void SetCalib(std::vector< double > sl, std::vector< double > offs)
Definition ad56x6.h:140
bool IsHVEnabled()
Check status of output 3, return whether it is HIGH (HV enabled) or LOW (HV disabled)
Definition ad56x6.cc:120
Parent class containing methods common to AD5666, AD5676, and AD5686 DAC chips.
Definition ad56x6.h:38
virtual uint8_t EncChan(uint8_t chan)
Format channel number for use in SPI word (3-bit number)
Definition ad56x6.h:82
const uint16_t vfull
Full scale output.
Definition ad56x6.h:76
virtual uint16_t GetSetting(double v, unsigned short chan=0)=0
Translates analog voltage to DAC setting, requires calibration.
virtual double GetVoltage(uint16_t s, unsigned short chan=0)=0
Translates DAC setting to analog voltage, requires calibration.
void Readback(short ch=-1)
Read back DAC settings and write them to vector readback.
Definition ad56x6.cc:24
void PowerOn()
Send POWER_UP command to DAC.
Definition ad56x6.h:68
unsigned char * AssembleWord(uint8_t cmd, uint8_t chan, uint16_t *data=NULL)
Assembles the SPI word from command, channel, and data.
Definition ad56x6.cc:8
uint16_t GetData()
Extract 16 bit setting from readback word.
Definition ad56x6.cc:19
const uint8_t nbytes
Size of data word in bytes.
Definition ad56x6.h:77
AD56x6(NanoPi &np, int cs)
Constructor.
Definition ad56x6.h:46
std::vector< uint16_t > readback
Read back DAC settings after Readback() was called.
Definition ad56x6.h:78
bool RampVoltage(unsigned short chan, uint16_t vset=-1, unsigned int steps=16, unsigned int tdelay=500)
Test method to ramp through voltage settings.
Definition ad56x6.cc:67
bool Set(unsigned short chan, uint16_t setting, bool confirm=true)
Sets the selected DAC channel to a value, confirms with readback.
Definition ad56x6.cc:43
Class to control GPIO and SPI communication from a nanoPi Neo2.
Definition nanopi.h:50
bool HasGPIO()
Check whether GPIO was correctly initialized.
Definition nanopi.h:60
bool HasSPI()
Check whether SPI was correctly initialized.
Definition nanopi.h:58