]>
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 | |
2ea1fdf1 | 20 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
ba508e22 UH |
21 | */ |
22 | ||
23 | #include <config.h> | |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
26 | #include <math.h> | |
27 | #include <libsigrok/libsigrok.h> | |
28 | #include "libsigrok-internal.h" | |
29 | #include "protocol.h" | |
30 | ||
31 | #define ANALOG_SAMPLES_PER_PERIOD 20 | |
32 | ||
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, | |
42 | }; | |
43 | ||
44 | SR_PRIV void demo_generate_analog_pattern(struct analog_gen *ag, uint64_t sample_rate) | |
45 | { | |
46 | double t, frequency; | |
47 | float value; | |
48 | unsigned int num_samples, i; | |
49 | int last_end; | |
50 | ||
51 | sr_dbg("Generating %s pattern.", analog_pattern_str[ag->pattern]); | |
52 | ||
53 | num_samples = ANALOG_BUFSIZE / sizeof(float); | |
54 | ||
55 | switch (ag->pattern) { | |
56 | case PATTERN_SQUARE: | |
57 | value = ag->amplitude; | |
58 | last_end = 0; | |
59 | for (i = 0; i < num_samples; i++) { | |
60 | if (i % 5 == 0) | |
61 | value = -value; | |
62 | if (i % 10 == 0) | |
63 | last_end = i; | |
64 | ag->pattern_data[i] = value; | |
65 | } | |
66 | ag->num_samples = last_end; | |
67 | break; | |
68 | case PATTERN_SINE: | |
69 | frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD; | |
70 | ||
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 | |
74 | * useful here. */ | |
75 | while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0) | |
76 | num_samples--; | |
77 | ||
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); | |
82 | } | |
83 | ||
84 | ag->num_samples = num_samples; | |
85 | break; | |
86 | case PATTERN_TRIANGLE: | |
87 | frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD; | |
88 | ||
89 | while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0) | |
90 | num_samples--; | |
91 | ||
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)); | |
96 | } | |
97 | ||
98 | ag->num_samples = num_samples; | |
99 | break; | |
100 | case PATTERN_SAWTOOTH: | |
101 | frequency = (double) sample_rate / ANALOG_SAMPLES_PER_PERIOD; | |
102 | ||
103 | while (num_samples % ANALOG_SAMPLES_PER_PERIOD != 0) | |
104 | num_samples--; | |
105 | ||
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)); | |
110 | } | |
111 | ||
112 | ag->num_samples = num_samples; | |
113 | break; | |
114 | } | |
115 | } | |
116 | ||
117 | static void logic_generator(struct sr_dev_inst *sdi, uint64_t size) | |
118 | { | |
119 | struct dev_context *devc; | |
120 | uint64_t i, j; | |
121 | uint8_t pat; | |
122 | ||
123 | devc = sdi->priv; | |
124 | ||
125 | switch (devc->logic_pattern) { | |
126 | case PATTERN_SIGROK: | |
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; | |
132 | } | |
133 | devc->step++; | |
134 | } | |
135 | break; | |
136 | case PATTERN_RANDOM: | |
137 | for (i = 0; i < size; i++) | |
138 | devc->logic_data[i] = (uint8_t)(rand() & 0xff); | |
139 | break; | |
140 | case PATTERN_INC: | |
141 | for (i = 0; i < size; i++) { | |
142 | for (j = 0; j < devc->logic_unitsize; j++) { | |
143 | devc->logic_data[i + j] = devc->step; | |
144 | } | |
145 | devc->step++; | |
146 | } | |
147 | break; | |
148 | case PATTERN_ALL_LOW: | |
149 | case PATTERN_ALL_HIGH: | |
150 | /* These were set when the pattern mode was selected. */ | |
151 | break; | |
152 | default: | |
153 | sr_err("Unknown pattern: %d.", devc->logic_pattern); | |
154 | break; | |
155 | } | |
156 | } | |
157 | ||
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) | |
161 | { | |
162 | struct sr_datafeed_packet packet; | |
163 | struct dev_context *devc; | |
164 | uint64_t sending_now, to_avg; | |
165 | int ag_pattern_pos; | |
166 | unsigned int i; | |
167 | ||
168 | devc = sdi->priv; | |
169 | packet.type = SR_DF_ANALOG; | |
170 | packet.payload = &ag->packet; | |
171 | ||
172 | if (!devc->avg) { | |
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); | |
178 | ||
179 | /* Whichever channel group gets there first. */ | |
180 | *analog_sent = MAX(*analog_sent, sending_now); | |
181 | } else { | |
182 | ag_pattern_pos = analog_pos % ag->num_samples; | |
183 | to_avg = MIN(analog_todo, ag->num_samples-ag_pattern_pos); | |
184 | ||
185 | for (i = 0; i < to_avg; i++) { | |
186 | ag->avg_val = (ag->avg_val + | |
187 | *(ag->pattern_data + | |
188 | ag_pattern_pos + i)) / 2; | |
189 | ag->num_avgs++; | |
190 | /* Time to send averaged data? */ | |
191 | if (devc->avg_samples > 0 && | |
192 | ag->num_avgs >= devc->avg_samples) | |
193 | goto do_send; | |
194 | } | |
195 | ||
196 | if (devc->avg_samples == 0) { | |
197 | /* We're averaging all the samples, so wait with | |
198 | * sending until the very end. | |
199 | */ | |
200 | *analog_sent = ag->num_avgs; | |
201 | return; | |
202 | } | |
203 | ||
204 | do_send: | |
205 | ag->packet.data = &ag->avg_val; | |
206 | ag->packet.num_samples = 1; | |
207 | ||
208 | sr_session_send(sdi, &packet); | |
209 | *analog_sent = ag->num_avgs; | |
210 | ||
211 | ag->num_avgs = 0; | |
212 | ag->avg_val = 0.0f; | |
213 | } | |
214 | } | |
215 | ||
216 | /* Callback handling data */ | |
217 | SR_PRIV int demo_prepare_data(int fd, int revents, void *cb_data) | |
218 | { | |
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; | |
224 | GHashTableIter iter; | |
225 | void *value; | |
226 | uint64_t samples_todo, logic_done, analog_done, analog_sent, sending_now; | |
227 | int64_t elapsed_us, limit_us, todo_us; | |
228 | ||
229 | (void)fd; | |
230 | (void)revents; | |
231 | ||
232 | sdi = cb_data; | |
233 | devc = sdi->priv; | |
234 | ||
235 | /* Just in case. */ | |
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; | |
241 | } | |
242 | ||
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); | |
248 | else | |
249 | todo_us = MAX(0, elapsed_us - devc->spent_us); | |
250 | ||
251 | /* How many samples are outstanding since the last round? */ | |
252 | samples_todo = (todo_us * devc->cur_samplerate + G_USEC_PER_SEC - 1) | |
253 | / G_USEC_PER_SEC; | |
254 | if (devc->limit_samples > 0) { | |
255 | if (devc->limit_samples < devc->sent_samples) | |
256 | samples_todo = 0; | |
257 | else if (devc->limit_samples - devc->sent_samples < samples_todo) | |
258 | samples_todo = devc->limit_samples - devc->sent_samples; | |
259 | } | |
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. | |
263 | */ | |
264 | todo_us = samples_todo * G_USEC_PER_SEC / devc->cur_samplerate; | |
265 | ||
266 | logic_done = devc->num_logic_channels > 0 ? 0 : samples_todo; | |
267 | analog_done = devc->num_analog_channels > 0 ? 0 : samples_todo; | |
268 | ||
269 | while (logic_done < samples_todo || analog_done < samples_todo) { | |
270 | /* Logic */ | |
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; | |
282 | } | |
283 | ||
284 | /* Analog, one channel at a time */ | |
285 | if (analog_done < samples_todo) { | |
286 | analog_sent = 0; | |
287 | ||
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); | |
293 | } | |
294 | analog_done += analog_sent; | |
295 | } | |
296 | } | |
297 | /* At this point, both logic_done and analog_done should be | |
298 | * exactly equal to samples_todo, or else. | |
299 | */ | |
300 | if (logic_done != samples_todo || analog_done != samples_todo) { | |
301 | sr_err("BUG: Sample count mismatch."); | |
302 | return G_SOURCE_REMOVE; | |
303 | } | |
304 | devc->sent_samples += samples_todo; | |
305 | devc->spent_us += todo_us; | |
306 | ||
307 | if ((devc->limit_samples > 0 && devc->sent_samples >= devc->limit_samples) | |
308 | || (limit_us > 0 && devc->spent_us >= limit_us)) { | |
309 | ||
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)) { | |
314 | ag = 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); | |
320 | } | |
321 | } | |
322 | sr_dbg("Requested number of samples reached."); | |
323 | sdi->driver->dev_acquisition_stop(sdi); | |
324 | } | |
325 | ||
326 | return G_SOURCE_CONTINUE; | |
327 | } |