2 * This file is part of the libsigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2011 Olivier Fauchon <olivier@aixmarseille.com>
6 * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
7 * Copyright (C) 2015 Bartosz Golaszewski <bgolaszewski@baylibre.com>
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.
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.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <libsigrok/libsigrok.h>
28 #include "libsigrok-internal.h"
31 #define ANALOG_SAMPLES_PER_PERIOD 20
33 static const uint8_t pattern_sigrok[] = {
34 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00,
35 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00,
36 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00,
37 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00,
38 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00,
39 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate)
48 unsigned int num_samples, i;
51 sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]);
53 num_samples = ANALOG_BUFSIZE / sizeof(float);
55 switch (ag->pattern) {
57 value = ag->amplitude;
59 for (i = 0; i < num_samples; i++) {
64 ag->pattern_data[i] = value;
66 ag->num_samples = last_end;
69 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
71 /* Make sure the number of samples we put out is an integer
72 * multiple of our period size */
73 /* FIXME we actually need only one period. A ringbuffer would be
75 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
78 for (i = 0; i < num_samples; i++) {
79 t = (double) i / (double) sample_rate;
80 ag->pattern_data[i] = ag->amplitude *
81 sin(2 * G_PI * frequency * t);
84 ag->num_samples = num_samples;
86 case PATTERN_TRIANGLE:
87 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
89 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
92 for (i = 0; i < num_samples; i++) {
93 t = (double) i / (double) sample_rate;
94 ag->pattern_data[i] = (2 * ag->amplitude / G_PI) *
95 asin(sin(2 * G_PI * frequency * t));
98 ag->num_samples = num_samples;
100 case PATTERN_SAWTOOTH:
101 frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD;
103 while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0)
106 for (i = 0; i < num_samples; i++) {
107 t = (double) i / (double) sample_rate;
108 ag->pattern_data[i] = 2 * ag->amplitude *
109 ((t * frequency) - floor(0.5f + t * frequency));
112 ag->num_samples = num_samples;
117 static void logic_generator(struct sr_dev_inst *sdi, uint64_t size)
119 struct dev_context *devc;
125 switch (devc->logic_pattern) {
127 memset(devc->logic_data, 0x00, size);
128 for (i = 0; i < size; i += devc->logic_unitsize) {
129 for (j = 0; j < devc->logic_unitsize; j++) {
130 pat = pattern_sigrok[(devc->step + j) % sizeof(pattern_sigrok)] >> 1;
131 devc->logic_data[i + j] = ~pat;
137 for (i = 0; i < size; i++)
138 devc->logic_data[i] = (uint8_t)(rand() & 0xff);
141 for (i = 0; i < size; i++) {
142 for (j = 0; j < devc->logic_unitsize; j++) {
143 devc->logic_data[i + j] = devc->step;
148 case PATTERN_ALL_LOW:
149 case PATTERN_ALL_HIGH:
150 /* These were set when the pattern mode was selected. */
153 sr_err("Unknown pattern: %d.", devc->logic_pattern);
158 static void send_analog_packet(struct analog_gen *ag,
159 struct sr_dev_inst *sdi, uint64_t *analog_sent,
160 uint64_t analog_pos, uint64_t analog_todo)
162 struct sr_datafeed_packet packet;
163 struct dev_context *devc;
164 uint64_t sending_now, to_avg;
169 packet.type = SR_DF_ANALOG;
170 packet.payload = &ag->packet;
173 ag_pattern_pos = analog_pos % ag->num_samples;
174 sending_now = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
175 ag->packet.data = ag->pattern_data + ag_pattern_pos;
176 ag->packet.num_samples = sending_now;
177 sr_session_send(sdi, &packet);
179 /* Whichever channel group gets there first. */
180 *analog_sent = MAX(*analog_sent, sending_now);
182 ag_pattern_pos = analog_pos % ag->num_samples;
183 to_avg = MIN(analog_todo, ag->num_samples-ag_pattern_pos);
185 for (i = 0; i < to_avg; i++) {
186 ag->avg_val = (ag->avg_val +
188 ag_pattern_pos + i)) / 2;
190 /* Time to send averaged data? */
191 if (devc->avg_samples > 0 &&
192 ag->num_avgs >= devc->avg_samples)
196 if (devc->avg_samples == 0) {
197 /* We're averaging all the samples, so wait with
198 * sending until the very end.
200 *analog_sent = ag->num_avgs;
205 ag->packet.data = &ag->avg_val;
206 ag->packet.num_samples = 1;
208 sr_session_send(sdi, &packet);
209 *analog_sent = ag->num_avgs;
216 /* Callback handling data */
217 SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data)
219 struct sr_dev_inst *sdi;
220 struct dev_context *devc;
221 struct sr_datafeed_packet packet;
222 struct sr_datafeed_logic logic;
223 struct analog_gen *ag;
226 uint64_t samples_todo, logic_done, analog_done, analog_sent, sending_now;
227 int64_t elapsed_us, limit_us, todo_us;
236 if (devc->cur_samplerate <= 0
237 || (devc->num_logic_channels <= 0
238 && devc->num_analog_channels <= 0)) {
239 sdi->driver->dev_acquisition_stop(sdi);
240 return G_SOURCE_CONTINUE;
243 /* What time span should we send samples for? */
244 elapsed_us = g_get_monotonic_time() - devc->start_us;
245 limit_us = 1000 * devc->limit_msec;
246 if (limit_us > 0 && limit_us < elapsed_us)
247 todo_us = MAX(0, limit_us - devc->spent_us);
249 todo_us = MAX(0, elapsed_us - devc->spent_us);
251 /* How many samples are outstanding since the last round? */
252 samples_todo = (todo_us * devc->cur_samplerate + G_USEC_PER_SEC - 1)
254 if (devc->limit_samples > 0) {
255 if (devc->limit_samples < devc->sent_samples)
257 else if (devc->limit_samples - devc->sent_samples < samples_todo)
258 samples_todo = devc->limit_samples - devc->sent_samples;
260 /* Calculate the actual time covered by this run back from the sample
261 * count, rounded towards zero. This avoids getting stuck on a too-low
262 * time delta with no samples being sent due to round-off.
264 todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate;
266 logic_done = devc->num_logic_channels > 0 ? 0 : samples_todo;
267 analog_done = devc->num_analog_channels > 0 ? 0 : samples_todo;
269 while (logic_done < samples_todo || analog_done < samples_todo) {
271 if (logic_done < samples_todo) {
272 sending_now = MIN(samples_todo - logic_done,
273 LOGIC_BUFSIZE / devc->logic_unitsize);
274 logic_generator(sdi, sending_now * devc->logic_unitsize);
275 packet.type = SR_DF_LOGIC;
276 packet.payload = &logic;
277 logic.length = sending_now * devc->logic_unitsize;
278 logic.unitsize = devc->logic_unitsize;
279 logic.data = devc->logic_data;
280 sr_session_send(sdi, &packet);
281 logic_done += sending_now;
284 /* Analog, one channel at a time */
285 if (analog_done < samples_todo) {
288 g_hash_table_iter_init(&iter, devc->ch_ag);
289 while (g_hash_table_iter_next(&iter, NULL, &value)) {
290 send_analog_packet(value, sdi, &analog_sent,
291 devc->sent_samples + analog_done,
292 samples_todo - analog_done);
294 analog_done += analog_sent;
297 /* At this point, both logic_done and analog_done should be
298 * exactly equal to samples_todo, or else.
300 if (logic_done != samples_todo || analog_done != samples_todo) {
301 sr_err("BUG: Sample count mismatch.");
302 return G_SOURCE_REMOVE;
304 devc->sent_samples += samples_todo;
305 devc->spent_us += todo_us;
307 if ((devc->limit_samples > 0 && devc->sent_samples >= devc->limit_samples)
308 || (limit_us > 0 && devc->spent_us >= limit_us)) {
310 /* If we're averaging everything - now is the time to send data */
311 if (devc->avg_samples == 0) {
312 g_hash_table_iter_init(&iter, devc->ch_ag);
313 while (g_hash_table_iter_next(&iter, NULL, &value)) {
315 packet.type = SR_DF_ANALOG;
316 packet.payload = &ag->packet;
317 ag->packet.data = &ag->avg_val;
318 ag->packet.num_samples = 1;
319 sr_session_send(sdi, &packet);
322 sr_dbg("Requested number of samples reached.");
323 sdi->driver->dev_acquisition_stop(sdi);
326 return G_SOURCE_CONTINUE;