Dawn Framework 1.0
Universal data acquisition framework for embedded systems
adc.cxx
1// dawn/src/porting/nuttx/adc.cxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#include "dawn/porting/adc.hxx"
7
8#include <errno.h>
9#include <fcntl.h>
10#include <nuttx/analog/adc.h>
11#include <nuttx/analog/ioctl.h>
12#include <sys/ioctl.h>
13#include <unistd.h>
14
15#include "dawn/debug.hxx"
16#include "dawn/porting/config.hxx"
17
18//***************************************************************************
19// Public Functions
20//***************************************************************************
21
22//***************************************************************************
23// Name: adc_open
24//***************************************************************************
25
26int adc_open(const char *path)
27{
28 int fd;
29
30 fd = open(path, O_RDWR);
31 DAWNINFO("ADC: open %s %d\n", path, fd);
32 if (fd < 0)
33 {
34 DAWNERR("Failed to open ADC file %s (error %d)\n", path, errno);
35 return -errno;
36 }
37 return fd;
38}
39
40//***************************************************************************
41// Name: adc_close
42//***************************************************************************
43
44void adc_close(int fd)
45{
46 if (fd >= 0)
47 {
48 close(fd);
49 }
50}
51
52//***************************************************************************
53// Name: adc_get_info
54//***************************************************************************
55
56int adc_get_nchans(int fd)
57{
58 int ret;
59 ret = ioctl(fd, ANIOC_GET_NCHANNELS, nullptr);
60 if (ret < 0)
61 {
62 return -errno;
63 }
64
65 // All channels must fit into FIFO
66
67 if (CONFIG_ADC_FIFOSIZE <= ret)
68 {
69 DAWNERR("ADC channel count %d exceeds FIFO size %d\n", ret, CONFIG_ADC_FIFOSIZE);
70 return -EINVAL;
71 }
72
73 return ret;
74}
75
76//***************************************************************************
77// Name: adc_start
78//***************************************************************************
79
80int adc_start(int fd)
81{
82 int ret;
83
84 ret = ioctl(fd, ANIOC_TRIGGER, 0);
85 if (ret < 0)
86 {
87 DAWNERR("ANIOC_TRIGGER failed %d\n", errno);
88 return -errno;
89 }
90
91 return OK;
92}
93
94//***************************************************************************
95// Name: adc_stop
96//***************************************************************************
97
98int adc_stop(int fd)
99{
100 int ret;
101
102 ret = ioctl(fd, ANIOC_STOP, 0);
103 if (ret < 0)
104 {
105 if (errno == ENOTTY)
106 {
107 return -ENOSYS;
108 }
109
110 DAWNERR("ANIOC_STOP failed %d\n", errno);
111 return -errno;
112 }
113
114 return OK;
115}
116
117//***************************************************************************
118// Name: adc_set_timer_freq
119//***************************************************************************
120
121int adc_set_timer_freq(int fd, uint32_t freq_hz)
122{
123 int ret;
124
125 ret = ioctl(fd, ANIOC_SET_TIMER_FREQ, freq_hz);
126 if (ret < 0)
127 {
128 if (errno == ENOTTY)
129 {
130 return -ENOSYS;
131 }
132
133 DAWNERR("ANIOC_SET_TIMER_FREQ failed %d\n", errno);
134 return -errno;
135 }
136
137 return OK;
138}
139
140//***************************************************************************
141// Name: adc_get_samples_count
142//***************************************************************************
143
144int adc_get_samples_count(int fd)
145{
146 int ret;
147
148 ret = ioctl(fd, ANIOC_SAMPLES_ON_READ, 0);
149 if (ret < 0)
150 {
151 if (errno == ENOTTY)
152 {
153 return -ENOSYS;
154 }
155
156 return -errno;
157 }
158
159 return ret;
160}
161
162//***************************************************************************
163// Name: adc_read
164//***************************************************************************
165
166int adc_read(int fd, dawn::porting::adc_read_s *adc, size_t len)
167{
168 ssize_t ret;
169
170 // Read data from FIFO
171 // NOTE: we read data without channel information!
172 // Correct order of samples must be done on board level!
173
174 ret = read(fd, adc, len);
175 if (ret < 0)
176 {
177 return -errno;
178 }
179
180 return static_cast<int>(ret);
181}
ADC read data.
Definition adc.hxx:23