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