-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathc_adc.c
146 lines (120 loc) · 3.67 KB
/
c_adc.c
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
/*
Copyright (c) 2013 Adafruit
Author: Justin Cooper
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "c_adc.h"
#include "common.h"
#ifdef BBBVERSION41
char adc_prefix_dir[49];
#else
char adc_prefix_dir[40];
#endif
int adc_initialized = 0;
BBIO_err initialize_adc(void)
{
#ifdef BBBVERSION41
char test_path[149];
#else
char test_path[140];
#endif
FILE *fh;
BBIO_err err;
if (adc_initialized) {
return BBIO_OK;
}
#ifdef BBBVERSION41
err = load_device_tree("BB-ADC");
if (err == BBIO_OK) {
strncat(adc_prefix_dir, "/sys/bus/iio/devices/iio:device0/in_voltage", sizeof(adc_prefix_dir));
snprintf(test_path, sizeof(test_path), "%s%d_raw", adc_prefix_dir, 1);
sleep(1);
fh = fopen(test_path, "r");
if (!fh) {
return BBIO_SYSFS;
}
fclose(fh);
adc_initialized = 1;
return BBIO_OK;
}
#else
err = load_device_tree("cape-bone-iio");
if (err == BBIO_OK) {
build_path("/sys/devices", "ocp.", ocp_dir, sizeof(ocp_dir));
build_path(ocp_dir, "helper.", adc_prefix_dir, sizeof(adc_prefix_dir));
strncat(adc_prefix_dir, "/AIN", sizeof(adc_prefix_dir));
snprintf(test_path, sizeof(test_path), "%s%d", adc_prefix_dir, 0);
fh = fopen(test_path, "r");
if (!fh) {
return BBIO_SYSFS;
}
fclose(fh);
adc_initialized = 1;
return BBIO_OK;
}
#endif
return BBIO_GEN;
}
BBIO_err read_value(unsigned int ain, float *value)
{
FILE * fh;
#ifdef BBBVERSION41
char ain_path[149];
snprintf(ain_path, sizeof(ain_path), "%s%d_raw", adc_prefix_dir, ain);
#else
char ain_path[140];
snprintf(ain_path, sizeof(ain_path), "%s%d", adc_prefix_dir, ain);
#endif
int err, try_count=0;
int read_successful;
read_successful = 0;
// Workaround to AIN bug where reading from more than one AIN would cause access failures
while (!read_successful && try_count < 3)
{
fh = fopen(ain_path, "r");
// Likely a bad path to the ocp device driver
if (!fh) {
return BBIO_SYSFS;
}
fseek(fh, 0, SEEK_SET);
err = fscanf(fh, "%f", value);
if (err != EOF) read_successful = 1;
fclose(fh);
try_count++;
}
if (read_successful) return BBIO_OK;
// Fall through and fail
return BBIO_GEN;
}
BBIO_err adc_setup(void)
{
return initialize_adc();
}
BBIO_err adc_cleanup(void)
{
#ifdef BBBVERSION41
return unload_device_tree("BB-ADC");
#else
return unload_device_tree("cape-bone-iio");
#endif
}