]>
Commit | Line | Data |
---|---|---|
01cf8814 DR |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> | |
a2936073 | 5 | * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt> |
01cf8814 DR |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
8a839354 UH |
11 | * |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
01cf8814 DR |
19 | */ |
20 | ||
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <unistd.h> | |
25 | #include <fcntl.h> | |
26 | #include <sys/time.h> | |
27 | #include <inttypes.h> | |
28 | #include <glib.h> | |
29 | #include <libudev.h> | |
01cf8814 | 30 | #include <arpa/inet.h> |
b7f09cf8 UH |
31 | #include "sigrok.h" |
32 | #include "sigrok-internal.h" | |
01cf8814 DR |
33 | #include "config.h" |
34 | #include "link-mso19.h" | |
35 | ||
36 | #define USB_VENDOR "3195" | |
37 | #define USB_PRODUCT "f190" | |
38 | ||
464d12c7 KS |
39 | #define NUM_PROBES 8 |
40 | ||
01cf8814 | 41 | static int capabilities[] = { |
5a2326a7 UH |
42 | SR_HWCAP_LOGIC_ANALYZER, |
43 | // SR_HWCAP_OSCILLOSCOPE, | |
44 | // SR_HWCAP_PAT_GENERATOR, | |
01cf8814 | 45 | |
5a2326a7 UH |
46 | SR_HWCAP_SAMPLERATE, |
47 | // SR_HWCAP_CAPTURE_RATIO, | |
48 | SR_HWCAP_LIMIT_SAMPLES, | |
01cf8814 DR |
49 | 0, |
50 | }; | |
51 | ||
464d12c7 KS |
52 | static const char *probe_names[NUM_PROBES + 1] = { |
53 | "0", | |
54 | "1", | |
55 | "2", | |
56 | "3", | |
57 | "4", | |
58 | "5", | |
59 | "6", | |
60 | "7", | |
61 | NULL, | |
62 | }; | |
63 | ||
01cf8814 | 64 | static uint64_t supported_samplerates[] = { |
c9140419 UH |
65 | SR_HZ(100), |
66 | SR_HZ(200), | |
67 | SR_HZ(500), | |
59df0c77 UH |
68 | SR_KHZ(1), |
69 | SR_KHZ(2), | |
70 | SR_KHZ(5), | |
71 | SR_KHZ(10), | |
72 | SR_KHZ(20), | |
73 | SR_KHZ(50), | |
74 | SR_KHZ(100), | |
75 | SR_KHZ(200), | |
76 | SR_KHZ(500), | |
77 | SR_MHZ(1), | |
78 | SR_MHZ(2), | |
79 | SR_MHZ(5), | |
80 | SR_MHZ(10), | |
81 | SR_MHZ(20), | |
82 | SR_MHZ(50), | |
83 | SR_MHZ(100), | |
84 | SR_MHZ(200), | |
85 | 0, | |
01cf8814 DR |
86 | }; |
87 | ||
60679b18 | 88 | static struct sr_samplerates samplerates = { |
c9140419 | 89 | SR_HZ(100), |
59df0c77 | 90 | SR_MHZ(200), |
c9140419 | 91 | SR_HZ(0), |
59df0c77 | 92 | supported_samplerates, |
01cf8814 DR |
93 | }; |
94 | ||
95 | static GSList *device_instances = NULL; | |
96 | ||
a00ba012 | 97 | static int mso_send_control_message(struct sr_device_instance *sdi, |
01cf8814 DR |
98 | uint16_t payload[], int n) |
99 | { | |
100 | int fd = sdi->serial->fd; | |
101 | int i, w, ret, s = n * 2 + sizeof(mso_head) + sizeof(mso_foot); | |
102 | char *p, *buf; | |
103 | ||
ecad043f UH |
104 | ret = SR_ERR; |
105 | ||
01cf8814 DR |
106 | if (fd < 0) |
107 | goto ret; | |
108 | ||
ecad043f UH |
109 | if (!(buf = g_try_malloc(s))) { |
110 | sr_err("mso19: %s: buf malloc failed", __func__); | |
111 | ret = SR_ERR_MALLOC; | |
01cf8814 | 112 | goto ret; |
ecad043f | 113 | } |
01cf8814 DR |
114 | |
115 | p = buf; | |
116 | memcpy(p, mso_head, sizeof(mso_head)); | |
117 | p += sizeof(mso_head); | |
118 | ||
119 | for (i = 0; i < n; i++) { | |
120 | *(uint16_t *) p = htons(payload[i]); | |
121 | p += 2; | |
122 | } | |
123 | memcpy(p, mso_foot, sizeof(mso_foot)); | |
124 | ||
125 | w = 0; | |
126 | while (w < s) { | |
2119ab03 | 127 | ret = serial_write(fd, buf + w, s - w); |
01cf8814 | 128 | if (ret < 0) { |
e46b8fb1 | 129 | ret = SR_ERR; |
01cf8814 DR |
130 | goto free; |
131 | } | |
132 | w += ret; | |
133 | } | |
e46b8fb1 | 134 | ret = SR_OK; |
01cf8814 | 135 | free: |
ecad043f | 136 | g_free(buf); |
01cf8814 DR |
137 | ret: |
138 | return ret; | |
139 | } | |
140 | ||
a00ba012 | 141 | static int mso_reset_adc(struct sr_device_instance *sdi) |
01cf8814 DR |
142 | { |
143 | struct mso *mso = sdi->priv; | |
144 | uint16_t ops[2]; | |
145 | ||
a8467191 RC |
146 | ops[0] = mso_trans(REG_CTL1, (mso->ctlbase1 | BIT_CTL1_RESETADC)); |
147 | ops[1] = mso_trans(REG_CTL1, mso->ctlbase1); | |
148 | mso->ctlbase1 |= BIT_CTL1_ADC_UNKNOWN4; | |
01cf8814 DR |
149 | |
150 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
151 | } | |
152 | ||
a00ba012 | 153 | static int mso_reset_fsm(struct sr_device_instance *sdi) |
01cf8814 DR |
154 | { |
155 | struct mso *mso = sdi->priv; | |
156 | uint16_t ops[1]; | |
157 | ||
a8467191 RC |
158 | mso->ctlbase1 |= BIT_CTL1_RESETFSM; |
159 | ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); | |
01cf8814 DR |
160 | |
161 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
162 | } | |
163 | ||
a00ba012 | 164 | static int mso_toggle_led(struct sr_device_instance *sdi, int state) |
01cf8814 DR |
165 | { |
166 | struct mso *mso = sdi->priv; | |
167 | uint16_t ops[1]; | |
168 | ||
a8467191 | 169 | mso->ctlbase1 &= BIT_CTL1_LED; |
01cf8814 | 170 | if (state) |
a8467191 RC |
171 | mso->ctlbase1 |= BIT_CTL1_LED; |
172 | ops[0] = mso_trans(REG_CTL1, mso->ctlbase1); | |
01cf8814 DR |
173 | |
174 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
175 | } | |
176 | ||
a00ba012 | 177 | static int mso_check_trigger(struct sr_device_instance *sdi, |
01cf8814 DR |
178 | uint8_t *info) |
179 | { | |
180 | uint16_t ops[] = { mso_trans(REG_TRIGGER, 0) }; | |
181 | char buf[1]; | |
182 | int ret; | |
183 | ||
184 | ret = mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
e46b8fb1 | 185 | if (info == NULL || ret != SR_OK) |
01cf8814 DR |
186 | return ret; |
187 | ||
188 | buf[0] = 0; | |
2119ab03 | 189 | if (serial_read(sdi->serial->fd, buf, 1) != 1) /* FIXME: Need timeout */ |
e46b8fb1 | 190 | ret = SR_ERR; |
01cf8814 DR |
191 | *info = buf[0]; |
192 | ||
193 | return ret; | |
194 | } | |
195 | ||
a00ba012 | 196 | static int mso_read_buffer(struct sr_device_instance *sdi) |
01cf8814 DR |
197 | { |
198 | uint16_t ops[] = { mso_trans(REG_BUFFER, 0) }; | |
199 | ||
200 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
201 | } | |
202 | ||
a00ba012 | 203 | static int mso_arm(struct sr_device_instance *sdi) |
01cf8814 DR |
204 | { |
205 | struct mso *mso = sdi->priv; | |
206 | uint16_t ops[] = { | |
a8467191 RC |
207 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETFSM), |
208 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_ARM), | |
209 | mso_trans(REG_CTL1, mso->ctlbase1), | |
01cf8814 DR |
210 | }; |
211 | ||
212 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
213 | } | |
214 | ||
a00ba012 | 215 | static int mso_force_capture(struct sr_device_instance *sdi) |
01cf8814 DR |
216 | { |
217 | struct mso *mso = sdi->priv; | |
218 | uint16_t ops[] = { | |
a8467191 RC |
219 | mso_trans(REG_CTL1, mso->ctlbase1 | 8), |
220 | mso_trans(REG_CTL1, mso->ctlbase1), | |
01cf8814 DR |
221 | }; |
222 | ||
223 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
224 | } | |
225 | ||
a00ba012 | 226 | static int mso_dac_out(struct sr_device_instance *sdi, uint16_t val) |
01cf8814 DR |
227 | { |
228 | struct mso *mso = sdi->priv; | |
229 | uint16_t ops[] = { | |
230 | mso_trans(REG_DAC1, (val >> 8) & 0xff), | |
231 | mso_trans(REG_DAC2, val & 0xff), | |
a8467191 | 232 | mso_trans(REG_CTL1, mso->ctlbase1 | BIT_CTL1_RESETADC), |
01cf8814 DR |
233 | }; |
234 | ||
235 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
236 | } | |
237 | ||
a00ba012 | 238 | static int mso_clkrate_out(struct sr_device_instance *sdi, uint16_t val) |
01cf8814 DR |
239 | { |
240 | uint16_t ops[] = { | |
241 | mso_trans(REG_CLKRATE1, (val >> 8) & 0xff), | |
242 | mso_trans(REG_CLKRATE2, val & 0xff), | |
243 | }; | |
244 | ||
245 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
246 | } | |
247 | ||
a00ba012 | 248 | static int mso_configure_rate(struct sr_device_instance *sdi, |
01cf8814 DR |
249 | uint32_t rate) |
250 | { | |
251 | struct mso *mso = sdi->priv; | |
252 | unsigned int i; | |
e46b8fb1 | 253 | int ret = SR_ERR; |
01cf8814 DR |
254 | |
255 | for (i = 0; i < ARRAY_SIZE(rate_map); i++) { | |
256 | if (rate_map[i].rate == rate) { | |
a8467191 | 257 | mso->ctlbase2 = rate_map[i].slowmode; |
01cf8814 | 258 | ret = mso_clkrate_out(sdi, rate_map[i].val); |
e46b8fb1 | 259 | if (ret == SR_OK) |
01cf8814 DR |
260 | mso->cur_rate = rate; |
261 | return ret; | |
262 | } | |
263 | } | |
264 | return ret; | |
265 | } | |
266 | ||
01cf8814 DR |
267 | static inline uint16_t mso_calc_raw_from_mv(struct mso *mso) |
268 | { | |
269 | return (uint16_t) (0x200 - | |
270 | ((mso->dso_trigger_voltage / mso->dso_probe_attn) / | |
271 | mso->vbit)); | |
272 | } | |
273 | ||
a00ba012 | 274 | static int mso_configure_trigger(struct sr_device_instance *sdi) |
01cf8814 DR |
275 | { |
276 | struct mso *mso = sdi->priv; | |
277 | uint16_t ops[16]; | |
278 | uint16_t dso_trigger = mso_calc_raw_from_mv(mso); | |
279 | ||
280 | dso_trigger &= 0x3ff; | |
281 | if ((!mso->trigger_slope && mso->trigger_chan == 1) || | |
282 | (mso->trigger_slope && | |
283 | (mso->trigger_chan == 0 || | |
284 | mso->trigger_chan == 2 || | |
285 | mso->trigger_chan == 3))) | |
286 | dso_trigger |= 0x400; | |
287 | ||
288 | switch (mso->trigger_chan) { | |
289 | case 1: | |
290 | dso_trigger |= 0xe000; | |
291 | case 2: | |
292 | dso_trigger |= 0x4000; | |
293 | break; | |
294 | case 3: | |
295 | dso_trigger |= 0x2000; | |
296 | break; | |
297 | case 4: | |
298 | dso_trigger |= 0xa000; | |
299 | break; | |
300 | case 5: | |
301 | dso_trigger |= 0x8000; | |
302 | break; | |
303 | default: | |
304 | case 0: | |
305 | break; | |
306 | } | |
307 | ||
308 | switch (mso->trigger_outsrc) { | |
309 | case 1: | |
310 | dso_trigger |= 0x800; | |
311 | break; | |
312 | case 2: | |
313 | dso_trigger |= 0x1000; | |
314 | break; | |
315 | case 3: | |
316 | dso_trigger |= 0x1800; | |
317 | break; | |
318 | ||
319 | } | |
320 | ||
321 | ops[0] = mso_trans(5, mso->la_trigger); | |
322 | ops[1] = mso_trans(6, mso->la_trigger_mask); | |
323 | ops[2] = mso_trans(3, dso_trigger & 0xff); | |
324 | ops[3] = mso_trans(4, (dso_trigger >> 8) & 0xff); | |
325 | ops[4] = mso_trans(11, | |
59df0c77 | 326 | mso->dso_trigger_width / SR_HZ_TO_NS(mso->cur_rate)); |
01cf8814 | 327 | |
a2936073 RC |
328 | /* Select the SPI/I2C trigger config bank */ |
329 | ops[5] = mso_trans(REG_CTL2, (mso->ctlbase2 | BITS_CTL2_BANK(2))); | |
330 | /* Configure the SPI/I2C protocol trigger */ | |
331 | ops[6] = mso_trans(REG_PT_WORD(0), mso->protocol_trigger.word[0]); | |
332 | ops[7] = mso_trans(REG_PT_WORD(1), mso->protocol_trigger.word[1]); | |
333 | ops[8] = mso_trans(REG_PT_WORD(2), mso->protocol_trigger.word[2]); | |
334 | ops[9] = mso_trans(REG_PT_WORD(3), mso->protocol_trigger.word[3]); | |
335 | ops[10] = mso_trans(REG_PT_MASK(0), mso->protocol_trigger.mask[0]); | |
336 | ops[11] = mso_trans(REG_PT_MASK(1), mso->protocol_trigger.mask[1]); | |
337 | ops[12] = mso_trans(REG_PT_MASK(2), mso->protocol_trigger.mask[2]); | |
338 | ops[13] = mso_trans(REG_PT_MASK(3), mso->protocol_trigger.mask[3]); | |
339 | ops[14] = mso_trans(REG_PT_SPIMODE, mso->protocol_trigger.spimode); | |
340 | /* Select the default config bank */ | |
a8467191 | 341 | ops[15] = mso_trans(REG_CTL2, mso->ctlbase2); |
01cf8814 DR |
342 | |
343 | return mso_send_control_message(sdi, ARRAY_AND_SIZE(ops)); | |
344 | } | |
345 | ||
a00ba012 | 346 | static int mso_configure_threshold_level(struct sr_device_instance *sdi) |
01cf8814 DR |
347 | { |
348 | struct mso *mso = sdi->priv; | |
349 | ||
350 | return mso_dac_out(sdi, la_threshold_map[mso->la_threshold]); | |
351 | } | |
352 | ||
353 | static int mso_parse_serial(const char *iSerial, const char *iProduct, | |
354 | struct mso *mso) | |
355 | { | |
356 | unsigned int u1, u2, u3, u4, u5, u6; | |
357 | ||
358 | iProduct = iProduct; | |
359 | /* FIXME: This code is in the original app, but I think its | |
360 | * used only for the GUI */ | |
361 | /* if (strstr(iProduct, "REV_02") || strstr(iProduct, "REV_03")) | |
362 | mso->num_sample_rates = 0x16; | |
363 | else | |
364 | mso->num_sample_rates = 0x10; */ | |
365 | ||
366 | /* parse iSerial */ | |
367 | if (iSerial[0] != '4' || sscanf(iSerial, "%5u%3u%3u%1u%1u%6u", | |
368 | &u1, &u2, &u3, &u4, &u5, &u6) != 6) | |
e46b8fb1 | 369 | return SR_ERR; |
01cf8814 DR |
370 | mso->hwmodel = u4; |
371 | mso->hwrev = u5; | |
372 | mso->serial = u6; | |
373 | mso->vbit = u1 / 10000; | |
374 | if (mso->vbit == 0) | |
375 | mso->vbit = 4.19195; | |
376 | mso->dac_offset = u2; | |
377 | if (mso->dac_offset == 0) | |
378 | mso->dac_offset = 0x1ff; | |
379 | mso->offset_range = u3; | |
380 | if (mso->offset_range == 0) | |
381 | mso->offset_range = 0x17d; | |
382 | ||
383 | /* | |
384 | * FIXME: There is more code on the original software to handle | |
385 | * bigger iSerial strings, but as I can't test on my device | |
386 | * I will not implement it yet | |
387 | */ | |
388 | ||
e46b8fb1 | 389 | return SR_OK; |
01cf8814 DR |
390 | } |
391 | ||
54ac5277 | 392 | static int hw_init(const char *deviceinfo) |
01cf8814 | 393 | { |
a00ba012 | 394 | struct sr_device_instance *sdi; |
01cf8814 DR |
395 | int devcnt = 0; |
396 | struct udev *udev; | |
397 | struct udev_enumerate *enumerate; | |
398 | struct udev_list_entry *devices, *dev_list_entry; | |
399 | struct mso *mso; | |
400 | ||
401 | deviceinfo = deviceinfo; | |
402 | ||
403 | /* It's easier to map usb<->serial using udev */ | |
404 | /* | |
405 | * FIXME: On windows we can get the same information from the | |
406 | * registry, add an #ifdef here later | |
407 | */ | |
408 | udev = udev_new(); | |
409 | if (!udev) { | |
b08024a8 | 410 | sr_warn("Failed to initialize udev."); |
01cf8814 DR |
411 | goto ret; |
412 | } | |
413 | enumerate = udev_enumerate_new(udev); | |
414 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
415 | udev_enumerate_scan_devices(enumerate); | |
416 | devices = udev_enumerate_get_list_entry(enumerate); | |
417 | udev_list_entry_foreach(dev_list_entry, devices) { | |
418 | const char *syspath, *sysname, *idVendor, *idProduct, | |
419 | *iSerial, *iProduct; | |
420 | char path[32], manufacturer[32], product[32], hwrev[32]; | |
421 | struct udev_device *dev, *parent; | |
422 | size_t s; | |
423 | ||
424 | syspath = udev_list_entry_get_name(dev_list_entry); | |
425 | dev = udev_device_new_from_syspath(udev, syspath); | |
426 | sysname = udev_device_get_sysname(dev); | |
427 | parent = udev_device_get_parent_with_subsystem_devtype( | |
428 | dev, "usb", "usb_device"); | |
429 | if (!parent) { | |
b08024a8 UH |
430 | sr_warn("Unable to find parent usb device for %s", |
431 | sysname); | |
01cf8814 DR |
432 | continue; |
433 | } | |
434 | ||
435 | idVendor = udev_device_get_sysattr_value(parent, "idVendor"); | |
436 | idProduct = udev_device_get_sysattr_value(parent, "idProduct"); | |
437 | if (strcmp(USB_VENDOR, idVendor) | |
438 | || strcmp(USB_PRODUCT, idProduct)) | |
439 | continue; | |
440 | ||
441 | iSerial = udev_device_get_sysattr_value(parent, "serial"); | |
442 | iProduct = udev_device_get_sysattr_value(parent, "product"); | |
443 | ||
444 | snprintf(path, sizeof(path), "/dev/%s", sysname); | |
445 | ||
446 | s = strcspn(iProduct, " "); | |
447 | if (s > sizeof(product) || | |
448 | strlen(iProduct) - s > sizeof(manufacturer)) { | |
b08024a8 | 449 | sr_warn("Could not parse iProduct: %s", iProduct); |
01cf8814 DR |
450 | continue; |
451 | } | |
452 | strncpy(product, iProduct, s); | |
453 | product[s] = 0; | |
454 | strcpy(manufacturer, iProduct + s); | |
01cf8814 | 455 | |
ecad043f UH |
456 | if (!(mso = g_try_malloc0(sizeof(struct mso)))) { |
457 | sr_err("mso19: %s: mso malloc failed", __func__); | |
458 | continue; /* TODO: Errors handled correctly? */ | |
459 | } | |
01cf8814 | 460 | |
e46b8fb1 | 461 | if (mso_parse_serial(iSerial, iProduct, mso) != SR_OK) { |
b08024a8 | 462 | sr_warn("Invalid iSerial: %s", iSerial); |
01cf8814 DR |
463 | goto err_free_mso; |
464 | } | |
d88b9393 | 465 | sprintf(hwrev, "r%d", mso->hwrev); |
a2936073 | 466 | |
01cf8814 | 467 | /* hardware initial state */ |
a8467191 | 468 | mso->ctlbase1 = 0; |
a2936073 RC |
469 | { |
470 | /* Initialize the protocol trigger configuration */ | |
471 | int i; | |
472 | for (i = 0; i < 4; i++) | |
473 | { | |
474 | mso->protocol_trigger.word[i] = 0; | |
475 | mso->protocol_trigger.mask[i] = 0xff; | |
476 | } | |
477 | mso->protocol_trigger.spimode = 0; | |
478 | } | |
01cf8814 | 479 | |
5a2326a7 | 480 | sdi = sr_device_instance_new(devcnt, SR_ST_INITIALIZING, |
01cf8814 DR |
481 | manufacturer, product, hwrev); |
482 | if (!sdi) { | |
b08024a8 UH |
483 | sr_warn("Unable to create device instance for %s", |
484 | sysname); | |
01cf8814 DR |
485 | goto err_free_mso; |
486 | } | |
487 | ||
488 | /* save a pointer to our private instance data */ | |
489 | sdi->priv = mso; | |
490 | ||
6c290072 | 491 | sdi->serial = sr_serial_device_instance_new(path, -1); |
01cf8814 DR |
492 | if (!sdi->serial) |
493 | goto err_device_instance_free; | |
494 | ||
495 | device_instances = g_slist_append(device_instances, sdi); | |
496 | devcnt++; | |
497 | continue; | |
498 | ||
499 | err_device_instance_free: | |
a00ba012 | 500 | sr_device_instance_free(sdi); |
01cf8814 DR |
501 | err_free_mso: |
502 | free(mso); | |
503 | } | |
504 | ||
505 | udev_enumerate_unref(enumerate); | |
506 | udev_unref(udev); | |
507 | ||
508 | ret: | |
509 | return devcnt; | |
510 | } | |
511 | ||
512 | static void hw_cleanup(void) | |
513 | { | |
514 | GSList *l; | |
a00ba012 | 515 | struct sr_device_instance *sdi; |
01cf8814 DR |
516 | |
517 | /* Properly close all devices. */ | |
518 | for (l = device_instances; l; l = l->next) { | |
519 | sdi = l->data; | |
520 | if (sdi->serial->fd != -1) | |
521 | serial_close(sdi->serial->fd); | |
522 | if (sdi->priv != NULL) | |
523 | free(sdi->priv); | |
a00ba012 | 524 | sr_device_instance_free(sdi); |
01cf8814 DR |
525 | } |
526 | g_slist_free(device_instances); | |
527 | device_instances = NULL; | |
528 | } | |
529 | ||
530 | static int hw_opendev(int device_index) | |
531 | { | |
a00ba012 | 532 | struct sr_device_instance *sdi; |
01cf8814 | 533 | struct mso *mso; |
e46b8fb1 | 534 | int ret = SR_ERR; |
01cf8814 | 535 | |
d32d961d | 536 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
01cf8814 DR |
537 | return ret; |
538 | ||
539 | mso = sdi->priv; | |
540 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
541 | if (sdi->serial->fd == -1) | |
542 | return ret; | |
543 | ||
544 | ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2); | |
e46b8fb1 | 545 | if (ret != SR_OK) |
01cf8814 DR |
546 | return ret; |
547 | ||
5a2326a7 | 548 | sdi->status = SR_ST_ACTIVE; |
01cf8814 DR |
549 | |
550 | /* FIXME: discard serial buffer */ | |
551 | ||
552 | mso_check_trigger(sdi, &mso->trigger_state); | |
b08024a8 | 553 | // sr_warn("trigger state: %c", mso->trigger_state); |
01cf8814 DR |
554 | |
555 | ret = mso_reset_adc(sdi); | |
e46b8fb1 | 556 | if (ret != SR_OK) |
01cf8814 DR |
557 | return ret; |
558 | ||
559 | mso_check_trigger(sdi, &mso->trigger_state); | |
b08024a8 | 560 | // sr_warn("trigger state: %c", mso->trigger_state); |
01cf8814 DR |
561 | |
562 | // ret = mso_reset_fsm(sdi); | |
e46b8fb1 | 563 | // if (ret != SR_OK) |
01cf8814 DR |
564 | // return ret; |
565 | ||
e46b8fb1 UH |
566 | // return SR_ERR; |
567 | return SR_OK; | |
01cf8814 DR |
568 | } |
569 | ||
697785d1 | 570 | static int hw_closedev(int device_index) |
01cf8814 | 571 | { |
a00ba012 | 572 | struct sr_device_instance *sdi; |
01cf8814 | 573 | |
697785d1 UH |
574 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { |
575 | sr_err("mso19: %s: sdi was NULL", __func__); | |
576 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
577 | } | |
01cf8814 | 578 | |
697785d1 | 579 | /* TODO */ |
01cf8814 DR |
580 | if (sdi->serial->fd != -1) { |
581 | serial_close(sdi->serial->fd); | |
582 | sdi->serial->fd = -1; | |
5a2326a7 | 583 | sdi->status = SR_ST_INACTIVE; |
01cf8814 | 584 | } |
697785d1 UH |
585 | |
586 | return SR_OK; | |
01cf8814 DR |
587 | } |
588 | ||
589 | static void *hw_get_device_info(int device_index, int device_info_id) | |
590 | { | |
a00ba012 | 591 | struct sr_device_instance *sdi; |
01cf8814 DR |
592 | struct mso *mso; |
593 | void *info = NULL; | |
594 | ||
d32d961d | 595 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
01cf8814 DR |
596 | return NULL; |
597 | mso = sdi->priv; | |
598 | ||
599 | switch (device_info_id) { | |
5a2326a7 | 600 | case SR_DI_INSTANCE: |
01cf8814 DR |
601 | info = sdi; |
602 | break; | |
5a2326a7 | 603 | case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ |
464d12c7 KS |
604 | info = GINT_TO_POINTER(NUM_PROBES); |
605 | break; | |
606 | case SR_DI_PROBE_NAMES: | |
607 | info = probe_names; | |
01cf8814 | 608 | break; |
5a2326a7 | 609 | case SR_DI_SAMPLERATES: |
01cf8814 DR |
610 | info = &samplerates; |
611 | break; | |
5a2326a7 | 612 | case SR_DI_TRIGGER_TYPES: |
01cf8814 DR |
613 | info = "01"; /* FIXME */ |
614 | break; | |
5a2326a7 | 615 | case SR_DI_CUR_SAMPLERATE: |
01cf8814 DR |
616 | info = &mso->cur_rate; |
617 | break; | |
618 | } | |
619 | return info; | |
620 | } | |
621 | ||
622 | static int hw_get_status(int device_index) | |
623 | { | |
a00ba012 | 624 | struct sr_device_instance *sdi; |
01cf8814 | 625 | |
d32d961d | 626 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
5a2326a7 | 627 | return SR_ST_NOT_FOUND; |
01cf8814 DR |
628 | |
629 | return sdi->status; | |
630 | } | |
631 | ||
632 | static int *hw_get_capabilities(void) | |
633 | { | |
634 | return capabilities; | |
635 | } | |
636 | ||
637 | static int hw_set_configuration(int device_index, int capability, void *value) | |
638 | { | |
a00ba012 | 639 | struct sr_device_instance *sdi; |
01cf8814 | 640 | |
d32d961d | 641 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
e46b8fb1 | 642 | return SR_ERR; |
01cf8814 DR |
643 | |
644 | switch (capability) { | |
5a2326a7 | 645 | case SR_HWCAP_SAMPLERATE: |
01cf8814 | 646 | return mso_configure_rate(sdi, *(uint64_t *) value); |
5a2326a7 UH |
647 | case SR_HWCAP_PROBECONFIG: |
648 | case SR_HWCAP_LIMIT_SAMPLES: | |
01cf8814 | 649 | default: |
e46b8fb1 | 650 | return SR_OK; /* FIXME */ |
01cf8814 | 651 | } |
01cf8814 DR |
652 | } |
653 | ||
654 | #define MSO_TRIGGER_UNKNOWN '!' | |
655 | #define MSO_TRIGGER_UNKNOWN1 '1' | |
656 | #define MSO_TRIGGER_UNKNOWN2 '2' | |
657 | #define MSO_TRIGGER_UNKNOWN3 '3' | |
658 | #define MSO_TRIGGER_WAIT '4' | |
659 | #define MSO_TRIGGER_FIRED '5' | |
660 | #define MSO_TRIGGER_DATAREADY '6' | |
661 | ||
662 | /* FIXME: Pass errors? */ | |
663 | static int receive_data(int fd, int revents, void *user_data) | |
664 | { | |
a00ba012 | 665 | struct sr_device_instance *sdi = user_data; |
01cf8814 | 666 | struct mso *mso = sdi->priv; |
b9c735a2 | 667 | struct sr_datafeed_packet packet; |
e42ef08d | 668 | struct sr_datafeed_logic logic; |
01cf8814 DR |
669 | uint8_t in[1024], logic_out[1024]; |
670 | double analog_out[1024]; | |
671 | size_t i, s; | |
672 | ||
673 | revents = revents; | |
674 | ||
2119ab03 | 675 | s = serial_read(fd, in, sizeof(in)); |
01cf8814 DR |
676 | if (s <= 0) |
677 | return FALSE; | |
678 | ||
679 | /* No samples */ | |
680 | if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { | |
681 | mso->trigger_state = in[0]; | |
682 | if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { | |
683 | mso_read_buffer(sdi); | |
684 | mso->buffer_n = 0; | |
685 | } else { | |
686 | mso_check_trigger(sdi, NULL); | |
687 | } | |
688 | return FALSE; | |
689 | } | |
690 | ||
691 | /* the hardware always dumps 1024 samples, 24bits each */ | |
692 | if (mso->buffer_n < 3072) { | |
693 | memcpy(mso->buffer + mso->buffer_n, in, s); | |
694 | mso->buffer_n += s; | |
695 | } | |
696 | if (mso->buffer_n < 3072) | |
697 | return FALSE; | |
698 | ||
699 | /* do the conversion */ | |
700 | for (i = 0; i < 1024; i++) { | |
701 | /* FIXME: Need to do conversion to mV */ | |
702 | analog_out[i] = (mso->buffer[i * 3] & 0x3f) | | |
703 | ((mso->buffer[i * 3 + 1] & 0xf) << 6); | |
704 | logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | | |
705 | ((mso->buffer[i * 3 + 2] & 0x3f) << 2); | |
706 | } | |
707 | ||
5a2326a7 | 708 | packet.type = SR_DF_LOGIC; |
42eb54fb | 709 | packet.payload = &logic; |
e42ef08d RC |
710 | logic.length = 1024; |
711 | logic.unitsize = 1; | |
712 | logic.data = logic_out; | |
8a2efef2 | 713 | sr_session_bus(mso->session_id, &packet); |
01cf8814 | 714 | |
42eb54fb UH |
715 | // Dont bother fixing this yet, keep it "old style" |
716 | /* | |
5a2326a7 | 717 | packet.type = SR_DF_ANALOG; |
01cf8814 DR |
718 | packet.length = 1024; |
719 | packet.unitsize = sizeof(double); | |
720 | packet.payload = analog_out; | |
42eb54fb UH |
721 | sr_session_bus(mso->session_id, &packet); |
722 | */ | |
01cf8814 | 723 | |
5a2326a7 | 724 | packet.type = SR_DF_END; |
8a2efef2 | 725 | sr_session_bus(mso->session_id, &packet); |
01cf8814 DR |
726 | |
727 | return TRUE; | |
728 | } | |
729 | ||
730 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
731 | { | |
a00ba012 | 732 | struct sr_device_instance *sdi; |
01cf8814 | 733 | struct mso *mso; |
b9c735a2 UH |
734 | struct sr_datafeed_packet packet; |
735 | struct sr_datafeed_header header; | |
e46b8fb1 | 736 | int ret = SR_ERR; |
01cf8814 | 737 | |
d32d961d | 738 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
01cf8814 DR |
739 | return ret; |
740 | mso = sdi->priv; | |
741 | ||
742 | /* FIXME: No need to do full reconfigure every time */ | |
743 | // ret = mso_reset_fsm(sdi); | |
e46b8fb1 | 744 | // if (ret != SR_OK) |
01cf8814 DR |
745 | // return ret; |
746 | ||
747 | /* FIXME: ACDC Mode */ | |
a8467191 RC |
748 | mso->ctlbase1 &= 0x7f; |
749 | // mso->ctlbase1 |= mso->acdcmode; | |
01cf8814 DR |
750 | |
751 | ret = mso_configure_rate(sdi, mso->cur_rate); | |
e46b8fb1 | 752 | if (ret != SR_OK) |
01cf8814 DR |
753 | return ret; |
754 | ||
755 | /* set dac offset */ | |
756 | ret = mso_dac_out(sdi, mso->dac_offset); | |
e46b8fb1 | 757 | if (ret != SR_OK) |
01cf8814 DR |
758 | return ret; |
759 | ||
760 | ret = mso_configure_threshold_level(sdi); | |
e46b8fb1 | 761 | if (ret != SR_OK) |
01cf8814 DR |
762 | return ret; |
763 | ||
764 | ret = mso_configure_trigger(sdi); | |
e46b8fb1 | 765 | if (ret != SR_OK) |
01cf8814 DR |
766 | return ret; |
767 | ||
768 | /* FIXME: trigger_position */ | |
769 | ||
770 | ||
771 | /* END of config hardware part */ | |
772 | ||
773 | /* with trigger */ | |
774 | ret = mso_arm(sdi); | |
e46b8fb1 | 775 | if (ret != SR_OK) |
01cf8814 DR |
776 | return ret; |
777 | ||
778 | /* without trigger */ | |
779 | // ret = mso_force_capture(sdi); | |
e46b8fb1 | 780 | // if (ret != SR_OK) |
01cf8814 DR |
781 | // return ret; |
782 | ||
783 | mso_check_trigger(sdi, &mso->trigger_state); | |
784 | ret = mso_check_trigger(sdi, NULL); | |
e46b8fb1 | 785 | if (ret != SR_OK) |
01cf8814 DR |
786 | return ret; |
787 | ||
788 | mso->session_id = session_device_id; | |
6f1be0a2 | 789 | sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); |
01cf8814 | 790 | |
5a2326a7 | 791 | packet.type = SR_DF_HEADER; |
01cf8814 DR |
792 | packet.payload = (unsigned char *) &header; |
793 | header.feed_version = 1; | |
794 | gettimeofday(&header.starttime, NULL); | |
795 | header.samplerate = mso->cur_rate; | |
796 | header.num_analog_probes = 1; | |
797 | header.num_logic_probes = 8; | |
8a2efef2 | 798 | sr_session_bus(session_device_id, &packet); |
01cf8814 DR |
799 | |
800 | return ret; | |
801 | } | |
802 | ||
803 | /* FIXME */ | |
804 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
805 | { | |
b9c735a2 | 806 | struct sr_datafeed_packet packet; |
01cf8814 DR |
807 | |
808 | device_index = device_index; | |
809 | ||
5a2326a7 | 810 | packet.type = SR_DF_END; |
8a2efef2 | 811 | sr_session_bus(session_device_id, &packet); |
01cf8814 DR |
812 | } |
813 | ||
5c2d46d1 | 814 | struct sr_device_plugin link_mso19_plugin_info = { |
01cf8814 | 815 | .name = "link-mso19", |
9f8274a5 | 816 | .longname = "Link Instruments MSO-19", |
01cf8814 DR |
817 | .api_version = 1, |
818 | .init = hw_init, | |
819 | .cleanup = hw_cleanup, | |
86f5e3d8 UH |
820 | .opendev = hw_opendev, |
821 | .closedev = hw_closedev, | |
01cf8814 DR |
822 | .get_device_info = hw_get_device_info, |
823 | .get_status = hw_get_status, | |
824 | .get_capabilities = hw_get_capabilities, | |
825 | .set_configuration = hw_set_configuration, | |
826 | .start_acquisition = hw_start_acquisition, | |
827 | .stop_acquisition = hw_stop_acquisition, | |
828 | }; |