]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <string.h> | |
23 | #include <sys/time.h> | |
24 | #include <inttypes.h> | |
25 | #include <glib.h> | |
26 | #include <libusb.h> | |
27 | #include "libsigrok.h" | |
28 | #include "libsigrok-internal.h" | |
29 | #include "analyzer.h" | |
30 | ||
31 | #define USB_VENDOR 0x0c12 | |
32 | ||
33 | #define VENDOR_NAME "ZEROPLUS" | |
34 | #define MODEL_NAME "Logic Cube LAP-C" | |
35 | #define MODEL_VERSION NULL | |
36 | ||
37 | #define NUM_PROBES 16 | |
38 | #define USB_INTERFACE 0 | |
39 | #define USB_CONFIGURATION 1 | |
40 | #define NUM_TRIGGER_STAGES 4 | |
41 | #define TRIGGER_TYPES "01" | |
42 | ||
43 | #define PACKET_SIZE 2048 /* ?? */ | |
44 | ||
45 | //#define ZP_EXPERIMENTAL | |
46 | ||
47 | typedef struct { | |
48 | unsigned short vid; | |
49 | unsigned short pid; | |
50 | char *model_name; | |
51 | unsigned int channels; | |
52 | unsigned int sample_depth; /* In Ksamples/channel */ | |
53 | unsigned int max_sampling_freq; | |
54 | } model_t; | |
55 | ||
56 | /* | |
57 | * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the | |
58 | * same 128K sample depth. | |
59 | */ | |
60 | static model_t zeroplus_models[] = { | |
61 | {0x0c12, 0x7009, "LAP-C(16064)", 16, 64, 100}, | |
62 | {0x0c12, 0x700A, "LAP-C(16128)", 16, 128, 200}, | |
63 | /* TODO: we don't know anything about these | |
64 | {0x0c12, 0x700B, "LAP-C(32128)", 32, 128, 200}, | |
65 | {0x0c12, 0x700C, "LAP-C(321000)", 32, 1024, 200}, | |
66 | {0x0c12, 0x700D, "LAP-C(322000)", 32, 2048, 200}, | |
67 | */ | |
68 | {0x0c12, 0x700E, "LAP-C(16032)", 16, 32, 100}, | |
69 | {0x0c12, 0x7016, "LAP-C(162000)", 16, 2048, 200}, | |
70 | { 0, 0, 0, 0, 0, 0 } | |
71 | }; | |
72 | ||
73 | static const int hwcaps[] = { | |
74 | SR_HWCAP_LOGIC_ANALYZER, | |
75 | SR_HWCAP_SAMPLERATE, | |
76 | SR_HWCAP_CAPTURE_RATIO, | |
77 | ||
78 | /* These are really implemented in the driver, not the hardware. */ | |
79 | SR_HWCAP_LIMIT_SAMPLES, | |
80 | 0, | |
81 | }; | |
82 | ||
83 | /* | |
84 | * ZEROPLUS LAP-C (16032) numbers the 16 probes A0-A7 and B0-B7. | |
85 | * We currently ignore other untested/unsupported devices here. | |
86 | */ | |
87 | static const char *probe_names[NUM_PROBES + 1] = { | |
88 | "A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", | |
89 | "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", | |
90 | NULL, | |
91 | }; | |
92 | ||
93 | /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */ | |
94 | SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info; | |
95 | static struct sr_dev_driver *di = &zeroplus_logic_cube_driver_info; | |
96 | ||
97 | /* | |
98 | * The hardware supports more samplerates than these, but these are the | |
99 | * options hardcoded into the vendor's Windows GUI. | |
100 | */ | |
101 | ||
102 | /* | |
103 | * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up | |
104 | * that high. | |
105 | */ | |
106 | static const uint64_t supported_samplerates[] = { | |
107 | SR_HZ(100), | |
108 | SR_HZ(500), | |
109 | SR_KHZ(1), | |
110 | SR_KHZ(5), | |
111 | SR_KHZ(25), | |
112 | SR_KHZ(50), | |
113 | SR_KHZ(100), | |
114 | SR_KHZ(200), | |
115 | SR_KHZ(400), | |
116 | SR_KHZ(800), | |
117 | SR_MHZ(1), | |
118 | SR_MHZ(10), | |
119 | SR_MHZ(25), | |
120 | SR_MHZ(50), | |
121 | SR_MHZ(80), | |
122 | SR_MHZ(100), | |
123 | SR_MHZ(150), | |
124 | SR_MHZ(200), | |
125 | 0, | |
126 | }; | |
127 | ||
128 | static const struct sr_samplerates samplerates = { | |
129 | 0, | |
130 | 0, | |
131 | 0, | |
132 | supported_samplerates, | |
133 | }; | |
134 | ||
135 | /* Private, per-device-instance driver context. */ | |
136 | struct dev_context { | |
137 | uint64_t cur_samplerate; | |
138 | uint64_t max_samplerate; | |
139 | uint64_t limit_samples; | |
140 | int num_channels; /* TODO: This isn't initialized before it's needed :( */ | |
141 | int memory_size; | |
142 | unsigned int max_memory_size; | |
143 | //uint8_t probe_mask; | |
144 | //uint8_t trigger_mask[NUM_TRIGGER_STAGES]; | |
145 | //uint8_t trigger_value[NUM_TRIGGER_STAGES]; | |
146 | // uint8_t trigger_buffer[NUM_TRIGGER_STAGES]; | |
147 | int trigger; | |
148 | unsigned int capture_ratio; | |
149 | ||
150 | /* TODO: this belongs in the device instance */ | |
151 | struct sr_usb_dev_inst *usb; | |
152 | }; | |
153 | ||
154 | static int hw_dev_close(struct sr_dev_inst *sdi); | |
155 | ||
156 | static unsigned int get_memory_size(int type) | |
157 | { | |
158 | if (type == MEMORY_SIZE_8K) | |
159 | return 8 * 1024; | |
160 | else if (type == MEMORY_SIZE_64K) | |
161 | return 64 * 1024; | |
162 | else if (type == MEMORY_SIZE_128K) | |
163 | return 128 * 1024; | |
164 | else if (type == MEMORY_SIZE_512K) | |
165 | return 512 * 1024; | |
166 | else | |
167 | return 0; | |
168 | } | |
169 | ||
170 | #if 0 | |
171 | static int configure_probes(const struct sr_dev_inst *sdi) | |
172 | { | |
173 | struct dev_context *devc; | |
174 | const struct sr_probe *probe; | |
175 | const GSList *l; | |
176 | int probe_bit, stage, i; | |
177 | char *tc; | |
178 | ||
179 | /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */ | |
180 | devc = sdi->priv; | |
181 | ||
182 | devc->probe_mask = 0; | |
183 | for (i = 0; i < NUM_TRIGGER_STAGES; i++) { | |
184 | devc->trigger_mask[i] = 0; | |
185 | devc->trigger_value[i] = 0; | |
186 | } | |
187 | ||
188 | stage = -1; | |
189 | for (l = sdi->probes; l; l = l->next) { | |
190 | probe = (struct sr_probe *)l->data; | |
191 | if (probe->enabled == FALSE) | |
192 | continue; | |
193 | probe_bit = 1 << (probe->index); | |
194 | devc->probe_mask |= probe_bit; | |
195 | ||
196 | if (probe->trigger) { | |
197 | stage = 0; | |
198 | for (tc = probe->trigger; *tc; tc++) { | |
199 | devc->trigger_mask[stage] |= probe_bit; | |
200 | if (*tc == '1') | |
201 | devc->trigger_value[stage] |= probe_bit; | |
202 | stage++; | |
203 | if (stage > NUM_TRIGGER_STAGES) | |
204 | return SR_ERR; | |
205 | } | |
206 | } | |
207 | } | |
208 | ||
209 | return SR_OK; | |
210 | } | |
211 | #endif | |
212 | ||
213 | static int configure_probes(const struct sr_dev_inst *sdi) | |
214 | { | |
215 | struct dev_context *devc; | |
216 | const GSList *l; | |
217 | const struct sr_probe *probe; | |
218 | char *tc; | |
219 | int type; | |
220 | ||
221 | /* Note: sdi and sdi->priv are non-NULL, the caller checked this. */ | |
222 | devc = sdi->priv; | |
223 | ||
224 | for (l = sdi->probes; l; l = l->next) { | |
225 | probe = (struct sr_probe *)l->data; | |
226 | if (probe->enabled == FALSE) | |
227 | continue; | |
228 | ||
229 | if ((tc = probe->trigger)) { | |
230 | switch (*tc) { | |
231 | case '1': | |
232 | type = TRIGGER_HIGH; | |
233 | break; | |
234 | case '0': | |
235 | type = TRIGGER_LOW; | |
236 | break; | |
237 | #if 0 | |
238 | case 'r': | |
239 | type = TRIGGER_POSEDGE; | |
240 | break; | |
241 | case 'f': | |
242 | type = TRIGGER_NEGEDGE; | |
243 | break; | |
244 | case 'c': | |
245 | type = TRIGGER_ANYEDGE; | |
246 | break; | |
247 | #endif | |
248 | default: | |
249 | return SR_ERR; | |
250 | } | |
251 | analyzer_add_trigger(probe->index, type); | |
252 | devc->trigger = 1; | |
253 | } | |
254 | } | |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
259 | static int clear_instances(void) | |
260 | { | |
261 | GSList *l; | |
262 | struct sr_dev_inst *sdi; | |
263 | struct drv_context *drvc; | |
264 | struct dev_context *devc; | |
265 | ||
266 | drvc = di->priv; | |
267 | for (l = drvc->instances; l; l = l->next) { | |
268 | sdi = l->data; | |
269 | if (!(devc = sdi->priv)) { | |
270 | /* Log error, but continue cleaning up the rest. */ | |
271 | sr_err("zeroplus: %s: sdi->priv was NULL, continuing", __func__); | |
272 | continue; | |
273 | } | |
274 | sr_usb_dev_inst_free(devc->usb); | |
275 | /* Properly close all devices... */ | |
276 | hw_dev_close(sdi); | |
277 | /* ...and free all their memory. */ | |
278 | sr_dev_inst_free(sdi); | |
279 | } | |
280 | g_slist_free(drvc->instances); | |
281 | drvc->instances = NULL; | |
282 | ||
283 | return SR_OK; | |
284 | } | |
285 | ||
286 | /* | |
287 | * API callbacks | |
288 | */ | |
289 | ||
290 | static int hw_init(struct sr_context *sr_ctx) | |
291 | { | |
292 | struct drv_context *drvc; | |
293 | ||
294 | if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) { | |
295 | sr_err("zeroplus: driver context malloc failed."); | |
296 | return SR_ERR_MALLOC; | |
297 | } | |
298 | drvc->sr_ctx = sr_ctx; | |
299 | di->priv = drvc; | |
300 | ||
301 | return SR_OK; | |
302 | } | |
303 | ||
304 | static GSList *hw_scan(GSList *options) | |
305 | { | |
306 | struct sr_dev_inst *sdi; | |
307 | struct sr_probe *probe; | |
308 | struct drv_context *drvc; | |
309 | struct dev_context *devc; | |
310 | model_t *prof; | |
311 | struct libusb_device_descriptor des; | |
312 | libusb_device **devlist; | |
313 | GSList *devices; | |
314 | int ret, devcnt, i, j; | |
315 | ||
316 | (void)options; | |
317 | ||
318 | drvc = di->priv; | |
319 | devices = NULL; | |
320 | ||
321 | clear_instances(); | |
322 | ||
323 | /* Find all ZEROPLUS analyzers and add them to device list. */ | |
324 | devcnt = 0; | |
325 | libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); /* TODO: Errors. */ | |
326 | ||
327 | for (i = 0; devlist[i]; i++) { | |
328 | ret = libusb_get_device_descriptor(devlist[i], &des); | |
329 | if (ret != 0) { | |
330 | sr_err("zp: failed to get device descriptor: %s", | |
331 | libusb_error_name(ret)); | |
332 | continue; | |
333 | } | |
334 | ||
335 | prof = NULL; | |
336 | for (j = 0; j < zeroplus_models[j].vid; j++) { | |
337 | if (des.idVendor == zeroplus_models[j].vid && | |
338 | des.idProduct == zeroplus_models[j].pid) { | |
339 | prof = &zeroplus_models[j]; | |
340 | } | |
341 | } | |
342 | /* Skip if the device was not found */ | |
343 | if (!prof) | |
344 | continue; | |
345 | sr_info("zp: Found ZEROPLUS model %s", prof->model_name); | |
346 | ||
347 | /* Register the device with libsigrok. */ | |
348 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, | |
349 | VENDOR_NAME, prof->model_name, NULL))) { | |
350 | sr_err("zp: %s: sr_dev_inst_new failed", __func__); | |
351 | return NULL; | |
352 | } | |
353 | sdi->driver = di; | |
354 | ||
355 | /* Allocate memory for our private driver context. */ | |
356 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
357 | sr_err("zp: %s: devc malloc failed", __func__); | |
358 | return NULL; | |
359 | } | |
360 | sdi->priv = devc; | |
361 | devc->num_channels = prof->channels; | |
362 | #ifdef ZP_EXPERIMENTAL | |
363 | devc->max_memory_size = 128 * 1024; | |
364 | devc->max_samplerate = 200; | |
365 | #else | |
366 | devc->max_memory_size = prof->sample_depth * 1024; | |
367 | devc->max_samplerate = prof->max_sampling_freq; | |
368 | #endif | |
369 | devc->max_samplerate *= SR_MHZ(1); | |
370 | devc->memory_size = MEMORY_SIZE_8K; | |
371 | // memset(devc->trigger_buffer, 0, NUM_TRIGGER_STAGES); | |
372 | ||
373 | /* Fill in probelist according to this device's profile. */ | |
374 | for (j = 0; j < devc->num_channels; j++) { | |
375 | if (!(probe = sr_probe_new(j, SR_PROBE_LOGIC, TRUE, | |
376 | probe_names[j]))) | |
377 | return NULL; | |
378 | sdi->probes = g_slist_append(sdi->probes, probe); | |
379 | } | |
380 | ||
381 | devices = g_slist_append(devices, sdi); | |
382 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
383 | devc->usb = sr_usb_dev_inst_new( | |
384 | libusb_get_bus_number(devlist[i]), | |
385 | libusb_get_device_address(devlist[i]), NULL); | |
386 | devcnt++; | |
387 | ||
388 | } | |
389 | libusb_free_device_list(devlist, 1); | |
390 | ||
391 | return devices; | |
392 | } | |
393 | ||
394 | static GSList *hw_dev_list(void) | |
395 | { | |
396 | struct drv_context *drvc; | |
397 | ||
398 | drvc = di->priv; | |
399 | ||
400 | return drvc->instances; | |
401 | } | |
402 | ||
403 | static int hw_dev_open(struct sr_dev_inst *sdi) | |
404 | { | |
405 | struct dev_context *devc; | |
406 | struct drv_context *drvc = di->priv; | |
407 | libusb_device **devlist, *dev; | |
408 | struct libusb_device_descriptor des; | |
409 | int device_count, ret, i; | |
410 | ||
411 | if (!(devc = sdi->priv)) { | |
412 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
413 | return SR_ERR_ARG; | |
414 | } | |
415 | ||
416 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, | |
417 | &devlist); | |
418 | if (device_count < 0) { | |
419 | sr_err("zp: Failed to retrieve device list"); | |
420 | return SR_ERR; | |
421 | } | |
422 | ||
423 | dev = NULL; | |
424 | for (i = 0; i < device_count; i++) { | |
425 | if ((ret = libusb_get_device_descriptor(devlist[i], &des))) { | |
426 | sr_err("zp: Failed to get device descriptor: %s.", | |
427 | libusb_error_name(ret)); | |
428 | continue; | |
429 | } | |
430 | if (libusb_get_bus_number(devlist[i]) == devc->usb->bus | |
431 | && libusb_get_device_address(devlist[i]) == devc->usb->address) { | |
432 | dev = devlist[i]; | |
433 | break; | |
434 | } | |
435 | } | |
436 | if (!dev) { | |
437 | sr_err("device on bus %d address %d disappeared!", | |
438 | devc->usb->bus, devc->usb->address); | |
439 | return SR_ERR; | |
440 | } | |
441 | ||
442 | if (!(ret = libusb_open(dev, &(devc->usb->devhdl)))) { | |
443 | sdi->status = SR_ST_ACTIVE; | |
444 | sr_info("zp: opened device %d on %d.%d interface %d", | |
445 | sdi->index, devc->usb->bus, | |
446 | devc->usb->address, USB_INTERFACE); | |
447 | } else { | |
448 | sr_err("zp: failed to open device: %s", libusb_error_name(ret)); | |
449 | return SR_ERR; | |
450 | } | |
451 | ||
452 | ret = libusb_set_configuration(devc->usb->devhdl, USB_CONFIGURATION); | |
453 | if (ret < 0) { | |
454 | sr_err("zp: Unable to set USB configuration %d: %s", | |
455 | USB_CONFIGURATION, libusb_error_name(ret)); | |
456 | return SR_ERR; | |
457 | } | |
458 | ||
459 | ret = libusb_claim_interface(devc->usb->devhdl, USB_INTERFACE); | |
460 | if (ret != 0) { | |
461 | sr_err("zp: Unable to claim interface: %s", | |
462 | libusb_error_name(ret)); | |
463 | return SR_ERR; | |
464 | } | |
465 | ||
466 | /* Set default configuration after power on */ | |
467 | if (analyzer_read_status(devc->usb->devhdl) == 0) | |
468 | analyzer_configure(devc->usb->devhdl); | |
469 | ||
470 | analyzer_reset(devc->usb->devhdl); | |
471 | analyzer_initialize(devc->usb->devhdl); | |
472 | ||
473 | //analyzer_set_memory_size(MEMORY_SIZE_512K); | |
474 | // analyzer_set_freq(g_freq, g_freq_scale); | |
475 | analyzer_set_trigger_count(1); | |
476 | // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger) | |
477 | // * get_memory_size(g_memory_size)) / 100) >> 2); | |
478 | ||
479 | #if 0 | |
480 | if (g_double_mode == 1) | |
481 | analyzer_set_compression(COMPRESSION_DOUBLE); | |
482 | else if (g_compression == 1) | |
483 | analyzer_set_compression(COMPRESSION_ENABLE); | |
484 | else | |
485 | #endif | |
486 | analyzer_set_compression(COMPRESSION_NONE); | |
487 | ||
488 | if (devc->cur_samplerate == 0) { | |
489 | /* Samplerate hasn't been set. Default to 1MHz. */ | |
490 | analyzer_set_freq(1, FREQ_SCALE_MHZ); | |
491 | devc->cur_samplerate = SR_MHZ(1); | |
492 | } | |
493 | ||
494 | return SR_OK; | |
495 | } | |
496 | ||
497 | static int hw_dev_close(struct sr_dev_inst *sdi) | |
498 | { | |
499 | struct dev_context *devc; | |
500 | ||
501 | if (!(devc = sdi->priv)) { | |
502 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
503 | return SR_ERR; | |
504 | } | |
505 | ||
506 | if (!devc->usb->devhdl) | |
507 | return SR_ERR; | |
508 | ||
509 | sr_info("zp: closing device %d on %d.%d interface %d", sdi->index, | |
510 | devc->usb->bus, devc->usb->address, USB_INTERFACE); | |
511 | libusb_release_interface(devc->usb->devhdl, USB_INTERFACE); | |
512 | libusb_reset_device(devc->usb->devhdl); | |
513 | libusb_close(devc->usb->devhdl); | |
514 | devc->usb->devhdl = NULL; | |
515 | sdi->status = SR_ST_INACTIVE; | |
516 | ||
517 | return SR_OK; | |
518 | } | |
519 | ||
520 | static int hw_cleanup(void) | |
521 | { | |
522 | struct drv_context *drvc; | |
523 | ||
524 | if (!(drvc = di->priv)) | |
525 | return SR_OK; | |
526 | ||
527 | clear_instances(); | |
528 | ||
529 | return SR_OK; | |
530 | } | |
531 | ||
532 | static int hw_info_get(int info_id, const void **data, | |
533 | const struct sr_dev_inst *sdi) | |
534 | { | |
535 | struct dev_context *devc; | |
536 | ||
537 | switch (info_id) { | |
538 | case SR_DI_HWCAPS: | |
539 | *data = hwcaps; | |
540 | break; | |
541 | case SR_DI_NUM_PROBES: | |
542 | if (sdi) { | |
543 | devc = sdi->priv; | |
544 | *data = GINT_TO_POINTER(devc->num_channels); | |
545 | sr_spew("zp: %s: Returning number of channels: %d.", | |
546 | __func__, devc->num_channels); | |
547 | } else | |
548 | return SR_ERR; | |
549 | break; | |
550 | case SR_DI_PROBE_NAMES: | |
551 | *data = probe_names; | |
552 | sr_spew("zp: %s: Returning probenames.", __func__); | |
553 | break; | |
554 | case SR_DI_SAMPLERATES: | |
555 | *data = &samplerates; | |
556 | sr_spew("zp: %s: Returning samplerates.", __func__); | |
557 | break; | |
558 | case SR_DI_TRIGGER_TYPES: | |
559 | *data = TRIGGER_TYPES; | |
560 | sr_spew("zp: %s: Returning triggertypes: %s.", __func__, TRIGGER_TYPES); | |
561 | break; | |
562 | case SR_DI_CUR_SAMPLERATE: | |
563 | if (sdi) { | |
564 | devc = sdi->priv; | |
565 | *data = &devc->cur_samplerate; | |
566 | sr_spew("zp: %s: Returning samplerate: %" PRIu64 "Hz.", | |
567 | __func__, devc->cur_samplerate); | |
568 | } else | |
569 | return SR_ERR; | |
570 | break; | |
571 | default: | |
572 | return SR_ERR_ARG; | |
573 | } | |
574 | ||
575 | return SR_OK; | |
576 | } | |
577 | ||
578 | static int set_samplerate(struct dev_context *devc, uint64_t samplerate) | |
579 | { | |
580 | int i; | |
581 | ||
582 | for (i = 0; supported_samplerates[i]; i++) | |
583 | if (samplerate == supported_samplerates[i]) | |
584 | break; | |
585 | if (!supported_samplerates[i] || samplerate > devc->max_samplerate) { | |
586 | sr_err("zp: %s: unsupported samplerate", __func__); | |
587 | return SR_ERR_ARG; | |
588 | } | |
589 | ||
590 | sr_info("zp: Setting samplerate to %" PRIu64 "Hz.", samplerate); | |
591 | ||
592 | if (samplerate >= SR_MHZ(1)) | |
593 | analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ); | |
594 | else if (samplerate >= SR_KHZ(1)) | |
595 | analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ); | |
596 | else | |
597 | analyzer_set_freq(samplerate, FREQ_SCALE_HZ); | |
598 | ||
599 | devc->cur_samplerate = samplerate; | |
600 | ||
601 | return SR_OK; | |
602 | } | |
603 | ||
604 | static int set_limit_samples(struct dev_context *devc, uint64_t samples) | |
605 | { | |
606 | devc->limit_samples = samples; | |
607 | ||
608 | if (samples <= 2 * 1024) | |
609 | devc->memory_size = MEMORY_SIZE_8K; | |
610 | else if (samples <= 16 * 1024) | |
611 | devc->memory_size = MEMORY_SIZE_64K; | |
612 | else if (samples <= 32 * 1024 || | |
613 | devc->max_memory_size <= 32 * 1024) | |
614 | devc->memory_size = MEMORY_SIZE_128K; | |
615 | else | |
616 | devc->memory_size = MEMORY_SIZE_512K; | |
617 | ||
618 | sr_info("zp: Setting memory size to %dK.", | |
619 | get_memory_size(devc->memory_size) / 1024); | |
620 | ||
621 | analyzer_set_memory_size(devc->memory_size); | |
622 | ||
623 | return SR_OK; | |
624 | } | |
625 | ||
626 | static int set_capture_ratio(struct dev_context *devc, uint64_t ratio) | |
627 | { | |
628 | if (ratio > 100) { | |
629 | sr_err("zp: %s: invalid capture ratio", __func__); | |
630 | return SR_ERR_ARG; | |
631 | } | |
632 | ||
633 | devc->capture_ratio = ratio; | |
634 | ||
635 | sr_info("zp: Setting capture ratio to %d%%.", devc->capture_ratio); | |
636 | ||
637 | return SR_OK; | |
638 | } | |
639 | ||
640 | static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, | |
641 | const void *value) | |
642 | { | |
643 | struct dev_context *devc; | |
644 | ||
645 | if (!sdi) { | |
646 | sr_err("zp: %s: sdi was NULL", __func__); | |
647 | return SR_ERR_ARG; | |
648 | } | |
649 | ||
650 | if (!(devc = sdi->priv)) { | |
651 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
652 | return SR_ERR_ARG; | |
653 | } | |
654 | ||
655 | switch (hwcap) { | |
656 | case SR_HWCAP_SAMPLERATE: | |
657 | return set_samplerate(devc, *(const uint64_t *)value); | |
658 | case SR_HWCAP_LIMIT_SAMPLES: | |
659 | return set_limit_samples(devc, *(const uint64_t *)value); | |
660 | case SR_HWCAP_CAPTURE_RATIO: | |
661 | return set_capture_ratio(devc, *(const uint64_t *)value); | |
662 | default: | |
663 | return SR_ERR; | |
664 | } | |
665 | } | |
666 | ||
667 | static void set_triggerbar(struct dev_context *devc) | |
668 | { | |
669 | unsigned int ramsize; | |
670 | unsigned int n; | |
671 | unsigned int triggerbar; | |
672 | ||
673 | ramsize = get_memory_size(devc->memory_size) / 4; | |
674 | if (devc->trigger) { | |
675 | n = ramsize; | |
676 | if (devc->max_memory_size < n) | |
677 | n = devc->max_memory_size; | |
678 | if (devc->limit_samples < n) | |
679 | n = devc->limit_samples; | |
680 | n = n * devc->capture_ratio / 100; | |
681 | if (n > ramsize - 8) | |
682 | triggerbar = ramsize - 8; | |
683 | else | |
684 | triggerbar = n; | |
685 | } else { | |
686 | triggerbar = 0; | |
687 | } | |
688 | analyzer_set_triggerbar_address(triggerbar); | |
689 | analyzer_set_ramsize_trigger_address(ramsize - triggerbar); | |
690 | ||
691 | sr_dbg("zp: triggerbar_address = %d(0x%x)", triggerbar, triggerbar); | |
692 | sr_dbg("zp: ramsize_triggerbar_address = %d(0x%x)", | |
693 | ramsize - triggerbar, ramsize - triggerbar); | |
694 | } | |
695 | ||
696 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, | |
697 | void *cb_data) | |
698 | { | |
699 | struct sr_datafeed_packet packet; | |
700 | struct sr_datafeed_logic logic; | |
701 | struct sr_datafeed_header header; | |
702 | struct sr_datafeed_meta_logic meta; | |
703 | //uint64_t samples_read; | |
704 | int res; | |
705 | unsigned int packet_num; | |
706 | unsigned int n; | |
707 | unsigned char *buf; | |
708 | struct dev_context *devc; | |
709 | ||
710 | if (!(devc = sdi->priv)) { | |
711 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
712 | return SR_ERR_ARG; | |
713 | } | |
714 | ||
715 | if (configure_probes(sdi) != SR_OK) { | |
716 | sr_err("zp: failed to configured probes"); | |
717 | return SR_ERR; | |
718 | } | |
719 | ||
720 | set_triggerbar(devc); | |
721 | ||
722 | /* push configured settings to device */ | |
723 | analyzer_configure(devc->usb->devhdl); | |
724 | ||
725 | analyzer_start(devc->usb->devhdl); | |
726 | sr_info("zp: Waiting for data"); | |
727 | analyzer_wait_data(devc->usb->devhdl); | |
728 | ||
729 | sr_info("zp: Stop address = 0x%x", | |
730 | analyzer_get_stop_address(devc->usb->devhdl)); | |
731 | sr_info("zp: Now address = 0x%x", | |
732 | analyzer_get_now_address(devc->usb->devhdl)); | |
733 | sr_info("zp: Trigger address = 0x%x", | |
734 | analyzer_get_trigger_address(devc->usb->devhdl)); | |
735 | ||
736 | packet.type = SR_DF_HEADER; | |
737 | packet.payload = &header; | |
738 | header.feed_version = 1; | |
739 | gettimeofday(&header.starttime, NULL); | |
740 | sr_session_send(cb_data, &packet); | |
741 | ||
742 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
743 | packet.type = SR_DF_META_LOGIC; | |
744 | packet.payload = &meta; | |
745 | meta.samplerate = devc->cur_samplerate; | |
746 | meta.num_probes = devc->num_channels; | |
747 | sr_session_send(cb_data, &packet); | |
748 | ||
749 | if (!(buf = g_try_malloc(PACKET_SIZE))) { | |
750 | sr_err("zp: %s: buf malloc failed", __func__); | |
751 | return SR_ERR_MALLOC; | |
752 | } | |
753 | ||
754 | //samples_read = 0; | |
755 | analyzer_read_start(devc->usb->devhdl); | |
756 | /* Send the incoming transfer to the session bus. */ | |
757 | n = get_memory_size(devc->memory_size); | |
758 | if (devc->max_memory_size * 4 < n) | |
759 | n = devc->max_memory_size * 4; | |
760 | for (packet_num = 0; packet_num < n / PACKET_SIZE; packet_num++) { | |
761 | res = analyzer_read_data(devc->usb->devhdl, buf, PACKET_SIZE); | |
762 | sr_info("zp: Tried to read %d bytes, actually read %d bytes", | |
763 | PACKET_SIZE, res); | |
764 | ||
765 | packet.type = SR_DF_LOGIC; | |
766 | packet.payload = &logic; | |
767 | logic.length = PACKET_SIZE; | |
768 | logic.unitsize = 4; | |
769 | logic.data = buf; | |
770 | sr_session_send(cb_data, &packet); | |
771 | //samples_read += res / 4; | |
772 | } | |
773 | analyzer_read_stop(devc->usb->devhdl); | |
774 | g_free(buf); | |
775 | ||
776 | packet.type = SR_DF_END; | |
777 | sr_session_send(cb_data, &packet); | |
778 | ||
779 | return SR_OK; | |
780 | } | |
781 | ||
782 | /* TODO: This stops acquisition on ALL devices, ignoring dev_index. */ | |
783 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
784 | { | |
785 | struct sr_datafeed_packet packet; | |
786 | struct dev_context *devc; | |
787 | ||
788 | packet.type = SR_DF_END; | |
789 | sr_session_send(cb_data, &packet); | |
790 | ||
791 | if (!(devc = sdi->priv)) { | |
792 | sr_err("zp: %s: sdi->priv was NULL", __func__); | |
793 | return SR_ERR_BUG; | |
794 | } | |
795 | ||
796 | analyzer_reset(devc->usb->devhdl); | |
797 | /* TODO: Need to cancel and free any queued up transfers. */ | |
798 | ||
799 | return SR_OK; | |
800 | } | |
801 | ||
802 | SR_PRIV struct sr_dev_driver zeroplus_logic_cube_driver_info = { | |
803 | .name = "zeroplus-logic-cube", | |
804 | .longname = "ZEROPLUS Logic Cube LAP-C series", | |
805 | .api_version = 1, | |
806 | .init = hw_init, | |
807 | .cleanup = hw_cleanup, | |
808 | .scan = hw_scan, | |
809 | .dev_list = hw_dev_list, | |
810 | .dev_clear = hw_cleanup, | |
811 | .dev_open = hw_dev_open, | |
812 | .dev_close = hw_dev_close, | |
813 | .info_get = hw_info_get, | |
814 | .dev_config_set = hw_dev_config_set, | |
815 | .dev_acquisition_start = hw_dev_acquisition_start, | |
816 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
817 | .priv = NULL, | |
818 | }; |