]>
Commit | Line | Data |
---|---|---|
6239c175 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
6239c175 UH |
3 | * |
4 | * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de> | |
fc96e6f8 | 5 | * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com> |
c216d623 | 6 | * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com> |
7a8a1aba | 7 | * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com> |
6239c175 UH |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
2ea1fdf1 | 20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
6239c175 UH |
21 | */ |
22 | ||
6ec6c43b | 23 | #include <config.h> |
6239c175 UH |
24 | #include <stdlib.h> |
25 | #include <string.h> | |
4374219b | 26 | #include <math.h> |
c1aae900 | 27 | #include <libsigrok/libsigrok.h> |
45c59c8b | 28 | #include "libsigrok-internal.h" |
ba508e22 | 29 | #include "protocol.h" |
92bcedf6 | 30 | |
8430c9b6 FS |
31 | #define DEFAULT_NUM_LOGIC_CHANNELS 8 |
32 | #define DEFAULT_LOGIC_PATTERN PATTERN_SIGROK | |
c03ed397 | 33 | |
8430c9b6 FS |
34 | #define DEFAULT_NUM_ANALOG_CHANNELS 4 |
35 | #define DEFAULT_ANALOG_ENCODING_DIGITS 4 | |
36 | #define DEFAULT_ANALOG_SPEC_DIGITS 4 | |
37 | #define DEFAULT_ANALOG_AMPLITUDE 10 | |
85b5af06 | 38 | |
77463bd3 | 39 | /* Note: No spaces allowed because of sigrok-cli. */ |
8b2d41ed | 40 | static const char *logic_pattern_str[] = { |
61c39f54 BV |
41 | "sigrok", |
42 | "random", | |
43 | "incremental", | |
77463bd3 SA |
44 | "walking-one", |
45 | "walking-zero", | |
61c39f54 BV |
46 | "all-low", |
47 | "all-high", | |
81d53a29 | 48 | "squid", |
03733430 | 49 | "graycode", |
61c39f54 BV |
50 | }; |
51 | ||
55fb76b3 UH |
52 | static const uint32_t scanopts[] = { |
53 | SR_CONF_NUM_LOGIC_CHANNELS, | |
54 | SR_CONF_NUM_ANALOG_CHANNELS, | |
53ea2461 | 55 | SR_CONF_LIMIT_FRAMES, |
55fb76b3 UH |
56 | }; |
57 | ||
1e4a7cac BV |
58 | static const uint32_t drvopts[] = { |
59 | SR_CONF_DEMO_DEV, | |
60 | SR_CONF_LOGIC_ANALYZER, | |
61 | SR_CONF_OSCILLOSCOPE, | |
62 | }; | |
63 | ||
390795c0 | 64 | static const uint32_t devopts[] = { |
e91bb0a6 | 65 | SR_CONF_CONTINUOUS, |
5827f61b BV |
66 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
67 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
fb193945 | 68 | SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET, |
390795c0 | 69 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
7a8a1aba BG |
70 | SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET, |
71 | SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
31f69b09 | 72 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, |
73 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
390795c0 BV |
74 | }; |
75 | ||
76 | static const uint32_t devopts_cg_logic[] = { | |
f12d9979 | 77 | SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
49224c28 BV |
78 | }; |
79 | ||
e2b99f04 SA |
80 | static const uint32_t devopts_cg_analog_group[] = { |
81 | SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET, | |
82 | }; | |
83 | ||
84 | static const uint32_t devopts_cg_analog_channel[] = { | |
f12d9979 BV |
85 | SR_CONF_PATTERN_MODE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
86 | SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET, | |
7a1da331 BV |
87 | }; |
88 | ||
31f69b09 | 89 | static const int32_t trigger_matches[] = { |
90 | SR_TRIGGER_ZERO, | |
91 | SR_TRIGGER_ONE, | |
92 | SR_TRIGGER_RISING, | |
93 | SR_TRIGGER_FALLING, | |
94 | SR_TRIGGER_EDGE, | |
95 | }; | |
96 | ||
d00088ca BV |
97 | static const uint64_t samplerates[] = { |
98 | SR_HZ(1), | |
99 | SR_GHZ(1), | |
100 | SR_HZ(1), | |
4bfbf9fc BV |
101 | }; |
102 | ||
4f840ce9 | 103 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
6239c175 | 104 | { |
33ef7573 | 105 | struct dev_context *devc; |
c07f60e7 | 106 | struct sr_dev_inst *sdi; |
ba7dd8bb | 107 | struct sr_channel *ch; |
49224c28 | 108 | struct sr_channel_group *cg, *acg; |
c07f60e7 | 109 | struct sr_config *src; |
8b2d41ed | 110 | struct analog_gen *ag; |
43376f33 | 111 | GSList *l; |
ba7dd8bb | 112 | int num_logic_channels, num_analog_channels, pattern, i; |
53ea2461 | 113 | uint64_t limit_frames; |
ba7dd8bb | 114 | char channel_name[16]; |
067d0716 | 115 | |
3f239f08 UH |
116 | num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS; |
117 | num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS; | |
53ea2461 | 118 | limit_frames = DEFAULT_LIMIT_FRAMES; |
c07f60e7 BV |
119 | for (l = options; l; l = l->next) { |
120 | src = l->data; | |
121 | switch (src->key) { | |
3f239f08 | 122 | case SR_CONF_NUM_LOGIC_CHANNELS: |
ba7dd8bb | 123 | num_logic_channels = g_variant_get_int32(src->data); |
c07f60e7 | 124 | break; |
3f239f08 | 125 | case SR_CONF_NUM_ANALOG_CHANNELS: |
ba7dd8bb | 126 | num_analog_channels = g_variant_get_int32(src->data); |
c07f60e7 | 127 | break; |
53ea2461 GS |
128 | case SR_CONF_LIMIT_FRAMES: |
129 | limit_frames = g_variant_get_uint64(src->data); | |
130 | break; | |
c07f60e7 BV |
131 | } |
132 | } | |
85b5af06 | 133 | |
aac29cc1 | 134 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
45884333 | 135 | sdi->status = SR_ST_INACTIVE; |
4b664cd6 | 136 | sdi->model = g_strdup("Demo device"); |
e15f48c2 | 137 | |
a49a320d | 138 | devc = g_malloc0(sizeof(struct dev_context)); |
8b2d41ed | 139 | devc->cur_samplerate = SR_KHZ(200); |
ba7dd8bb UH |
140 | devc->num_logic_channels = num_logic_channels; |
141 | devc->logic_unitsize = (devc->num_logic_channels + 7) / 8; | |
015f0970 GS |
142 | devc->all_logic_channels_mask = 1UL << 0; |
143 | devc->all_logic_channels_mask <<= devc->num_logic_channels; | |
144 | devc->all_logic_channels_mask--; | |
b1e6eec6 | 145 | devc->logic_pattern = DEFAULT_LOGIC_PATTERN; |
ba7dd8bb | 146 | devc->num_analog_channels = num_analog_channels; |
53ea2461 | 147 | devc->limit_frames = limit_frames; |
6fc51fb1 | 148 | devc->capture_ratio = 20; |
149 | devc->stl = NULL; | |
8b2d41ed | 150 | |
f18e0db3 LPC |
151 | if (num_logic_channels > 0) { |
152 | /* Logic channels, all in one channel group. */ | |
153 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
154 | cg->name = g_strdup("Logic"); | |
155 | for (i = 0; i < num_logic_channels; i++) { | |
156 | sprintf(channel_name, "D%d", i); | |
f1c79a6a | 157 | ch = sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name); |
f18e0db3 LPC |
158 | cg->channels = g_slist_append(cg->channels, ch); |
159 | } | |
160 | sdi->channel_groups = g_slist_append(NULL, cg); | |
87ca93c5 BV |
161 | } |
162 | ||
ba7dd8bb | 163 | /* Analog channels, channel groups and pattern generators. */ |
d91d0b12 | 164 | devc->ch_ag = g_hash_table_new(g_direct_hash, g_direct_equal); |
f18e0db3 LPC |
165 | if (num_analog_channels > 0) { |
166 | pattern = 0; | |
167 | /* An "Analog" channel group with all analog channels in it. */ | |
168 | acg = g_malloc0(sizeof(struct sr_channel_group)); | |
169 | acg->name = g_strdup("Analog"); | |
170 | sdi->channel_groups = g_slist_append(sdi->channel_groups, acg); | |
171 | ||
f18e0db3 LPC |
172 | for (i = 0; i < num_analog_channels; i++) { |
173 | snprintf(channel_name, 16, "A%d", i); | |
174 | ch = sr_channel_new(sdi, i + num_logic_channels, SR_CHANNEL_ANALOG, | |
f1c79a6a | 175 | TRUE, channel_name); |
f18e0db3 LPC |
176 | acg->channels = g_slist_append(acg->channels, ch); |
177 | ||
178 | /* Every analog channel gets its own channel group as well. */ | |
179 | cg = g_malloc0(sizeof(struct sr_channel_group)); | |
180 | cg->name = g_strdup(channel_name); | |
181 | cg->channels = g_slist_append(NULL, ch); | |
182 | sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); | |
183 | ||
184 | /* Every channel gets a generator struct. */ | |
185 | ag = g_malloc(sizeof(struct analog_gen)); | |
01f2adb0 | 186 | ag->ch = ch; |
f18e0db3 | 187 | ag->amplitude = DEFAULT_ANALOG_AMPLITUDE; |
3be044aa | 188 | sr_analog_init(&ag->packet, &ag->encoding, &ag->meaning, &ag->spec, 2); |
4b770103 | 189 | ag->packet.meaning->channels = cg->channels; |
8430c9b6 FS |
190 | ag->packet.meaning->mq = SR_MQ_VOLTAGE; |
191 | ag->packet.meaning->mqflags = SR_MQFLAG_DC; | |
4b770103 | 192 | ag->packet.meaning->unit = SR_UNIT_VOLT; |
8430c9b6 FS |
193 | ag->packet.encoding->digits = DEFAULT_ANALOG_ENCODING_DIGITS; |
194 | ag->packet.spec->spec_digits = DEFAULT_ANALOG_SPEC_DIGITS; | |
f18e0db3 LPC |
195 | ag->packet.data = ag->pattern_data; |
196 | ag->pattern = pattern; | |
197 | ag->avg_val = 0.0f; | |
198 | ag->num_avgs = 0; | |
199 | g_hash_table_insert(devc->ch_ag, ch, ag); | |
200 | ||
201 | if (++pattern == ARRAY_SIZE(analog_pattern_str)) | |
202 | pattern = 0; | |
203 | } | |
33ef7573 | 204 | } |
33ef7573 JH |
205 | |
206 | sdi->priv = devc; | |
207 | ||
43376f33 | 208 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
6239c175 UH |
209 | } |
210 | ||
3553451f | 211 | static void clear_helper(struct dev_context *devc) |
ed0b7fed | 212 | { |
49224c28 BV |
213 | GHashTableIter iter; |
214 | void *value; | |
ed0b7fed | 215 | |
49224c28 BV |
216 | /* Analog generators. */ |
217 | g_hash_table_iter_init(&iter, devc->ch_ag); | |
218 | while (g_hash_table_iter_next(&iter, NULL, &value)) | |
219 | g_free(value); | |
220 | g_hash_table_unref(devc->ch_ag); | |
ed0b7fed BV |
221 | } |
222 | ||
6ab68731 | 223 | static int dev_clear(const struct sr_dev_driver *di) |
6239c175 | 224 | { |
3553451f | 225 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); |
6239c175 UH |
226 | } |
227 | ||
dd7a72ea UH |
228 | static int config_get(uint32_t key, GVariant **data, |
229 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
6239c175 | 230 | { |
c07f60e7 | 231 | struct dev_context *devc; |
ba7dd8bb | 232 | struct sr_channel *ch; |
2388ae86 BV |
233 | struct analog_gen *ag; |
234 | int pattern; | |
6f57fd96 | 235 | |
2388ae86 BV |
236 | if (!sdi) |
237 | return SR_ERR_ARG; | |
8f996b89 | 238 | |
c07f60e7 | 239 | devc = sdi->priv; |
584560f1 | 240 | switch (key) { |
123e1313 | 241 | case SR_CONF_SAMPLERATE: |
a7684294 | 242 | *data = g_variant_new_uint64(devc->cur_samplerate); |
6239c175 | 243 | break; |
2474d87e | 244 | case SR_CONF_LIMIT_SAMPLES: |
a7684294 | 245 | *data = g_variant_new_uint64(devc->limit_samples); |
2474d87e BV |
246 | break; |
247 | case SR_CONF_LIMIT_MSEC: | |
a7684294 | 248 | *data = g_variant_new_uint64(devc->limit_msec); |
2474d87e | 249 | break; |
fb193945 GS |
250 | case SR_CONF_LIMIT_FRAMES: |
251 | *data = g_variant_new_uint64(devc->limit_frames); | |
252 | break; | |
7a8a1aba BG |
253 | case SR_CONF_AVERAGING: |
254 | *data = g_variant_new_boolean(devc->avg); | |
255 | break; | |
256 | case SR_CONF_AVG_SAMPLES: | |
257 | *data = g_variant_new_uint64(devc->avg_samples); | |
258 | break; | |
2474d87e | 259 | case SR_CONF_PATTERN_MODE: |
53b4680f | 260 | if (!cg) |
660e398f | 261 | return SR_ERR_CHANNEL_GROUP; |
49224c28 | 262 | /* Any channel in the group will do. */ |
ba7dd8bb | 263 | ch = cg->channels->data; |
3f239f08 | 264 | if (ch->type == SR_CHANNEL_LOGIC) { |
2388ae86 BV |
265 | pattern = devc->logic_pattern; |
266 | *data = g_variant_new_string(logic_pattern_str[pattern]); | |
3f239f08 | 267 | } else if (ch->type == SR_CHANNEL_ANALOG) { |
49224c28 | 268 | ag = g_hash_table_lookup(devc->ch_ag, ch); |
2388ae86 BV |
269 | pattern = ag->pattern; |
270 | *data = g_variant_new_string(analog_pattern_str[pattern]); | |
271 | } else | |
272 | return SR_ERR_BUG; | |
c07f60e7 | 273 | break; |
dddabe37 BV |
274 | case SR_CONF_AMPLITUDE: |
275 | if (!cg) | |
276 | return SR_ERR_CHANNEL_GROUP; | |
49224c28 | 277 | /* Any channel in the group will do. */ |
dddabe37 BV |
278 | ch = cg->channels->data; |
279 | if (ch->type != SR_CHANNEL_ANALOG) | |
280 | return SR_ERR_ARG; | |
49224c28 | 281 | ag = g_hash_table_lookup(devc->ch_ag, ch); |
dddabe37 BV |
282 | *data = g_variant_new_double(ag->amplitude); |
283 | break; | |
31f69b09 | 284 | case SR_CONF_CAPTURE_RATIO: |
285 | *data = g_variant_new_uint64(devc->capture_ratio); | |
286 | break; | |
7dfcf010 | 287 | default: |
bd6fbf62 | 288 | return SR_ERR_NA; |
6239c175 UH |
289 | } |
290 | ||
dfb0fa1a | 291 | return SR_OK; |
6239c175 UH |
292 | } |
293 | ||
dd7a72ea UH |
294 | static int config_set(uint32_t key, GVariant *data, |
295 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
6239c175 | 296 | { |
8b2d41ed | 297 | struct dev_context *devc; |
4374219b | 298 | struct analog_gen *ag; |
ba7dd8bb | 299 | struct sr_channel *ch; |
49224c28 | 300 | GSList *l; |
a9010323 | 301 | int logic_pattern, analog_pattern; |
6239c175 | 302 | |
8b2d41ed | 303 | devc = sdi->priv; |
6239c175 | 304 | |
584560f1 | 305 | switch (key) { |
2388ae86 | 306 | case SR_CONF_SAMPLERATE: |
a7684294 | 307 | devc->cur_samplerate = g_variant_get_uint64(data); |
2388ae86 BV |
308 | break; |
309 | case SR_CONF_LIMIT_SAMPLES: | |
a7684294 JH |
310 | devc->limit_msec = 0; |
311 | devc->limit_samples = g_variant_get_uint64(data); | |
2388ae86 BV |
312 | break; |
313 | case SR_CONF_LIMIT_MSEC: | |
a7684294 JH |
314 | devc->limit_msec = g_variant_get_uint64(data); |
315 | devc->limit_samples = 0; | |
2388ae86 | 316 | break; |
fb193945 GS |
317 | case SR_CONF_LIMIT_FRAMES: |
318 | devc->limit_frames = g_variant_get_uint64(data); | |
319 | break; | |
7a8a1aba BG |
320 | case SR_CONF_AVERAGING: |
321 | devc->avg = g_variant_get_boolean(data); | |
322 | sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling"); | |
323 | break; | |
324 | case SR_CONF_AVG_SAMPLES: | |
325 | devc->avg_samples = g_variant_get_uint64(data); | |
326 | sr_dbg("Setting averaging rate to %" PRIu64, devc->avg_samples); | |
327 | break; | |
2388ae86 | 328 | case SR_CONF_PATTERN_MODE: |
53b4680f | 329 | if (!cg) |
660e398f | 330 | return SR_ERR_CHANNEL_GROUP; |
697fb6dd UH |
331 | logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str)); |
332 | analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str)); | |
333 | if (logic_pattern < 0 && analog_pattern < 0) | |
49224c28 BV |
334 | return SR_ERR_ARG; |
335 | for (l = cg->channels; l; l = l->next) { | |
336 | ch = l->data; | |
337 | if (ch->type == SR_CHANNEL_LOGIC) { | |
338 | if (logic_pattern == -1) | |
339 | return SR_ERR_ARG; | |
340 | sr_dbg("Setting logic pattern to %s", | |
341 | logic_pattern_str[logic_pattern]); | |
342 | devc->logic_pattern = logic_pattern; | |
343 | /* Might as well do this now, these are static. */ | |
344 | if (logic_pattern == PATTERN_ALL_LOW) | |
345 | memset(devc->logic_data, 0x00, LOGIC_BUFSIZE); | |
346 | else if (logic_pattern == PATTERN_ALL_HIGH) | |
347 | memset(devc->logic_data, 0xff, LOGIC_BUFSIZE); | |
348 | } else if (ch->type == SR_CHANNEL_ANALOG) { | |
349 | if (analog_pattern == -1) | |
350 | return SR_ERR_ARG; | |
351 | sr_dbg("Setting analog pattern for channel %s to %s", | |
352 | ch->name, analog_pattern_str[analog_pattern]); | |
353 | ag = g_hash_table_lookup(devc->ch_ag, ch); | |
354 | ag->pattern = analog_pattern; | |
355 | } else | |
356 | return SR_ERR_BUG; | |
357 | } | |
2388ae86 | 358 | break; |
dddabe37 BV |
359 | case SR_CONF_AMPLITUDE: |
360 | if (!cg) | |
361 | return SR_ERR_CHANNEL_GROUP; | |
49224c28 BV |
362 | for (l = cg->channels; l; l = l->next) { |
363 | ch = l->data; | |
364 | if (ch->type != SR_CHANNEL_ANALOG) | |
365 | return SR_ERR_ARG; | |
366 | ag = g_hash_table_lookup(devc->ch_ag, ch); | |
367 | ag->amplitude = g_variant_get_double(data); | |
368 | } | |
dddabe37 | 369 | break; |
31f69b09 | 370 | case SR_CONF_CAPTURE_RATIO: |
371 | devc->capture_ratio = g_variant_get_uint64(data); | |
372 | break; | |
2388ae86 | 373 | default: |
a9010323 | 374 | return SR_ERR_NA; |
6239c175 UH |
375 | } |
376 | ||
a9010323 | 377 | return SR_OK; |
6239c175 UH |
378 | } |
379 | ||
dd7a72ea UH |
380 | static int config_list(uint32_t key, GVariant **data, |
381 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
a1c743fc | 382 | { |
ba7dd8bb | 383 | struct sr_channel *ch; |
a1c743fc | 384 | |
53b4680f | 385 | if (!cg) { |
7a1da331 | 386 | switch (key) { |
e66d1892 | 387 | case SR_CONF_SCAN_OPTIONS: |
7a1da331 | 388 | case SR_CONF_DEVICE_OPTIONS: |
e66d1892 | 389 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
7a1da331 | 390 | case SR_CONF_SAMPLERATE: |
53012da6 | 391 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); |
7a1da331 | 392 | break; |
31f69b09 | 393 | case SR_CONF_TRIGGER_MATCH: |
394 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
395 | break; | |
7a1da331 BV |
396 | default: |
397 | return SR_ERR_NA; | |
398 | } | |
399 | } else { | |
ba7dd8bb | 400 | ch = cg->channels->data; |
7a1da331 BV |
401 | switch (key) { |
402 | case SR_CONF_DEVICE_OPTIONS: | |
49224c28 | 403 | if (ch->type == SR_CHANNEL_LOGIC) |
53012da6 | 404 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic)); |
e2b99f04 SA |
405 | else if (ch->type == SR_CHANNEL_ANALOG) { |
406 | if (strcmp(cg->name, "Analog") == 0) | |
53012da6 | 407 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group)); |
e2b99f04 | 408 | else |
53012da6 | 409 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel)); |
e2b99f04 | 410 | } |
49224c28 BV |
411 | else |
412 | return SR_ERR_BUG; | |
7a1da331 BV |
413 | break; |
414 | case SR_CONF_PATTERN_MODE: | |
e2b99f04 SA |
415 | /* The analog group (with all 4 channels) shall not have a pattern property. */ |
416 | if (strcmp(cg->name, "Analog") == 0) | |
417 | return SR_ERR_NA; | |
418 | ||
3f239f08 | 419 | if (ch->type == SR_CHANNEL_LOGIC) |
53012da6 | 420 | *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str)); |
3f239f08 | 421 | else if (ch->type == SR_CHANNEL_ANALOG) |
53012da6 | 422 | *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str)); |
2388ae86 BV |
423 | else |
424 | return SR_ERR_BUG; | |
7a1da331 BV |
425 | break; |
426 | default: | |
427 | return SR_ERR_NA; | |
428 | } | |
a1c743fc BV |
429 | } |
430 | ||
431 | return SR_OK; | |
432 | } | |
433 | ||
695dc859 | 434 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
6239c175 | 435 | { |
61c39f54 | 436 | struct dev_context *devc; |
1b7b72d4 GS |
437 | GSList *l; |
438 | struct sr_channel *ch; | |
4a465510 GS |
439 | int bitpos; |
440 | uint8_t mask; | |
49224c28 BV |
441 | GHashTableIter iter; |
442 | void *value; | |
6fc51fb1 | 443 | struct sr_trigger *trigger; |
85b5af06 | 444 | |
61c39f54 | 445 | devc = sdi->priv; |
a49a320d | 446 | devc->sent_samples = 0; |
767ca135 | 447 | devc->sent_frame_samples = 0; |
85b5af06 | 448 | |
d1078180 SA |
449 | /* Setup triggers */ |
450 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
451 | int pre_trigger_samples = 0; | |
452 | if (devc->limit_samples > 0) | |
453 | pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; | |
454 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); | |
455 | if (!devc->stl) | |
456 | return SR_ERR_MALLOC; | |
457 | ||
458 | /* Disable all analog channels since using them when there are logic | |
459 | * triggers set up would require having pre-trigger sample buffers | |
460 | * for analog sample data. | |
461 | */ | |
462 | for (l = sdi->channels; l; l = l->next) { | |
463 | ch = l->data; | |
464 | if (ch->type == SR_CHANNEL_ANALOG) | |
465 | ch->enabled = FALSE; | |
466 | } | |
467 | } | |
468 | devc->trigger_fired = FALSE; | |
469 | ||
4a465510 GS |
470 | /* |
471 | * Determine the numbers of logic and analog channels that are | |
472 | * involved in the acquisition. Determine an offset and a mask to | |
473 | * remove excess logic data content before datafeed submission. | |
474 | */ | |
1b7b72d4 GS |
475 | devc->enabled_logic_channels = 0; |
476 | devc->enabled_analog_channels = 0; | |
477 | for (l = sdi->channels; l; l = l->next) { | |
478 | ch = l->data; | |
479 | if (!ch->enabled) | |
480 | continue; | |
481 | if (ch->type == SR_CHANNEL_ANALOG) { | |
482 | devc->enabled_analog_channels++; | |
483 | continue; | |
484 | } | |
4a465510 | 485 | if (ch->type != SR_CHANNEL_LOGIC) |
1b7b72d4 | 486 | continue; |
4a465510 GS |
487 | /* |
488 | * TODO: Need we create a channel map here, such that the | |
489 | * session datafeed packets will have a dense representation | |
490 | * of the enabled channels' data? For example store channels | |
491 | * D3 and D5 in bit positions 0 and 1 respectively, when all | |
492 | * other channels are disabled? The current implementation | |
493 | * generates a sparse layout, might provide data for logic | |
494 | * channels that are disabled while it might suppress data | |
495 | * from enabled channels at the same time. | |
496 | */ | |
497 | devc->enabled_logic_channels++; | |
1b7b72d4 | 498 | } |
4a465510 GS |
499 | devc->first_partial_logic_index = devc->enabled_logic_channels / 8; |
500 | bitpos = devc->enabled_logic_channels % 8; | |
501 | mask = (1 << bitpos) - 1; | |
502 | devc->first_partial_logic_mask = mask; | |
91057d2f UH |
503 | sr_dbg("num logic %zu, partial off %zu, mask 0x%02x.", |
504 | devc->enabled_logic_channels, | |
4a465510 GS |
505 | devc->first_partial_logic_index, |
506 | devc->first_partial_logic_mask); | |
507 | ||
508 | /* | |
509 | * Have the waveform for analog patterns pre-generated. It's | |
510 | * supposed to be periodic, so the generator just needs to | |
511 | * access the prepared sample data (DDS style). | |
512 | */ | |
49224c28 BV |
513 | g_hash_table_iter_init(&iter, devc->ch_ag); |
514 | while (g_hash_table_iter_next(&iter, NULL, &value)) | |
ba508e22 | 515 | demo_generate_analog_pattern(value, devc->cur_samplerate); |
4374219b | 516 | |
98c01fe1 | 517 | sr_session_source_add(sdi->session, -1, 0, 100, |
ba508e22 | 518 | demo_prepare_data, (struct sr_dev_inst *)sdi); |
85b5af06 | 519 | |
bee2b016 | 520 | std_session_send_df_header(sdi); |
f366e86c | 521 | |
fb193945 | 522 | if (devc->limit_frames > 0) |
f55bea76 SA |
523 | std_session_send_frame_begin(sdi); |
524 | ||
3b203673 | 525 | /* We use this timestamp to decide how many more samples to send. */ |
a49a320d DE |
526 | devc->start_us = g_get_monotonic_time(); |
527 | devc->spent_us = 0; | |
471ac344 | 528 | devc->step = 0; |
3b203673 | 529 | |
e46b8fb1 | 530 | return SR_OK; |
6239c175 UH |
531 | } |
532 | ||
695dc859 | 533 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
6239c175 | 534 | { |
fb193945 GS |
535 | struct dev_context *devc; |
536 | ||
027bf077 | 537 | sr_session_source_remove(sdi->session, -1); |
f55bea76 | 538 | |
fb193945 GS |
539 | devc = sdi->priv; |
540 | if (devc->limit_frames > 0) | |
f55bea76 SA |
541 | std_session_send_frame_end(sdi); |
542 | ||
bee2b016 | 543 | std_session_send_df_end(sdi); |
7fd3e859 | 544 | |
6fc51fb1 | 545 | if (devc->stl) { |
546 | soft_trigger_logic_free(devc->stl); | |
547 | devc->stl = NULL; | |
548 | } | |
549 | ||
3010f21c | 550 | return SR_OK; |
6239c175 UH |
551 | } |
552 | ||
dd5c48a6 | 553 | static struct sr_dev_driver demo_driver_info = { |
e519ba86 UH |
554 | .name = "demo", |
555 | .longname = "Demo driver and pattern generator", | |
556 | .api_version = 1, | |
c2fdcc25 | 557 | .init = std_init, |
700d6b64 | 558 | .cleanup = std_cleanup, |
6078d2c9 | 559 | .scan = scan, |
c01bf34c | 560 | .dev_list = std_dev_list, |
6ab68731 | 561 | .dev_clear = dev_clear, |
035a1078 BV |
562 | .config_get = config_get, |
563 | .config_set = config_set, | |
a1c743fc | 564 | .config_list = config_list, |
4d67b9d9 UH |
565 | .dev_open = std_dummy_dev_open, |
566 | .dev_close = std_dummy_dev_close, | |
6078d2c9 UH |
567 | .dev_acquisition_start = dev_acquisition_start, |
568 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 569 | .context = NULL, |
6239c175 | 570 | }; |
dd5c48a6 | 571 | SR_REGISTER_DEV_DRIVER(demo_driver_info); |