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