2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
22 #define SERIALCOMM "460800/8n1" //Default communication params
23 #define SERIALCONN "/dev/ttyUSB0" //Default communication params
25 static const int hwcaps[] = {
26 SR_HWCAP_LOGIC_ANALYZER,
28 // SR_HWCAP_CAPTURE_RATIO,
29 SR_HWCAP_LIMIT_SAMPLES,
35 * Probes are numbered 0 to 7.
37 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
39 SR_PRIV const char *mso19_probe_names[NUM_PROBES + 1] = {
40 "0", "1", "2", "3", "4", "5", "6", "7", NULL
43 /*supported samplerates */
44 static const struct sr_samplerates samplerates = {
68 SR_PRIV struct sr_dev_driver link_mso19_driver_info;
69 static struct sr_dev_driver *di = &link_mso19_driver_info;
71 static int hw_init(struct sr_context *sr_ctx)
73 printf("Init driver\n");
75 struct drv_context *drvc;
77 if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
78 sr_err("Driver context malloc failed.");
81 drvc->sr_ctx = sr_ctx;
87 static GSList *hw_scan(GSList *options)
89 //struct sr_hwopt *opt;
90 //struct sr_probe *probe;
98 GSList *devices = NULL;
100 sr_info("Checking for link mso19\n");
102 const char* conn = NULL;
103 const char* serialcomm = NULL;
105 for (l = options; l; l = l->next) {
106 struct sr_hwopt* opt = l->data;
107 switch (opt->hwopt) {
111 case SR_HWOPT_SERIALCOMM:
112 serialcomm = opt->value;
118 if (serialcomm == NULL)
119 serialcomm = SERIALCOMM;
121 struct udev *udev = udev_new();
123 sr_err("Failed to initialize udev.");
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))
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");
141 sr_err("Unable to find parent usb device for %s",
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))
152 const char* iSerial = udev_device_get_sysattr_value(parent, "serial");
153 const char* iProduct = udev_device_get_sysattr_value(parent, "product");
156 snprintf(path, sizeof(path), "/dev/%s", sysname);
158 size_t s = strcspn(iProduct, " ");
160 char manufacturer[32];
161 if (s > sizeof(product) ||
162 strlen(iProduct) - s > sizeof(manufacturer)) {
163 sr_err("Could not parse iProduct: %s.", iProduct);
166 strncpy(product, iProduct, s);
168 strcpy(manufacturer, iProduct + s);
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.");
177 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
178 sr_err("Invalid iSerial: %s.", iSerial);
184 sprintf(hwrev, "r%d", devc->hwrev);
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;
192 if (!(devc->serial = sr_serial_dev_inst_new(conn, serialcomm)))
198 struct sr_dev_inst *sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
199 manufacturer, product, hwrev);
202 sr_err("Unable to create device instance for %s",
204 sr_dev_inst_free(sdi);
213 //sdi->version = "Testing1234";
214 //struct sr_probe *probe;
215 //sdi->probes = g_slist_append(sdi->probes, probe);
217 printf("Add the context\n");
219 struct drv_context *drvc = di->priv;
220 drvc->instances = g_slist_append(drvc->instances, sdi);
221 devices = g_slist_append(devices, sdi);
224 printf("Return devices\n");
228 static GSList *hw_dev_list(void)
230 printf("Dev list\n");
231 struct drv_context *drvc;
235 return drvc->instances;
238 static int hw_dev_open(struct sr_dev_inst *sdi)
240 printf("Dev opewn\n");
241 struct dev_context *devc;
245 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
248 sdi->status = SR_ST_ACTIVE;
250 /* FIXME: discard serial buffer */
251 mso_check_trigger(devc->serial, &devc->trigger_state);
252 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
254 int ret = mso_reset_adc(sdi);
258 mso_check_trigger(devc->serial, &devc->trigger_state);
259 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
261 // ret = mso_reset_fsm(sdi);
269 static int hw_dev_close(struct sr_dev_inst *sdi)
271 printf("dev close\n");
272 struct dev_context *devc;
276 if (devc->serial && devc->serial->fd != -1) {
277 serial_close(devc->serial);
278 sdi->status = SR_ST_INACTIVE;
284 static int hw_cleanup(void)
286 printf("*Dev clearup\n");
288 struct sr_dev_inst *sdi;
289 struct drv_context *drvc;
290 struct dev_context *devc;
293 if (!(drvc = di->priv))
296 /* Properly close and free all devices. */
297 for (l = drvc->instances; l; l = l->next) {
298 if (!(sdi = l->data)) {
299 /* Log error, but continue cleaning up the rest. */
300 sr_err("%s: sdi was NULL, continuing", __func__);
304 if (!(devc = sdi->priv)) {
305 /* Log error, but continue cleaning up the rest. */
306 sr_err("%s: sdi->priv was NULL, continuing", __func__);
311 sr_serial_dev_inst_free(devc->serial);
312 sr_dev_inst_free(sdi);
314 g_slist_free(drvc->instances);
315 drvc->instances = NULL;
320 static int hw_info_get(int info_id, const void **data,
321 const struct sr_dev_inst *sdi)
323 struct dev_context *devc;
325 printf("Get info\n");
331 case SR_DI_NUM_PROBES:
332 *data = GINT_TO_POINTER(1);
334 case SR_DI_PROBE_NAMES:
335 *data = mso19_probe_names;
337 case SR_DI_SAMPLERATES:
338 *data = &samplerates;
340 case SR_DI_TRIGGER_TYPES:
341 *data = (char *)TRIGGER_TYPES;
343 case SR_DI_CUR_SAMPLERATE:
346 *data = &devc->cur_rate;
357 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
360 struct dev_context *devc;
362 const uint64_t *tmp_u64;
364 printf("Config set\n");
367 if (sdi->status != SR_ST_ACTIVE)
371 case SR_HWCAP_SAMPLERATE:
372 return mso_configure_rate(sdi, *(const uint64_t *) value);
375 case SR_HWCAP_LIMIT_SAMPLES:
378 case SR_HWCAP_CAPTURE_RATIO:
391 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
394 struct sr_datafeed_packet *packet;
395 struct sr_datafeed_header *header;
396 struct sr_datafeed_meta_logic meta;
397 struct dev_context *devc;
398 uint32_t trigger_config[4];
400 uint16_t readcount, delaycount;
401 uint8_t changrp_mask;
407 printf("Accquistion start\n");
410 if (sdi->status != SR_ST_ACTIVE)
413 //TODO if (ols_configure_probes(sdi) != SR_OK) {
414 //TODO sr_err("Failed to configure probes.");
415 //TODO return SR_ERR;
419 * Enable/disable channel groups in the flag register according to the
420 * probe mask. Calculate this here, because num_channels is needed
421 * to limit readcount.
425 //for (i = 0; i < 4; i++) {
426 // if (devc->probe_mask & (0xff << (i * 8))) {
427 // changrp_mask |= (1 << i);
432 /* FIXME: No need to do full reconfigure every time */
433 // ret = mso_reset_fsm(sdi);
437 /* FIXME: ACDC Mode */
438 devc->ctlbase1 &= 0x7f;
439 // devc->ctlbase1 |= devc->acdcmode;
441 ret = mso_configure_rate(sdi, devc->cur_rate);
446 ret = mso_dac_out(sdi, devc->dac_offset);
450 ret = mso_configure_threshold_level(sdi);
454 ret = mso_configure_trigger(sdi);
458 /* FIXME: trigger_position */
461 /* END of config hardware part */
468 /* without trigger */
469 // ret = mso_force_capture(sdi);
473 /* Start acquisition on the device. */
474 mso_check_trigger(sdi, &devc->trigger_state);
475 ret = mso_check_trigger(sdi, NULL);
479 sr_source_add(devc->serial->fd, G_IO_IN, -1, mso_receive_data, cb_data);
481 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
482 sr_err("Datafeed packet malloc failed.");
483 return SR_ERR_MALLOC;
486 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
487 sr_err("Datafeed header malloc failed.");
489 return SR_ERR_MALLOC;
492 packet->type = SR_DF_HEADER;
493 packet->payload = (unsigned char *)header;
494 header->feed_version = 1;
495 gettimeofday(&header->starttime, NULL);
496 sr_session_send(cb_data, packet);
498 packet->type = SR_DF_META_LOGIC;
499 packet->payload = &meta;
500 meta.samplerate = devc->cur_rate;
501 meta.num_probes = NUM_PROBES;
502 sr_session_send(cb_data, packet);
510 /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */
511 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
513 printf("Accuqstion stop\n");
514 /* Avoid compiler warnings. */
517 stop_acquisition(sdi);
522 SR_PRIV struct sr_dev_driver link_mso19_driver_info = {
523 .name = "link-mso19",
524 .longname = "Link Instruments MSO-19",
527 .cleanup = hw_cleanup,
529 .dev_list = hw_dev_list,
530 .dev_clear = hw_cleanup,
531 .dev_open = hw_dev_open,
532 .dev_close = hw_dev_close,
533 .info_get = hw_info_get,
534 .dev_config_set = hw_dev_config_set,
535 .dev_acquisition_start = hw_dev_acquisition_start,
536 .dev_acquisition_stop = hw_dev_acquisition_stop,