]>
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 | ||
f020a997 | 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) | |
e084a804 | 533 | { |
01cf8814 | 534 | free(sdi->priv); |
e084a804 RC |
535 | sdi->priv = NULL; |
536 | } | |
a00ba012 | 537 | sr_device_instance_free(sdi); |
01cf8814 DR |
538 | } |
539 | g_slist_free(device_instances); | |
540 | device_instances = NULL; | |
541 | } | |
542 | ||
543 | static int hw_opendev(int device_index) | |
544 | { | |
a00ba012 | 545 | struct sr_device_instance *sdi; |
01cf8814 | 546 | struct mso *mso; |
e46b8fb1 | 547 | int ret = SR_ERR; |
01cf8814 | 548 | |
d32d961d | 549 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
01cf8814 DR |
550 | return ret; |
551 | ||
552 | mso = sdi->priv; | |
553 | sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR); | |
554 | if (sdi->serial->fd == -1) | |
555 | return ret; | |
556 | ||
557 | ret = serial_set_params(sdi->serial->fd, 460800, 8, 0, 1, 2); | |
e46b8fb1 | 558 | if (ret != SR_OK) |
01cf8814 DR |
559 | return ret; |
560 | ||
5a2326a7 | 561 | sdi->status = SR_ST_ACTIVE; |
01cf8814 DR |
562 | |
563 | /* FIXME: discard serial buffer */ | |
564 | ||
565 | mso_check_trigger(sdi, &mso->trigger_state); | |
357285a9 | 566 | sr_dbg("trigger state: 0x%x", mso->trigger_state); |
01cf8814 DR |
567 | |
568 | ret = mso_reset_adc(sdi); | |
e46b8fb1 | 569 | if (ret != SR_OK) |
01cf8814 DR |
570 | return ret; |
571 | ||
572 | mso_check_trigger(sdi, &mso->trigger_state); | |
357285a9 | 573 | sr_dbg("trigger state: 0x%x", mso->trigger_state); |
01cf8814 DR |
574 | |
575 | // ret = mso_reset_fsm(sdi); | |
e46b8fb1 | 576 | // if (ret != SR_OK) |
01cf8814 DR |
577 | // return ret; |
578 | ||
357285a9 RC |
579 | sr_dbg("Finished %s", __func__); |
580 | ||
e46b8fb1 UH |
581 | // return SR_ERR; |
582 | return SR_OK; | |
01cf8814 DR |
583 | } |
584 | ||
697785d1 | 585 | static int hw_closedev(int device_index) |
01cf8814 | 586 | { |
a00ba012 | 587 | struct sr_device_instance *sdi; |
01cf8814 | 588 | |
697785d1 UH |
589 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) { |
590 | sr_err("mso19: %s: sdi was NULL", __func__); | |
591 | return SR_ERR; /* TODO: SR_ERR_ARG? */ | |
592 | } | |
01cf8814 | 593 | |
697785d1 | 594 | /* TODO */ |
01cf8814 DR |
595 | if (sdi->serial->fd != -1) { |
596 | serial_close(sdi->serial->fd); | |
597 | sdi->serial->fd = -1; | |
5a2326a7 | 598 | sdi->status = SR_ST_INACTIVE; |
01cf8814 | 599 | } |
697785d1 | 600 | |
357285a9 | 601 | sr_dbg("finished %s", __func__); |
697785d1 | 602 | return SR_OK; |
01cf8814 DR |
603 | } |
604 | ||
605 | static void *hw_get_device_info(int device_index, int device_info_id) | |
606 | { | |
a00ba012 | 607 | struct sr_device_instance *sdi; |
01cf8814 DR |
608 | struct mso *mso; |
609 | void *info = NULL; | |
610 | ||
d32d961d | 611 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
01cf8814 DR |
612 | return NULL; |
613 | mso = sdi->priv; | |
614 | ||
615 | switch (device_info_id) { | |
5a2326a7 | 616 | case SR_DI_INSTANCE: |
01cf8814 DR |
617 | info = sdi; |
618 | break; | |
5a2326a7 | 619 | case SR_DI_NUM_PROBES: /* FIXME: How to report analog probe? */ |
464d12c7 KS |
620 | info = GINT_TO_POINTER(NUM_PROBES); |
621 | break; | |
622 | case SR_DI_PROBE_NAMES: | |
623 | info = probe_names; | |
01cf8814 | 624 | break; |
5a2326a7 | 625 | case SR_DI_SAMPLERATES: |
01cf8814 DR |
626 | info = &samplerates; |
627 | break; | |
5a2326a7 | 628 | case SR_DI_TRIGGER_TYPES: |
01cf8814 DR |
629 | info = "01"; /* FIXME */ |
630 | break; | |
5a2326a7 | 631 | case SR_DI_CUR_SAMPLERATE: |
01cf8814 DR |
632 | info = &mso->cur_rate; |
633 | break; | |
634 | } | |
635 | return info; | |
636 | } | |
637 | ||
638 | static int hw_get_status(int device_index) | |
639 | { | |
a00ba012 | 640 | struct sr_device_instance *sdi; |
01cf8814 | 641 | |
d32d961d | 642 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
5a2326a7 | 643 | return SR_ST_NOT_FOUND; |
01cf8814 DR |
644 | |
645 | return sdi->status; | |
646 | } | |
647 | ||
648 | static int *hw_get_capabilities(void) | |
649 | { | |
650 | return capabilities; | |
651 | } | |
652 | ||
653 | static int hw_set_configuration(int device_index, int capability, void *value) | |
654 | { | |
a00ba012 | 655 | struct sr_device_instance *sdi; |
01cf8814 | 656 | |
d32d961d | 657 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
e46b8fb1 | 658 | return SR_ERR; |
01cf8814 DR |
659 | |
660 | switch (capability) { | |
5a2326a7 | 661 | case SR_HWCAP_SAMPLERATE: |
01cf8814 | 662 | return mso_configure_rate(sdi, *(uint64_t *) value); |
5a2326a7 UH |
663 | case SR_HWCAP_PROBECONFIG: |
664 | case SR_HWCAP_LIMIT_SAMPLES: | |
01cf8814 | 665 | default: |
e46b8fb1 | 666 | return SR_OK; /* FIXME */ |
01cf8814 | 667 | } |
01cf8814 DR |
668 | } |
669 | ||
670 | #define MSO_TRIGGER_UNKNOWN '!' | |
671 | #define MSO_TRIGGER_UNKNOWN1 '1' | |
672 | #define MSO_TRIGGER_UNKNOWN2 '2' | |
673 | #define MSO_TRIGGER_UNKNOWN3 '3' | |
674 | #define MSO_TRIGGER_WAIT '4' | |
675 | #define MSO_TRIGGER_FIRED '5' | |
676 | #define MSO_TRIGGER_DATAREADY '6' | |
677 | ||
678 | /* FIXME: Pass errors? */ | |
679 | static int receive_data(int fd, int revents, void *user_data) | |
680 | { | |
a00ba012 | 681 | struct sr_device_instance *sdi = user_data; |
01cf8814 | 682 | struct mso *mso = sdi->priv; |
b9c735a2 | 683 | struct sr_datafeed_packet packet; |
e42ef08d | 684 | struct sr_datafeed_logic logic; |
01cf8814 DR |
685 | uint8_t in[1024], logic_out[1024]; |
686 | double analog_out[1024]; | |
687 | size_t i, s; | |
688 | ||
689 | revents = revents; | |
690 | ||
2119ab03 | 691 | s = serial_read(fd, in, sizeof(in)); |
01cf8814 DR |
692 | if (s <= 0) |
693 | return FALSE; | |
694 | ||
695 | /* No samples */ | |
696 | if (mso->trigger_state != MSO_TRIGGER_DATAREADY) { | |
697 | mso->trigger_state = in[0]; | |
698 | if (mso->trigger_state == MSO_TRIGGER_DATAREADY) { | |
699 | mso_read_buffer(sdi); | |
700 | mso->buffer_n = 0; | |
701 | } else { | |
702 | mso_check_trigger(sdi, NULL); | |
703 | } | |
704 | return FALSE; | |
705 | } | |
706 | ||
707 | /* the hardware always dumps 1024 samples, 24bits each */ | |
708 | if (mso->buffer_n < 3072) { | |
709 | memcpy(mso->buffer + mso->buffer_n, in, s); | |
710 | mso->buffer_n += s; | |
711 | } | |
712 | if (mso->buffer_n < 3072) | |
713 | return FALSE; | |
714 | ||
715 | /* do the conversion */ | |
716 | for (i = 0; i < 1024; i++) { | |
717 | /* FIXME: Need to do conversion to mV */ | |
718 | analog_out[i] = (mso->buffer[i * 3] & 0x3f) | | |
719 | ((mso->buffer[i * 3 + 1] & 0xf) << 6); | |
720 | logic_out[i] = ((mso->buffer[i * 3 + 1] & 0x30) >> 4) | | |
721 | ((mso->buffer[i * 3 + 2] & 0x3f) << 2); | |
722 | } | |
723 | ||
5a2326a7 | 724 | packet.type = SR_DF_LOGIC; |
42eb54fb | 725 | packet.payload = &logic; |
e42ef08d RC |
726 | logic.length = 1024; |
727 | logic.unitsize = 1; | |
728 | logic.data = logic_out; | |
8a2efef2 | 729 | sr_session_bus(mso->session_id, &packet); |
01cf8814 | 730 | |
42eb54fb UH |
731 | // Dont bother fixing this yet, keep it "old style" |
732 | /* | |
5a2326a7 | 733 | packet.type = SR_DF_ANALOG; |
01cf8814 DR |
734 | packet.length = 1024; |
735 | packet.unitsize = sizeof(double); | |
736 | packet.payload = analog_out; | |
42eb54fb UH |
737 | sr_session_bus(mso->session_id, &packet); |
738 | */ | |
01cf8814 | 739 | |
5a2326a7 | 740 | packet.type = SR_DF_END; |
8a2efef2 | 741 | sr_session_bus(mso->session_id, &packet); |
01cf8814 DR |
742 | |
743 | return TRUE; | |
744 | } | |
745 | ||
746 | static int hw_start_acquisition(int device_index, gpointer session_device_id) | |
747 | { | |
a00ba012 | 748 | struct sr_device_instance *sdi; |
01cf8814 | 749 | struct mso *mso; |
b9c735a2 UH |
750 | struct sr_datafeed_packet packet; |
751 | struct sr_datafeed_header header; | |
e46b8fb1 | 752 | int ret = SR_ERR; |
01cf8814 | 753 | |
d32d961d | 754 | if (!(sdi = sr_get_device_instance(device_instances, device_index))) |
01cf8814 DR |
755 | return ret; |
756 | mso = sdi->priv; | |
757 | ||
758 | /* FIXME: No need to do full reconfigure every time */ | |
759 | // ret = mso_reset_fsm(sdi); | |
e46b8fb1 | 760 | // if (ret != SR_OK) |
01cf8814 DR |
761 | // return ret; |
762 | ||
763 | /* FIXME: ACDC Mode */ | |
a8467191 RC |
764 | mso->ctlbase1 &= 0x7f; |
765 | // mso->ctlbase1 |= mso->acdcmode; | |
01cf8814 DR |
766 | |
767 | ret = mso_configure_rate(sdi, mso->cur_rate); | |
e46b8fb1 | 768 | if (ret != SR_OK) |
01cf8814 DR |
769 | return ret; |
770 | ||
771 | /* set dac offset */ | |
772 | ret = mso_dac_out(sdi, mso->dac_offset); | |
e46b8fb1 | 773 | if (ret != SR_OK) |
01cf8814 DR |
774 | return ret; |
775 | ||
776 | ret = mso_configure_threshold_level(sdi); | |
e46b8fb1 | 777 | if (ret != SR_OK) |
01cf8814 DR |
778 | return ret; |
779 | ||
780 | ret = mso_configure_trigger(sdi); | |
e46b8fb1 | 781 | if (ret != SR_OK) |
01cf8814 DR |
782 | return ret; |
783 | ||
784 | /* FIXME: trigger_position */ | |
785 | ||
786 | ||
787 | /* END of config hardware part */ | |
788 | ||
789 | /* with trigger */ | |
790 | ret = mso_arm(sdi); | |
e46b8fb1 | 791 | if (ret != SR_OK) |
01cf8814 DR |
792 | return ret; |
793 | ||
794 | /* without trigger */ | |
795 | // ret = mso_force_capture(sdi); | |
e46b8fb1 | 796 | // if (ret != SR_OK) |
01cf8814 DR |
797 | // return ret; |
798 | ||
799 | mso_check_trigger(sdi, &mso->trigger_state); | |
800 | ret = mso_check_trigger(sdi, NULL); | |
e46b8fb1 | 801 | if (ret != SR_OK) |
01cf8814 DR |
802 | return ret; |
803 | ||
804 | mso->session_id = session_device_id; | |
6f1be0a2 | 805 | sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, sdi); |
01cf8814 | 806 | |
5a2326a7 | 807 | packet.type = SR_DF_HEADER; |
01cf8814 DR |
808 | packet.payload = (unsigned char *) &header; |
809 | header.feed_version = 1; | |
810 | gettimeofday(&header.starttime, NULL); | |
811 | header.samplerate = mso->cur_rate; | |
812 | header.num_analog_probes = 1; | |
813 | header.num_logic_probes = 8; | |
8a2efef2 | 814 | sr_session_bus(session_device_id, &packet); |
01cf8814 DR |
815 | |
816 | return ret; | |
817 | } | |
818 | ||
819 | /* FIXME */ | |
820 | static void hw_stop_acquisition(int device_index, gpointer session_device_id) | |
821 | { | |
b9c735a2 | 822 | struct sr_datafeed_packet packet; |
01cf8814 DR |
823 | |
824 | device_index = device_index; | |
825 | ||
5a2326a7 | 826 | packet.type = SR_DF_END; |
8a2efef2 | 827 | sr_session_bus(session_device_id, &packet); |
01cf8814 DR |
828 | } |
829 | ||
5c2d46d1 | 830 | struct sr_device_plugin link_mso19_plugin_info = { |
01cf8814 | 831 | .name = "link-mso19", |
9f8274a5 | 832 | .longname = "Link Instruments MSO-19", |
01cf8814 DR |
833 | .api_version = 1, |
834 | .init = hw_init, | |
835 | .cleanup = hw_cleanup, | |
86f5e3d8 UH |
836 | .opendev = hw_opendev, |
837 | .closedev = hw_closedev, | |
01cf8814 DR |
838 | .get_device_info = hw_get_device_info, |
839 | .get_status = hw_get_status, | |
840 | .get_capabilities = hw_get_capabilities, | |
841 | .set_configuration = hw_set_configuration, | |
842 | .start_acquisition = hw_start_acquisition, | |
843 | .stop_acquisition = hw_stop_acquisition, | |
844 | }; |