lolxnano
Code to control the LoLX SiPM biasing board from the connected nanoPi
Loading...
Searching...
No Matches
ads1248.h
1#ifndef ADS1248_H
2#define ADS1248_H
3
4// ADC commands:
5#define ADC_WAKE 0b00000000
6#define ADC_SYNC 0b00000100
7#define ADC_RESET 0b00000110
8#define ADC_RDATA 0b00010010
9#define ADC_RDATAC 0b00010100
10#define ADC_SDATAC 0b00010110
11#define ADC_SELFOCAL 0b01100010
12#define ADC_NOP 0b11111111
13#define ADC_NOP24 0xFFFFFF
14#define ADC_RREG 0b0010
15#define ADC_WREG 0b0100
16
17// ADC registers:
18#define ADC_REG_MUX0 0x0
19#define ADC_REG_VBIAS 0x1
20#define ADC_REG_MUX1 0x2
21#define ADC_REG_SYS0 0x3
22#define ADC_REG_OFC0 0x4
23#define ADC_REG_OFC1 0x5
24#define ADC_REG_OFC2 0x6
25#define ADC_REG_FSC0 0x7
26#define ADC_REG_FSC1 0x8
27#define ADC_REG_FSC2 0x9
28#define ADC_REG_IDAC0 0xA
29#define ADC_REG_IDAC1 0xB
30#define ADC_REG_GPIOCFG 0xC
31#define ADC_REG_GPIODIR 0xD
32#define ADC_REG_GPIODAT 0xE
33
34// System monitoring functions
35#define ADC_MUX1_NORMAL 0x0
36#define ADC_MUX1_OFFCAL 0x1
37#define ADC_MUX1_GAINCAL 0x2
38#define ADC_MUX1_TEMP 0x3
39#define ADC_MUX1_REF1 0x4
40#define ADC_MUX1_REF0 0x5
41#define ADC_MUX1_ASMON 0x6
42#define ADC_MUX1_DSMON 0x7
43
44// Conversion
45#define ADC_TEMP_CONV 0.405
46#define ADC_TEMP_25C 118.
47
48#include <iostream>
49#include <utility> // std::pair
50
51#include "nanopi.h"
52
59{
60public:
67 Ads1248(NanoPi &np, int cs) : nanoPi(&np), cselect(cs)
68 {
69 if (!np.HasSPI() || !np.HasGPIO())
70 std::cout << "SPI/GPIO not yet initialized!" << std::endl;
71 if (gverbose)
72 std::cout << "Ads1248::cselect = " << cselect << ", nanoPi = " << nanoPi << std::endl;
73 };
74
75 void Reset() { nanoPi->Exch8(cselect, ADC_RESET); }
77 void StopC() { nanoPi->Exch8(cselect, ADC_SDATAC); }
78 uint8_t ReadReg(uint8_t reg);
79 void WriteReg(uint8_t reg, uint8_t data);
80 int32_t ReadData();
81 bool SetCh(unsigned short ch0, unsigned short ch1);
82 bool SetGainRate(int gain, int rate);
83 std::pair<unsigned short, unsigned short> GetGainRate();
95 double GetVal(double data)
96 {
97 double adc2val = 2. * vref / ampLSB / double(gainrate.first) * cgain;
98 return adc2val * data;
99 };
100
101 void SetCGain(double g) { cgain = g; }
103 void SetResist(double r)
104 {
105 resist = r;
106 cgain = 1e9 / resist;
107 }
108
109 void SetVref(double v) { vref = v; }
115 double GetTemp(unsigned short n = 5);
116
117private:
118 uint32_t bits = 0;
119 NanoPi *nanoPi = NULL;
120 const int cselect;
121#ifdef DEBUG
122 bool verbose = true;
123#else
124 bool verbose = false;
125#endif
126#ifdef REV0
127 double resist = 40.2e3;
128 double vref = 4.096;
129#else
130 double resist = 30.0e3;
131 double vref = 2.5;
132#endif
133 double cgain = 1. / resist;
134 const uint32_t ampLSB = 1 << 24;
135 std::pair<unsigned short, unsigned short> gainrate = std::pair<unsigned short, unsigned short>(1, 5); /* default settings */
136 std::pair<unsigned short, unsigned short> GetGainRate(uint8_t readback); // return (gain, rate) in real units (rate in Hz)
137};
138#endif
139
140/* emacs
141 * Local Variables:
142 * tab-width: 8
143 * c-basic-offset: 3
144 * indent-tabs-mode: nil
145 * End:
146 */
void SetVref(double v)
Set reference voltage applied to ADC.
Definition ads1248.h:109
void WriteReg(uint8_t reg, uint8_t data)
Write register.
Definition ads1248.cc:15
void SetResist(double r)
Convenience function to set conversion gain via load resistor to measure current.
Definition ads1248.h:103
Ads1248(NanoPi &np, int cs)
Constructor.
Definition ads1248.h:67
double GetVal(double data)
Convert readout into voltage using set constant.
Definition ads1248.h:95
void SetCGain(double g)
Set conversion gain (mutliplied in GetVal())
Definition ads1248.h:101
uint8_t ReadReg(uint8_t reg)
Read register.
Definition ads1248.cc:6
std::pair< unsigned short, unsigned short > GetGainRate()
Return (gain, rate) in real units (rate in Hz)
Definition ads1248.cc:101
void Reset()
Reset registers to default values.
Definition ads1248.h:75
int32_t ReadData()
Read data from ADC.
Definition ads1248.cc:119
void StopC()
Turn off continuous data readout.
Definition ads1248.h:77
bool SetGainRate(int gain, int rate)
Set gain (0..7) and rate (0..9) settings, see manual.
Definition ads1248.cc:79
bool SetCh(unsigned short ch0, unsigned short ch1)
Select which inputs to measure between.
Definition ads1248.cc:24
double GetTemp(unsigned short n=5)
Read out chip temperature.
Definition ads1248.cc:129
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