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