]> sigrok.org Git - libsigrok.git/blame - hardware/nexus-osciprime/api.c
Python bindings: Fix reported libsigrok version.
[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
c8733a2b 32static const int32_t hwopts[] = {
1953564a
BV
33 SR_CONF_CONN,
34 SR_CONF_SERIALCOMM,
523dfc24
BV
35};
36
c8733a2b 37static const int32_t hwcaps[] = {
1953564a
BV
38 SR_CONF_OSCILLOSCOPE,
39 SR_CONF_LIMIT_SAMPLES,
40 SR_CONF_CONTINUOUS,
41 SR_CONF_TIMEBASE,
42 SR_CONF_VDIV,
523dfc24
BV
43};
44
45static const struct sr_rational timebases[] = {
46 /* 24 MHz */
47 { 42, 1e9 },
48 /* 12 MHz */
49 { 83, 1e9 },
50 /* 6 MHz */
51 { 167, 1e9 },
52 /* 3 MHz */
53 { 333, 1e9 },
54 /* 1.5 MHz */
55 { 667, 1e9 },
56 /* 750 kHz */
57 { 1333, 1e9 },
58 /* 375 kHz */
59 { 2667, 1e9 },
60 /* 187.5 kHz */
61 { 5333, 1e9 },
62 /* 93.25 kHz */
63 { 10724, 1e9 },
64 /* 46.875 kHz */
65 { 21333, 1e9 },
66 /* 23.4375 kHz */
67 { 42666, 1e9 },
68 /* 11.718 kHz */
69 { 85339, 1e9 },
70 /* 5.859 kHz */
71 { 170678, 1e9 },
72 /* 2.929 kHz */
73 { 341413, 1e9 },
74 /* 1.465 kHz */
75 { 682594, 1e9 },
76 /* 732 Hz */
77 { 1366, 1e6 },
78 /* 366 Hz */
79 { 2732, 1e6 },
80 /* 183 Hz */
81 { 5464, 1e6 },
82 /* 91 Hz */
83 { 10989, 1e6 },
84 /* 46 Hz */
85 { 21739, 1e6 },
86 /* 23 Hz */
87 { 43478, 1e6 },
88 /* 12 Hz */
89 { 83333, 1e6 },
90};
91
92static const char *probe_names[] = {
78693401 93 "CHA", "CHB",
523dfc24
BV
94 NULL,
95};
96
97static const struct sr_rational vdivs[] = {
98 { 1, 1 },
99 { 2, 1 },
100 { 5, 2 },
101 { 5, 1 },
102 { 10, 1 },
103};
104
105
35a078bc
BV
106SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info;
107static struct sr_dev_driver *di = &nexus_osciprime_driver_info;
523dfc24 108static int hw_dev_close(struct sr_dev_inst *sdi);
35a078bc
BV
109
110/* Properly close and free all devices. */
111static int clear_instances(void)
112{
113 struct sr_dev_inst *sdi;
114 struct drv_context *drvc;
115 struct dev_context *devc;
116 GSList *l;
117
118 if (!(drvc = di->priv))
119 return SR_OK;
120
121 for (l = drvc->instances; l; l = l->next) {
122 if (!(sdi = l->data))
123 continue;
124 if (!(devc = sdi->priv))
125 continue;
126
523dfc24
BV
127 hw_dev_close(sdi);
128 sr_usb_dev_inst_free(devc->usb);
35a078bc
BV
129 sr_dev_inst_free(sdi);
130 }
131
132 g_slist_free(drvc->instances);
133 drvc->instances = NULL;
134
135 return SR_OK;
136}
137
523dfc24 138static int hw_init(struct sr_context *sr_ctx)
35a078bc 139{
063e7aef 140 return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
35a078bc
BV
141}
142
143static GSList *hw_scan(GSList *options)
144{
145 struct drv_context *drvc;
523dfc24
BV
146 struct dev_context *devc;
147 struct sr_dev_inst *sdi;
148 struct sr_usb_dev_inst *usb;
1987b8d6 149 struct sr_config *src;
523dfc24
BV
150 struct sr_probe *probe;
151 libusb_device *dev;
152 GSList *usb_devices, *devices, *l;
153 int i;
154 const char *conn;
35a078bc
BV
155
156 (void)options;
157
4b97c74e 158 drvc = di->priv;
35a078bc 159
523dfc24
BV
160 /* USB scan is always authoritative. */
161 clear_instances();
162
163 conn = NULL;
164 for (l = options; l; l = l->next) {
1987b8d6
BV
165 src = l->data;
166 switch (src->key) {
1953564a 167 case SR_CONF_CONN:
c8733a2b 168 conn = g_variant_get_string(src->data, NULL);
523dfc24
BV
169 break;
170 }
171 }
172 if (!conn)
173 conn = OSCI_VIDPID;
174
175 devices = NULL;
176 if ((usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
177 for (l = usb_devices; l; l = l->next) {
178 usb = l->data;
179 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
180 OSCI_VENDOR, OSCI_MODEL, OSCI_VERSION)))
181 return NULL;
182 sdi->driver = di;
183 for (i = 0; probe_names[i]; i++) {
184 if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE,
185 probe_names[i])))
186 return NULL;
187 sdi->probes = g_slist_append(sdi->probes, probe);
188 }
189
190 if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
191 return NULL;
192 sdi->priv = devc;
193 devc->usb = usb;
194
195 if (strcmp(conn, OSCI_VIDPID)) {
196 if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK)
197 break;
198 dev = libusb_get_device(usb->devhdl);
199 if (ezusb_upload_firmware(dev, 0, OSCI_FIRMWARE) == SR_OK)
200 /* Remember when the firmware on this device was updated */
201 devc->fw_updated = g_get_monotonic_time();
202 else
203 sr_err("Firmware upload failed for device "
204 "at bus %d address %d.", usb->bus, usb->address);
205 }
206
207 drvc->instances = g_slist_append(drvc->instances, sdi);
208 devices = g_slist_append(devices, sdi);
209 }
210 g_slist_free(usb_devices);
211 } else
212 g_slist_free_full(usb_devices, g_free);
35a078bc
BV
213
214 return devices;
215}
216
217static GSList *hw_dev_list(void)
218{
0e94d524 219 return ((struct drv_context *)(di->priv))->instances;
35a078bc
BV
220}
221
222static int hw_dev_open(struct sr_dev_inst *sdi)
223{
df36acb3 224
35a078bc 225 /* TODO */
df36acb3 226 (void)sdi;
35a078bc
BV
227
228 return SR_OK;
229}
230
231static int hw_dev_close(struct sr_dev_inst *sdi)
232{
df36acb3 233
35a078bc 234 /* TODO */
df36acb3 235 (void)sdi;
35a078bc
BV
236
237 return SR_OK;
238}
239
240static int hw_cleanup(void)
241{
242 clear_instances();
243
244 /* TODO */
245
246 return SR_OK;
247}
248
c8733a2b 249static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi)
35a078bc
BV
250{
251 int ret;
252
df36acb3 253 /* TODO */
c8733a2b 254 (void)data;
df36acb3 255
35a078bc
BV
256 if (sdi->status != SR_ST_ACTIVE) {
257 sr_err("Device inactive, can't set config options.");
258 return SR_ERR;
259 }
260
261 ret = SR_OK;
035a1078 262 switch (id) {
df36acb3 263
35a078bc 264 default:
035a1078 265 sr_err("Unknown hardware capability: %d.", id);
35a078bc
BV
266 ret = SR_ERR_ARG;
267 }
268
269 return ret;
270}
271
c8733a2b 272static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
a1c743fc 273{
a1c743fc 274 (void)sdi;
ff08a52a 275 (void)data;
a1c743fc
BV
276
277 switch (key) {
278 default:
279 return SR_ERR_ARG;
280 }
281
282 return SR_OK;
283}
284
35a078bc
BV
285static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
286 void *cb_data)
287{
288 /* TODO */
df36acb3
BV
289 (void)sdi;
290 (void)cb_data;
35a078bc
BV
291
292 return SR_OK;
293}
294
295static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
296{
297 (void)cb_data;
298
299 if (sdi->status != SR_ST_ACTIVE) {
300 sr_err("Device inactive, can't stop acquisition.");
301 return SR_ERR;
302 }
303
304 /* TODO */
305
306 return SR_OK;
307}
308
309SR_PRIV struct sr_dev_driver nexus_osciprime_driver_info = {
310 .name = "nexus-osciprime",
523dfc24 311 .longname = "Nexus OsciPrime",
35a078bc
BV
312 .api_version = 1,
313 .init = hw_init,
314 .cleanup = hw_cleanup,
315 .scan = hw_scan,
316 .dev_list = hw_dev_list,
317 .dev_clear = clear_instances,
6fab7b8f 318 .config_get = NULL,
035a1078 319 .config_set = config_set,
a1c743fc 320 .config_list = config_list,
35a078bc
BV
321 .dev_open = hw_dev_open,
322 .dev_close = hw_dev_close,
35a078bc
BV
323 .dev_acquisition_start = hw_dev_acquisition_start,
324 .dev_acquisition_stop = hw_dev_acquisition_stop,
325 .priv = NULL,
326};