]>
Commit | Line | Data |
---|---|---|
ba508e22 UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
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> | |
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 | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | */ | |
23 | ||
24 | #include <config.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <math.h> | |
28 | #include <libsigrok/libsigrok.h> | |
29 | #include "libsigrok-internal.h" | |
30 | #include "protocol.h" | |
31 | ||
32 | #define ANALOG_SAMPLES_PER_PERIOD 20 | |
33 | ||
34 | static const uint8_t pattern_sigrok[] = { | |
35 | 0x4c, 0x92, 0x92, 0x92, 0x64, 0x00, 0x00, 0x00, | |
36 | 0x82, 0xfe, 0xfe, 0x82, 0x00, 0x00, 0x00, 0x00, | |
37 | 0x7c, 0x82, 0x82, 0x92, 0x74, 0x00, 0x00, 0x00, | |
38 | 0xfe, 0x12, 0x12, 0x32, 0xcc, 0x00, 0x00, 0x00, | |
39 | 0x7c, 0x82, 0x82, 0x82, 0x7c, 0x00, 0x00, 0x00, | |
40 | 0xfe, 0x10, 0x28, 0x44, 0x82, 0x00, 0x00, 0x00, | |
41 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
42 | 0xbe, 0xbe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
43 | }; | |
44 | ||
45 | SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate) | |
46 | { | |
47 | double t, frequency; | |
48 | float value; | |
49 | unsigned int num_samples, i; | |
50 | int last_end; | |
51 | ||
52 | sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]); | |
53 | ||
54 | num_samples = ANALOG_BUFSIZE / sizeof(float); | |
55 | ||
56 | switch (ag->pattern) { | |
57 | case PATTERN_SQUARE: | |
58 | value = ag->amplitude; | |
59 | last_end = 0; | |
60 | for (i = 0; i < num_samples; i++) { | |
61 | if (i % 5 == 0) | |
62 | value = -value; | |
63 | if (i % 10 == 0) | |
64 | last_end = i; | |
65 | ag->pattern_data[i] = value; | |
66 | } | |
67 | ag->num_samples = last_end; | |
68 | break; | |
69 | case PATTERN_SINE: | |
70 | frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD; | |
71 | ||
72 | /* Make sure the number of samples we put out is an integer | |
73 | * multiple of our period size */ | |
74 | /* FIXME we actually need only one period. A ringbuffer would be | |
75 | * useful here. */ | |
76 | while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0) | |
77 | num_samples--; | |
78 | ||
79 | for (i = 0; i < num_samples; i++) { | |
80 | t = (double) i / (double) sample_rate; | |
81 | ag->pattern_data[i] = ag->amplitude * | |
82 | sin(2 * G_PI * frequency * t); | |
83 | } | |
84 | ||
85 | ag->num_samples = num_samples; | |
86 | break; | |
87 | case PATTERN_TRIANGLE: | |
88 | frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD; | |
89 | ||
90 | while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0) | |
91 | num_samples--; | |
92 | ||
93 | for (i = 0; i < num_samples; i++) { | |
94 | t = (double) i / (double) sample_rate; | |
95 | ag->pattern_data[i] = (2 * ag->amplitude / G_PI) * | |
96 | asin(sin(2 * G_PI * frequency * t)); | |
97 | } | |
98 | ||
99 | ag->num_samples = num_samples; | |
100 | break; | |
101 | case PATTERN_SAWTOOTH: | |
102 | frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD; | |
103 | ||
104 | while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0) | |
105 | num_samples--; | |
106 | ||
107 | for (i = 0; i < num_samples; i++) { | |
108 | t = (double) i / (double) sample_rate; | |
109 | ag->pattern_data[i] = 2 * ag->amplitude * | |
110 | ((t * frequency) - floor(0.5f + t * frequency)); | |
111 | } | |
112 | ||
113 | ag->num_samples = num_samples; | |
114 | break; | |
115 | } | |
116 | } | |
117 | ||
118 | static void logic_generator(struct sr_dev_inst *sdi, uint64_t size) | |
119 | { | |
120 | struct dev_context *devc; | |
121 | uint64_t i, j; | |
122 | uint8_t pat; | |
123 | ||
124 | devc = sdi->priv; | |
125 | ||
126 | switch (devc->logic_pattern) { | |
127 | case PATTERN_SIGROK: | |
128 | memset(devc->logic_data, 0x00, size); | |
129 | for (i = 0; i < size; i += devc->logic_unitsize) { | |
130 | for (j = 0; j < devc->logic_unitsize; j++) { | |
131 | pat = pattern_sigrok[(devc->step + j) % sizeof(pattern_sigrok)] >> 1; | |
132 | devc->logic_data[i + j] = ~pat; | |
133 | } | |
134 | devc->step++; | |
135 | } | |
136 | break; | |
137 | case PATTERN_RANDOM: | |
138 | for (i = 0; i < size; i++) | |
139 | devc->logic_data[i] = (uint8_t)(rand() & 0xff); | |
140 | break; | |
141 | case PATTERN_INC: | |
142 | for (i = 0; i < size; i++) { | |
143 | for (j = 0; j < devc->logic_unitsize; j++) { | |
144 | devc->logic_data[i + j] = devc->step; | |
145 | } | |
146 | devc->step++; | |
147 | } | |
148 | break; | |
149 | case PATTERN_ALL_LOW: | |
150 | case PATTERN_ALL_HIGH: | |
151 | /* These were set when the pattern mode was selected. */ | |
152 | break; | |
153 | default: | |
154 | sr_err("Unknown pattern: %d.", devc->logic_pattern); | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
159 | static void send_analog_packet(struct analog_gen *ag, | |
160 | struct sr_dev_inst *sdi, uint64_t *analog_sent, | |
161 | uint64_t analog_pos, uint64_t analog_todo) | |
162 | { | |
163 | struct sr_datafeed_packet packet; | |
164 | struct dev_context *devc; | |
165 | uint64_t sending_now, to_avg; | |
166 | int ag_pattern_pos; | |
167 | unsigned int i; | |
168 | ||
169 | devc = sdi->priv; | |
170 | packet.type = SR_DF_ANALOG; | |
171 | packet.payload = &ag->packet; | |
172 | ||
173 | if (!devc->avg) { | |
174 | ag_pattern_pos = analog_pos % ag->num_samples; | |
175 | sending_now = MIN(analog_todo, ag->num_samples-ag_pattern_pos); | |
176 | ag->packet.data = ag->pattern_data + ag_pattern_pos; | |
177 | ag->packet.num_samples = sending_now; | |
178 | sr_session_send(sdi, &packet); | |
179 | ||
180 | /* Whichever channel group gets there first. */ | |
181 | *analog_sent = MAX(*analog_sent, sending_now); | |
182 | } else { | |
183 | ag_pattern_pos = analog_pos % ag->num_samples; | |
184 | to_avg = MIN(analog_todo, ag->num_samples-ag_pattern_pos); | |
185 | ||
186 | for (i = 0; i < to_avg; i++) { | |
187 | ag->avg_val = (ag->avg_val + | |
188 | *(ag->pattern_data + | |
189 | ag_pattern_pos + i)) / 2; | |
190 | ag->num_avgs++; | |
191 | /* Time to send averaged data? */ | |
192 | if (devc->avg_samples > 0 && | |
193 | ag->num_avgs >= devc->avg_samples) | |
194 | goto do_send; | |
195 | } | |
196 | ||
197 | if (devc->avg_samples == 0) { | |
198 | /* We're averaging all the samples, so wait with | |
199 | * sending until the very end. | |
200 | */ | |
201 | *analog_sent = ag->num_avgs; | |
202 | return; | |
203 | } | |
204 | ||
205 | do_send: | |
206 | ag->packet.data = &ag->avg_val; | |
207 | ag->packet.num_samples = 1; | |
208 | ||
209 | sr_session_send(sdi, &packet); | |
210 | *analog_sent = ag->num_avgs; | |
211 | ||
212 | ag->num_avgs = 0; | |
213 | ag->avg_val = 0.0f; | |
214 | } | |
215 | } | |
216 | ||
217 | /* Callback handling data */ | |
218 | SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data) | |
219 | { | |
220 | struct sr_dev_inst *sdi; | |
221 | struct dev_context *devc; | |
222 | struct sr_datafeed_packet packet; | |
223 | struct sr_datafeed_logic logic; | |
224 | struct analog_gen *ag; | |
225 | GHashTableIter iter; | |
226 | void *value; | |
227 | uint64_t samples_todo, logic_done, analog_done, analog_sent, sending_now; | |
228 | int64_t elapsed_us, limit_us, todo_us; | |
229 | ||
230 | (void)fd; | |
231 | (void)revents; | |
232 | ||
233 | sdi = cb_data; | |
234 | devc = sdi->priv; | |
235 | ||
236 | /* Just in case. */ | |
237 | if (devc->cur_samplerate <= 0 | |
238 | || (devc->num_logic_channels <= 0 | |
239 | && devc->num_analog_channels <= 0)) { | |
240 | sdi->driver->dev_acquisition_stop(sdi); | |
241 | return G_SOURCE_CONTINUE; | |
242 | } | |
243 | ||
244 | /* What time span should we send samples for? */ | |
245 | elapsed_us = g_get_monotonic_time() - devc->start_us; | |
246 | limit_us = 1000 * devc->limit_msec; | |
247 | if (limit_us > 0 && limit_us < elapsed_us) | |
248 | todo_us = MAX(0, limit_us - devc->spent_us); | |
249 | else | |
250 | todo_us = MAX(0, elapsed_us - devc->spent_us); | |
251 | ||
252 | /* How many samples are outstanding since the last round? */ | |
253 | samples_todo = (todo_us * devc->cur_samplerate + G_USEC_PER_SEC - 1) | |
254 | / G_USEC_PER_SEC; | |
255 | if (devc->limit_samples > 0) { | |
256 | if (devc->limit_samples < devc->sent_samples) | |
257 | samples_todo = 0; | |
258 | else if (devc->limit_samples - devc->sent_samples < samples_todo) | |
259 | samples_todo = devc->limit_samples - devc->sent_samples; | |
260 | } | |
261 | /* Calculate the actual time covered by this run back from the sample | |
262 | * count, rounded towards zero. This avoids getting stuck on a too-low | |
263 | * time delta with no samples being sent due to round-off. | |
264 | */ | |
265 | todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate; | |
266 | ||
267 | logic_done = devc->num_logic_channels > 0 ? 0 : samples_todo; | |
268 | analog_done = devc->num_analog_channels > 0 ? 0 : samples_todo; | |
269 | ||
270 | while (logic_done < samples_todo || analog_done < samples_todo) { | |
271 | /* Logic */ | |
272 | if (logic_done < samples_todo) { | |
273 | sending_now = MIN(samples_todo - logic_done, | |
274 | LOGIC_BUFSIZE / devc->logic_unitsize); | |
275 | logic_generator(sdi, sending_now * devc->logic_unitsize); | |
276 | packet.type = SR_DF_LOGIC; | |
277 | packet.payload = &logic; | |
278 | logic.length = sending_now * devc->logic_unitsize; | |
279 | logic.unitsize = devc->logic_unitsize; | |
280 | logic.data = devc->logic_data; | |
281 | sr_session_send(sdi, &packet); | |
282 | logic_done += sending_now; | |
283 | } | |
284 | ||
285 | /* Analog, one channel at a time */ | |
286 | if (analog_done < samples_todo) { | |
287 | analog_sent = 0; | |
288 | ||
289 | g_hash_table_iter_init(&iter, devc->ch_ag); | |
290 | while (g_hash_table_iter_next(&iter, NULL, &value)) { | |
291 | send_analog_packet(value, sdi, &analog_sent, | |
292 | devc->sent_samples + analog_done, | |
293 | samples_todo - analog_done); | |
294 | } | |
295 | analog_done += analog_sent; | |
296 | } | |
297 | } | |
298 | /* At this point, both logic_done and analog_done should be | |
299 | * exactly equal to samples_todo, or else. | |
300 | */ | |
301 | if (logic_done != samples_todo || analog_done != samples_todo) { | |
302 | sr_err("BUG: Sample count mismatch."); | |
303 | return G_SOURCE_REMOVE; | |
304 | } | |
305 | devc->sent_samples += samples_todo; | |
306 | devc->spent_us += todo_us; | |
307 | ||
308 | if ((devc->limit_samples > 0 && devc->sent_samples >= devc->limit_samples) | |
309 | || (limit_us > 0 && devc->spent_us >= limit_us)) { | |
310 | ||
311 | /* If we're averaging everything - now is the time to send data */ | |
312 | if (devc->avg_samples == 0) { | |
313 | g_hash_table_iter_init(&iter, devc->ch_ag); | |
314 | while (g_hash_table_iter_next(&iter, NULL, &value)) { | |
315 | ag = value; | |
316 | packet.type = SR_DF_ANALOG; | |
317 | packet.payload = &ag->packet; | |
318 | ag->packet.data = &ag->avg_val; | |
319 | ag->packet.num_samples = 1; | |
320 | sr_session_send(sdi, &packet); | |
321 | } | |
322 | } | |
323 | sr_dbg("Requested number of samples reached."); | |
324 | sdi->driver->dev_acquisition_stop(sdi); | |
325 | } | |
326 | ||
327 | return G_SOURCE_CONTINUE; | |
328 | } |