]> sigrok.org Git - libsigrok.git/blame - hardware/link-mso19/api.c
More cleanup. Communication with mso19 is working, but its not triggering. Need to...
[libsigrok.git] / hardware / link-mso19 / api.c
CommitLineData
df92e5cf 1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-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
20#include "protocol.h"
21
df92e5cf 22static const int hwcaps[] = {
23 SR_HWCAP_LOGIC_ANALYZER,
24 SR_HWCAP_SAMPLERATE,
25// SR_HWCAP_CAPTURE_RATIO,
26 SR_HWCAP_LIMIT_SAMPLES,
27// SR_HWCAP_RLE,
28 0,
29};
30
31/*
32 * Probes are numbered 0 to 7.
33 *
34 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
35 */
36SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = {
37 "0", "1", "2", "3", "4", "5", "6", "7", NULL
38};
39
40/*supported samplerates */
41static const struct sr_samplerates samplerates = {
42 SR_HZ(100),
df92e5cf 43 SR_MHZ(200),
4db2aaff 44 SR_HZ(1),
45 //SR_HZ(100),
46 //SR_HZ(200),
47 //SR_HZ(500),
48 //SR_KHZ(1),
49 //SR_KHZ(2),
50 //SR_KHZ(5),
51 //SR_KHZ(10),
52 //SR_KHZ(20),
53 //SR_KHZ(50),
54 //SR_KHZ(100),
55 //SR_KHZ(200),
56 //SR_KHZ(500),
57 //SR_MHZ(1),
58 //SR_MHZ(2),
59 //SR_MHZ(5),
60 //SR_MHZ(10),
61 //SR_MHZ(20),
62 //SR_MHZ(50),
63 //SR_MHZ(100),
64 //SR_MHZ(200),
df92e5cf 65 NULL,
66};
67
68SR_PRIV struct sr_dev_driver link_mso19_driver_info;
69static struct sr_dev_driver *di = &link_mso19_driver_info;
70
71static int hw_init(struct sr_context *sr_ctx)
72{
73 printf("Init driver\n");
74
75 struct drv_context *drvc;
76
77 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
78 sr_err("Driver context malloc failed.");
79 return SR_ERR_MALLOC;
80 }
81 drvc->sr_ctx = sr_ctx;
82 di->priv = drvc;
83
84 return SR_OK;
85}
86
87static GSList *hw_scan(GSList *options)
88{
89 //struct sr_hwopt *opt;
90 //struct sr_probe *probe;
91 //GPollFD probefd;
92 //int ret, i;
93 //char buf[8];
94 //struct udev *udev;
95 int i;
96
97 (void)options;
98 GSList *devices = NULL;
99
100 sr_info("Checking for link mso19\n");
101
102 const char* conn = NULL;
103 const char* serialcomm = NULL;
104 GSList *l;
105 for (l = options; l; l = l->next) {
106 struct sr_hwopt* opt = l->data;
107 switch (opt->hwopt) {
108 case SR_HWOPT_CONN:
109 conn = opt->value;
110 break;
111 case SR_HWOPT_SERIALCOMM:
112 serialcomm = opt->value;
113 break;
114 }
115 }
116 if (!conn)
117 conn = SERIALCONN;
118 if (serialcomm == NULL)
119 serialcomm = SERIALCOMM;
120
121 struct udev *udev = udev_new();
122 if (!udev) {
123 sr_err("Failed to initialize udev.");
124 }
125 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
126 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
127 udev_enumerate_scan_devices(enumerate);
128 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
129 struct udev_list_entry *dev_list_entry;
130 for (dev_list_entry = devs;
131 dev_list_entry != NULL;
132 dev_list_entry = udev_list_entry_get_next(dev_list_entry))
133 {
134 const char *syspath = udev_list_entry_get_name(dev_list_entry);
135 struct udev_device *dev = udev_device_new_from_syspath(udev, syspath);
136 const char *sysname = udev_device_get_sysname(dev);
137 struct udev_device *parent = udev_device_get_parent_with_subsystem_devtype(
138 dev, "usb", "usb_device");
139
140 if (!parent) {
141 sr_err("Unable to find parent usb device for %s",
142 sysname);
143 continue;
144 }
145
146 const char *idVendor = udev_device_get_sysattr_value(parent, "idVendor");
147 const char *idProduct = udev_device_get_sysattr_value(parent, "idProduct");
148 if (strcmp(USB_VENDOR, idVendor)
149 || strcmp(USB_PRODUCT, idProduct))
150 continue;
151
152 const char* iSerial = udev_device_get_sysattr_value(parent, "serial");
153 const char* iProduct = udev_device_get_sysattr_value(parent, "product");
154
155 char path[32];
156 snprintf(path, sizeof(path), "/dev/%s", sysname);
157
158 size_t s = strcspn(iProduct, " ");
159 char product[32];
160 char manufacturer[32];
161 if (s > sizeof(product) ||
162 strlen(iProduct) - s > sizeof(manufacturer)) {
163 sr_err("Could not parse iProduct: %s.", iProduct);
164 continue;
165 }
166 strncpy(product, iProduct, s);
167 product[s] = 0;
4db2aaff 168 strcpy(manufacturer, iProduct + s + 1);
df92e5cf 169
170 //Create the device context and set its params
171 struct dev_context *devc;
172 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
173 sr_err("Device context malloc failed.");
174 return devices;
175 }
176
177 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
178 sr_err("Invalid iSerial: %s.", iSerial);
179 g_free(devc);
180 return devices;
181 }
182
183 char hwrev[32];
184 sprintf(hwrev, "r%d", devc->hwrev);
185 devc->ctlbase1 = 0;
186 devc->protocol_trigger.spimode = 0;
187 for (i = 0; i < 4; i++) {
188 devc->protocol_trigger.word[i] = 0;
189 devc->protocol_trigger.mask[i] = 0xff;
190 }
191
192 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
193 {
194 g_free(devc);
195 return devices;
196 }
197
198 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
199 manufacturer, product, hwrev);
200
201 if (!sdi) {
202 sr_err("Unable to create device instance for %s",
203 sysname);
204 sr_dev_inst_free(sdi);
205 g_free(devc);
206 return devices;
207 }
208
df92e5cf 209 sdi->driver = di;
210 sdi->priv = devc;
4db2aaff 211
212 for (i = 0; i < NUM_PROBES; i++) {
213 struct sr_probe *probe;
214 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE,
215 mso19_probe_names[i])))
216 return 0;
217 sdi->probes = g_slist_append(sdi->probes, probe);
218 }
219
df92e5cf 220
221 printf("Add the context\n");
222 //Add the driver
223 struct drv_context *drvc = di->priv;
224 drvc->instances = g_slist_append(drvc->instances, sdi);
225 devices = g_slist_append(devices, sdi);
226 }
227
228 printf("Return devices\n");
229 return devices;
230}
231
232static GSList *hw_dev_list(void)
233{
234 printf("Dev list\n");
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 printf("Dev opewn\n");
245 struct dev_context *devc;
246
247 devc = sdi->priv;
248
249 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
250 return SR_ERR;
251
252 sdi->status = SR_ST_ACTIVE;
253
254 /* FIXME: discard serial buffer */
255 mso_check_trigger(devc->serial, &devc->trigger_state);
256 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
257
258 int ret = mso_reset_adc(sdi);
259 if (ret != SR_OK)
260 return ret;
261
262 mso_check_trigger(devc->serial, &devc->trigger_state);
263 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
264
265 // ret = mso_reset_fsm(sdi);
266 // if (ret != SR_OK)
267 // return ret;
268 // return SR_ERR;
269
270 return SR_OK;
271}
272
273static int hw_dev_close(struct sr_dev_inst *sdi)
274{
275 printf("dev close\n");
276 struct dev_context *devc;
277
278 devc = sdi->priv;
279
280 if (devc->serial && devc->serial->fd != -1) {
281 serial_close(devc->serial);
282 sdi->status = SR_ST_INACTIVE;
283 }
284
285 return SR_OK;
286}
287
288static int hw_cleanup(void)
289{
290 printf("*Dev clearup\n");
291 GSList *l;
292 struct sr_dev_inst *sdi;
293 struct drv_context *drvc;
294 struct dev_context *devc;
295 int ret = SR_OK;
296
297 if (!(drvc = di->priv))
298 return SR_OK;
299
300 /* Properly close and free all devices. */
301 for (l = drvc->instances; l; l = l->next) {
302 if (!(sdi = l->data)) {
303 /* Log error, but continue cleaning up the rest. */
304 sr_err("%s: sdi was NULL, continuing", __func__);
305 ret = SR_ERR_BUG;
306 continue;
307 }
308 if (!(devc = sdi->priv)) {
309 /* Log error, but continue cleaning up the rest. */
310 sr_err("%s: sdi->priv was NULL, continuing", __func__);
311 ret = SR_ERR_BUG;
312 continue;
313 }
314 hw_dev_close(sdi);
315 sr_serial_dev_inst_free(devc->serial);
316 sr_dev_inst_free(sdi);
317 }
318 g_slist_free(drvc->instances);
319 drvc->instances = NULL;
320
321 return ret;
322}
323
324static int hw_info_get(int info_id, const void **data,
325 const struct sr_dev_inst *sdi)
326{
327 struct dev_context *devc;
328
329 printf("Get info\n");
330
331 switch (info_id) {
332 case SR_DI_HWCAPS:
333 *data = hwcaps;
334 break;
335 case SR_DI_NUM_PROBES:
336 *data = GINT_TO_POINTER(1);
337 break;
338 case SR_DI_PROBE_NAMES:
339 *data = mso19_probe_names;
340 break;
341 case SR_DI_SAMPLERATES:
342 *data = &samplerates;
343 break;
344 case SR_DI_TRIGGER_TYPES:
345 *data = (char *)TRIGGER_TYPES;
346 break;
347 case SR_DI_CUR_SAMPLERATE:
348 if (sdi) {
349 devc = sdi->priv;
350 *data = &devc->cur_rate;
351 } else
352 return SR_ERR;
353 break;
354 default:
355 return SR_ERR_ARG;
356 }
357
358 return SR_OK;
359}
360
361static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
362 const void *value)
363{
df92e5cf 364 int ret;
df92e5cf 365
366 if (sdi->status != SR_ST_ACTIVE)
367 return SR_ERR;
368
369 switch (hwcap) {
370 case SR_HWCAP_SAMPLERATE:
371 return mso_configure_rate(sdi, *(const uint64_t *) value);
372 ret = SR_OK;
373 break;
374 case SR_HWCAP_LIMIT_SAMPLES:
375 ret = SR_OK;
376 break;
377 case SR_HWCAP_CAPTURE_RATIO:
378 ret = SR_OK;
379 break;
380 case SR_HWCAP_RLE:
381 ret = SR_OK;
382 break;
383 default:
384 ret = SR_ERR;
385 }
386
387 return ret;
388}
389
390static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
391 void *cb_data)
392{
393 struct sr_datafeed_packet *packet;
394 struct sr_datafeed_header *header;
395 struct sr_datafeed_meta_logic meta;
396 struct dev_context *devc;
df92e5cf 397 int ret = SR_ERR;
398
399
400 printf("Accquistion start\n");
401 devc = sdi->priv;
402
403 if (sdi->status != SR_ST_ACTIVE)
404 return SR_ERR;
405
406 //TODO if (ols_configure_probes(sdi) != SR_OK) {
407 //TODO sr_err("Failed to configure probes.");
408 //TODO return SR_ERR;
409 //TODO }
410
411 /*
412 * Enable/disable channel groups in the flag register according to the
413 * probe mask. Calculate this here, because num_channels is needed
414 * to limit readcount.
415 */
416 //changrp_mask = 0;
417 //num_channels = 0;
418 //for (i = 0; i < 4; i++) {
419 // if (devc->probe_mask & (0xff << (i * 8))) {
420 // changrp_mask |= (1 << i);
421 // num_channels++;
422 // }
423 //}
424
425 /* FIXME: No need to do full reconfigure every time */
426// ret = mso_reset_fsm(sdi);
427// if (ret != SR_OK)
428// return ret;
429
430 /* FIXME: ACDC Mode */
431 devc->ctlbase1 &= 0x7f;
432// devc->ctlbase1 |= devc->acdcmode;
433
4db2aaff 434 printf("Configure rate\n");
df92e5cf 435 ret = mso_configure_rate(sdi, devc->cur_rate);
436 if (ret != SR_OK)
437 return ret;
438
439 /* set dac offset */
4db2aaff 440 printf("Configure dac\n");
df92e5cf 441 ret = mso_dac_out(sdi, devc->dac_offset);
442 if (ret != SR_OK)
443 return ret;
444
4db2aaff 445 printf("Configure threshold level\n");
df92e5cf 446 ret = mso_configure_threshold_level(sdi);
447 if (ret != SR_OK)
448 return ret;
449
4db2aaff 450 printf("Configure trigger\n");
df92e5cf 451 ret = mso_configure_trigger(sdi);
452 if (ret != SR_OK)
453 return ret;
454
455 /* FIXME: trigger_position */
456
457
458 /* END of config hardware part */
459
460 /* with trigger */
4db2aaff 461 printf("arm\n");
df92e5cf 462 ret = mso_arm(sdi);
463 if (ret != SR_OK)
464 return ret;
465
466 /* without trigger */
467// ret = mso_force_capture(sdi);
468// if (ret != SR_OK)
469// return ret;
470
471 /* Start acquisition on the device. */
4db2aaff 472 printf("Check trigger\n");
473 mso_check_trigger(devc->serial, &devc->trigger_state);
474 ret = mso_check_trigger(devc->serial, NULL);
df92e5cf 475 if (ret != SR_OK)
476 return ret;
477
4db2aaff 478 printf("Source add\n");
df92e5cf 479 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
480
4db2aaff 481 printf("Create packet\n");
df92e5cf 482 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
483 sr_err("Datafeed packet malloc failed.");
484 return SR_ERR_MALLOC;
485 }
486
487 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
488 sr_err("Datafeed header malloc failed.");
489 g_free(packet);
490 return SR_ERR_MALLOC;
491 }
492
493 packet->type = SR_DF_HEADER;
494 packet->payload = (unsigned char *)header;
495 header->feed_version = 1;
496 gettimeofday(&header->starttime, NULL);
497 sr_session_send(cb_data, packet);
498
499 packet->type = SR_DF_META_LOGIC;
500 packet->payload = &meta;
501 meta.samplerate = devc->cur_rate;
502 meta.num_probes = NUM_PROBES;
503 sr_session_send(cb_data, packet);
504
505 g_free(header);
506 g_free(packet);
507
508 return SR_OK;
509}
510
511/* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
512static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513{
514 printf("Accuqstion stop\n");
515 /* Avoid compiler warnings. */
516 (void)cb_data;
517
518 stop_acquisition(sdi);
519
520 return SR_OK;
521}
522
523SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
524 .name = "link-mso19",
525 .longname = "Link Instruments MSO-19",
526 .api_version = 1,
527 .init = hw_init,
528 .cleanup = hw_cleanup,
529 .scan = hw_scan,
530 .dev_list = hw_dev_list,
531 .dev_clear = hw_cleanup,
532 .dev_open = hw_dev_open,
533 .dev_close = hw_dev_close,
534 .info_get = hw_info_get,
535 .dev_config_set = hw_dev_config_set,
536 .dev_acquisition_start = hw_dev_acquisition_start,
537 .dev_acquisition_stop = hw_dev_acquisition_stop,
538 .priv = NULL,
539};