2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Sven Peter <sven@fail0verflow.com>
5 * Copyright (C) 2010 Haxx Enterprises <bushing@gmail.com>
8 * Redistribution and use in source and binary forms, with or
9 * without modification, are permitted provided that the following
12 * * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29 * THE POSSIBILITY OF SUCH DAMAGE.
34 #include <libsigrok/libsigrok.h>
35 #include "libsigrok-internal.h"
41 HARD_DATA_CHECK_SUM = 0x00,
49 FREQUENCY_REG0 = 0x30,
57 TRIGGER_STATUS0 = 0x40,
67 TRIGGER_COUNT0 = 0x50,
70 TRIGGER_LEVEL0 = 0x55,
75 RAMSIZE_TRIGGERBAR_ADDRESS0 = 0x60,
76 RAMSIZE_TRIGGERBAR_ADDRESS1,
77 RAMSIZE_TRIGGERBAR_ADDRESS2,
86 ENABLE_DELAY_TIME0 = 0x7a,
89 ENABLE_INSERT_DATA0 = 0x80,
96 TRIGGER_ADDRESS0 = 0x90,
104 STOP_ADDRESS0 = 0x9b,
108 READ_RAM_STATUS = 0xa0,
111 static int g_trigger_status[9] = { 0 };
112 static int g_trigger_count = 1;
113 static int g_filter_status[8] = { 0 };
114 static int g_filter_enable = 0;
116 static int g_freq_value = 1;
117 static int g_freq_scale = FREQ_SCALE_MHZ;
118 static int g_memory_size = MEMORY_SIZE_8K;
119 static int g_ramsize_triggerbar_addr = 2 * 1024;
120 static int g_triggerbar_addr = 0;
121 static int g_compression = COMPRESSION_NONE;
122 static int g_thresh = 0x31; /* 1.5V */
124 /* Maybe unk specifies an "endpoint" or "register" of sorts. */
125 static int analyzer_write_status(libusb_device_handle *devh, unsigned char unk,
129 return gl_reg_write(devh, START_STATUS, unk << 6 | flags);
133 static int __analyzer_set_freq(libusb_device_handle *devh, int freq, int scale)
135 int reg0 = 0, divisor = 0, reg2 = 0;
138 case FREQ_SCALE_MHZ: /* MHz */
139 if (freq >= 100 && freq <= 200) {
145 if (freq >= 50 && freq < 100) {
151 if (freq >= 10 && freq < 50) {
164 if (freq >= 2 && freq < 10) {
180 case FREQ_SCALE_HZ: /* Hz */
181 if (freq >= 500 && freq < 1000) {
187 if (freq >= 300 && freq < 500) {
188 reg0 = freq * 0.005 * 8;
193 if (freq >= 100 && freq < 300) {
194 reg0 = freq * 0.005 * 16;
203 case FREQ_SCALE_KHZ: /* kHz */
204 if (freq >= 500 && freq < 1000) {
210 if (freq >= 100 && freq < 500) {
216 if (freq >= 50 && freq < 100) {
222 if (freq >= 10 && freq < 50) {
234 if (freq >= 2 && freq < 10) {
251 sr_dbg("Setting samplerate regs (freq=%d, scale=%d): "
252 "reg0: %d, reg1: %d, reg2: %d, reg3: %d.",
253 freq, scale, divisor, reg0, 0x02, reg2);
255 if (gl_reg_write(devh, FREQUENCY_REG0, divisor) < 0)
256 return -1; /* Divisor maybe? */
258 if (gl_reg_write(devh, FREQUENCY_REG1, reg0) < 0)
259 return -1; /* 10 / 0.2 */
261 if (gl_reg_write(devh, FREQUENCY_REG2, 0x02) < 0)
262 return -1; /* Always 2 */
264 if (gl_reg_write(devh, FREQUENCY_REG4, reg2) < 0)
273 * FREQUENCT_REG0 - division factor (?)
274 * FREQUENCT_REG1 - multiplication factor (?)
275 * FREQUENCT_REG4 - clock selection (?)
278 * 0 10MHz 16 1MHz 32 100kHz 48 10kHz 64 1kHz
279 * 1 5MHz 17 500kHz 33 50kHz 49 5kHz 65 500Hz
280 * 2 2.5MHz . . 50 2.5kHz 66 250Hz
284 static int __analyzer_set_freq(libusb_device_handle *devh, int freq, int scale)
294 static const struct freq_factor f[] = {
295 { 200, FREQ_SCALE_MHZ, 0, 1, 20 },
296 { 150, FREQ_SCALE_MHZ, 0, 1, 15 },
297 { 100, FREQ_SCALE_MHZ, 0, 1, 10 },
298 { 80, FREQ_SCALE_MHZ, 0, 2, 16 },
299 { 50, FREQ_SCALE_MHZ, 0, 2, 10 },
300 { 25, FREQ_SCALE_MHZ, 1, 5, 25 },
301 { 10, FREQ_SCALE_MHZ, 1, 5, 10 },
302 { 1, FREQ_SCALE_MHZ, 16, 5, 5 },
303 { 800, FREQ_SCALE_KHZ, 17, 5, 8 },
304 { 400, FREQ_SCALE_KHZ, 32, 5, 20 },
305 { 200, FREQ_SCALE_KHZ, 32, 5, 10 },
306 { 100, FREQ_SCALE_KHZ, 32, 5, 5 },
307 { 50, FREQ_SCALE_KHZ, 33, 5, 5 },
308 { 25, FREQ_SCALE_KHZ, 49, 5, 25 },
309 { 5, FREQ_SCALE_KHZ, 50, 5, 10 },
310 { 1, FREQ_SCALE_KHZ, 64, 5, 5 },
311 { 500, FREQ_SCALE_HZ, 64, 10, 5 },
312 { 100, FREQ_SCALE_HZ, 68, 5, 8 },
318 for (i = 0; f[i].freq; i++) {
319 if (scale == f[i].scale && freq == f[i].freq)
325 sr_dbg("Setting samplerate regs (freq=%d, scale=%d): "
326 "reg0: %d, reg1: %d, reg2: %d, reg3: %d.",
327 freq, scale, f[i].div, f[i].mul, 0x02, f[i].sel);
329 if (gl_reg_write(devh, FREQUENCY_REG0, f[i].div) < 0)
332 if (gl_reg_write(devh, FREQUENCY_REG1, f[i].mul) < 0)
335 if (gl_reg_write(devh, FREQUENCY_REG2, 0x02) < 0)
338 if (gl_reg_write(devh, FREQUENCY_REG4, f[i].sel) < 0)
344 static void __analyzer_set_ramsize_trigger_address(libusb_device_handle *devh,
345 unsigned int address)
347 gl_reg_write(devh, RAMSIZE_TRIGGERBAR_ADDRESS0, (address >> 0) & 0xFF);
348 gl_reg_write(devh, RAMSIZE_TRIGGERBAR_ADDRESS1, (address >> 8) & 0xFF);
349 gl_reg_write(devh, RAMSIZE_TRIGGERBAR_ADDRESS2, (address >> 16) & 0xFF);
352 static void __analyzer_set_triggerbar_address(libusb_device_handle *devh,
353 unsigned int address)
355 gl_reg_write(devh, TRIGGERBAR_ADDRESS0, (address >> 0) & 0xFF);
356 gl_reg_write(devh, TRIGGERBAR_ADDRESS1, (address >> 8) & 0xFF);
357 gl_reg_write(devh, TRIGGERBAR_ADDRESS2, (address >> 16) & 0xFF);
360 static void __analyzer_set_compression(libusb_device_handle *devh,
363 gl_reg_write(devh, COMPRESSION_TYPE0, (type >> 0) & 0xFF);
364 gl_reg_write(devh, COMPRESSION_TYPE1, (type >> 8) & 0xFF);
367 static void __analyzer_set_trigger_count(libusb_device_handle *devh,
370 gl_reg_write(devh, TRIGGER_COUNT0, (count >> 0) & 0xFF);
371 gl_reg_write(devh, TRIGGER_COUNT1, (count >> 8) & 0xFF);
374 static void analyzer_write_enable_insert_data(libusb_device_handle *devh)
376 gl_reg_write(devh, ENABLE_INSERT_DATA0, 0x12);
377 gl_reg_write(devh, ENABLE_INSERT_DATA1, 0x34);
378 gl_reg_write(devh, ENABLE_INSERT_DATA2, 0x56);
379 gl_reg_write(devh, ENABLE_INSERT_DATA3, 0x78);
382 static void analyzer_set_filter(libusb_device_handle *devh)
385 gl_reg_write(devh, FILTER_ENABLE, g_filter_enable);
386 for (i = 0; i < 8; i++)
387 gl_reg_write(devh, FILTER_STATUS + i, g_filter_status[i]);
390 SR_PRIV void analyzer_reset(libusb_device_handle *devh)
392 analyzer_write_status(devh, 3, STATUS_FLAG_NONE); // reset device
393 analyzer_write_status(devh, 3, STATUS_FLAG_RESET); // reset device
396 SR_PRIV void analyzer_initialize(libusb_device_handle *devh)
398 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
399 analyzer_write_status(devh, 1, STATUS_FLAG_INIT);
400 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
403 SR_PRIV void analyzer_wait(libusb_device_handle *devh, int set, int unset)
408 status = gl_reg_read(devh, DEV_STATUS);
409 if ((!set || (status & set)) && ((status & unset) == 0))
414 SR_PRIV void analyzer_read_start(libusb_device_handle *devh)
416 analyzer_write_status(devh, 3, STATUS_FLAG_20 | STATUS_FLAG_READ);
418 /* Prep for bulk reads */
419 gl_reg_read_buf(devh, READ_RAM_STATUS, NULL, 0);
422 SR_PRIV int analyzer_read_data(libusb_device_handle *devh, void *buffer,
425 return gl_read_bulk(devh, buffer, size);
428 SR_PRIV void analyzer_read_stop(libusb_device_handle *devh)
430 analyzer_write_status(devh, 3, STATUS_FLAG_20);
431 analyzer_write_status(devh, 3, STATUS_FLAG_NONE);
434 SR_PRIV void analyzer_start(libusb_device_handle *devh)
436 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
437 analyzer_write_status(devh, 1, STATUS_FLAG_INIT);
438 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
439 analyzer_write_status(devh, 1, STATUS_FLAG_GO);
442 SR_PRIV void analyzer_configure(libusb_device_handle *devh)
446 /* Write_Start_Status */
447 analyzer_write_status(devh, 1, STATUS_FLAG_RESET);
448 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
450 /* Start_Config_Outside_Device ? */
451 analyzer_write_status(devh, 1, STATUS_FLAG_INIT);
452 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
454 /* SetData_To_Frequence_Reg */
455 __analyzer_set_freq(devh, g_freq_value, g_freq_scale);
457 /* SetMemory_Length */
458 gl_reg_write(devh, MEMORY_LENGTH, g_memory_size);
460 /* Sele_Inside_Outside_Clock */
461 gl_reg_write(devh, CLOCK_SOURCE, 0x03);
463 /* Set_Trigger_Status */
464 for (i = 0; i < 9; i++)
465 gl_reg_write(devh, TRIGGER_STATUS0 + i, g_trigger_status[i]);
467 __analyzer_set_trigger_count(devh, g_trigger_count);
469 /* Set_Trigger_Level */
470 gl_reg_write(devh, TRIGGER_LEVEL0, g_thresh);
471 gl_reg_write(devh, TRIGGER_LEVEL1, g_thresh);
472 gl_reg_write(devh, TRIGGER_LEVEL2, g_thresh);
473 gl_reg_write(devh, TRIGGER_LEVEL3, g_thresh);
475 /* Size of actual memory >> 2 */
476 __analyzer_set_ramsize_trigger_address(devh, g_ramsize_triggerbar_addr);
477 __analyzer_set_triggerbar_address(devh, g_triggerbar_addr);
479 /* Set_Dont_Care_TriggerBar */
480 gl_reg_write(devh, DONT_CARE_TRIGGERBAR, 0x01);
483 analyzer_set_filter(devh);
485 /* Set_Enable_Delay_Time */
486 gl_reg_write(devh, 0x7a, 0x00);
487 gl_reg_write(devh, 0x7b, 0x00);
488 analyzer_write_enable_insert_data(devh);
489 __analyzer_set_compression(devh, g_compression);
492 SR_PRIV int analyzer_add_triggers(const struct sr_dev_inst *sdi)
494 struct dev_context *devc;
495 struct sr_trigger *trigger;
496 struct sr_trigger_stage *stage;
497 struct sr_trigger_match *match;
503 if (!(trigger = sr_session_trigger_get(sdi->session)))
506 for (l = trigger->stages; l; l = l->next) {
508 for (m = stage->matches; m; m = m->next) {
511 if (!match->channel->enabled)
512 /* Ignore disabled channels with a trigger. */
514 channel = match->channel->index;
515 switch (match->match) {
516 case SR_TRIGGER_ZERO:
517 g_trigger_status[channel / 4] |= 2 << (channel % 4 * 2);
520 g_trigger_status[channel / 4] |= 1 << (channel % 4 * 2);
523 sr_err("Unsupported match %d", match->match);
532 SR_PRIV void analyzer_add_filter(int channel, int type)
536 if (type != FILTER_HIGH && type != FILTER_LOW)
538 if ((channel & 0xf) >= 8)
541 if (channel & CHANNEL_A)
543 else if (channel & CHANNEL_B)
545 else if (channel & CHANNEL_C)
547 else if (channel & CHANNEL_D)
552 if ((channel & 0xf) >= 4) {
557 g_filter_status[i] |=
558 1 << ((2 * channel) + (type == FILTER_LOW ? 1 : 0));
563 SR_PRIV void analyzer_set_trigger_count(int count)
565 g_trigger_count = count;
568 SR_PRIV void analyzer_set_freq(int freq, int scale)
571 g_freq_scale = scale;
574 SR_PRIV void analyzer_set_memory_size(unsigned int size)
576 g_memory_size = size;
579 SR_PRIV void analyzer_set_ramsize_trigger_address(unsigned int address)
581 g_ramsize_triggerbar_addr = address;
584 SR_PRIV unsigned int analyzer_get_ramsize_trigger_address(void)
586 return g_ramsize_triggerbar_addr;
589 SR_PRIV void analyzer_set_triggerbar_address(unsigned int address)
591 g_triggerbar_addr = address;
594 SR_PRIV unsigned int analyzer_get_triggerbar_address(void)
596 return g_triggerbar_addr;
599 SR_PRIV unsigned int analyzer_read_status(libusb_device_handle *devh)
601 return gl_reg_read(devh, DEV_STATUS);
604 SR_PRIV unsigned int analyzer_read_id(libusb_device_handle *devh)
606 return gl_reg_read(devh, DEV_ID1) << 8 | gl_reg_read(devh, DEV_ID0);
609 SR_PRIV unsigned int analyzer_get_stop_address(libusb_device_handle *devh)
611 return gl_reg_read(devh, STOP_ADDRESS2) << 16 | gl_reg_read(devh,
612 STOP_ADDRESS1) << 8 | gl_reg_read(devh, STOP_ADDRESS0);
615 SR_PRIV unsigned int analyzer_get_now_address(libusb_device_handle *devh)
617 return gl_reg_read(devh, NOW_ADDRESS2) << 16 | gl_reg_read(devh,
618 NOW_ADDRESS1) << 8 | gl_reg_read(devh, NOW_ADDRESS0);
621 SR_PRIV unsigned int analyzer_get_trigger_address(libusb_device_handle *devh)
623 return gl_reg_read(devh, TRIGGER_ADDRESS2) << 16 | gl_reg_read(devh,
624 TRIGGER_ADDRESS1) << 8 | gl_reg_read(devh, TRIGGER_ADDRESS0);
627 SR_PRIV void analyzer_set_compression(unsigned int type)
629 g_compression = type;
632 SR_PRIV void analyzer_set_voltage_threshold(int thresh)
637 SR_PRIV void analyzer_wait_button(libusb_device_handle *devh)
639 analyzer_wait(devh, STATUS_BUTTON_PRESSED, 0);
642 SR_PRIV void analyzer_wait_data(libusb_device_handle *devh)
644 analyzer_wait(devh, 0, STATUS_BUSY);
647 SR_PRIV int analyzer_decompress(void *input, unsigned int input_len,
648 void *output, unsigned int output_len)
650 unsigned char *in = input;
651 unsigned char *out = output;
652 unsigned int A, B, C, count;
653 unsigned int written = 0;
655 while (input_len > 0) {
661 if (count > output_len)
670 *out++ = 0; /* Channel D */