]> sigrok.org Git - libsigrok.git/blame - hardware/zeroplus-logic-cube/zeroplus.c
Make ARRAY_SIZE/ARRAY_AND_SIZE private.
[libsigrok.git] / hardware / zeroplus-logic-cube / zeroplus.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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
22b02383 20#include "config.h"
a1bb33af
UH
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/time.h>
24#include <inttypes.h>
25#include <glib.h>
26#include <libusb.h>
fed16f06 27#include <sigrok.h>
4cea9eb2 28#include <sigrok-internal.h>
a1bb33af
UH
29#include "analyzer.h"
30
fed16f06
UH
31#define USB_VENDOR 0x0c12
32#define USB_VENDOR_NAME "Zeroplus"
a1bb33af
UH
33#define USB_MODEL_NAME "Logic Cube"
34#define USB_MODEL_VERSION ""
35
36#define USB_INTERFACE 0
37#define USB_CONFIGURATION 1
38#define NUM_TRIGGER_STAGES 4
39#define TRIGGER_TYPES "01"
40
fed16f06 41#define PACKET_SIZE 2048 /* ?? */
a1bb33af
UH
42
43typedef struct {
44 unsigned short pid;
45 char model_name[64];
46 unsigned int channels;
fed16f06 47 unsigned int sample_depth; /* In Ksamples/channel */
a1bb33af
UH
48 unsigned int max_sampling_freq;
49} model_t;
50
fed16f06
UH
51/*
52 * Note -- 16032, 16064 and 16128 *usually* -- but not always -- have the
53 * same 128K sample depth.
54 */
29cbfeaf 55static model_t zeroplus_models[] = {
a1bb33af
UH
56 {0x7009, "LAP-C(16064)", 16, 64, 100},
57 {0x700A, "LAP-C(16128)", 16, 128, 200},
58 {0x700B, "LAP-C(32128)", 32, 128, 200},
59 {0x700C, "LAP-C(321000)", 32, 1024, 200},
60 {0x700D, "LAP-C(322000)", 32, 2048, 200},
61 {0x700E, "LAP-C(16032)", 16, 32, 100},
62 {0x7016, "LAP-C(162000)", 16, 2048, 200},
63};
64
65static int capabilities[] = {
5a2326a7
UH
66 SR_HWCAP_LOGIC_ANALYZER,
67 SR_HWCAP_SAMPLERATE,
68 SR_HWCAP_PROBECONFIG,
69 SR_HWCAP_CAPTURE_RATIO,
a1bb33af 70
fed16f06 71 /* These are really implemented in the driver, not the hardware. */
5a2326a7 72 SR_HWCAP_LIMIT_SAMPLES,
fed16f06 73 0,
a1bb33af
UH
74};
75
a00ba012 76/* List of struct sr_device_instance, maintained by opendev()/closedev(). */
a1bb33af
UH
77static GSList *device_instances = NULL;
78
79static libusb_context *usb_context = NULL;
80
fed16f06
UH
81/*
82 * The hardware supports more samplerates than these, but these are the
83 * options hardcoded into the vendor's Windows GUI.
84 */
a1bb33af 85
fed16f06
UH
86/*
87 * TODO: We shouldn't support 150MHz and 200MHz on devices that don't go up
88 * that high.
89 */
a1bb33af
UH
90static uint64_t supported_samplerates[] = {
91 100,
92 500,
93 KHZ(1),
94 KHZ(5),
95 KHZ(25),
96 KHZ(50),
97 KHZ(100),
98 KHZ(200),
99 KHZ(400),
100 KHZ(800),
101 MHZ(1),
102 MHZ(10),
103 MHZ(25),
104 MHZ(50),
105 MHZ(80),
106 MHZ(100),
107 MHZ(150),
108 MHZ(200),
fed16f06 109 0,
a1bb33af
UH
110};
111
60679b18 112static struct sr_samplerates samplerates = {
fed16f06
UH
113 0, 0, 0,
114 supported_samplerates,
a1bb33af
UH
115};
116
fed16f06 117/* TODO: All of these should go in a device-specific struct. */
a1bb33af
UH
118static uint64_t cur_samplerate = 0;
119static uint64_t limit_samples = 0;
29cbfeaf
UH
120static int num_channels = 32; /* TODO: This isn't initialized before it's needed :( */
121static uint64_t memory_size = 0;
86c5e279 122static uint8_t probe_mask = 0;
fed16f06
UH
123static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
124static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
a1bb33af 125
fed16f06 126// static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
a1bb33af
UH
127
128static int hw_set_configuration(int device_index, int capability, void *value);
129
130static unsigned int get_memory_size(int type)
131{
fed16f06
UH
132 if (type == MEMORY_SIZE_8K)
133 return 8 * 1024;
134 else if (type == MEMORY_SIZE_64K)
135 return 64 * 1024;
136 else if (type == MEMORY_SIZE_128K)
137 return 128 * 1024;
138 else if (type == MEMORY_SIZE_512K)
139 return 512 * 1024;
140 else
141 return 0;
a1bb33af
UH
142}
143
a00ba012 144static int opendev4(struct sr_device_instance **sdi, libusb_device *dev,
408e7199
UH
145 struct libusb_device_descriptor *des)
146{
9a498834
UH
147 unsigned int i;
148 int err;
408e7199
UH
149
150 if ((err = libusb_get_device_descriptor(dev, des))) {
151 g_warning("failed to get device descriptor: %d", err);
152 return -1;
153 }
154
9a498834 155 if (des->idVendor != USB_VENDOR)
408e7199
UH
156 return 0;
157
158 if (libusb_get_bus_number(dev) == (*sdi)->usb->bus
159 && libusb_get_device_address(dev) == (*sdi)->usb->address) {
160
9a498834
UH
161 for (i = 0; i < ARRAY_SIZE(zeroplus_models); i++) {
162 if (!(des->idProduct == zeroplus_models[i].pid))
408e7199
UH
163 continue;
164
9a498834
UH
165 g_message("Found PID=%04X (%s)", des->idProduct,
166 zeroplus_models[i].model_name);
167 num_channels = zeroplus_models[i].channels;
168 memory_size = zeroplus_models[i].sample_depth * 1024;
408e7199
UH
169 break;
170 }
171
172 if (num_channels == 0) {
173 g_warning("Unknown ZeroPlus device %04X",
9a498834 174 des->idProduct);
408e7199
UH
175 return -2;
176 }
177
178 /* Found it. */
179 if (!(err = libusb_open(dev, &((*sdi)->usb->devhdl)))) {
5a2326a7 180 (*sdi)->status = SR_ST_ACTIVE;
408e7199
UH
181 g_message("opened device %d on %d.%d interface %d",
182 (*sdi)->index, (*sdi)->usb->bus,
183 (*sdi)->usb->address, USB_INTERFACE);
184 } else {
185 g_warning("failed to open device: %d", err);
186 *sdi = NULL;
187 }
188 }
189
190 return 0;
191}
192
29cbfeaf 193static struct sr_device_instance *zp_open_device(int device_index)
a1bb33af 194{
a00ba012 195 struct sr_device_instance *sdi;
a1bb33af
UH
196 libusb_device **devlist;
197 struct libusb_device_descriptor des;
afc8e4de 198 int err, i;
a1bb33af 199
d32d961d 200 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af
UH
201 return NULL;
202
203 libusb_get_device_list(usb_context, &devlist);
5a2326a7 204 if (sdi->status == SR_ST_INACTIVE) {
fed16f06 205 /* Find the device by vendor, product, bus and address. */
a1bb33af 206 libusb_get_device_list(usb_context, &devlist);
fed16f06 207 for (i = 0; devlist[i]; i++) {
408e7199 208 /* TODO: Error handling. */
9a5c6dcf 209 err = opendev4(&sdi, devlist[i], &des);
a1bb33af 210 }
fed16f06 211 } else {
5a2326a7 212 /* Status must be SR_ST_ACTIVE, i.e. already in use... */
a1bb33af
UH
213 sdi = NULL;
214 }
215 libusb_free_device_list(devlist, 1);
216
5a2326a7 217 if (sdi && sdi->status != SR_ST_ACTIVE)
a1bb33af
UH
218 sdi = NULL;
219
220 return sdi;
221}
222
a00ba012 223static void close_device(struct sr_device_instance *sdi)
a1bb33af 224{
408e7199
UH
225 if (!sdi->usb->devhdl)
226 return;
227
228 g_message("closing device %d on %d.%d interface %d", sdi->index,
229 sdi->usb->bus, sdi->usb->address, USB_INTERFACE);
230 libusb_release_interface(sdi->usb->devhdl, USB_INTERFACE);
231 libusb_close(sdi->usb->devhdl);
232 sdi->usb->devhdl = NULL;
5a2326a7 233 sdi->status = SR_ST_INACTIVE;
a1bb33af
UH
234}
235
a1bb33af
UH
236static int configure_probes(GSList *probes)
237{
1afe8989 238 struct sr_probe *probe;
a1bb33af
UH
239 GSList *l;
240 int probe_bit, stage, i;
241 char *tc;
242
243 probe_mask = 0;
fed16f06 244 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
a1bb33af
UH
245 trigger_mask[i] = 0;
246 trigger_value[i] = 0;
247 }
248
249 stage = -1;
fed16f06 250 for (l = probes; l; l = l->next) {
1afe8989 251 probe = (struct sr_probe *)l->data;
fed16f06 252 if (probe->enabled == FALSE)
a1bb33af
UH
253 continue;
254 probe_bit = 1 << (probe->index - 1);
255 probe_mask |= probe_bit;
fed16f06
UH
256
257 if (probe->trigger) {
a1bb33af 258 stage = 0;
fed16f06 259 for (tc = probe->trigger; *tc; tc++) {
a1bb33af 260 trigger_mask[stage] |= probe_bit;
fed16f06 261 if (*tc == '1')
a1bb33af
UH
262 trigger_value[stage] |= probe_bit;
263 stage++;
fed16f06 264 if (stage > NUM_TRIGGER_STAGES)
e46b8fb1 265 return SR_ERR;
a1bb33af
UH
266 }
267 }
268 }
269
e46b8fb1 270 return SR_OK;
a1bb33af
UH
271}
272
a1bb33af
UH
273/*
274 * API callbacks
275 */
276
54ac5277 277static int hw_init(const char *deviceinfo)
a1bb33af 278{
a00ba012 279 struct sr_device_instance *sdi;
a1bb33af
UH
280 struct libusb_device_descriptor des;
281 libusb_device **devlist;
282 int err, devcnt, i;
283
17e1afcb 284 /* Avoid compiler warnings. */
afc8e4de
UH
285 deviceinfo = deviceinfo;
286
fed16f06 287 if (libusb_init(&usb_context) != 0) {
a1bb33af
UH
288 g_warning("Failed to initialize USB.");
289 return 0;
290 }
291
fed16f06 292 /* Find all ZeroPlus analyzers and add them to device list. */
a1bb33af
UH
293 devcnt = 0;
294 libusb_get_device_list(usb_context, &devlist);
fed16f06
UH
295
296 for (i = 0; devlist[i]; i++) {
a1bb33af 297 err = libusb_get_device_descriptor(devlist[i], &des);
fed16f06 298 if (err != 0) {
a1bb33af
UH
299 g_warning("failed to get device descriptor: %d", err);
300 continue;
301 }
302
fed16f06
UH
303 if (des.idVendor == USB_VENDOR) {
304 /*
305 * Definitely a Zeroplus.
306 * TODO: Any way to detect specific model/version in
307 * the zeroplus range?
308 */
a00ba012 309 sdi = sr_device_instance_new(devcnt,
5a2326a7 310 SR_ST_INACTIVE, USB_VENDOR_NAME,
fed16f06
UH
311 USB_MODEL_NAME, USB_MODEL_VERSION);
312 if (!sdi)
a1bb33af 313 return 0;
fed16f06
UH
314 device_instances =
315 g_slist_append(device_instances, sdi);
6c290072 316 sdi->usb = sr_usb_device_instance_new(
fed16f06
UH
317 libusb_get_bus_number(devlist[i]),
318 libusb_get_device_address(devlist[i]), NULL);
a1bb33af
UH
319 devcnt++;
320 }
321 }
322 libusb_free_device_list(devlist, 1);
323
324 return devcnt;
325}
326
a1bb33af
UH
327static int hw_opendev(int device_index)
328{
a00ba012 329 struct sr_device_instance *sdi;
a1bb33af
UH
330 int err;
331
fed16f06 332 if (!(sdi = zp_open_device(device_index))) {
a1bb33af 333 g_warning("unable to open device");
e46b8fb1 334 return SR_ERR;
a1bb33af
UH
335 }
336
337 err = libusb_claim_interface(sdi->usb->devhdl, USB_INTERFACE);
fed16f06 338 if (err != 0) {
a1bb33af 339 g_warning("Unable to claim interface: %d", err);
e46b8fb1 340 return SR_ERR;
a1bb33af
UH
341 }
342 analyzer_reset(sdi->usb->devhdl);
343 analyzer_initialize(sdi->usb->devhdl);
a1bb33af
UH
344
345 analyzer_set_memory_size(MEMORY_SIZE_512K);
fed16f06 346 // analyzer_set_freq(g_freq, g_freq_scale);
a1bb33af 347 analyzer_set_trigger_count(1);
408e7199
UH
348 // analyzer_set_ramsize_trigger_address((((100 - g_pre_trigger)
349 // * get_memory_size(g_memory_size)) / 100) >> 2);
fed16f06
UH
350 analyzer_set_ramsize_trigger_address(
351 (100 * get_memory_size(MEMORY_SIZE_512K) / 100) >> 2);
a1bb33af 352
fed16f06
UH
353#if 0
354 if (g_double_mode == 1)
a1bb33af
UH
355 analyzer_set_compression(COMPRESSION_DOUBLE);
356 else if (g_compression == 1)
357 analyzer_set_compression(COMPRESSION_ENABLE);
fed16f06
UH
358 else
359#endif
360 analyzer_set_compression(COMPRESSION_NONE);
a1bb33af 361
fed16f06 362 if (cur_samplerate == 0) {
408e7199 363 /* Samplerate hasn't been set. Default to the slowest one. */
5a2326a7 364 if (hw_set_configuration(device_index, SR_HWCAP_SAMPLERATE,
e46b8fb1
UH
365 &samplerates.low) == SR_ERR)
366 return SR_ERR;
a1bb33af
UH
367 }
368
e46b8fb1 369 return SR_OK;
a1bb33af
UH
370}
371
a1bb33af
UH
372static void hw_closedev(int device_index)
373{
a00ba012 374 struct sr_device_instance *sdi;
a1bb33af 375
d32d961d 376 if ((sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af 377 close_device(sdi);
a1bb33af
UH
378}
379
a1bb33af
UH
380static void hw_cleanup(void)
381{
382 GSList *l;
383
408e7199 384 /* Properly close all devices... */
fed16f06 385 for (l = device_instances; l; l = l->next)
a00ba012 386 close_device((struct sr_device_instance *)l->data);
a1bb33af 387
408e7199 388 /* ...and free all their memory. */
fed16f06 389 for (l = device_instances; l; l = l->next)
a1bb33af
UH
390 g_free(l->data);
391 g_slist_free(device_instances);
392 device_instances = NULL;
393
fed16f06 394 if (usb_context)
a1bb33af
UH
395 libusb_exit(usb_context);
396 usb_context = NULL;
a1bb33af
UH
397}
398
a1bb33af
UH
399static void *hw_get_device_info(int device_index, int device_info_id)
400{
a00ba012 401 struct sr_device_instance *sdi;
408e7199 402 void *info = NULL;
a1bb33af 403
d32d961d 404 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
a1bb33af
UH
405 return NULL;
406
fed16f06 407 switch (device_info_id) {
5a2326a7 408 case SR_DI_INSTANCE:
a1bb33af
UH
409 info = sdi;
410 break;
5a2326a7 411 case SR_DI_NUM_PROBES:
a1bb33af
UH
412 info = GINT_TO_POINTER(num_channels);
413 break;
5a2326a7 414 case SR_DI_SAMPLERATES:
a1bb33af
UH
415 info = &samplerates;
416 break;
5a2326a7 417 case SR_DI_TRIGGER_TYPES:
a1bb33af
UH
418 info = TRIGGER_TYPES;
419 break;
5a2326a7 420 case SR_DI_CUR_SAMPLERATE:
a1bb33af
UH
421 info = &cur_samplerate;
422 break;
423 }
424
425 return info;
426}
427
a1bb33af
UH
428static int hw_get_status(int device_index)
429{
a00ba012 430 struct sr_device_instance *sdi;
a1bb33af 431
d32d961d 432 sdi = sr_get_device_instance(device_instances, device_index);
fed16f06 433 if (sdi)
a1bb33af
UH
434 return sdi->status;
435 else
5a2326a7 436 return SR_ST_NOT_FOUND;
a1bb33af
UH
437}
438
a1bb33af
UH
439static int *hw_get_capabilities(void)
440{
a1bb33af
UH
441 return capabilities;
442}
443
fed16f06 444/* TODO: This will set the same samplerate for all devices. */
afc8e4de 445static int set_configuration_samplerate(uint64_t samplerate)
a1bb33af 446{
5e2ddeb0 447 g_message("%s(%" PRIu64 ")", __FUNCTION__, samplerate);
a1bb33af
UH
448 if (samplerate > MHZ(1))
449 analyzer_set_freq(samplerate / MHZ(1), FREQ_SCALE_MHZ);
450 else if (samplerate > KHZ(1))
451 analyzer_set_freq(samplerate / KHZ(1), FREQ_SCALE_KHZ);
452 else
fed16f06 453 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
a1bb33af
UH
454
455 cur_samplerate = samplerate;
456
e46b8fb1 457 return SR_OK;
a1bb33af
UH
458}
459
460static int hw_set_configuration(int device_index, int capability, void *value)
461{
a00ba012 462 struct sr_device_instance *sdi;
a1bb33af
UH
463 uint64_t *tmp_u64;
464
d32d961d 465 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 466 return SR_ERR;
a1bb33af
UH
467
468 switch (capability) {
5a2326a7 469 case SR_HWCAP_SAMPLERATE:
fed16f06
UH
470 tmp_u64 = value;
471 return set_configuration_samplerate(*tmp_u64);
5a2326a7 472 case SR_HWCAP_PROBECONFIG:
fed16f06 473 return configure_probes((GSList *) value);
5a2326a7 474 case SR_HWCAP_LIMIT_SAMPLES:
2458ea65
BV
475 tmp_u64 = value;
476 limit_samples = *tmp_u64;
e46b8fb1 477 return SR_OK;
fed16f06 478 default:
e46b8fb1 479 return SR_ERR;
a1bb33af
UH
480 }
481}
482
483static int hw_start_acquisition(int device_index, gpointer session_device_id)
484{
a00ba012 485 struct sr_device_instance *sdi;
b9c735a2
UH
486 struct sr_datafeed_packet packet;
487 struct sr_datafeed_header header;
a1bb33af 488 int res;
afc8e4de 489 unsigned int packet_num;
a1bb33af
UH
490 unsigned char *buf;
491
d32d961d 492 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
e46b8fb1 493 return SR_ERR;
a1bb33af 494
a143e4e5
BV
495 /* push configured settings to device */
496 analyzer_configure(sdi->usb->devhdl);
497
a1bb33af
UH
498 analyzer_start(sdi->usb->devhdl);
499 g_message("Waiting for data");
500 analyzer_wait_data(sdi->usb->devhdl);
501
fed16f06
UH
502 g_message("Stop address = 0x%x",
503 analyzer_get_stop_address(sdi->usb->devhdl));
504 g_message("Now address = 0x%x",
505 analyzer_get_now_address(sdi->usb->devhdl));
506 g_message("Trigger address = 0x%x",
507 analyzer_get_trigger_address(sdi->usb->devhdl));
a1bb33af 508
5a2326a7 509 packet.type = SR_DF_HEADER;
b9c735a2 510 packet.length = sizeof(struct sr_datafeed_header);
fed16f06 511 packet.payload = (unsigned char *)&header;
a1bb33af
UH
512 header.feed_version = 1;
513 gettimeofday(&header.starttime, NULL);
4c100f32 514 header.samplerate = cur_samplerate;
5a2326a7 515 header.protocol_id = SR_PROTO_RAW;
c2616fb9
DR
516 header.num_logic_probes = num_channels;
517 header.num_analog_probes = 0;
8a2efef2 518 sr_session_bus(session_device_id, &packet);
a1bb33af
UH
519
520 buf = g_malloc(PACKET_SIZE);
521 if (!buf)
e46b8fb1 522 return SR_ERR;
a1bb33af 523 analyzer_read_start(sdi->usb->devhdl);
fed16f06
UH
524 /* Send the incoming transfer to the session bus. */
525 for (packet_num = 0; packet_num < (memory_size * 4 / PACKET_SIZE);
526 packet_num++) {
a1bb33af 527 res = analyzer_read_data(sdi->usb->devhdl, buf, PACKET_SIZE);
408e7199
UH
528#if 0
529 g_message("Tried to read %llx bytes, actually read %x bytes",
530 PACKET_SIZE, res);
531#endif
a1bb33af 532
5a2326a7 533 packet.type = SR_DF_LOGIC;
a1bb33af 534 packet.length = PACKET_SIZE;
4c046c6b 535 packet.unitsize = 4;
a1bb33af 536 packet.payload = buf;
8a2efef2 537 sr_session_bus(session_device_id, &packet);
a1bb33af
UH
538 }
539 analyzer_read_stop(sdi->usb->devhdl);
540 g_free(buf);
541
5a2326a7 542 packet.type = SR_DF_END;
8a2efef2 543 sr_session_bus(session_device_id, &packet);
a1bb33af 544
e46b8fb1 545 return SR_OK;
a1bb33af
UH
546}
547
fed16f06 548/* This stops acquisition on ALL devices, ignoring device_index. */
a1bb33af
UH
549static void hw_stop_acquisition(int device_index, gpointer session_device_id)
550{
b9c735a2 551 struct sr_datafeed_packet packet;
a00ba012 552 struct sr_device_instance *sdi;
a1bb33af 553
5a2326a7 554 packet.type = SR_DF_END;
8a2efef2 555 sr_session_bus(session_device_id, &packet);
a1bb33af 556
d32d961d 557 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
fed16f06 558 return; /* TODO: Cry? */
a1bb33af
UH
559
560 analyzer_reset(sdi->usb->devhdl);
fed16f06 561 /* TODO: Need to cancel and free any queued up transfers. */
a1bb33af
UH
562}
563
5c2d46d1 564struct sr_device_plugin zeroplus_logic_cube_plugin_info = {
a1bb33af 565 "zeroplus-logic-cube",
9f8274a5 566 "Zeroplus Logic Cube LAP-C series",
a1bb33af
UH
567 1,
568 hw_init,
569 hw_cleanup,
a1bb33af
UH
570 hw_opendev,
571 hw_closedev,
572 hw_get_device_info,
573 hw_get_status,
574 hw_get_capabilities,
575 hw_set_configuration,
576 hw_start_acquisition,
fed16f06 577 hw_stop_acquisition,
a1bb33af 578};