]>
Commit | Line | Data |
---|---|---|
b908f067 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
b908f067 | 3 | * |
67f890d5 | 4 | * Copyright (C) 2011-2015 Uwe Hermann <uwe@hermann-uwe.de> |
b908f067 UH |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
45e080b6 | 22 | #include "protocol.h" |
b908f067 | 23 | |
7b356712 UH |
24 | SR_PRIV struct sr_dev_driver chronovu_la_driver_info; |
25 | static struct sr_dev_driver *di = &chronovu_la_driver_info; | |
b908f067 | 26 | |
67f890d5 | 27 | static const uint32_t drvopts[] = { |
1bec72d2 | 28 | SR_CONF_LOGIC_ANALYZER, |
67f890d5 UH |
29 | }; |
30 | ||
31 | static const uint32_t scanopts[] = { | |
32 | SR_CONF_CONN, | |
33 | }; | |
34 | ||
35 | static const uint32_t devopts[] = { | |
5827f61b BV |
36 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, |
37 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET | SR_CONF_LIST, | |
67f890d5 | 38 | SR_CONF_CONN | SR_CONF_GET, |
5827f61b BV |
39 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
40 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
1bec72d2 BV |
41 | }; |
42 | ||
aeff7fa2 BV |
43 | static const int32_t trigger_matches[] = { |
44 | SR_TRIGGER_ZERO, | |
45 | SR_TRIGGER_ONE, | |
46 | SR_TRIGGER_RISING, | |
47 | SR_TRIGGER_FALLING, | |
48 | }; | |
49 | ||
6078d2c9 | 50 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); |
b908f067 | 51 | |
97900799 | 52 | static void clear_helper(void *priv) |
c4f3ed4b | 53 | { |
1644fb24 | 54 | struct dev_context *devc; |
b172c130 | 55 | |
97900799 | 56 | devc = priv; |
1644fb24 | 57 | |
97900799 UH |
58 | ftdi_free(devc->ftdic); |
59 | g_free(devc->final_buf); | |
60 | } | |
c4f3ed4b | 61 | |
4f840ce9 | 62 | static int dev_clear(const struct sr_dev_driver *di) |
97900799 UH |
63 | { |
64 | return std_dev_clear(di, clear_helper); | |
c4f3ed4b BV |
65 | } |
66 | ||
4f840ce9 | 67 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
61136ea6 | 68 | { |
f6beaac5 | 69 | return std_init(sr_ctx, di, LOG_PREFIX); |
61136ea6 BV |
70 | } |
71 | ||
67f890d5 UH |
72 | static int add_device(int model, struct libusb_device_descriptor *des, |
73 | const char *serial_num, const char *connection_id, | |
74 | libusb_device *usbdev, GSList **devices) | |
b908f067 | 75 | { |
00910580 UH |
76 | int ret; |
77 | unsigned int i; | |
b908f067 | 78 | struct sr_dev_inst *sdi; |
1644fb24 BV |
79 | struct drv_context *drvc; |
80 | struct dev_context *devc; | |
c4f3ed4b | 81 | |
00910580 | 82 | ret = SR_OK; |
f3a35908 | 83 | |
41812aca | 84 | drvc = di->context; |
4b97c74e | 85 | |
1644fb24 | 86 | /* Allocate memory for our private device context. */ |
f57d8ffe | 87 | devc = g_malloc0(sizeof(struct dev_context)); |
b908f067 UH |
88 | |
89 | /* Set some sane defaults. */ | |
00910580 UH |
90 | devc->prof = &cv_profiles[model]; |
91 | devc->ftdic = NULL; /* Will be set in the open() API call. */ | |
92 | devc->cur_samplerate = 0; /* Set later (different for LA8/LA16). */ | |
1644fb24 BV |
93 | devc->limit_msec = 0; |
94 | devc->limit_samples = 0; | |
3e9b7f9c | 95 | devc->cb_data = NULL; |
1644fb24 BV |
96 | memset(devc->mangled_buf, 0, BS); |
97 | devc->final_buf = NULL; | |
00910580 UH |
98 | devc->trigger_pattern = 0x0000; /* Irrelevant, see trigger_mask. */ |
99 | devc->trigger_mask = 0x0000; /* All channels: "don't care". */ | |
100 | devc->trigger_edgemask = 0x0000; /* All channels: "state triggered". */ | |
1644fb24 BV |
101 | devc->trigger_found = 0; |
102 | devc->done = 0; | |
103 | devc->block_counter = 0; | |
00910580 | 104 | devc->divcount = 0; |
67f890d5 UH |
105 | devc->usb_vid = des->idVendor; |
106 | devc->usb_pid = des->idProduct; | |
00910580 | 107 | memset(devc->samplerates, 0, sizeof(uint64_t) * 255); |
b908f067 UH |
108 | |
109 | /* Allocate memory where we'll store the de-mangled data. */ | |
1644fb24 | 110 | if (!(devc->final_buf = g_try_malloc(SDRAM_SIZE))) { |
b172c130 | 111 | sr_err("Failed to allocate memory for sample buffer."); |
00910580 | 112 | ret = SR_ERR_MALLOC; |
1644fb24 | 113 | goto err_free_devc; |
b908f067 UH |
114 | } |
115 | ||
00910580 UH |
116 | /* We now know the device, set its max. samplerate as default. */ |
117 | devc->cur_samplerate = devc->prof->max_samplerate; | |
b908f067 UH |
118 | |
119 | /* Register the device with libsigrok. */ | |
aac29cc1 | 120 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
121 | sdi->status = SR_ST_INITIALIZING; |
122 | sdi->vendor = g_strdup("ChronoVu"); | |
123 | sdi->model = g_strdup(devc->prof->modelname); | |
67f890d5 UH |
124 | sdi->serial_num = g_strdup(serial_num); |
125 | sdi->connection_id = g_strdup(connection_id); | |
126 | sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(usbdev), | |
127 | libusb_get_device_address(usbdev), NULL); | |
f3a35908 | 128 | sdi->driver = di; |
1644fb24 | 129 | sdi->priv = devc; |
b908f067 | 130 | |
5e23fcab ML |
131 | for (i = 0; i < devc->prof->num_channels; i++) |
132 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, | |
0f34cb47 | 133 | cv_channel_names[i]); |
87ca93c5 | 134 | |
00910580 | 135 | *devices = g_slist_append(*devices, sdi); |
1644fb24 | 136 | drvc->instances = g_slist_append(drvc->instances, sdi); |
b908f067 | 137 | |
65c8d48f BV |
138 | if (ret == SR_OK) |
139 | return SR_OK; | |
b908f067 | 140 | |
1644fb24 BV |
141 | err_free_devc: |
142 | g_free(devc); | |
b908f067 | 143 | |
00910580 UH |
144 | return ret; |
145 | } | |
146 | ||
4f840ce9 | 147 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
00910580 | 148 | { |
67f890d5 UH |
149 | int i, ret, model; |
150 | struct drv_context *drvc; | |
151 | GSList *devices, *conn_devices, *l; | |
152 | struct sr_usb_dev_inst *usb; | |
153 | struct sr_config *src; | |
154 | struct libusb_device_descriptor des; | |
155 | libusb_device **devlist; | |
156 | struct libusb_device_handle *hdl; | |
157 | const char *conn; | |
158 | char product[64], serial_num[64], connection_id[64]; | |
00910580 | 159 | |
67f890d5 UH |
160 | drvc = di->context; |
161 | drvc->instances = NULL; | |
162 | ||
163 | conn = NULL; | |
164 | for (l = options; l; l = l->next) { | |
165 | src = l->data; | |
166 | switch (src->key) { | |
167 | case SR_CONF_CONN: | |
168 | conn = g_variant_get_string(src->data, NULL); | |
169 | break; | |
170 | } | |
00910580 | 171 | } |
67f890d5 UH |
172 | if (conn) |
173 | conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn); | |
174 | else | |
175 | conn_devices = NULL; | |
00910580 | 176 | |
67f890d5 UH |
177 | devices = NULL; |
178 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
179 | ||
180 | for (i = 0; devlist[i]; i++) { | |
181 | if (conn) { | |
182 | for (l = conn_devices; l; l = l->next) { | |
183 | usb = l->data; | |
184 | if (usb->bus == libusb_get_bus_number(devlist[i]) | |
185 | && usb->address == libusb_get_device_address(devlist[i])) | |
186 | break; | |
187 | } | |
188 | if (!l) | |
189 | /* This device matched none of the ones that | |
190 | * matched the conn specification. */ | |
191 | continue; | |
192 | } | |
193 | ||
194 | libusb_get_device_descriptor(devlist[i], &des); | |
195 | ||
196 | if ((ret = libusb_open(devlist[i], &hdl)) < 0) | |
197 | continue; | |
198 | ||
199 | if (des.iProduct == 0) { | |
200 | product[0] = '\0'; | |
201 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
202 | des.iProduct, (unsigned char *)product, | |
203 | sizeof(product))) < 0) { | |
204 | sr_warn("Failed to get product string descriptor: %s.", | |
205 | libusb_error_name(ret)); | |
206 | continue; | |
207 | } | |
208 | ||
209 | if (des.iSerialNumber == 0) { | |
210 | serial_num[0] = '\0'; | |
211 | } else if ((ret = libusb_get_string_descriptor_ascii(hdl, | |
212 | des.iSerialNumber, (unsigned char *)serial_num, | |
213 | sizeof(serial_num))) < 0) { | |
214 | sr_warn("Failed to get serial number string descriptor: %s.", | |
215 | libusb_error_name(ret)); | |
216 | continue; | |
217 | } | |
218 | ||
219 | usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)); | |
220 | ||
221 | libusb_close(hdl); | |
222 | ||
223 | if (!strcmp(product, "ChronoVu LA8")) { | |
224 | model = 0; | |
225 | } else if (!strcmp(product, "ChronoVu LA16")) { | |
226 | model = 1; | |
227 | } else { | |
228 | sr_spew("Unknown iProduct string '%s'.", product); | |
229 | continue; | |
230 | } | |
231 | ||
232 | sr_dbg("Found %s (%04x:%04x, %d.%d, %s).", | |
233 | product, des.idVendor, des.idProduct, | |
234 | libusb_get_bus_number(devlist[i]), | |
235 | libusb_get_device_address(devlist[i]), connection_id); | |
236 | ||
237 | if ((ret = add_device(model, &des, serial_num, connection_id, | |
238 | devlist[i], &devices)) < 0) { | |
00910580 | 239 | sr_dbg("Failed to add device: %d.", ret); |
67f890d5 | 240 | } |
00910580 UH |
241 | } |
242 | ||
67f890d5 UH |
243 | libusb_free_device_list(devlist, 1); |
244 | g_slist_free_full(conn_devices, (GDestroyNotify)sr_usb_dev_inst_free); | |
00910580 UH |
245 | |
246 | return devices; | |
b908f067 UH |
247 | } |
248 | ||
4f840ce9 | 249 | static GSList *dev_list(const struct sr_dev_driver *di) |
811deee4 | 250 | { |
41812aca | 251 | return ((struct drv_context *)(di->context))->instances; |
811deee4 BV |
252 | } |
253 | ||
6078d2c9 | 254 | static int dev_open(struct sr_dev_inst *sdi) |
b908f067 | 255 | { |
1644fb24 | 256 | struct dev_context *devc; |
25a0f108 | 257 | int ret; |
b908f067 | 258 | |
b172c130 | 259 | if (!(devc = sdi->priv)) |
b908f067 | 260 | return SR_ERR_BUG; |
b908f067 | 261 | |
00910580 UH |
262 | /* Allocate memory for the FTDI context and initialize it. */ |
263 | if (!(devc->ftdic = ftdi_new())) { | |
264 | sr_err("Failed to initialize libftdi."); | |
265 | return SR_ERR; | |
266 | } | |
267 | ||
268 | sr_dbg("Opening %s device (%04x:%04x).", devc->prof->modelname, | |
269 | devc->usb_vid, devc->usb_pid); | |
b908f067 UH |
270 | |
271 | /* Open the device. */ | |
00910580 UH |
272 | if ((ret = ftdi_usb_open_desc(devc->ftdic, devc->usb_vid, |
273 | devc->usb_pid, devc->prof->iproduct, NULL)) < 0) { | |
b172c130 UH |
274 | sr_err("Failed to open FTDI device (%d): %s.", |
275 | ret, ftdi_get_error_string(devc->ftdic)); | |
00910580 | 276 | goto err_ftdi_free; |
b908f067 | 277 | } |
f3a35908 | 278 | sr_dbg("Device opened successfully."); |
b908f067 UH |
279 | |
280 | /* Purge RX/TX buffers in the FTDI chip. */ | |
1644fb24 | 281 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { |
b172c130 UH |
282 | sr_err("Failed to purge FTDI buffers (%d): %s.", |
283 | ret, ftdi_get_error_string(devc->ftdic)); | |
00910580 | 284 | goto err_ftdi_free; |
b908f067 | 285 | } |
f3a35908 | 286 | sr_dbg("FTDI buffers purged successfully."); |
b908f067 UH |
287 | |
288 | /* Enable flow control in the FTDI chip. */ | |
1644fb24 | 289 | if ((ret = ftdi_setflowctrl(devc->ftdic, SIO_RTS_CTS_HS)) < 0) { |
b172c130 UH |
290 | sr_err("Failed to enable FTDI flow control (%d): %s.", |
291 | ret, ftdi_get_error_string(devc->ftdic)); | |
00910580 | 292 | goto err_ftdi_free; |
b908f067 | 293 | } |
f3a35908 | 294 | sr_dbg("FTDI flow control enabled successfully."); |
b908f067 UH |
295 | |
296 | /* Wait 100ms. */ | |
297 | g_usleep(100 * 1000); | |
298 | ||
299 | sdi->status = SR_ST_ACTIVE; | |
300 | ||
65c8d48f BV |
301 | if (ret == SR_OK) |
302 | return SR_OK; | |
b908f067 | 303 | |
00910580 UH |
304 | err_ftdi_free: |
305 | ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */ | |
306 | devc->ftdic = NULL; | |
307 | return ret; | |
b908f067 UH |
308 | } |
309 | ||
6078d2c9 | 310 | static int dev_close(struct sr_dev_inst *sdi) |
b908f067 | 311 | { |
00910580 | 312 | int ret; |
1644fb24 | 313 | struct dev_context *devc; |
b908f067 | 314 | |
00910580 UH |
315 | if (sdi->status != SR_ST_ACTIVE) |
316 | return SR_OK; | |
b908f067 | 317 | |
00910580 | 318 | devc = sdi->priv; |
b908f067 | 319 | |
00910580 UH |
320 | if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0) |
321 | sr_err("Failed to close FTDI device (%d): %s.", | |
322 | ret, ftdi_get_error_string(devc->ftdic)); | |
b908f067 UH |
323 | sdi->status = SR_ST_INACTIVE; |
324 | ||
b908f067 UH |
325 | return SR_OK; |
326 | } | |
327 | ||
4f840ce9 | 328 | static int cleanup(const struct sr_dev_driver *di) |
b908f067 | 329 | { |
4f840ce9 | 330 | return dev_clear(di); |
b908f067 UH |
331 | } |
332 | ||
584560f1 | 333 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 334 | const struct sr_channel_group *cg) |
b908f067 | 335 | { |
1644fb24 | 336 | struct dev_context *devc; |
67f890d5 UH |
337 | struct sr_usb_dev_inst *usb; |
338 | char str[128]; | |
b908f067 | 339 | |
53b4680f | 340 | (void)cg; |
8f996b89 | 341 | |
584560f1 | 342 | switch (key) { |
67f890d5 UH |
343 | case SR_CONF_CONN: |
344 | if (!sdi || !(usb = sdi->conn)) | |
345 | return SR_ERR_ARG; | |
346 | snprintf(str, 128, "%d.%d", usb->bus, usb->address); | |
347 | *data = g_variant_new_string(str); | |
348 | break; | |
123e1313 | 349 | case SR_CONF_SAMPLERATE: |
b172c130 UH |
350 | if (!sdi || !(devc = sdi->priv)) |
351 | return SR_ERR_BUG; | |
352 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
b908f067 | 353 | break; |
cfe8a84d | 354 | default: |
bd6fbf62 | 355 | return SR_ERR_NA; |
b908f067 UH |
356 | } |
357 | ||
6a2761fd | 358 | return SR_OK; |
b908f067 UH |
359 | } |
360 | ||
584560f1 | 361 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 362 | const struct sr_channel_group *cg) |
b908f067 | 363 | { |
1644fb24 | 364 | struct dev_context *devc; |
b908f067 | 365 | |
53b4680f | 366 | (void)cg; |
8f996b89 | 367 | |
e73ffd42 BV |
368 | if (sdi->status != SR_ST_ACTIVE) |
369 | return SR_ERR_DEV_CLOSED; | |
370 | ||
b172c130 | 371 | if (!(devc = sdi->priv)) |
b908f067 | 372 | return SR_ERR_BUG; |
b908f067 | 373 | |
584560f1 | 374 | switch (key) { |
1953564a | 375 | case SR_CONF_SAMPLERATE: |
00910580 | 376 | if (cv_set_samplerate(sdi, g_variant_get_uint64(data)) < 0) |
b908f067 | 377 | return SR_ERR; |
b908f067 | 378 | break; |
1953564a | 379 | case SR_CONF_LIMIT_MSEC: |
b172c130 UH |
380 | if (g_variant_get_uint64(data) == 0) |
381 | return SR_ERR_ARG; | |
1bec72d2 | 382 | devc->limit_msec = g_variant_get_uint64(data); |
b908f067 | 383 | break; |
1953564a | 384 | case SR_CONF_LIMIT_SAMPLES: |
b172c130 UH |
385 | if (g_variant_get_uint64(data) == 0) |
386 | return SR_ERR_ARG; | |
1bec72d2 | 387 | devc->limit_samples = g_variant_get_uint64(data); |
b908f067 UH |
388 | break; |
389 | default: | |
bd6fbf62 | 390 | return SR_ERR_NA; |
b908f067 UH |
391 | } |
392 | ||
393 | return SR_OK; | |
394 | } | |
395 | ||
584560f1 | 396 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 397 | const struct sr_channel_group *cg) |
a1c743fc | 398 | { |
f0de2dd0 | 399 | GVariant *gvar, *grange[2]; |
1bec72d2 | 400 | GVariantBuilder gvb; |
00910580 | 401 | struct dev_context *devc; |
a1c743fc | 402 | |
53b4680f | 403 | (void)cg; |
a1c743fc BV |
404 | |
405 | switch (key) { | |
67f890d5 | 406 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 407 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
67f890d5 UH |
408 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
409 | break; | |
410 | case SR_CONF_DEVICE_OPTIONS: | |
411 | if (!sdi) | |
412 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
413 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
414 | else | |
415 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
416 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
9a6517d1 | 417 | break; |
a1c743fc | 418 | case SR_CONF_SAMPLERATE: |
00910580 UH |
419 | if (!sdi || !sdi->priv || !(devc = sdi->priv)) |
420 | return SR_ERR_BUG; | |
421 | cv_fill_samplerates_if_needed(sdi); | |
1bec72d2 BV |
422 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); |
423 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), | |
00910580 UH |
424 | devc->samplerates, |
425 | ARRAY_SIZE(devc->samplerates), | |
1bec72d2 BV |
426 | sizeof(uint64_t)); |
427 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
428 | *data = g_variant_builder_end(&gvb); | |
a1c743fc | 429 | break; |
f0de2dd0 | 430 | case SR_CONF_LIMIT_SAMPLES: |
69bdcd8b UH |
431 | if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof) |
432 | return SR_ERR_BUG; | |
f0de2dd0 | 433 | grange[0] = g_variant_new_uint64(0); |
69bdcd8b UH |
434 | if (devc->prof->model == CHRONOVU_LA8) |
435 | grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES); | |
436 | else | |
437 | grange[1] = g_variant_new_uint64(MAX_NUM_SAMPLES / 2); | |
f0de2dd0 BV |
438 | *data = g_variant_new_tuple(grange, 2); |
439 | break; | |
aeff7fa2 | 440 | case SR_CONF_TRIGGER_MATCH: |
00910580 | 441 | if (!sdi || !sdi->priv || !(devc = sdi->priv) || !devc->prof) |
13dd25bd | 442 | return SR_ERR_BUG; |
af945a66 | 443 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
aeff7fa2 BV |
444 | trigger_matches, devc->prof->num_trigger_matches, |
445 | sizeof(int32_t)); | |
c50277a6 | 446 | break; |
a1c743fc | 447 | default: |
bd6fbf62 | 448 | return SR_ERR_NA; |
a1c743fc BV |
449 | } |
450 | ||
451 | return SR_OK; | |
452 | } | |
453 | ||
b908f067 UH |
454 | static int receive_data(int fd, int revents, void *cb_data) |
455 | { | |
456 | int i, ret; | |
457 | struct sr_dev_inst *sdi; | |
1644fb24 | 458 | struct dev_context *devc; |
b908f067 | 459 | |
b908f067 UH |
460 | (void)fd; |
461 | (void)revents; | |
462 | ||
463 | if (!(sdi = cb_data)) { | |
b172c130 | 464 | sr_err("cb_data was NULL."); |
b908f067 UH |
465 | return FALSE; |
466 | } | |
467 | ||
1644fb24 | 468 | if (!(devc = sdi->priv)) { |
b172c130 | 469 | sr_err("sdi->priv was NULL."); |
b908f067 UH |
470 | return FALSE; |
471 | } | |
472 | ||
1644fb24 | 473 | if (!devc->ftdic) { |
b172c130 | 474 | sr_err("devc->ftdic was NULL."); |
b908f067 UH |
475 | return FALSE; |
476 | } | |
477 | ||
478 | /* Get one block of data. */ | |
b172c130 UH |
479 | if ((ret = cv_read_block(devc)) < 0) { |
480 | sr_err("Failed to read data block: %d.", ret); | |
6078d2c9 | 481 | dev_acquisition_stop(sdi, sdi); |
b908f067 UH |
482 | return FALSE; |
483 | } | |
484 | ||
485 | /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ | |
1644fb24 BV |
486 | if (devc->block_counter != (NUM_BLOCKS - 1)) { |
487 | devc->block_counter++; | |
b908f067 UH |
488 | return TRUE; |
489 | } | |
490 | ||
f3a35908 | 491 | sr_dbg("Sampling finished, sending data to session bus now."); |
b908f067 | 492 | |
b0efc84e UH |
493 | /* |
494 | * All data was received and demangled, send it to the session bus. | |
495 | * | |
496 | * Note: Due to the method how data is spread across the 8MByte of | |
497 | * SDRAM, we can _not_ send it to the session bus in a streaming | |
498 | * manner while we receive it. We have to receive and de-mangle the | |
499 | * full 8MByte first, only then the whole buffer contains valid data. | |
500 | */ | |
b908f067 | 501 | for (i = 0; i < NUM_BLOCKS; i++) |
b172c130 | 502 | cv_send_block_to_session_bus(devc, i); |
b908f067 | 503 | |
6078d2c9 | 504 | dev_acquisition_stop(sdi, sdi); |
b908f067 | 505 | |
b908f067 UH |
506 | return TRUE; |
507 | } | |
508 | ||
6078d2c9 | 509 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
b908f067 | 510 | { |
1644fb24 | 511 | struct dev_context *devc; |
00910580 UH |
512 | uint8_t buf[8]; |
513 | int bytes_to_write, bytes_written; | |
b908f067 | 514 | |
e73ffd42 BV |
515 | if (sdi->status != SR_ST_ACTIVE) |
516 | return SR_ERR_DEV_CLOSED; | |
517 | ||
1644fb24 | 518 | if (!(devc = sdi->priv)) { |
b172c130 | 519 | sr_err("sdi->priv was NULL."); |
b908f067 UH |
520 | return SR_ERR_BUG; |
521 | } | |
522 | ||
1644fb24 | 523 | if (!devc->ftdic) { |
b172c130 | 524 | sr_err("devc->ftdic was NULL."); |
b908f067 UH |
525 | return SR_ERR_BUG; |
526 | } | |
527 | ||
00910580 | 528 | devc->divcount = cv_samplerate_to_divcount(sdi, devc->cur_samplerate); |
1644fb24 | 529 | if (devc->divcount == 0xff) { |
b172c130 | 530 | sr_err("Invalid divcount/samplerate."); |
b908f067 UH |
531 | return SR_ERR; |
532 | } | |
533 | ||
aeff7fa2 BV |
534 | if (cv_convert_trigger(sdi) != SR_OK) { |
535 | sr_err("Failed to configure trigger."); | |
014359e3 BV |
536 | return SR_ERR; |
537 | } | |
538 | ||
b908f067 | 539 | /* Fill acquisition parameters into buf[]. */ |
00910580 UH |
540 | if (devc->prof->model == CHRONOVU_LA8) { |
541 | buf[0] = devc->divcount; | |
542 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
543 | buf[2] = devc->trigger_pattern & 0xff; | |
544 | buf[3] = devc->trigger_mask & 0xff; | |
545 | bytes_to_write = 4; | |
546 | } else { | |
547 | buf[0] = devc->divcount; | |
548 | buf[1] = 0xff; /* This byte must always be 0xff. */ | |
549 | buf[2] = (devc->trigger_pattern & 0xff00) >> 8; /* LSB */ | |
550 | buf[3] = (devc->trigger_pattern & 0x00ff) >> 0; /* MSB */ | |
551 | buf[4] = (devc->trigger_mask & 0xff00) >> 8; /* LSB */ | |
552 | buf[5] = (devc->trigger_mask & 0x00ff) >> 0; /* MSB */ | |
553 | buf[6] = (devc->trigger_edgemask & 0xff00) >> 8; /* LSB */ | |
554 | buf[7] = (devc->trigger_edgemask & 0x00ff) >> 0; /* MSB */ | |
555 | bytes_to_write = 8; | |
556 | } | |
b908f067 UH |
557 | |
558 | /* Start acquisition. */ | |
00910580 | 559 | bytes_written = cv_write(devc, buf, bytes_to_write); |
b908f067 | 560 | |
00910580 UH |
561 | if (bytes_written < 0 || bytes_written != bytes_to_write) { |
562 | sr_err("Acquisition failed to start."); | |
afc88319 | 563 | return SR_ERR; |
b908f067 UH |
564 | } |
565 | ||
4afdfd46 | 566 | sr_dbg("Hardware acquisition started successfully."); |
b908f067 | 567 | |
3e9b7f9c | 568 | devc->cb_data = cb_data; |
b908f067 | 569 | |
102f1239 | 570 | std_session_send_df_header(sdi, LOG_PREFIX); |
b908f067 | 571 | |
b908f067 | 572 | /* Time when we should be done (for detecting trigger timeouts). */ |
00910580 UH |
573 | devc->done = (devc->divcount + 1) * devc->prof->trigger_constant + |
574 | g_get_monotonic_time() + (10 * G_TIME_SPAN_SECOND); | |
1644fb24 BV |
575 | devc->block_counter = 0; |
576 | devc->trigger_found = 0; | |
b908f067 | 577 | |
b172c130 | 578 | /* Hook up a dummy handler to receive data from the device. */ |
c650d3ec | 579 | sr_session_source_add(sdi->session, -1, 0, 0, receive_data, (void *)sdi); |
b908f067 UH |
580 | |
581 | return SR_OK; | |
582 | } | |
583 | ||
6078d2c9 | 584 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
b908f067 | 585 | { |
102f1239 | 586 | (void)cb_data; |
b908f067 | 587 | |
f3a35908 | 588 | sr_dbg("Stopping acquisition."); |
102f1239 | 589 | sr_session_source_remove(sdi->session, -1); |
3be42bc2 | 590 | std_session_send_df_end(sdi, LOG_PREFIX); |
b908f067 UH |
591 | |
592 | return SR_OK; | |
593 | } | |
594 | ||
7b356712 UH |
595 | SR_PRIV struct sr_dev_driver chronovu_la_driver_info = { |
596 | .name = "chronovu-la", | |
597 | .longname = "ChronoVu LA8/LA16", | |
b908f067 | 598 | .api_version = 1, |
6078d2c9 UH |
599 | .init = init, |
600 | .cleanup = cleanup, | |
601 | .scan = scan, | |
602 | .dev_list = dev_list, | |
3b412e3a | 603 | .dev_clear = dev_clear, |
035a1078 BV |
604 | .config_get = config_get, |
605 | .config_set = config_set, | |
a1c743fc | 606 | .config_list = config_list, |
6078d2c9 UH |
607 | .dev_open = dev_open, |
608 | .dev_close = dev_close, | |
609 | .dev_acquisition_start = dev_acquisition_start, | |
610 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 611 | .context = NULL, |
b908f067 | 612 | }; |