]>
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> |
6239c175 UH |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
23 | #include <stdlib.h> | |
85b5af06 | 24 | #include <unistd.h> |
6239c175 | 25 | #include <string.h> |
f1b296fc | 26 | #include <math.h> |
d35aaf02 UH |
27 | #ifdef _WIN32 |
28 | #include <io.h> | |
29 | #include <fcntl.h> | |
30 | #define pipe(fds) _pipe(fds, 4096, _O_BINARY) | |
31 | #endif | |
45c59c8b BV |
32 | #include "libsigrok.h" |
33 | #include "libsigrok-internal.h" | |
6239c175 | 34 | |
29a27196 UH |
35 | /* Message logging helpers with subsystem-specific prefix string. */ |
36 | #define LOG_PREFIX "demo: " | |
37 | #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args) | |
38 | #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args) | |
39 | #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args) | |
40 | #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args) | |
41 | #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args) | |
42 | #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args) | |
92bcedf6 | 43 | |
c03ed397 | 44 | /* TODO: Number of probes should be configurable. */ |
f1b296fc | 45 | #define NUM_PROBES 16 |
c03ed397 | 46 | |
e15f48c2 | 47 | #define DEMONAME "Demo device" |
c03ed397 UH |
48 | |
49 | /* The size of chunks to send through the session bus. */ | |
50 | /* TODO: Should be configurable. */ | |
f1b296fc | 51 | #define BUFSIZE 1024*1024 |
85b5af06 | 52 | |
f1b296fc | 53 | #define PERIOD 4000 |
2474d87e | 54 | |
f1b296fc | 55 | #define PI 3.14159265 |
c03ed397 | 56 | |
f1b296fc | 57 | #define CONST_LEN 50 |
c03ed397 | 58 | |
f1b296fc BV |
59 | /* Supported patterns which we can generate */ |
60 | enum { | |
61 | PATTERN_SINE = 0, | |
62 | PATTERN_SQUARE = 1, | |
63 | PATTERN_TRIANGLE = 2, | |
64 | PATTERN_SAWTOOTH = 3, | |
65 | PATTERN_RANDOM = 4, | |
66 | }; | |
67 | static const char *pattern_strings[] = { | |
68 | "Sine", | |
69 | "Square", | |
70 | "Triangle", | |
71 | "Sawtooth", | |
72 | "Random", | |
e15f48c2 | 73 | }; |
85b5af06 | 74 | |
b4750a3a BV |
75 | /* Private, per-device-instance driver context. */ |
76 | struct dev_context { | |
33ef7573 | 77 | struct sr_dev_inst *sdi; |
e15f48c2 | 78 | int pipe_fds[2]; |
e0532047 | 79 | GIOChannel *channel; |
a7684294 JH |
80 | uint64_t cur_samplerate; |
81 | uint64_t limit_samples; | |
82 | uint64_t limit_msec; | |
e15f48c2 | 83 | uint8_t sample_generator; |
e15f48c2 | 84 | uint64_t samples_counter; |
3e9b7f9c | 85 | void *cb_data; |
3b203673 | 86 | int64_t starttime; |
f1b296fc BV |
87 | int stop; |
88 | ||
89 | int trigger_stage; | |
90 | uint16_t trigger_mask; | |
91 | uint16_t trigger_value; | |
92 | uint16_t trigger_edge; | |
e15f48c2 | 93 | }; |
85b5af06 | 94 | |
915f7cc8 | 95 | static const int hwcaps[] = { |
1953564a BV |
96 | SR_CONF_LOGIC_ANALYZER, |
97 | SR_CONF_DEMO_DEV, | |
98 | SR_CONF_SAMPLERATE, | |
99 | SR_CONF_PATTERN_MODE, | |
100 | SR_CONF_LIMIT_SAMPLES, | |
101 | SR_CONF_LIMIT_MSEC, | |
102 | SR_CONF_CONTINUOUS, | |
6239c175 UH |
103 | }; |
104 | ||
f1b296fc BV |
105 | static const int hwoptions[] = { |
106 | SR_CONF_PATTERN_MODE, | |
4bfbf9fc BV |
107 | }; |
108 | ||
f1b296fc BV |
109 | static const uint64_t samplerates[] = { |
110 | SR_KHZ(10), | |
111 | SR_KHZ(20), | |
112 | SR_KHZ(50), | |
113 | SR_KHZ(100), | |
114 | SR_KHZ(200), | |
115 | SR_KHZ(500), | |
116 | SR_MHZ(1), | |
117 | SR_MHZ(2), | |
118 | SR_MHZ(5), | |
119 | SR_MHZ(10), | |
120 | SR_MHZ(20), | |
121 | SR_MHZ(50), | |
122 | SR_MHZ(100), | |
123 | SR_MHZ(200), | |
85b5af06 UH |
124 | }; |
125 | ||
f1b296fc BV |
126 | |
127 | ||
d261dbbf | 128 | /* We name the probes 0-7 on our demo driver. */ |
464d12c7 | 129 | static const char *probe_names[NUM_PROBES + 1] = { |
f1b296fc BV |
130 | "Channel 0", "Channel 1", "Channel 2", "Channel 3", |
131 | "Channel 4", "Channel 5", "Channel 6", "Channel 7", | |
132 | "Channel 8", "Channel 9", "Channel 10", "Channel 11", | |
133 | "Channel 12", "Channel 13", "Channel 14", "Channel 15", | |
464d12c7 KS |
134 | NULL, |
135 | }; | |
136 | ||
ea9cfed7 UH |
137 | /* Private, per-device-instance driver context. */ |
138 | /* TODO: struct context as with the other drivers. */ | |
139 | ||
e7eb703f | 140 | /* List of struct sr_dev_inst, maintained by dev_open()/dev_close(). */ |
dcf03d6d | 141 | SR_PRIV struct sr_dev_driver demo_driver_info; |
a873c594 | 142 | static struct sr_dev_driver *di = &demo_driver_info; |
6239c175 | 143 | |
f1b296fc BV |
144 | extern struct ds_trigger *trigger; |
145 | ||
69b07d14 | 146 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data); |
6239c175 | 147 | |
eebb6067 UH |
148 | static int clear_instances(void) |
149 | { | |
150 | /* Nothing needed so far. */ | |
151 | ||
152 | return SR_OK; | |
153 | } | |
154 | ||
34f06b90 | 155 | static int hw_init(struct sr_context *sr_ctx) |
61136ea6 | 156 | { |
29a27196 | 157 | return std_hw_init(sr_ctx, di, LOG_PREFIX); |
61136ea6 BV |
158 | } |
159 | ||
067d0716 | 160 | static GSList *hw_scan(GSList *options) |
6239c175 | 161 | { |
d68e2d1a | 162 | struct sr_dev_inst *sdi; |
87ca93c5 | 163 | struct sr_probe *probe; |
b4750a3a | 164 | struct drv_context *drvc; |
33ef7573 | 165 | struct dev_context *devc; |
067d0716 | 166 | GSList *devices; |
87ca93c5 | 167 | int i; |
067d0716 BV |
168 | |
169 | (void)options; | |
64d33dc2 | 170 | |
a873c594 | 171 | drvc = di->priv; |
4b97c74e | 172 | |
067d0716 | 173 | devices = NULL; |
85b5af06 | 174 | |
f1b296fc | 175 | sdi = sr_dev_inst_new(LOGIC, 0, SR_ST_ACTIVE, DEMONAME, NULL, NULL); |
c03ed397 | 176 | if (!sdi) { |
f1b296fc | 177 | sr_err("Device instance creation failed."); |
e45ad6e2 | 178 | return NULL; |
c03ed397 | 179 | } |
a873c594 | 180 | sdi->driver = di; |
e15f48c2 | 181 | |
067d0716 | 182 | devices = g_slist_append(devices, sdi); |
b4750a3a | 183 | drvc->instances = g_slist_append(drvc->instances, sdi); |
85b5af06 | 184 | |
33ef7573 | 185 | if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { |
e45ad6e2 UH |
186 | sr_err("Device context malloc failed."); |
187 | return NULL; | |
33ef7573 JH |
188 | } |
189 | ||
190 | devc->sdi = sdi; | |
f1b296fc | 191 | devc->cur_samplerate = SR_MHZ(200); |
a7684294 JH |
192 | devc->limit_samples = 0; |
193 | devc->limit_msec = 0; | |
f1b296fc | 194 | devc->sample_generator = PATTERN_SINE; |
33ef7573 JH |
195 | |
196 | sdi->priv = devc; | |
197 | ||
f1b296fc BV |
198 | if (sdi->mode == LOGIC) { |
199 | for (i = 0; probe_names[i]; i++) { | |
200 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
201 | probe_names[i]))) | |
202 | return NULL; | |
203 | sdi->probes = g_slist_append(sdi->probes, probe); | |
204 | } | |
205 | } else if (sdi->mode == ANALOG) { | |
206 | for (i = 0; i < DS_MAX_ANALOG_PROBES_NUM; i++) { | |
207 | if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, | |
208 | probe_names[i]))) | |
209 | return NULL; | |
210 | sdi->probes = g_slist_append(sdi->probes, probe); | |
211 | } | |
212 | } | |
213 | ||
067d0716 | 214 | return devices; |
6239c175 UH |
215 | } |
216 | ||
811deee4 BV |
217 | static GSList *hw_dev_list(void) |
218 | { | |
0e94d524 | 219 | return ((struct drv_context *)(di->priv))->instances; |
811deee4 BV |
220 | } |
221 | ||
25a0f108 | 222 | static int hw_dev_open(struct sr_dev_inst *sdi) |
6239c175 | 223 | { |
25a0f108 | 224 | (void)sdi; |
6239c175 | 225 | |
e73ffd42 | 226 | sdi->status = SR_ST_ACTIVE; |
697785d1 | 227 | |
e46b8fb1 | 228 | return SR_OK; |
6239c175 UH |
229 | } |
230 | ||
25a0f108 | 231 | static int hw_dev_close(struct sr_dev_inst *sdi) |
6239c175 | 232 | { |
25a0f108 | 233 | (void)sdi; |
6239c175 | 234 | |
e73ffd42 | 235 | sdi->status = SR_ST_INACTIVE; |
697785d1 UH |
236 | |
237 | return SR_OK; | |
6239c175 UH |
238 | } |
239 | ||
57ab7d9f | 240 | static int hw_cleanup(void) |
6239c175 | 241 | { |
ed20a428 JH |
242 | GSList *l; |
243 | struct sr_dev_inst *sdi; | |
244 | struct drv_context *drvc; | |
245 | int ret = SR_OK; | |
246 | ||
247 | if (!(drvc = di->priv)) | |
248 | return SR_OK; | |
249 | ||
250 | /* Properly close and free all devices. */ | |
251 | for (l = drvc->instances; l; l = l->next) { | |
252 | if (!(sdi = l->data)) { | |
253 | /* Log error, but continue cleaning up the rest. */ | |
254 | sr_err("%s: sdi was NULL, continuing", __func__); | |
255 | ret = SR_ERR_BUG; | |
256 | continue; | |
257 | } | |
258 | sr_dev_inst_free(sdi); | |
259 | } | |
260 | g_slist_free(drvc->instances); | |
261 | drvc->instances = NULL; | |
262 | ||
263 | return ret; | |
6239c175 UH |
264 | } |
265 | ||
d00088ca | 266 | static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi) |
6239c175 | 267 | { |
a7684294 | 268 | struct dev_context *const devc = sdi->priv; |
6f57fd96 | 269 | |
035a1078 | 270 | switch (id) { |
123e1313 | 271 | case SR_CONF_SAMPLERATE: |
a7684294 | 272 | *data = g_variant_new_uint64(devc->cur_samplerate); |
6239c175 | 273 | break; |
2474d87e | 274 | case SR_CONF_LIMIT_SAMPLES: |
a7684294 | 275 | *data = g_variant_new_uint64(devc->limit_samples); |
2474d87e BV |
276 | break; |
277 | case SR_CONF_LIMIT_MSEC: | |
a7684294 | 278 | *data = g_variant_new_uint64(devc->limit_msec); |
2474d87e | 279 | break; |
f1b296fc BV |
280 | case SR_CONF_DEVICE_MODE: |
281 | *data = g_variant_new_string(mode_strings[sdi->mode]); | |
282 | break; | |
283 | case SR_CONF_PATTERN_MODE: | |
284 | *data = g_variant_new_string(pattern_strings[devc->sample_generator]); | |
2474d87e | 285 | break; |
7dfcf010 | 286 | default: |
bd6fbf62 | 287 | return SR_ERR_NA; |
6239c175 UH |
288 | } |
289 | ||
dfb0fa1a | 290 | return SR_OK; |
6239c175 UH |
291 | } |
292 | ||
f1b296fc | 293 | static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi) |
6239c175 | 294 | { |
f1b296fc | 295 | int i, ret; |
1b79df2f | 296 | const char *stropt; |
f1b296fc | 297 | struct sr_probe *probe; |
6239c175 | 298 | |
a7684294 | 299 | struct dev_context *const devc = sdi->priv; |
6239c175 | 300 | |
e73ffd42 BV |
301 | if (sdi->status != SR_ST_ACTIVE) |
302 | return SR_ERR_DEV_CLOSED; | |
303 | ||
035a1078 | 304 | if (id == SR_CONF_SAMPLERATE) { |
a7684294 | 305 | devc->cur_samplerate = g_variant_get_uint64(data); |
92bcedf6 | 306 | sr_dbg("%s: setting samplerate to %" PRIu64, __func__, |
a7684294 | 307 | devc->cur_samplerate); |
e46b8fb1 | 308 | ret = SR_OK; |
035a1078 | 309 | } else if (id == SR_CONF_LIMIT_SAMPLES) { |
a7684294 JH |
310 | devc->limit_msec = 0; |
311 | devc->limit_samples = g_variant_get_uint64(data); | |
92bcedf6 | 312 | sr_dbg("%s: setting limit_samples to %" PRIu64, __func__, |
a7684294 | 313 | devc->limit_samples); |
e46b8fb1 | 314 | ret = SR_OK; |
035a1078 | 315 | } else if (id == SR_CONF_LIMIT_MSEC) { |
a7684294 JH |
316 | devc->limit_msec = g_variant_get_uint64(data); |
317 | devc->limit_samples = 0; | |
92bcedf6 | 318 | sr_dbg("%s: setting limit_msec to %" PRIu64, __func__, |
a7684294 | 319 | devc->limit_msec); |
f1b296fc BV |
320 | ret = SR_OK; |
321 | } else if (id == SR_CONF_DEVICE_MODE) { | |
322 | stropt = g_variant_get_string(data, NULL); | |
323 | ret = SR_OK; | |
324 | if (!strcmp(stropt, mode_strings[LOGIC])) { | |
325 | sdi->mode = LOGIC; | |
326 | sr_dev_probes_free(sdi); | |
327 | for (i = 0; probe_names[i]; i++) { | |
328 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, | |
329 | probe_names[i]))) | |
330 | ret = SR_ERR; | |
331 | else | |
332 | sdi->probes = g_slist_append(sdi->probes, probe); | |
333 | } | |
334 | } else if (!strcmp(stropt, mode_strings[ANALOG])) { | |
335 | sdi->mode = ANALOG; | |
336 | sr_dev_probes_free(sdi); | |
337 | for (i = 0; i < DS_MAX_ANALOG_PROBES_NUM; i++) { | |
338 | if (!(probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, | |
339 | probe_names[i]))) | |
340 | ret = SR_ERR; | |
341 | else | |
342 | sdi->probes = g_slist_append(sdi->probes, probe); | |
343 | } | |
344 | } else { | |
345 | ret = SR_ERR; | |
346 | } | |
347 | sr_dbg("%s: setting mode to %d", __func__, sdi->mode); | |
348 | }else if (id == SR_CONF_PATTERN_MODE) { | |
d00088ca | 349 | stropt = g_variant_get_string(data, NULL); |
f1b296fc BV |
350 | ret = SR_OK; |
351 | if (!strcmp(stropt, pattern_strings[PATTERN_SINE])) { | |
352 | devc->sample_generator = PATTERN_SINE; | |
353 | } else if (!strcmp(stropt, pattern_strings[PATTERN_SQUARE])) { | |
354 | devc->sample_generator = PATTERN_SQUARE; | |
355 | } else if (!strcmp(stropt, pattern_strings[PATTERN_TRIANGLE])) { | |
356 | devc->sample_generator = PATTERN_TRIANGLE; | |
357 | } else if (!strcmp(stropt, pattern_strings[PATTERN_SAWTOOTH])) { | |
358 | devc->sample_generator = PATTERN_SAWTOOTH; | |
359 | } else if (!strcmp(stropt, pattern_strings[PATTERN_RANDOM])) { | |
360 | devc->sample_generator = PATTERN_RANDOM; | |
e15f48c2 | 361 | } else { |
f1b296fc | 362 | ret = SR_ERR; |
e15f48c2 | 363 | } |
f1b296fc | 364 | sr_dbg("%s: setting pattern to %d", |
a7684294 | 365 | __func__, devc->sample_generator); |
6239c175 | 366 | } else { |
f1b296fc | 367 | ret = SR_ERR_NA; |
6239c175 UH |
368 | } |
369 | ||
370 | return ret; | |
371 | } | |
372 | ||
d00088ca | 373 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi) |
a1c743fc | 374 | { |
d00088ca BV |
375 | GVariant *gvar; |
376 | GVariantBuilder gvb; | |
a1c743fc BV |
377 | |
378 | (void)sdi; | |
379 | ||
380 | switch (key) { | |
f1b296fc BV |
381 | case SR_CONF_DEVICE_OPTIONS: |
382 | // *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
383 | // hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
384 | *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"), | |
385 | hwcaps, ARRAY_SIZE(hwcaps)*sizeof(int32_t), TRUE, NULL, NULL); | |
9a6517d1 | 386 | break; |
f1b296fc BV |
387 | case SR_CONF_DEVICE_CONFIGS: |
388 | // *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
389 | // hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
390 | *data = g_variant_new_from_data(G_VARIANT_TYPE("ai"), | |
391 | hwoptions, ARRAY_SIZE(hwoptions)*sizeof(int32_t), TRUE, NULL, NULL); | |
392 | break; | |
393 | case SR_CONF_SAMPLERATE: | |
d00088ca | 394 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); |
f1b296fc BV |
395 | // gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, |
396 | // ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
397 | gvar = g_variant_new_from_data(G_VARIANT_TYPE("at"), | |
398 | samplerates, ARRAY_SIZE(samplerates)*sizeof(uint64_t), TRUE, NULL, NULL); | |
399 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); | |
d00088ca | 400 | *data = g_variant_builder_end(&gvb); |
a1c743fc | 401 | break; |
f1b296fc BV |
402 | case SR_CONF_DEVICE_MODE: |
403 | *data = g_variant_new_strv(mode_strings, ARRAY_SIZE(mode_strings)); | |
404 | break; | |
405 | case SR_CONF_PATTERN_MODE: | |
d00088ca | 406 | *data = g_variant_new_strv(pattern_strings, ARRAY_SIZE(pattern_strings)); |
dd96ea98 | 407 | break; |
a1c743fc | 408 | default: |
f1b296fc | 409 | return SR_ERR_NA; |
a1c743fc BV |
410 | } |
411 | ||
f1b296fc | 412 | return SR_OK; |
a1c743fc BV |
413 | } |
414 | ||
f1b296fc | 415 | static void samples_generator(uint16_t *buf, uint64_t size, |
3b203673 | 416 | struct dev_context *devc) |
85b5af06 | 417 | { |
f1b296fc | 418 | static uint16_t p = 0; |
cddd1c5f | 419 | uint64_t i; |
f1b296fc | 420 | uint16_t demo_data; |
85b5af06 | 421 | |
b4750a3a | 422 | switch (devc->sample_generator) { |
f1b296fc BV |
423 | case PATTERN_SINE: /* Sine */ |
424 | for (i = 0; i < size; i++) { | |
425 | if (i%CONST_LEN == 0) { | |
426 | demo_data = 0x8000 * sin(2 * PI * p / 0xffff) + 0x8000; | |
427 | p += CONST_LEN; | |
428 | } | |
429 | *(buf + i) = demo_data; | |
430 | } | |
917e0e71 | 431 | break; |
f1b296fc BV |
432 | case PATTERN_SQUARE: |
433 | for (i = 0; i < size; i++) { | |
434 | if (i%CONST_LEN == 0) { | |
435 | demo_data = p > 0x7fff ? 0xf000 : 0x1000; | |
436 | p += CONST_LEN; | |
437 | } | |
438 | *(buf + i) = demo_data; | |
439 | } | |
440 | break; | |
441 | case PATTERN_TRIANGLE: | |
442 | for (i = 0; i < size; i++) { | |
443 | if (i%CONST_LEN == 0) { | |
444 | demo_data = p > 0x7fff ? 0xffff * (2.0f * (0x10000 - p) / 0x10000) : | |
445 | 0xffff * (2.0f * p / 0x10000); | |
446 | p += CONST_LEN; | |
447 | } | |
448 | *(buf + i) = demo_data; | |
449 | } | |
450 | break; | |
451 | case PATTERN_SAWTOOTH: | |
452 | for (i = 0; i < size; i++) { | |
453 | if (i%CONST_LEN == 0) { | |
454 | demo_data = p; | |
455 | p += CONST_LEN; | |
456 | } | |
457 | *(buf + i) = demo_data; | |
458 | } | |
459 | break; | |
c8f4624d | 460 | case PATTERN_RANDOM: /* Random */ |
f1b296fc BV |
461 | for (i = 0; i < size; i++) { |
462 | if (i%CONST_LEN == 0) | |
463 | demo_data = (uint16_t)(rand() * (0x10000 * 1.0f / RAND_MAX)); | |
464 | *(buf + i) = demo_data; | |
465 | } | |
c03ed397 UH |
466 | break; |
467 | default: | |
f1b296fc | 468 | sr_err("Unknown pattern: %d.", devc->sample_generator); |
c03ed397 | 469 | break; |
85b5af06 UH |
470 | } |
471 | } | |
472 | ||
85b5af06 | 473 | /* Callback handling data */ |
f1b296fc | 474 | static int receive_data(int fd, int revents, const struct sr_dev_inst *sdi) |
85b5af06 | 475 | { |
f1b296fc BV |
476 | struct dev_context *devc = sdi->priv; |
477 | struct sr_datafeed_packet packet; | |
478 | struct sr_datafeed_logic logic; | |
479 | struct sr_datafeed_analog analog; | |
480 | //uint16_t buf[BUFSIZE]; | |
481 | uint16_t *buf; | |
3b203673 AG |
482 | static uint64_t samples_to_send, expected_samplenum, sending_now; |
483 | int64_t time, elapsed; | |
f1b296fc BV |
484 | static uint16_t last_sample = 0; |
485 | uint16_t cur_sample; | |
486 | int i; | |
1924f59f | 487 | |
cb93f8a9 UH |
488 | (void)fd; |
489 | (void)revents; | |
1924f59f | 490 | |
f1b296fc BV |
491 | if (!(buf = g_try_malloc(BUFSIZE*sizeof(uint16_t)))) { |
492 | sr_err("buf for receive_data malloc failed."); | |
493 | return FALSE; | |
494 | } | |
495 | ||
3b203673 AG |
496 | /* How many "virtual" samples should we have collected by now? */ |
497 | time = g_get_monotonic_time(); | |
498 | elapsed = time - devc->starttime; | |
f1b296fc | 499 | devc->starttime = time; |
a7684294 | 500 | expected_samplenum = elapsed * devc->cur_samplerate / 1000000; |
3b203673 | 501 | /* Of those, how many do we still have to send? */ |
f1b296fc BV |
502 | //samples_to_send = (expected_samplenum - devc->samples_counter) / CONST_LEN * CONST_LEN; |
503 | samples_to_send = expected_samplenum / CONST_LEN * CONST_LEN; | |
504 | ||
505 | if (devc->limit_samples) { | |
506 | if (sdi->mode == LOGIC) | |
507 | samples_to_send = MIN(samples_to_send, | |
508 | devc->limit_samples - devc->samples_counter); | |
509 | else if (sdi->mode == ANALOG) | |
510 | samples_to_send = MIN(samples_to_send, | |
511 | devc->limit_samples); | |
512 | } | |
513 | ||
514 | while (samples_to_send > 0) { | |
515 | sending_now = MIN(samples_to_send, BUFSIZE); | |
3b203673 AG |
516 | samples_generator(buf, sending_now, devc); |
517 | ||
f1b296fc BV |
518 | if (devc->trigger_stage != 0) { |
519 | for (i = 0; i < sending_now; i++) { | |
520 | if (devc->trigger_edge == 0) { | |
521 | if ((*(buf + i) | devc->trigger_mask) == | |
522 | (devc->trigger_value | devc->trigger_mask)) { | |
523 | devc->trigger_stage = 0; | |
524 | break; | |
525 | } | |
526 | } else { | |
527 | cur_sample = *(buf + i); | |
528 | if (((last_sample & devc->trigger_edge) == | |
529 | (~devc->trigger_value & devc->trigger_edge)) && | |
530 | ((cur_sample | devc->trigger_mask) == | |
531 | (devc->trigger_value | devc->trigger_mask)) && | |
532 | ((cur_sample & devc->trigger_edge) == | |
533 | (devc->trigger_value & devc->trigger_edge))) { | |
534 | devc->trigger_stage = 0; | |
535 | break; | |
536 | } | |
537 | last_sample = cur_sample; | |
538 | } | |
539 | } | |
540 | if (devc->trigger_stage == 0) { | |
541 | struct ds_trigger_pos demo_trigger_pos; | |
542 | demo_trigger_pos.real_pos = i; | |
543 | packet.type = SR_DF_TRIGGER; | |
544 | packet.payload = &demo_trigger_pos; | |
545 | sr_session_send(sdi, &packet); | |
546 | } | |
547 | } | |
548 | ||
549 | if (devc->trigger_stage == 0){ | |
550 | samples_to_send -= sending_now; | |
551 | if (sdi->mode == LOGIC) { | |
552 | packet.type = SR_DF_LOGIC; | |
553 | packet.payload = &logic; | |
554 | logic.length = sending_now * (NUM_PROBES >> 3); | |
555 | logic.unitsize = (NUM_PROBES >> 3); | |
556 | logic.data = buf; | |
557 | } else if (sdi->mode == ANALOG) { | |
558 | packet.type = SR_DF_ANALOG; | |
559 | packet.payload = &analog; | |
560 | analog.probes = sdi->probes; | |
561 | analog.num_samples = sending_now; | |
562 | analog.mq = SR_MQ_VOLTAGE; | |
563 | analog.unit = SR_UNIT_VOLT; | |
564 | analog.mqflags = SR_MQFLAG_AC; | |
565 | analog.data = buf; | |
566 | } | |
567 | ||
568 | sr_session_send(sdi, &packet); | |
569 | if (sdi->mode == LOGIC) | |
570 | devc->samples_counter += sending_now; | |
571 | else if (sdi->mode == ANALOG) | |
572 | devc->samples_counter = (devc->samples_counter + sending_now) % devc->limit_samples; | |
573 | } else { | |
574 | break; | |
575 | } | |
3b203673 AG |
576 | } |
577 | ||
f1b296fc BV |
578 | if (sdi->mode == LOGIC && |
579 | devc->limit_samples && | |
580 | devc->samples_counter >= devc->limit_samples) { | |
581 | sr_info("Requested number of samples reached."); | |
582 | hw_dev_acquisition_stop(sdi, NULL); | |
583 | g_free(buf); | |
584 | return TRUE; | |
585 | } | |
586 | ||
587 | g_free(buf); | |
1924f59f | 588 | |
85b5af06 UH |
589 | return TRUE; |
590 | } | |
591 | ||
3ffb6964 BV |
592 | static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, |
593 | void *cb_data) | |
6239c175 | 594 | { |
33ef7573 | 595 | struct dev_context *const devc = sdi->priv; |
85b5af06 | 596 | |
f1b296fc BV |
597 | (void)cb_data; |
598 | ||
599 | if (sdi->status != SR_ST_ACTIVE) | |
600 | return SR_ERR_DEV_CLOSED; | |
e73ffd42 | 601 | |
f1b296fc | 602 | //devc->cb_data = cb_data; |
b4750a3a | 603 | devc->samples_counter = 0; |
f1b296fc BV |
604 | devc->stop = FALSE; |
605 | ||
606 | /* | |
607 | * trigger setting | |
608 | */ | |
609 | if (!trigger->trigger_en || sdi->mode == ANALOG) { | |
610 | devc->trigger_stage = 0; | |
611 | } else { | |
612 | devc->trigger_mask = ds_trigger_get_mask0(TriggerStages); | |
613 | devc->trigger_value = ds_trigger_get_value0(TriggerStages); | |
614 | devc->trigger_edge = ds_trigger_get_edge0(TriggerStages); | |
615 | if (devc->trigger_edge != 0) | |
616 | devc->trigger_stage = 2; | |
617 | else | |
618 | devc->trigger_stage = 1; | |
619 | } | |
85b5af06 | 620 | |
3b203673 AG |
621 | /* |
622 | * Setting two channels connected by a pipe is a remnant from when the | |
623 | * demo driver generated data in a thread, and collected and sent the | |
624 | * data in the main program loop. | |
625 | * They are kept here because it provides a convenient way of setting | |
626 | * up a timeout-based polling mechanism. | |
627 | */ | |
b4750a3a | 628 | if (pipe(devc->pipe_fds)) { |
c03ed397 | 629 | /* TODO: Better error message. */ |
92bcedf6 | 630 | sr_err("%s: pipe() failed", __func__); |
e46b8fb1 | 631 | return SR_ERR; |
c03ed397 | 632 | } |
85b5af06 | 633 | |
e0532047 | 634 | devc->channel = g_io_channel_unix_new(devc->pipe_fds[0]); |
d35aaf02 | 635 | |
e0532047 | 636 | g_io_channel_set_flags(devc->channel, G_IO_FLAG_NONBLOCK, NULL); |
e6e8f8e0 | 637 | |
d35aaf02 | 638 | /* Set channel encoding to binary (default is UTF-8). */ |
e0532047 | 639 | g_io_channel_set_encoding(devc->channel, NULL, NULL); |
d35aaf02 UH |
640 | |
641 | /* Make channels to unbuffered. */ | |
e0532047 | 642 | g_io_channel_set_buffered(devc->channel, FALSE); |
d35aaf02 | 643 | |
f1b296fc BV |
644 | sr_session_source_add_channel(devc->channel, G_IO_IN | G_IO_ERR, |
645 | 100, receive_data, sdi); | |
85b5af06 | 646 | |
4afdfd46 | 647 | /* Send header packet to the session bus. */ |
f1b296fc BV |
648 | //std_session_send_df_header(cb_data, LOG_PREFIX); |
649 | std_session_send_df_header(sdi, LOG_PREFIX); | |
f366e86c | 650 | |
3b203673 AG |
651 | /* We use this timestamp to decide how many more samples to send. */ |
652 | devc->starttime = g_get_monotonic_time(); | |
653 | ||
e46b8fb1 | 654 | return SR_OK; |
6239c175 UH |
655 | } |
656 | ||
69b07d14 | 657 | static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
6239c175 | 658 | { |
33ef7573 | 659 | struct dev_context *const devc = sdi->priv; |
c216d623 | 660 | struct sr_datafeed_packet packet; |
7fd3e859 | 661 | |
33ef7573 | 662 | (void)cb_data; |
7fd3e859 | 663 | |
49145a63 AG |
664 | sr_dbg("Stopping aquisition."); |
665 | ||
f1b296fc BV |
666 | devc->stop = TRUE; |
667 | sr_session_source_remove_channel(devc->channel); | |
e0532047 | 668 | g_io_channel_shutdown(devc->channel, FALSE, NULL); |
2150a69b JH |
669 | g_io_channel_unref(devc->channel); |
670 | devc->channel = NULL; | |
c216d623 AG |
671 | |
672 | /* Send last packet. */ | |
f1b296fc BV |
673 | packet.type = SR_DF_END; |
674 | sr_session_send(sdi, &packet); | |
7fd3e859 | 675 | |
3010f21c | 676 | return SR_OK; |
6239c175 UH |
677 | } |
678 | ||
f1b296fc BV |
679 | static int hw_dev_test(struct sr_dev_inst *sdi) |
680 | { | |
681 | if (sdi) | |
682 | return SR_OK; | |
683 | else | |
684 | return SR_ERR; | |
685 | } | |
686 | ||
c09f0b57 | 687 | SR_PRIV struct sr_dev_driver demo_driver_info = { |
e519ba86 UH |
688 | .name = "demo", |
689 | .longname = "Demo driver and pattern generator", | |
690 | .api_version = 1, | |
691 | .init = hw_init, | |
692 | .cleanup = hw_cleanup, | |
61136ea6 | 693 | .scan = hw_scan, |
811deee4 | 694 | .dev_list = hw_dev_list, |
eebb6067 | 695 | .dev_clear = clear_instances, |
035a1078 BV |
696 | .config_get = config_get, |
697 | .config_set = config_set, | |
a1c743fc | 698 | .config_list = config_list, |
e7eb703f UH |
699 | .dev_open = hw_dev_open, |
700 | .dev_close = hw_dev_close, | |
f1b296fc | 701 | .dev_test = hw_dev_test, |
69040b7c UH |
702 | .dev_acquisition_start = hw_dev_acquisition_start, |
703 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
b4750a3a | 704 | .priv = NULL, |
6239c175 | 705 | }; |