]> sigrok.org Git - libsigrok.git/blame - hardware/nexus-osciprime/api.c
serial-dmm: Handle time-limited acquisition
[libsigrok.git] / hardware / nexus-osciprime / api.c
CommitLineData
35a078bc
BV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
523dfc24 20#include <string.h>
35a078bc
BV
21#include <glib.h>
22#include "libsigrok.h"
23#include "libsigrok-internal.h"
24#include "protocol.h"
25
523dfc24
BV
26#define OSCI_VENDOR "Nexus Computing"
27#define OSCI_MODEL "OsciPrime"
28#define OSCI_VERSION "1.0"
29#define OSCI_FIRMWARE FIRMWARE_DIR "/nexus-osciprime.fw"
30#define OSCI_VIDPID "04b4.1004"
31
32static const int hwopts[] = {
33 SR_HWOPT_CONN,
34 SR_HWOPT_SERIALCOMM,
35 0,
36};
37
38static const int hwcaps[] = {
39 SR_HWCAP_OSCILLOSCOPE,
40 SR_HWCAP_LIMIT_SAMPLES,
41 SR_HWCAP_CONTINUOUS,
42 SR_HWCAP_TIMEBASE,
43 SR_HWCAP_VDIV,
44 0,
45};
46
47static const struct sr_rational timebases[] = {
48 /* 24 MHz */
49 { 42, 1e9 },
50 /* 12 MHz */
51 { 83, 1e9 },
52 /* 6 MHz */
53 { 167, 1e9 },
54 /* 3 MHz */
55 { 333, 1e9 },
56 /* 1.5 MHz */
57 { 667, 1e9 },
58 /* 750 kHz */
59 { 1333, 1e9 },
60 /* 375 kHz */
61 { 2667, 1e9 },
62 /* 187.5 kHz */
63 { 5333, 1e9 },
64 /* 93.25 kHz */
65 { 10724, 1e9 },
66 /* 46.875 kHz */
67 { 21333, 1e9 },
68 /* 23.4375 kHz */
69 { 42666, 1e9 },
70 /* 11.718 kHz */
71 { 85339, 1e9 },
72 /* 5.859 kHz */
73 { 170678, 1e9 },
74 /* 2.929 kHz */
75 { 341413, 1e9 },
76 /* 1.465 kHz */
77 { 682594, 1e9 },
78 /* 732 Hz */
79 { 1366, 1e6 },
80 /* 366 Hz */
81 { 2732, 1e6 },
82 /* 183 Hz */
83 { 5464, 1e6 },
84 /* 91 Hz */
85 { 10989, 1e6 },
86 /* 46 Hz */
87 { 21739, 1e6 },
88 /* 23 Hz */
89 { 43478, 1e6 },
90 /* 12 Hz */
91 { 83333, 1e6 },
92};
93
94static const char *probe_names[] = {
95 "CHA",
96 "CHB",
97 NULL,
98};
99
100static const struct sr_rational vdivs[] = {
101 { 1, 1 },
102 { 2, 1 },
103 { 5, 2 },
104 { 5, 1 },
105 { 10, 1 },
106};
107
108
35a078bc
BV
109SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info;
110static struct sr_dev_driver *di = &nexus_osciprime_driver_info;
523dfc24 111static int hw_dev_close(struct sr_dev_inst *sdi);
35a078bc
BV
112
113/* Properly close and free all devices. */
114static int clear_instances(void)
115{
116 struct sr_dev_inst *sdi;
117 struct drv_context *drvc;
118 struct dev_context *devc;
119 GSList *l;
120
121 if (!(drvc = di->priv))
122 return SR_OK;
123
124 for (l = drvc->instances; l; l = l->next) {
125 if (!(sdi = l->data))
126 continue;
127 if (!(devc = sdi->priv))
128 continue;
129
523dfc24
BV
130 hw_dev_close(sdi);
131 sr_usb_dev_inst_free(devc->usb);
35a078bc
BV
132 sr_dev_inst_free(sdi);
133 }
134
135 g_slist_free(drvc->instances);
136 drvc->instances = NULL;
137
138 return SR_OK;
139}
140
523dfc24 141static int hw_init(struct sr_context *sr_ctx)
35a078bc
BV
142{
143 struct drv_context *drvc;
144
145 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
146 sr_err("Driver context malloc failed.");
147 return SR_ERR_MALLOC;
148 }
149
523dfc24 150 drvc->sr_ctx = sr_ctx;
35a078bc
BV
151 di->priv = drvc;
152
153 return SR_OK;
154}
155
156static GSList *hw_scan(GSList *options)
157{
158 struct drv_context *drvc;
523dfc24
BV
159 struct dev_context *devc;
160 struct sr_dev_inst *sdi;
161 struct sr_usb_dev_inst *usb;
162 struct sr_hwopt *opt;
163 struct sr_probe *probe;
164 libusb_device *dev;
165 GSList *usb_devices, *devices, *l;
166 int i;
167 const char *conn;
35a078bc
BV
168
169 (void)options;
170
523dfc24
BV
171 if (!(drvc = di->priv)) {
172 sr_err("Driver was not initialized.");
173 return NULL;
174 }
35a078bc 175
523dfc24
BV
176 /* USB scan is always authoritative. */
177 clear_instances();
178
179 conn = NULL;
180 for (l = options; l; l = l->next) {
181 opt = l->data;
182 switch (opt->hwopt) {
183 case SR_HWOPT_CONN:
184 conn = opt->value;
185 break;
186 }
187 }
188 if (!conn)
189 conn = OSCI_VIDPID;
190
191 devices = NULL;
192 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
193 for (l = usb_devices; l; l = l->next) {
194 usb = l->data;
195 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
196 OSCI_VENDOR, OSCI_MODEL, OSCI_VERSION)))
197 return NULL;
198 sdi->driver = di;
199 for (i = 0; probe_names[i]; i++) {
200 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
201 probe_names[i])))
202 return NULL;
203 sdi->probes = g_slist_append(sdi->probes, probe);
204 }
205
206 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
207 return NULL;
208 sdi->priv = devc;
209 devc->usb = usb;
210
211 if (strcmp(conn, OSCI_VIDPID)) {
212 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
213 break;
214 dev = libusb_get_device(usb->devhdl);
215 if (ezusb_upload_firmware(dev, 0, OSCI_FIRMWARE) == SR_OK)
216 /* Remember when the firmware on this device was updated */
217 devc->fw_updated = g_get_monotonic_time();
218 else
219 sr_err("Firmware upload failed for device "
220 "at bus %d address %d.", usb->bus, usb->address);
221 }
222
223 drvc->instances = g_slist_append(drvc->instances, sdi);
224 devices = g_slist_append(devices, sdi);
225 }
226 g_slist_free(usb_devices);
227 } else
228 g_slist_free_full(usb_devices, g_free);
35a078bc
BV
229
230 return devices;
231}
232
233static GSList *hw_dev_list(void)
234{
235 struct drv_context *drvc;
236
237 drvc = di->priv;
238
239 return drvc->instances;
240}
241
242static int hw_dev_open(struct sr_dev_inst *sdi)
243{
244 /* TODO */
245
246 return SR_OK;
247}
248
249static int hw_dev_close(struct sr_dev_inst *sdi)
250{
251 /* TODO */
252
253 return SR_OK;
254}
255
256static int hw_cleanup(void)
257{
258 clear_instances();
259
260 /* TODO */
261
262 return SR_OK;
263}
264
265static int hw_info_get(int info_id, const void **data,
266 const struct sr_dev_inst *sdi)
267{
268 switch (info_id) {
269 /* TODO */
270 default:
271 sr_err("Unknown info_id: %d.", info_id);
272 return SR_ERR_ARG;
273 }
274
275 return SR_OK;
276}
277
278static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
279 const void *value)
280{
281 int ret;
282
283 if (sdi->status != SR_ST_ACTIVE) {
284 sr_err("Device inactive, can't set config options.");
285 return SR_ERR;
286 }
287
288 ret = SR_OK;
289 switch (hwcap) {
290 /* TODO */
291 default:
292 sr_err("Unknown hardware capability: %d.", hwcap);
293 ret = SR_ERR_ARG;
294 }
295
296 return ret;
297}
298
299static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
300 void *cb_data)
301{
302 /* TODO */
303
304 return SR_OK;
305}
306
307static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
308{
309 (void)cb_data;
310
311 if (sdi->status != SR_ST_ACTIVE) {
312 sr_err("Device inactive, can't stop acquisition.");
313 return SR_ERR;
314 }
315
316 /* TODO */
317
318 return SR_OK;
319}
320
321SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info = {
322 .name = "nexus-osciprime",
523dfc24 323 .longname = "Nexus OsciPrime",
35a078bc
BV
324 .api_version = 1,
325 .init = hw_init,
326 .cleanup = hw_cleanup,
327 .scan = hw_scan,
328 .dev_list = hw_dev_list,
329 .dev_clear = clear_instances,
330 .dev_open = hw_dev_open,
331 .dev_close = hw_dev_close,
332 .info_get = hw_info_get,
333 .dev_config_set = hw_dev_config_set,
334 .dev_acquisition_start = hw_dev_acquisition_start,
335 .dev_acquisition_stop = hw_dev_acquisition_stop,
336 .priv = NULL,
337};