]> sigrok.org Git - libsigrok.git/blob - hardware/zeroplus-logic-cube/protocol.c
zeroplus: Only report supported samplerates.
[libsigrok.git] / hardware / zeroplus-logic-cube / protocol.c
1 /*
2  * This file is part of the sigrok project.
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
20 #include "protocol.h"
21
22 SR_PRIV unsigned int get_memory_size(int type)
23 {
24         if (type == MEMORY_SIZE_8K)
25                 return 8 * 1024;
26         else if (type == MEMORY_SIZE_64K)
27                 return 64 * 1024;
28         else if (type == MEMORY_SIZE_128K)
29                 return 128 * 1024;
30         else if (type == MEMORY_SIZE_512K)
31                 return 512 * 1024;
32         else
33                 return 0;
34 }
35
36 SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate)
37 {
38         int i;
39
40         for (i = 0; zp_supported_samplerates_200[i]; i++)
41                 if (samplerate == zp_supported_samplerates_200[i])
42                         break;
43
44         if (!zp_supported_samplerates_200[i] || samplerate > devc->max_samplerate) {
45                 sr_err("Unsupported samplerate: %" PRIu64 "Hz.", samplerate);
46                 return SR_ERR_ARG;
47         }
48
49         sr_info("Setting samplerate to %" PRIu64 "Hz.", samplerate);
50
51         if (samplerate >= SR_MHZ(1))
52                 analyzer_set_freq(samplerate / SR_MHZ(1), FREQ_SCALE_MHZ);
53         else if (samplerate >= SR_KHZ(1))
54                 analyzer_set_freq(samplerate / SR_KHZ(1), FREQ_SCALE_KHZ);
55         else
56                 analyzer_set_freq(samplerate, FREQ_SCALE_HZ);
57
58         devc->cur_samplerate = samplerate;
59
60         return SR_OK;
61 }
62
63 SR_PRIV int set_limit_samples(struct dev_context *devc, uint64_t samples)
64 {
65         devc->limit_samples = samples;
66
67         if (samples <= 2 * 1024)
68                 devc->memory_size = MEMORY_SIZE_8K;
69         else if (samples <= 16 * 1024)
70                 devc->memory_size = MEMORY_SIZE_64K;
71         else if (samples <= 32 * 1024 || devc->max_memory_size <= 32 * 1024)
72                 devc->memory_size = MEMORY_SIZE_128K;
73         else
74                 devc->memory_size = MEMORY_SIZE_512K;
75
76         sr_info("Setting memory size to %dK.",
77                 get_memory_size(devc->memory_size) / 1024);
78
79         analyzer_set_memory_size(devc->memory_size);
80
81         return SR_OK;
82 }
83
84 SR_PRIV int set_capture_ratio(struct dev_context *devc, uint64_t ratio)
85 {
86         if (ratio > 100) {
87                 sr_err("Invalid capture ratio: %" PRIu64 ".", ratio);
88                 return SR_ERR_ARG;
89         }
90
91         devc->capture_ratio = ratio;
92
93         sr_info("Setting capture ratio to %d%%.", devc->capture_ratio);
94
95         return SR_OK;
96 }
97
98 SR_PRIV void set_triggerbar(struct dev_context *devc)
99 {
100         unsigned int ramsize, n, triggerbar;
101
102         ramsize = get_memory_size(devc->memory_size) / 4;
103         if (devc->trigger) {
104                 n = ramsize;
105                 if (devc->max_memory_size < n)
106                         n = devc->max_memory_size;
107                 if (devc->limit_samples < n)
108                         n = devc->limit_samples;
109                 n = n * devc->capture_ratio / 100;
110                 if (n > ramsize - 8)
111                         triggerbar = ramsize - 8;
112                 else
113                         triggerbar = n;
114         } else {
115                 triggerbar = 0;
116         }
117         analyzer_set_triggerbar_address(triggerbar);
118         analyzer_set_ramsize_trigger_address(ramsize - triggerbar);
119
120         sr_dbg("triggerbar_address = %d(0x%x)", triggerbar, triggerbar);
121         sr_dbg("ramsize_triggerbar_address = %d(0x%x)",
122                ramsize - triggerbar, ramsize - triggerbar);
123 }