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