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