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