/
ad7705_test.cpp
213 lines (172 loc) · 4.98 KB
/
ad7705_test.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
/*
* AD7705 test program for the Raspberry PI
*
* Copyright (c) 2007 MontaVista Software, Inc.
* Copyright (c) 2007 Anton Vorontsov <avorontsov@ru.mvista.com>
* Copyright (c) 2013-2015 Bernd Porr <mail@berndporr.me.uk>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* Cross-compile with cross-gcc -I/path/to/cross-kernel/include
*/
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include "gz_clk.h"
#include "gpio-sysfs.h"
static void pabort(const char *s)
{
perror(s);
abort();
}
static const char *device = "/dev/spidev0.0";
static uint8_t mode = SPI_CPHA | SPI_CPOL;;
static uint8_t bits = 8;
static int drdy_GPIO = 22;
static void writeReset(int fd)
{
int ret;
uint8_t tx1[5] = {0xff,0xff,0xff,0xff,0xff};
uint8_t rx1[5] = {0};
struct spi_ioc_transfer tr;
memset(&tr,0,sizeof(struct spi_ioc_transfer));
tr.tx_buf = (unsigned long)tx1;
tr.rx_buf = (unsigned long)rx1;
tr.len = sizeof(tx1);
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1) {
printf("\nerr=%d when trying to reset. \n",ret);
pabort("Can't send spi message");
}
}
static void writeReg(int fd, uint8_t v)
{
int ret;
uint8_t tx1[1];
tx1[0] = v;
uint8_t rx1[1] = {0};
struct spi_ioc_transfer tr;
memset(&tr,0,sizeof(struct spi_ioc_transfer));
tr.tx_buf = (unsigned long)tx1;
tr.rx_buf = (unsigned long)rx1;
tr.len = sizeof(tx1);
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
pabort("can't send spi message");
}
static uint8_t readReg(int fd)
{
int ret;
uint8_t tx1[1];
tx1[0] = 0;
uint8_t rx1[1] = {0};
struct spi_ioc_transfer tr;
memset(&tr,0,sizeof(struct spi_ioc_transfer));
tr.tx_buf = (unsigned long)tx1;
tr.rx_buf = (unsigned long)rx1;
tr.len = sizeof(tx1);
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
pabort("can't send spi message");
return rx1[0];
}
static int readData(int fd)
{
int ret;
uint8_t tx1[2] = {0,0};
uint8_t rx1[2] = {0,0};
struct spi_ioc_transfer tr;
memset(&tr,0,sizeof(struct spi_ioc_transfer));
tr.tx_buf = (unsigned long)tx1;
tr.rx_buf = (unsigned long)rx1;
tr.len = sizeof(tx1);
ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
if (ret < 1)
{
printf("\n can't send spi message, ret = %d\n",ret);
exit(1);
}
return (rx1[0]<<8)|(rx1[1]);
}
int main(int argc, char *argv[])
{
int ret = 0;
int fd;
int sysfs_fd;
int no_tty = !isatty( fileno(stdout) );
fd = open(device, O_RDWR);
if (fd < 0)
pabort("can't open device");
/*
* spi mode
*/
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1)
pabort("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1)
pabort("can't get spi mode");
/*
* bits per word
*/
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't set bits per word");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1)
pabort("can't get bits per word");
fprintf(stderr, "spi mode: %d\n", mode);
fprintf(stderr, "bits per word: %d\n", bits);
// enable master clock for the AD
// divisor results in roughly 4.9MHz
// this also inits the general purpose IO
gz_clock_ena(GZ_CLK_5MHz,5);
// enables sysfs entry for the GPIO pin
gpio_export(drdy_GPIO);
// set to input
gpio_set_dir(drdy_GPIO,0);
// set interrupt detection to falling edge
gpio_set_edge(drdy_GPIO,"falling");
// get a file descriptor for the GPIO pin
sysfs_fd = gpio_fd_open(drdy_GPIO);
// resets the AD7705 so that it expects a write to the communication register
printf("sending reset\n");
writeReset(fd);
// tell the AD7705 that the next write will be to the clock register
writeReg(fd,0x20);
// write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock
writeReg(fd,0x0C);
// tell the AD7705 that the next write will be the setup register
writeReg(fd,0x10);
// intiates a self calibration and then after that starts converting
writeReg(fd,0x40);
// we read data in an endless loop and display it
// this needs to run in a thread ideally
int comReg, clkReg, setReg;
// tell the AD7705 to read the communication register (8 bits)
writeReg(fd,0x08);
// read the data register by performing two 8 bit reads
comReg = (int) readReg(fd);
printf("communication register: %d/n",comReg);
// tell the AD7705 to read the clock register (8 bits)
writeReg(fd,0x28);
// read the data register by performing two 8 bit reads
clkReg = (int) readReg(fd);
printf("clock register: %d/n",comReg);
// tell the AD7705 to read the setup register (8 bits)
writeReg(fd,0x18);
// read the data register by performing two 8 bit reads
setReg = (int) readReg(fd);
printf("se-tup register: %d/n",comReg);
// tell the AD7705 to read the clock register (8 bits)
close(fd);
gpio_fd_close(sysfs_fd);
return ret;
}