]> sigrok.org Git - libsigrok.git/blame - src/hardware/zeroplus-logic-cube/protocol.c
korad-kaxxxxp: use ID text prefix with optional version for RND models
[libsigrok.git] / src / hardware / zeroplus-logic-cube / protocol.c
CommitLineData
58c5f2ed 1/*
50985c20 2 * This file is part of the libsigrok project.
58c5f2ed
UH
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
6ec6c43b 20#include <config.h>
7142d6b9 21#include <math.h>
58c5f2ed
UH
22#include "protocol.h"
23
24SR_PRIV unsigned int get_memory_size(int type)
25{
26 if (type == MEMORY_SIZE_8K)
1a46cc62 27 return (8 * 1024);
3e43da1f
RD
28 else if (type <= MEMORY_SIZE_8M)
29 return (32 * 1024) << type;
58c5f2ed
UH
30 else
31 return 0;
32}
33
d87c1766 34static int clz(unsigned int x)
3e43da1f
RD
35{
36 int n = 0;
37 if (x == 0)
38 return 32;
39 if (!(x & 0xFFFF0000)) {
40 n = n + 16;
41 x = x << 16;
42 }
43 if (!(x & 0xFF000000)) {
44 n = n + 8;
45 x = x << 8;
46 }
47 if (!(x & 0xF0000000)) {
48 n = n + 4;
49 x = x << 4;
50 }
51 if (!(x & 0xC0000000)) {
52 n = n + 2;
53 x = x << 2;
54 }
55 if (!(x & 0x80000000))
56 n = n + 1;
57 return n;
58}
59
58c5f2ed
UH
60SR_PRIV int set_limit_samples(struct dev_context *devc, uint64_t samples)
61{
c38e64c7
RD
62 if (samples > devc->max_sample_depth)
63 samples = devc->max_sample_depth;
64
58c5f2ed
UH
65 devc->limit_samples = samples;
66
1a46cc62 67 if (samples <= (2 * 1024))
58c5f2ed 68 devc->memory_size = MEMORY_SIZE_8K;
1a46cc62 69 else if (samples <= (16 * 1024))
58c5f2ed 70 devc->memory_size = MEMORY_SIZE_64K;
58c5f2ed 71 else
3e43da1f 72 devc->memory_size = 19 - clz(samples - 1);
58c5f2ed
UH
73
74 sr_info("Setting memory size to %dK.",
75 get_memory_size(devc->memory_size) / 1024);
76
77 analyzer_set_memory_size(devc->memory_size);
78
79 return SR_OK;
80}
81
7142d6b9
RD
82SR_PRIV int set_voltage_threshold(struct dev_context *devc, double thresh)
83{
84 if (thresh > 6.0)
85 thresh = 6.0;
86 if (thresh < -6.0)
87 thresh = -6.0;
88
89 devc->cur_threshold = thresh;
90
91 analyzer_set_voltage_threshold((int) round(-9.1*thresh + 62.6));
92
93 sr_info("Setting voltage threshold to %fV.", devc->cur_threshold);
94
95 return SR_OK;
96}
97
58c5f2ed
UH
98SR_PRIV void set_triggerbar(struct dev_context *devc)
99{
b9a5614d
RD
100 unsigned int trigger_depth, triggerbar, ramsize_trigger;
101
102 trigger_depth = get_memory_size(devc->memory_size) / 4;
103 if (devc->limit_samples < trigger_depth)
104 trigger_depth = devc->limit_samples;
4c1433d1
RD
105
106 if (devc->trigger)
efad7ccc 107 triggerbar = (trigger_depth * devc->capture_ratio) / 100;
4c1433d1
RD
108 else
109 triggerbar = 0;
b9a5614d
RD
110
111 ramsize_trigger = trigger_depth - triggerbar;
112 /* Matches USB packet captures from official app/driver */
113 if (triggerbar > 2)
114 triggerbar -= 2;
115 else {
116 ramsize_trigger -= 1;
58c5f2ed
UH
117 triggerbar = 0;
118 }
b9a5614d 119
58c5f2ed 120 analyzer_set_triggerbar_address(triggerbar);
b9a5614d 121 analyzer_set_ramsize_trigger_address(ramsize_trigger);
58c5f2ed
UH
122
123 sr_dbg("triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
124 sr_dbg("ramsize_triggerbar_address = %d(0x%x)",
b9a5614d 125 ramsize_trigger, ramsize_trigger);
58c5f2ed 126}