]>
Commit | Line | Data |
---|---|---|
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 | ||
25 | static const uint32_t drvopts[] = { | |
26 | SR_CONF_OSCILLOSCOPE, | |
27 | SR_CONF_LOGIC_ANALYZER, | |
28 | }; | |
29 | ||
30 | static 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 | */ | |
45 | static const char *channel_names[] = { | |
46 | /* Note: DSO needs to be first. */ | |
47 | "DSO", "0", "1", "2", "3", "4", "5", "6", "7", | |
48 | }; | |
49 | ||
50 | static const uint64_t samplerates[] = { | |
51 | SR_HZ(100), | |
52 | SR_MHZ(200), | |
53 | SR_HZ(100), | |
54 | }; | |
55 | ||
56 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
57 | { | |
58 | int i; | |
59 | GSList *devices = NULL; | |
60 | const char *conn = NULL; | |
61 | const char *serialcomm = NULL; | |
62 | GSList *l; | |
63 | struct sr_config *src; | |
64 | struct udev *udev; | |
65 | int chtype; | |
66 | ||
67 | for (l = options; l; l = l->next) { | |
68 | src = l->data; | |
69 | switch (src->key) { | |
70 | case SR_CONF_CONN: | |
71 | conn = g_variant_get_string(src->data, NULL); | |
72 | break; | |
73 | case SR_CONF_SERIALCOMM: | |
74 | serialcomm = g_variant_get_string(src->data, NULL); | |
75 | break; | |
76 | } | |
77 | } | |
78 | if (!conn) | |
79 | conn = SERIALCONN; | |
80 | if (!serialcomm) | |
81 | serialcomm = SERIALCOMM; | |
82 | ||
83 | udev = udev_new(); | |
84 | if (!udev) { | |
85 | sr_err("Failed to initialize udev."); | |
86 | } | |
87 | ||
88 | struct udev_enumerate *enumerate = udev_enumerate_new(udev); | |
89 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
90 | udev_enumerate_scan_devices(enumerate); | |
91 | struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate); | |
92 | struct udev_list_entry *dev_list_entry; | |
93 | for (dev_list_entry = devs; | |
94 | dev_list_entry != NULL; | |
95 | dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { | |
96 | const char *syspath = udev_list_entry_get_name(dev_list_entry); | |
97 | struct udev_device *dev = | |
98 | udev_device_new_from_syspath(udev, syspath); | |
99 | const char *sysname = udev_device_get_sysname(dev); | |
100 | struct udev_device *parent = | |
101 | udev_device_get_parent_with_subsystem_devtype(dev, "usb", | |
102 | "usb_device"); | |
103 | ||
104 | if (!parent) { | |
105 | sr_err("Unable to find parent usb device for %s", | |
106 | sysname); | |
107 | continue; | |
108 | } | |
109 | ||
110 | const char *idVendor = | |
111 | udev_device_get_sysattr_value(parent, "idVendor"); | |
112 | const char *idProduct = | |
113 | udev_device_get_sysattr_value(parent, "idProduct"); | |
114 | if (strcmp(USB_VENDOR, idVendor) | |
115 | || strcmp(USB_PRODUCT, idProduct)) | |
116 | continue; | |
117 | ||
118 | const char *iSerial = | |
119 | udev_device_get_sysattr_value(parent, "serial"); | |
120 | const char *iProduct = | |
121 | udev_device_get_sysattr_value(parent, "product"); | |
122 | ||
123 | char path[32]; | |
124 | snprintf(path, sizeof(path), "/dev/%s", sysname); | |
125 | conn = path; | |
126 | ||
127 | size_t s = strcspn(iProduct, " "); | |
128 | char product[32]; | |
129 | char manufacturer[32]; | |
130 | if (s > sizeof(product) || | |
131 | strlen(iProduct) - s > sizeof(manufacturer)) { | |
132 | sr_err("Could not parse iProduct: %s.", iProduct); | |
133 | continue; | |
134 | } | |
135 | strncpy(product, iProduct, s); | |
136 | product[s] = 0; | |
137 | strcpy(manufacturer, iProduct + s + 1); | |
138 | ||
139 | //Create the device context and set its params | |
140 | struct dev_context *devc; | |
141 | devc = g_malloc0(sizeof(struct dev_context)); | |
142 | ||
143 | if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) { | |
144 | sr_err("Invalid iSerial: %s.", iSerial); | |
145 | g_free(devc); | |
146 | return devices; | |
147 | } | |
148 | ||
149 | char hwrev[32]; | |
150 | sprintf(hwrev, "r%d", devc->hwrev); | |
151 | devc->ctlbase1 = 0; | |
152 | devc->protocol_trigger.spimode = 0; | |
153 | for (i = 0; i < 4; i++) { | |
154 | devc->protocol_trigger.word[i] = 0; | |
155 | devc->protocol_trigger.mask[i] = 0xff; | |
156 | } | |
157 | ||
158 | devc->serial = sr_serial_dev_inst_new(conn, serialcomm); | |
159 | ||
160 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
161 | sdi->status = SR_ST_INACTIVE; | |
162 | sdi->vendor = g_strdup(manufacturer); | |
163 | sdi->model = g_strdup(product); | |
164 | sdi->version = g_strdup(hwrev); | |
165 | sdi->priv = devc; | |
166 | ||
167 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) { | |
168 | chtype = (i == 0) ? SR_CHANNEL_ANALOG : SR_CHANNEL_LOGIC; | |
169 | sr_channel_new(sdi, i, chtype, TRUE, channel_names[i]); | |
170 | } | |
171 | } | |
172 | ||
173 | return std_scan_complete(di, devices); | |
174 | } | |
175 | ||
176 | static int dev_open(struct sr_dev_inst *sdi) | |
177 | { | |
178 | int ret; | |
179 | struct dev_context *devc; | |
180 | ||
181 | devc = sdi->priv; | |
182 | ||
183 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) | |
184 | return SR_ERR; | |
185 | ||
186 | /* FIXME: discard serial buffer */ | |
187 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
188 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
189 | ||
190 | ret = mso_reset_adc(sdi); | |
191 | if (ret != SR_OK) | |
192 | return ret; | |
193 | ||
194 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
195 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
196 | ||
197 | // ret = mso_reset_fsm(sdi); | |
198 | // if (ret != SR_OK) | |
199 | // return ret; | |
200 | // return SR_ERR; | |
201 | ||
202 | return SR_OK; | |
203 | } | |
204 | ||
205 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
206 | const struct sr_channel_group *cg) | |
207 | { | |
208 | struct dev_context *devc; | |
209 | ||
210 | (void)cg; | |
211 | ||
212 | if (!sdi) | |
213 | return SR_ERR_ARG; | |
214 | ||
215 | devc = sdi->priv; | |
216 | ||
217 | switch (key) { | |
218 | case SR_CONF_SAMPLERATE: | |
219 | *data = g_variant_new_uint64(devc->cur_rate); | |
220 | break; | |
221 | default: | |
222 | return SR_ERR_NA; | |
223 | } | |
224 | ||
225 | return SR_OK; | |
226 | } | |
227 | ||
228 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
229 | const struct sr_channel_group *cg) | |
230 | { | |
231 | int ret; | |
232 | struct dev_context *devc; | |
233 | uint64_t num_samples; | |
234 | const char *slope; | |
235 | int trigger_pos; | |
236 | double pos; | |
237 | ||
238 | (void)cg; | |
239 | ||
240 | devc = sdi->priv; | |
241 | ||
242 | ret = SR_OK; | |
243 | switch (key) { | |
244 | case SR_CONF_SAMPLERATE: | |
245 | // FIXME | |
246 | return mso_configure_rate(sdi, g_variant_get_uint64(data)); | |
247 | case SR_CONF_LIMIT_SAMPLES: | |
248 | num_samples = g_variant_get_uint64(data); | |
249 | if (num_samples != 1024) { | |
250 | sr_err("Only 1024 samples are supported."); | |
251 | ret = SR_ERR_ARG; | |
252 | break; | |
253 | }; | |
254 | devc->limit_samples = num_samples; | |
255 | break; | |
256 | case SR_CONF_CAPTURE_RATIO: | |
257 | break; | |
258 | case SR_CONF_TRIGGER_SLOPE: | |
259 | slope = g_variant_get_string(data, NULL); | |
260 | ||
261 | if (!slope || !(slope[0] == 'f' || slope[0] == 'r')) | |
262 | sr_err("Invalid trigger slope"); | |
263 | ret = SR_ERR_ARG; | |
264 | break; | |
265 | } | |
266 | devc->trigger_slope = (slope[0] == 'r') | |
267 | ? SLOPE_POSITIVE : SLOPE_NEGATIVE; | |
268 | break; | |
269 | case SR_CONF_HORIZ_TRIGGERPOS: | |
270 | pos = g_variant_get_double(data); | |
271 | if (pos < 0 || pos > 255) { | |
272 | sr_err("Trigger position (%f) should be between 0 and 255.", pos); | |
273 | ret = SR_ERR_ARG; | |
274 | break; | |
275 | } | |
276 | trigger_pos = (int)pos; | |
277 | devc->trigger_holdoff[0] = trigger_pos & 0xff; | |
278 | break; | |
279 | case SR_CONF_RLE: | |
280 | break; | |
281 | default: | |
282 | ret = SR_ERR_NA; | |
283 | } | |
284 | ||
285 | return ret; | |
286 | } | |
287 | ||
288 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
289 | const struct sr_channel_group *cg) | |
290 | { | |
291 | GVariant *gvar; | |
292 | GVariantBuilder gvb; | |
293 | ||
294 | switch (key) { | |
295 | case SR_CONF_DEVICE_OPTIONS: | |
296 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); | |
297 | case SR_CONF_SAMPLERATE: | |
298 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
299 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, | |
300 | ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
301 | g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar); | |
302 | *data = g_variant_builder_end(&gvb); | |
303 | break; | |
304 | case SR_CONF_TRIGGER_TYPE: | |
305 | *data = g_variant_new_string(TRIGGER_TYPE); | |
306 | break; | |
307 | default: | |
308 | return SR_ERR_NA; | |
309 | } | |
310 | ||
311 | return SR_OK; | |
312 | } | |
313 | ||
314 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
315 | { | |
316 | struct dev_context *devc; | |
317 | int ret = SR_ERR; | |
318 | ||
319 | devc = sdi->priv; | |
320 | ||
321 | if (mso_configure_channels(sdi) != SR_OK) { | |
322 | sr_err("Failed to configure channels."); | |
323 | return SR_ERR; | |
324 | } | |
325 | ||
326 | /* FIXME: No need to do full reconfigure every time */ | |
327 | // ret = mso_reset_fsm(sdi); | |
328 | // if (ret != SR_OK) | |
329 | // return ret; | |
330 | ||
331 | /* FIXME: ACDC Mode */ | |
332 | devc->ctlbase1 &= 0x7f; | |
333 | // devc->ctlbase1 |= devc->acdcmode; | |
334 | ||
335 | ret = mso_configure_rate(sdi, devc->cur_rate); | |
336 | if (ret != SR_OK) | |
337 | return ret; | |
338 | ||
339 | /* set dac offset */ | |
340 | ret = mso_dac_out(sdi, devc->dac_offset); | |
341 | if (ret != SR_OK) | |
342 | return ret; | |
343 | ||
344 | ret = mso_configure_threshold_level(sdi); | |
345 | if (ret != SR_OK) | |
346 | return ret; | |
347 | ||
348 | ret = mso_configure_trigger(sdi); | |
349 | if (ret != SR_OK) | |
350 | return ret; | |
351 | ||
352 | /* END of config hardware part */ | |
353 | ret = mso_arm(sdi); | |
354 | if (ret != SR_OK) | |
355 | return ret; | |
356 | ||
357 | /* Start acquisition on the device. */ | |
358 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
359 | ret = mso_check_trigger(devc->serial, NULL); | |
360 | if (ret != SR_OK) | |
361 | return ret; | |
362 | ||
363 | /* Reset trigger state. */ | |
364 | devc->trigger_state = 0x00; | |
365 | ||
366 | std_session_send_df_header(sdi); | |
367 | ||
368 | /* Our first channel is analog, the other 8 are of type 'logic'. */ | |
369 | /* TODO. */ | |
370 | ||
371 | serial_source_add(sdi->session, devc->serial, G_IO_IN, -1, | |
372 | mso_receive_data, sdi); | |
373 | ||
374 | return SR_OK; | |
375 | } | |
376 | ||
377 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
378 | { | |
379 | stop_acquisition(sdi); | |
380 | ||
381 | return SR_OK; | |
382 | } | |
383 | ||
384 | static struct sr_dev_driver link_mso19_driver_info = { | |
385 | .name = "link-mso19", | |
386 | .longname = "Link Instruments MSO-19", | |
387 | .api_version = 1, | |
388 | .init = std_init, | |
389 | .cleanup = std_cleanup, | |
390 | .scan = scan, | |
391 | .dev_list = std_dev_list, | |
392 | .dev_clear = std_dev_clear, | |
393 | .config_get = config_get, | |
394 | .config_set = config_set, | |
395 | .config_list = config_list, | |
396 | .dev_open = dev_open, | |
397 | .dev_close = std_serial_dev_close, | |
398 | .dev_acquisition_start = dev_acquisition_start, | |
399 | .dev_acquisition_stop = dev_acquisition_stop, | |
400 | .context = NULL, | |
401 | }; | |
402 | SR_REGISTER_DEV_DRIVER(link_mso19_driver_info); |