]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/link-mso19/api.c
drivers: Simplify some more trigger slope settings.
[libsigrok.git] / src / hardware / link-mso19 / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com>
5 * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt>
6 * Copyright (C) 2013 Lior Elazary <lelazary@yahoo.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <config.h>
23#include "protocol.h"
24
25static const uint32_t drvopts[] = {
26 SR_CONF_OSCILLOSCOPE,
27 SR_CONF_LOGIC_ANALYZER,
28};
29
30static const uint32_t devopts[] = {
31 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
32 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
33 SR_CONF_TRIGGER_TYPE | SR_CONF_LIST,
34 SR_CONF_TRIGGER_SLOPE | SR_CONF_SET,
35 SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_SET,
36 SR_CONF_CAPTURE_RATIO | SR_CONF_SET,
37 SR_CONF_RLE | SR_CONF_SET,
38};
39
40/*
41 * Channels are numbered 0 to 7.
42 *
43 * See also: http://www.linkinstruments.com/images/mso19_1113.gif
44 */
45static const char *channel_names[] = {
46 /* Note: DSO needs to be first. */
47 "DSO", "0", "1", "2", "3", "4", "5", "6", "7",
48};
49
50static const uint64_t samplerates[] = {
51 SR_HZ(100),
52 SR_MHZ(200),
53 SR_HZ(100),
54};
55
56static const char *trigger_slopes[2] = {
57 "r", "f",
58};
59
60static GSList *scan(struct sr_dev_driver *di, GSList *options)
61{
62 int i;
63 GSList *devices = NULL;
64 const char *conn = NULL;
65 const char *serialcomm = NULL;
66 GSList *l;
67 struct sr_config *src;
68 struct udev *udev;
69 int chtype;
70
71 for (l = options; l; l = l->next) {
72 src = l->data;
73 switch (src->key) {
74 case SR_CONF_CONN:
75 conn = g_variant_get_string(src->data, NULL);
76 break;
77 case SR_CONF_SERIALCOMM:
78 serialcomm = g_variant_get_string(src->data, NULL);
79 break;
80 }
81 }
82 if (!conn)
83 conn = SERIALCONN;
84 if (!serialcomm)
85 serialcomm = SERIALCOMM;
86
87 udev = udev_new();
88 if (!udev) {
89 sr_err("Failed to initialize udev.");
90 }
91
92 struct udev_enumerate *enumerate = udev_enumerate_new(udev);
93 udev_enumerate_add_match_subsystem(enumerate, "usb-serial");
94 udev_enumerate_scan_devices(enumerate);
95 struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate);
96 struct udev_list_entry *dev_list_entry;
97 for (dev_list_entry = devs;
98 dev_list_entry != NULL;
99 dev_list_entry = udev_list_entry_get_next(dev_list_entry)) {
100 const char *syspath = udev_list_entry_get_name(dev_list_entry);
101 struct udev_device *dev =
102 udev_device_new_from_syspath(udev, syspath);
103 const char *sysname = udev_device_get_sysname(dev);
104 struct udev_device *parent =
105 udev_device_get_parent_with_subsystem_devtype(dev, "usb",
106 "usb_device");
107
108 if (!parent) {
109 sr_err("Unable to find parent usb device for %s",
110 sysname);
111 continue;
112 }
113
114 const char *idVendor =
115 udev_device_get_sysattr_value(parent, "idVendor");
116 const char *idProduct =
117 udev_device_get_sysattr_value(parent, "idProduct");
118 if (strcmp(USB_VENDOR, idVendor)
119 || strcmp(USB_PRODUCT, idProduct))
120 continue;
121
122 const char *iSerial =
123 udev_device_get_sysattr_value(parent, "serial");
124 const char *iProduct =
125 udev_device_get_sysattr_value(parent, "product");
126
127 char path[32];
128 snprintf(path, sizeof(path), "/dev/%s", sysname);
129 conn = path;
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;
141 strcpy(manufacturer, iProduct + s + 1);
142
143 //Create the device context and set its params
144 struct dev_context *devc;
145 devc = g_malloc0(sizeof(struct dev_context));
146
147 if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) {
148 sr_err("Invalid iSerial: %s.", iSerial);
149 g_free(devc);
150 return devices;
151 }
152
153 char hwrev[32];
154 sprintf(hwrev, "r%d", devc->hwrev);
155 devc->ctlbase1 = 0;
156 devc->protocol_trigger.spimode = 0;
157 for (i = 0; i < 4; i++) {
158 devc->protocol_trigger.word[i] = 0;
159 devc->protocol_trigger.mask[i] = 0xff;
160 }
161
162 devc->serial = sr_serial_dev_inst_new(conn, serialcomm);
163
164 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
165 sdi->status = SR_ST_INACTIVE;
166 sdi->vendor = g_strdup(manufacturer);
167 sdi->model = g_strdup(product);
168 sdi->version = g_strdup(hwrev);
169 sdi->priv = devc;
170
171 for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
172 chtype = (i == 0) ? SR_CHANNEL_ANALOG : SR_CHANNEL_LOGIC;
173 sr_channel_new(sdi, i, chtype, TRUE, channel_names[i]);
174 }
175 }
176
177 return std_scan_complete(di, devices);
178}
179
180static int dev_open(struct sr_dev_inst *sdi)
181{
182 int ret;
183 struct dev_context *devc;
184
185 devc = sdi->priv;
186
187 if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
188 return SR_ERR;
189
190 /* FIXME: discard serial buffer */
191 mso_check_trigger(devc->serial, &devc->trigger_state);
192 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
193
194 ret = mso_reset_adc(sdi);
195 if (ret != SR_OK)
196 return ret;
197
198 mso_check_trigger(devc->serial, &devc->trigger_state);
199 sr_dbg("Trigger state: 0x%x.", devc->trigger_state);
200
201 // ret = mso_reset_fsm(sdi);
202 // if (ret != SR_OK)
203 // return ret;
204 // return SR_ERR;
205
206 return SR_OK;
207}
208
209static int config_get(int key, GVariant **data,
210 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
211{
212 struct dev_context *devc;
213
214 (void)cg;
215
216 if (!sdi)
217 return SR_ERR_ARG;
218
219 devc = sdi->priv;
220
221 switch (key) {
222 case SR_CONF_SAMPLERATE:
223 *data = g_variant_new_uint64(devc->cur_rate);
224 break;
225 default:
226 return SR_ERR_NA;
227 }
228
229 return SR_OK;
230}
231
232static int config_set(int key, GVariant *data,
233 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
234{
235 struct dev_context *devc;
236 uint64_t num_samples;
237 const char *slope;
238 int trigger_pos;
239 double pos;
240
241 (void)cg;
242
243 devc = sdi->priv;
244
245 switch (key) {
246 case SR_CONF_SAMPLERATE:
247 // FIXME
248 return mso_configure_rate(sdi, g_variant_get_uint64(data));
249 case SR_CONF_LIMIT_SAMPLES:
250 num_samples = g_variant_get_uint64(data);
251 if (num_samples != 1024) {
252 sr_err("Only 1024 samples are supported.");
253 return SR_ERR_ARG;
254 }
255 devc->limit_samples = num_samples;
256 break;
257 case SR_CONF_CAPTURE_RATIO:
258 break;
259 case SR_CONF_TRIGGER_SLOPE:
260 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0)
261 return SR_ERR_ARG;
262 devc->trigger_slope = idx;
263 break;
264 case SR_CONF_HORIZ_TRIGGERPOS:
265 pos = g_variant_get_double(data);
266 if (pos < 0 || pos > 255) {
267 sr_err("Trigger position (%f) should be between 0 and 255.", pos);
268 return SR_ERR_ARG;
269 }
270 trigger_pos = (int)pos;
271 devc->trigger_holdoff[0] = trigger_pos & 0xff;
272 break;
273 case SR_CONF_RLE:
274 break;
275 default:
276 return SR_ERR_NA;
277 }
278
279 return SR_OK;
280}
281
282static int config_list(int key, GVariant **data,
283 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
284{
285 switch (key) {
286 case SR_CONF_DEVICE_OPTIONS:
287 return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
288 case SR_CONF_SAMPLERATE:
289 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
290 break;
291 case SR_CONF_TRIGGER_TYPE:
292 *data = g_variant_new_string(TRIGGER_TYPE);
293 break;
294 default:
295 return SR_ERR_NA;
296 }
297
298 return SR_OK;
299}
300
301static int dev_acquisition_start(const struct sr_dev_inst *sdi)
302{
303 struct dev_context *devc;
304 int ret = SR_ERR;
305
306 devc = sdi->priv;
307
308 if (mso_configure_channels(sdi) != SR_OK) {
309 sr_err("Failed to configure channels.");
310 return SR_ERR;
311 }
312
313 /* FIXME: No need to do full reconfigure every time */
314// ret = mso_reset_fsm(sdi);
315// if (ret != SR_OK)
316// return ret;
317
318 /* FIXME: ACDC Mode */
319 devc->ctlbase1 &= 0x7f;
320// devc->ctlbase1 |= devc->acdcmode;
321
322 ret = mso_configure_rate(sdi, devc->cur_rate);
323 if (ret != SR_OK)
324 return ret;
325
326 /* set dac offset */
327 ret = mso_dac_out(sdi, devc->dac_offset);
328 if (ret != SR_OK)
329 return ret;
330
331 ret = mso_configure_threshold_level(sdi);
332 if (ret != SR_OK)
333 return ret;
334
335 ret = mso_configure_trigger(sdi);
336 if (ret != SR_OK)
337 return ret;
338
339 /* END of config hardware part */
340 ret = mso_arm(sdi);
341 if (ret != SR_OK)
342 return ret;
343
344 /* Start acquisition on the device. */
345 mso_check_trigger(devc->serial, &devc->trigger_state);
346 ret = mso_check_trigger(devc->serial, NULL);
347 if (ret != SR_OK)
348 return ret;
349
350 /* Reset trigger state. */
351 devc->trigger_state = 0x00;
352
353 std_session_send_df_header(sdi);
354
355 /* Our first channel is analog, the other 8 are of type 'logic'. */
356 /* TODO. */
357
358 serial_source_add(sdi->session, devc->serial, G_IO_IN, -1,
359 mso_receive_data, sdi);
360
361 return SR_OK;
362}
363
364static int dev_acquisition_stop(struct sr_dev_inst *sdi)
365{
366 stop_acquisition(sdi);
367
368 return SR_OK;
369}
370
371static struct sr_dev_driver link_mso19_driver_info = {
372 .name = "link-mso19",
373 .longname = "Link Instruments MSO-19",
374 .api_version = 1,
375 .init = std_init,
376 .cleanup = std_cleanup,
377 .scan = scan,
378 .dev_list = std_dev_list,
379 .dev_clear = std_dev_clear,
380 .config_get = config_get,
381 .config_set = config_set,
382 .config_list = config_list,
383 .dev_open = dev_open,
384 .dev_close = std_serial_dev_close,
385 .dev_acquisition_start = dev_acquisition_start,
386 .dev_acquisition_stop = dev_acquisition_stop,
387 .context = NULL,
388};
389SR_REGISTER_DEV_DRIVER(link_mso19_driver_info);