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