Data acquisition with the AD7705 on the raspberry PI
AD7705Comm.h
1 #ifndef __AD7705COMM_H
2 #define __AD7705COMM_H
3 
4 /*
5  * AD7705 class to read data at a given sampling rate
6  *
7  * Copyright (c) 2007 MontaVista Software, Inc.
8  * Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
9  * Copyright (c) 2013-2022 Bernd Porr <mail@berndporr.me.uk>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License.
14  *
15  */
16 #include <stdint.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <linux/types.h>
23 #include <linux/spi/spidev.h>
24 #include <thread>
25 #include <string>
26 
27 #ifndef NDEBUG
28 #define DEBUG
29 #endif
30 
31 
37 public:
42  virtual void hasSample(float sample) = 0;
43 };
44 
45 
53  std::string spiDevice = "/dev/spidev0.0";
54 
59  FS50HZ = 0,
60  FS60HZ = 1,
61  FS250HZ = 2,
62  FS500HZ = 3
63  };
64 
69 
73  enum PGAGains {
74  G1 = 0,
75  G2 = 1,
76  G4 = 2,
77  G8 = 3,
78  G16 = 4,
79  G32 = 5,
80  G64 = 6,
81  G128 = 7
82  };
83 
88 
92  enum AIN {
93  AIN1 = 0,
94  AIN2 = 1
95  };
96 
100  AIN channel = AIN1;
101 
105  enum Modes {
106  Bipolar = 0,
107  Unipolar = 1
108  };
109 
113  Modes mode = Unipolar;
114 };
115 
116 
121 class AD7705Comm {
122 
123 public:
129  AD7705Comm(AD7705settings settings);
130 
135  ~AD7705Comm();
136 
142 
146  void unRegisterCallback();
147 
151  void start();
152 
156  void stop();
157 
158 private:
159  const uint8_t mode = SPI_CPHA | SPI_CPOL;
160  const int drdy_GPIO = 22;
161  const uint32_t speed = 500000;
162  const uint16_t delay = 0;
163  const uint8_t bpw = 8;
164  static constexpr float ADC_REF = 2.5;
165  int fd = 0;
166  std::thread* daqThread = nullptr;
167  int running = 0;
168  AD7705callback* ad7705callback = nullptr;
169  AD7705settings ad7705settings;
170 
171  int spi_transfer(int fd, uint8_t* tx, uint8_t* rx, int n);
172  void writeReset(int fd);
173  void writeReg(int fd, uint8_t v);
174  uint8_t readReg(int fd);
175  int16_t readData(int fd);
176  static void run(AD7705Comm* ad7705comm);
177 
178  inline float pgaGain() {
179  return (float)(1 << ad7705settings.pgaGain);
180  }
181 
182  inline uint8_t commReg() {
183  return ((uint8_t)(ad7705settings.channel));
184  }
185 
186  static int getSysfsIRQfd(int gpio);
187  static int fdPoll(int gpio_fd, int timeout);
188  static void gpio_unexport(int gpio);
189 };
190 
191 
192 #endif
AD7705Comm
This class reads data from the AD7705 in the background (separate thread) and calls a callback functi...
Definition: AD7705Comm.h:121
AD7705Comm::registerCallback
void registerCallback(AD7705callback *cb)
Registers the callback which is called whenever there is a sample.
AD7705callback
Callback for new samples which needs to be implemented by the main program.
Definition: AD7705Comm.h:36
AD7705Comm::start
void start()
Starts the data acquisition.
AD7705settings::channel
AIN channel
Requested input channel (0 or 1)
Definition: AD7705Comm.h:100
AD7705callback::hasSample
virtual void hasSample(float sample)=0
Called after a sample has arrived.
AD7705settings::pgaGain
PGAGains pgaGain
Requested gain.
Definition: AD7705Comm.h:87
AD7705Comm::unRegisterCallback
void unRegisterCallback()
Unregisters the callback to the callback interface.
AD7705Comm::~AD7705Comm
~AD7705Comm()
Destructor which makes sure the data acquisition has stopped.
AD7705settings
Contains all settings for the data acquisition.
Definition: AD7705Comm.h:49
AD7705settings::samplingRate
SamplingRates samplingRate
Sampling rate requested.
Definition: AD7705Comm.h:68
AD7705Comm::stop
void stop()
Stops the data acquistion.
AD7705settings::PGAGains
PGAGains
Gains of the PGA.
Definition: AD7705Comm.h:73
AD7705settings::spiDevice
std::string spiDevice
The SPI device in /dev used.
Definition: AD7705Comm.h:53
AD7705Comm::AD7705Comm
AD7705Comm(AD7705settings settings)
Constructor.
AD7705settings::AIN
AIN
Channel indices.
Definition: AD7705Comm.h:92
AD7705settings::Modes
Modes
Unipolar or bipolar mode.
Definition: AD7705Comm.h:105
AD7705settings::SamplingRates
SamplingRates
Sampling rates.
Definition: AD7705Comm.h:58
AD7705settings::mode
Modes mode
Unipolar or biploar.
Definition: AD7705Comm.h:113