2 Copyright (c) 2010 Sven Peter <sven@fail0verflow.com>
3 Copyright (c) 2010 Haxx Enterprises <bushing@gmail.com>
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 THE POSSIBILITY OF SUCH DAMAGE.
36 HARD_DATA_CHECK_SUM = 0x00,
44 FREQUENCY_REG0 = 0x30,
52 TRIGGER_STATUS0 = 0x40,
62 TRIGGER_COUNT0 = 0x50,
65 TRIGGER_LEVEL0 = 0x55,
70 RAMSIZE_TRIGGERBAR_ADDRESS0 = 0x60,
71 RAMSIZE_TRIGGERBAR_ADDRESS1,
72 RAMSIZE_TRIGGERBAR_ADDRESS2,
81 ENABLE_DELAY_TIME0 = 0x7A,
84 ENABLE_INSERT_DATA0 = 0x80,
91 TRIGGER_ADDRESS0 = 0x90,
103 READ_RAM_STATUS = 0xA0
106 static int g_trigger_status[9] = {0};
107 static int g_trigger_count = 1;
109 static int g_filter_status[8] = {0};
110 static int g_filter_enable = 0;
112 static int g_freq_value = 100;
113 static int g_freq_scale = FREQ_SCALE_MHZ;
114 static int g_memory_size = MEMORY_SIZE_512K;
115 static int g_ramsize_triggerbar_addr = 0x80000>>2;
116 static int g_triggerbar_addr = 0x3fe;
117 static int g_compression = COMPRESSION_NONE;
119 // maybe unk specifies an "endpoint" or "register" of sorts
120 static int analyzer_write_status(libusb_device_handle *devh, unsigned char unk, unsigned char flags)
123 return gl_reg_write(devh, START_STATUS, unk << 6 | flags);
126 static int __analyzer_set_freq(libusb_device_handle *devh, int freq, int scale)
128 int reg0=0, divisor=0, reg2=0;
130 case FREQ_SCALE_MHZ: // MHz
131 if (freq >= 100 && freq <= 200) {
137 if (freq >= 50 && freq < 100) {
143 if (freq >= 10 && freq < 50) {
156 if (freq >= 2 && freq < 10) {
172 case FREQ_SCALE_HZ: // Hz
173 if (freq >= 500 && freq < 1000) {
179 if (freq >= 300 && freq < 500) {
180 reg0 = freq * 0.005 * 8;
185 if (freq >= 100 && freq < 300) {
186 reg0 = freq * 0.005 * 16;
195 case FREQ_SCALE_KHZ: // KHz
196 if (freq >= 500 && freq < 1000) {
202 if (freq >= 100 && freq < 500) {
208 if (freq >= 50 && freq < 100) {
214 if (freq >= 10 && freq < 50) {
226 if (freq >= 2 && freq < 10) {
242 if (gl_reg_write(devh, FREQUENCY_REG0, divisor) < 0)
243 return -1; // divisor maybe?
245 if (gl_reg_write(devh, FREQUENCY_REG1, reg0) < 0)
246 return -1; // 10 / 0.2
248 if (gl_reg_write(devh, FREQUENCY_REG2, 0x02) < 0)
249 return -1; // always 2
251 if (gl_reg_write(devh, FREQUENCY_REG4, reg2) < 0)
257 static void __analyzer_set_ramsize_trigger_address(libusb_device_handle *devh, unsigned int address)
259 gl_reg_write(devh, RAMSIZE_TRIGGERBAR_ADDRESS0, (address >> 0) & 0xFF);
260 gl_reg_write(devh, RAMSIZE_TRIGGERBAR_ADDRESS1, (address >> 8) & 0xFF);
261 gl_reg_write(devh, RAMSIZE_TRIGGERBAR_ADDRESS2, (address >> 16) & 0xFF);
264 static void __analyzer_set_triggerbar_address(libusb_device_handle *devh, unsigned int address)
266 gl_reg_write(devh, TRIGGERBAR_ADDRESS0, (address >> 0) & 0xFF);
267 gl_reg_write(devh, TRIGGERBAR_ADDRESS1, (address >> 8) & 0xFF);
268 gl_reg_write(devh, TRIGGERBAR_ADDRESS2, (address >> 16) & 0xFF);
271 static void __analyzer_set_compression(libusb_device_handle *devh, unsigned int type)
273 gl_reg_write(devh, COMPRESSION_TYPE0, (type >> 0) & 0xFF);
274 gl_reg_write(devh, COMPRESSION_TYPE1, (type >> 8) & 0xFF);
277 static void __analyzer_set_trigger_count(libusb_device_handle *devh, unsigned int count)
279 gl_reg_write(devh, TRIGGER_COUNT0, (count >> 0) & 0xFF);
280 gl_reg_write(devh, TRIGGER_COUNT1, (count >> 8) & 0xFF);
283 static void analyzer_write_enable_insert_data(libusb_device_handle *devh)
285 gl_reg_write(devh, ENABLE_INSERT_DATA0, 0x12);
286 gl_reg_write(devh, ENABLE_INSERT_DATA1, 0x34);
287 gl_reg_write(devh, ENABLE_INSERT_DATA2, 0x56);
288 gl_reg_write(devh, ENABLE_INSERT_DATA3, 0x78);
291 static void analyzer_set_filter(libusb_device_handle *devh)
294 gl_reg_write(devh, FILTER_ENABLE, g_filter_enable);
295 for (i = 0; i < 8; i++)
296 gl_reg_write(devh, FILTER_STATUS + i, g_filter_status[i]);
299 void analyzer_reset(libusb_device_handle *devh)
301 analyzer_write_status(devh, 3, STATUS_FLAG_NONE); // reset device
302 analyzer_write_status(devh, 3, STATUS_FLAG_RESET); // reset device
305 void analyzer_initialize(libusb_device_handle *devh)
307 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
308 analyzer_write_status(devh, 1, STATUS_FLAG_INIT);
309 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
312 void analyzer_wait(libusb_device_handle *devh, int set, int unset)
316 status = gl_reg_read(devh, DEVICE_STATUS);
317 if ((status & set) && ((status & unset) == 0))
322 void analyzer_read_start(libusb_device_handle *devh)
326 analyzer_write_status(devh, 3, STATUS_FLAG_20 | STATUS_FLAG_READ);
328 for (i = 0; i < 8; i++)
329 (void)gl_reg_read(devh, READ_RAM_STATUS);
332 int analyzer_read_data(libusb_device_handle *devh, void *buffer, unsigned int size)
334 return gl_read_bulk(devh, buffer, size);
337 void analyzer_read_stop(libusb_device_handle *devh)
339 analyzer_write_status(devh, 3, STATUS_FLAG_20);
340 analyzer_write_status(devh, 3, STATUS_FLAG_NONE);
343 void analyzer_start(libusb_device_handle *devh)
345 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
346 analyzer_write_status(devh, 1, STATUS_FLAG_INIT);
347 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
348 analyzer_write_status(devh, 1, STATUS_FLAG_GO);
351 void analyzer_configure(libusb_device_handle *devh)
353 // Write_Start_Status
354 analyzer_write_status(devh, 1, STATUS_FLAG_RESET);
355 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
357 // Start_Config_Outside_Device ?
358 analyzer_write_status(devh, 1, STATUS_FLAG_INIT);
359 analyzer_write_status(devh, 1, STATUS_FLAG_NONE);
361 //SetData_To_Frequence_Reg
362 __analyzer_set_freq(devh, g_freq_value, g_freq_scale);
365 gl_reg_write(devh, MEMORY_LENGTH, g_memory_size);
367 //Sele_Inside_Outside_Clock
368 gl_reg_write(devh, CLOCK_SOURCE, 0x03);
372 for (i = 0; i < 9; i++)
373 gl_reg_write(devh, TRIGGER_STATUS0 + i, g_trigger_status[i]);
375 __analyzer_set_trigger_count(devh, g_trigger_count);
378 gl_reg_write(devh, TRIGGER_LEVEL0, 0x31);
379 gl_reg_write(devh, TRIGGER_LEVEL1, 0x31);
380 gl_reg_write(devh, TRIGGER_LEVEL2, 0x31);
381 gl_reg_write(devh, TRIGGER_LEVEL3, 0x31);
383 __analyzer_set_ramsize_trigger_address(devh, g_ramsize_triggerbar_addr); // size of actual memory >> 2
384 __analyzer_set_triggerbar_address(devh, g_triggerbar_addr);
386 //Set_Dont_Care_TriggerBar
387 gl_reg_write(devh, DONT_CARE_TRIGGERBAR, 0x01);
390 analyzer_set_filter(devh);
392 //Set_Enable_Delay_Time
393 gl_reg_write(devh, 0x7a, 0x00);
394 gl_reg_write(devh, 0x7b, 0x00);
395 analyzer_write_enable_insert_data(devh);
396 __analyzer_set_compression(devh,g_compression);
399 void analyzer_add_trigger(int channel, int type)
401 if ((channel & 0xf) >= 8)
404 if (type == TRIGGER_HIGH || type == TRIGGER_LOW) {
406 if (channel & CHANNEL_A)
408 else if (channel & CHANNEL_B)
410 else if (channel & CHANNEL_C)
412 else if (channel & CHANNEL_D)
416 if ((channel & 0xf) >= 4) {
420 g_trigger_status[i] |= 1 << ((2 * channel) + (type == TRIGGER_LOW ? 1 : 0));
422 if (type == TRIGGER_POSEDGE)
423 g_trigger_status[8] = 0x40;
424 else if(type == TRIGGER_NEGEDGE)
425 g_trigger_status[8] = 0x80;
427 g_trigger_status[8] = 0xc0;
429 // FIXME: just guessed the index; need to verify
430 if (channel & CHANNEL_B)
431 g_trigger_status[8] += 8;
432 else if (channel & CHANNEL_C)
433 g_trigger_status[8] += 16;
434 else if (channel & CHANNEL_D)
435 g_trigger_status[8] += 24;
436 g_trigger_status[8] += channel % 8;
440 void analyzer_add_filter(int channel, int type)
442 if (type != FILTER_HIGH && type != FILTER_LOW)
444 if ((channel & 0xf) >= 8)
448 if (channel & CHANNEL_A)
450 else if (channel & CHANNEL_B)
452 else if (channel & CHANNEL_C)
454 else if (channel & CHANNEL_D)
458 if ((channel & 0xf) >= 4) {
462 g_filter_status[i] |= 1 << ((2 * channel) + (type == FILTER_LOW ? 1 : 0));
466 void analyzer_set_trigger_count(int count)
468 g_trigger_count = count;
471 void analyzer_set_freq(int freq, int scale)
474 g_freq_scale = scale;
477 void analyzer_set_memory_size(unsigned int size)
479 g_memory_size = size;
483 void analyzer_set_ramsize_trigger_address(unsigned int address)
485 g_ramsize_triggerbar_addr = address;
488 void analyzer_set_triggerbar_address(unsigned int address)
490 g_triggerbar_addr = address;
493 unsigned int analyzer_read_id(libusb_device_handle *devh)
495 return gl_reg_read(devh, DEVICE_ID1) << 8 | gl_reg_read(devh, DEVICE_ID0);
498 unsigned int analyzer_get_stop_address(libusb_device_handle *devh)
500 return gl_reg_read(devh, STOP_ADDRESS2) << 16 | gl_reg_read(devh, STOP_ADDRESS1) << 8 | gl_reg_read(devh, STOP_ADDRESS0);
503 unsigned int analyzer_get_now_address(libusb_device_handle *devh)
505 return gl_reg_read(devh, NOW_ADDRESS2) << 16 | gl_reg_read(devh, NOW_ADDRESS1) << 8 | gl_reg_read(devh, NOW_ADDRESS0);
508 unsigned int analyzer_get_trigger_address(libusb_device_handle *devh)
510 return gl_reg_read(devh, TRIGGER_ADDRESS2) << 16 | gl_reg_read(devh, TRIGGER_ADDRESS1) << 8 | gl_reg_read(devh, TRIGGER_ADDRESS0);
513 void analyzer_set_compression(unsigned int type)
515 g_compression = type;
518 void analyzer_wait_button(libusb_device_handle *devh)
520 analyzer_wait(devh, STATUS_BUTTON_PRESSED, 0);
523 void analyzer_wait_data(libusb_device_handle *devh)
525 analyzer_wait(devh, STATUS_READY | 8, STATUS_BUSY);
528 int analyzer_decompress(void *input, unsigned int input_len, void *output, unsigned int output_len)
530 unsigned char *in = input;
531 unsigned char *out = output;
532 unsigned int A, B, C, count;
533 unsigned int written = 0;
535 while (input_len > 0) {
541 if (count > output_len)
550 *out++ = 0; // channel D