forked from graeme-hattan/rpi_AD7705_test_software
-
Notifications
You must be signed in to change notification settings - Fork 10
/
AD7705Comm.cpp
288 lines (234 loc) · 5.59 KB
/
AD7705Comm.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
#include "AD7705Comm.h"
#include <fcntl.h>
#include <poll.h>
#include <string.h>
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 256
AD7705Comm::AD7705Comm(AD7705settings settings) {
fd = open(settings.spiDevice.c_str(), O_RDWR);
if (fd < 0)
throw "Can't open device";
ad7705settings = settings;
// set SPI mode
int ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) {
throw "can't set spi mode";
} else {
#ifdef DEBUG
fprintf(stderr,"SPI mode %d set (ret=%d).\n",mode,ret);
#endif
}
}
AD7705Comm::~AD7705Comm() {
stop();
close(fd);
}
void AD7705Comm::registerCallback(AD7705callback* cb) {
ad7705callback = cb;
}
void AD7705Comm::unRegisterCallback() {
ad7705callback = nullptr;
}
int AD7705Comm::spi_transfer(int fd, uint8_t* tx, uint8_t* rx, int n) {
struct spi_ioc_transfer tr;
memset(&tr,0,sizeof(struct spi_ioc_transfer));
tr.tx_buf = (unsigned long)tx;
tr.rx_buf = (unsigned long)rx;
tr.len = n;
tr.delay_usecs = delay;
tr.speed_hz = speed;
tr.bits_per_word = bpw;
int ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
return ret;
}
void AD7705Comm::writeReset(int fd) {
uint8_t tx[5] = {0xff,0xff,0xff,0xff,0xff};
uint8_t rx[5] = {0};
int ret = spi_transfer(fd, tx, rx, 5);
if (ret < 1) {
#ifdef DEBUG
printf("\nerr=%d when trying to reset. \n",ret);
#endif
throw "Can't send spi message";
}
}
void AD7705Comm::writeReg(int fd, uint8_t v) {
uint8_t tx = v;
uint8_t rx = 0;
int ret = spi_transfer(fd, &tx, &rx, 1);
if (ret < 1)
throw "can't send spi message";
}
uint8_t AD7705Comm::readReg(int fd) {
uint8_t tx = 0;
uint8_t rx = 0;
int ret = spi_transfer(fd, &tx, &rx, 1);
if (ret < 1)
throw "can't send spi message";
return rx;
}
int16_t AD7705Comm::readData(int fd) {
uint8_t tx[2] = {0,0};
uint8_t rx[2] = {0,0};
int ret = spi_transfer(fd, tx, rx, 2);
if (ret < 1) {
printf("\n can't send spi message, ret = %d\n",ret);
throw "Can't send spi message";
}
return (rx[0]<<8)|(rx[1]);
}
void AD7705Comm::run() {
// get a file descriptor for the IRQ GPIO pin
int sysfs_fd = getSysfsIRQfd(drdy_GPIO);
running = 1;
while (running) {
// let's wait for data for max one second
// goes to sleep until an interrupt happens
int ret = fdPoll(sysfs_fd,1000);
if (ret<1) {
#ifdef DEBUG
fprintf(stderr,"Poll error %d\n",ret);
#endif
throw "Interrupt timeout";
}
// tell the AD7705 to read the data register (16 bits)
writeReg(fd, commReg() | 0x38);
// read the data register by performing two 8 bit reads
const float norm = 0x8000;
const float value =(readData(fd))/norm *
ADC_REF / pgaGain();
if (nullptr != ad7705callback) {
ad7705callback->hasSample(value);
}
}
close(sysfs_fd);
gpio_unexport(drdy_GPIO);
}
void AD7705Comm::start() {
if (running) return;
#ifdef DEBUG
fprintf(stderr,"Sending reset.\n");
#endif
writeReset(fd);
// tell the AD7705 that the next write will be to the clock register
writeReg(fd,commReg() | 0x20);
// write 00000100 : CLOCKDIV=0,CLK=1,expects 4.9152MHz input clock, sampling rate
writeReg(fd,0x04 | ad7705settings.samplingRate);
// tell the AD7705 that the next write will be the setup register
writeReg(fd,commReg() | 0x10);
// intiates a self calibration and then converting starts
writeReg(fd,0x40 | (ad7705settings.mode << 2) | ( ad7705settings.pgaGain << 3) );
#ifdef DEBUG
fprintf(stderr,"Starting DAQ thread.\n");
#endif
daqThread = std::thread(&AD7705Comm::run,this);
}
void AD7705Comm::stop() {
if (!running) return;
running = 0;
daqThread.join();
#ifdef DEBUG
fprintf(stderr,"DAQ thread stopped.\n");
#endif
}
int AD7705Comm::getSysfsIRQfd(int gpio) {
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
#ifdef DEBUG
perror("gpio/export");
#endif
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
int r = write(fd, buf, len);
close(fd);
if (r < 0) {
#ifdef DEBUG
perror("gpio/export");
#endif
return fd;
}
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
#ifdef DEBUG
perror("gpio/direction");
#endif
return fd;
}
r = write(fd, "in", 3);
close(fd);
if (r < 0) {
#ifdef DEBUG
perror("gpio/export");
#endif
return fd;
}
const char edge[] = "falling";
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
#ifdef DEBUG
perror("gpio/set-edge");
#endif
return fd;
}
r = write(fd, edge, strlen(edge) + 1);
close(fd);
if (r < 0) {
#ifdef DEBUG
perror("gpio/export");
#endif
return fd;
}
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0) {
#ifdef DEBUG
perror("gpio/fd_open");
#endif
}
return fd;
}
void AD7705Comm::gpio_unexport(int gpio) {
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0) {
#ifdef DEBUG
perror("gpio/unexport");
#endif
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
int r = write(fd, buf, len);
if (r < 0) {
#ifdef DEBUG
perror("gpio/export");
#endif
}
close(fd);
}
int AD7705Comm::fdPoll(int gpio_fd, int timeout)
{
struct pollfd fdset[1];
int nfds = 1;
int rc;
char buf[MAX_BUF];
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = gpio_fd;
fdset[0].events = POLLPRI;
rc = poll(fdset, nfds, timeout);
if (fdset[0].revents & POLLPRI) {
// dummy read
int r = read(fdset[0].fd, buf, MAX_BUF);
if (r < 0) {
#ifdef DEBUG
perror("gpio/export");
#endif
return r;
}
}
return rc;
}