]>
Commit | Line | Data |
---|---|---|
dc90146e A |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
bac2a8b8 | 4 | * Copyright (C) 2022 Shawn Walker <ac0bi00@gmail.com> |
dc90146e A |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
bac2a8b8 A |
20 | #define _GNU_SOURCE |
21 | ||
dc90146e | 22 | #include <config.h> |
bac2a8b8 A |
23 | #include <errno.h> |
24 | #include <glib.h> | |
25 | #include <math.h> | |
26 | #include <stdlib.h> | |
27 | #include <stdarg.h> | |
28 | #include <string.h> | |
29 | #include <time.h> | |
30 | #include <unistd.h> | |
31 | #include <libsigrok/libsigrok.h> | |
32 | #include "libsigrok-internal.h" | |
dc90146e A |
33 | #include "protocol.h" |
34 | ||
ac132f83 A |
35 | SR_PRIV int send_serial_str(struct sr_serial_dev_inst *serial, char *str) |
36 | { | |
37 | int len = strlen(str); | |
38 | if ((len > 15) || (len < 1)) { //limit length to catch errant strings | |
39 | sr_err("ERROR:Serial string len %d invalid ", len); | |
40 | return SR_ERR; | |
41 | } | |
42 | //100ms timeout. With USB CDC serial we can't define the timeout | |
43 | //based on link rate, so just pick something large as we shouldn't normally see them | |
44 | if (serial_write_blocking(serial, str, len, 100) != len) { | |
45 | sr_err("ERROR:Serial str write failed"); | |
46 | return SR_ERR; | |
47 | } | |
48 | ||
49 | return SR_OK; | |
50 | } | |
51 | ||
52 | SR_PRIV int send_serial_char(struct sr_serial_dev_inst *serial, char ch) | |
53 | { | |
54 | char buf[1]; | |
55 | buf[0] = ch; | |
56 | if (serial_write_blocking(serial, buf, 1, 100) != 1) { //100ms | |
57 | sr_err("ERROR:Serial char write failed"); | |
58 | return SR_ERR; | |
59 | } | |
60 | return SR_OK; | |
bac2a8b8 | 61 | } |
ac132f83 | 62 | |
bac2a8b8 | 63 | //Issue a command that expects a string return, return length of string |
ac132f83 A |
64 | int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str, |
65 | char *resp, size_t cnt) | |
66 | { | |
67 | int num_read, i; | |
68 | send_serial_str(serial, str); | |
69 | //Using the serial_read_blocking function when reading a response of unknown length requires | |
70 | //a long worst case timeout to always be taken. So, instead loop waiting for a first byte, and | |
71 | //then a final small delay for the rest. | |
72 | for (i = 0; i < 1000; i++) { //wait up to 1 second in ms increments | |
73 | num_read = serial_read_blocking(serial, resp, cnt, 1); | |
74 | if (num_read > 0) | |
75 | break; | |
76 | } | |
77 | //sr_spew("rwprsp1 i %d nr %d",i,num_read); | |
78 | //Since the serial port is usb CDC we can't calculate timeouts based on baud rate but | |
79 | //even if the response is split between two USB transfers 10ms should be plenty. | |
80 | num_read += | |
81 | serial_read_blocking(serial, &(resp[num_read]), cnt - num_read, | |
82 | 10); | |
83 | //sr_spew("rwrsp2 nr %d",num_read); | |
84 | ||
85 | if ((num_read < 1) || (num_read > 30)) { | |
86 | sr_err("ERROR:Serial_w_resp failed (%d).", num_read); | |
87 | return -1; | |
88 | } else { | |
89 | return num_read; | |
90 | } | |
91 | } | |
92 | ||
bac2a8b8 | 93 | //Issue a command that expects a single char ack |
ac132f83 A |
94 | SR_PRIV int send_serial_w_ack(struct sr_serial_dev_inst *serial, char *str) |
95 | { | |
96 | char buf[2]; | |
97 | int num_read; | |
98 | //In case we have left over transfer from the device, drain them | |
99 | while ((num_read = serial_read_blocking(serial, buf, 2, 10))) { | |
100 | //sr_dbg("swack drops 2 previous bytes %d %d",buf[0],buf[1]); | |
101 | } | |
102 | send_serial_str(serial, str); | |
103 | //1000ms timeout | |
104 | num_read = serial_read_blocking(serial, buf, 1, 1000); | |
105 | if ((num_read == 1) && (buf[0] == '*')) { | |
106 | return SR_OK; | |
107 | } else { | |
108 | sr_err("ERROR:Serial_w_ack %s failed (%d).", str, | |
109 | num_read); | |
110 | if (num_read) { | |
111 | sr_err("ack resp char %c d %d\n\r", buf[0], | |
112 | buf[0]); | |
113 | } | |
114 | return SR_ERR; | |
115 | } | |
116 | } | |
bac2a8b8 A |
117 | |
118 | //Process incoming data stream assuming it is optimized packing of 4 channels or less | |
119 | //Each byte is 4 channels of data and a 3 bit rle value, or a larger rle value, or a control signal. | |
120 | //This also checks for aborts and ends. | |
121 | //If an end is seen we stop processing but do not check the byte_cnt | |
122 | //The output is a set of samples fed to process group to perform sw triggering and sending of data to the session | |
123 | //as well as maintenance of the serial rx byte cnt. | |
124 | //Since we can get huge rle values we chop them up for processing into smaller groups | |
125 | //In this mode we can always consume all bytes because there are no cases where the processing of one | |
126 | //byte requires the one after it. | |
ac132f83 A |
127 | void process_D4(struct sr_dev_inst *sdi, struct dev_context *d) |
128 | { | |
129 | uint32_t j; | |
130 | uint8_t cbyte; | |
131 | uint8_t cval; | |
132 | uint32_t rlecnt = 0; | |
133 | uint32_t sampcnt = 0; //number of samples received with no rles | |
134 | while (d->ser_rdptr < d->bytes_avail) { | |
135 | cbyte = d->buffer[(d->ser_rdptr)]; | |
136 | //RLE only byte | |
137 | if (cbyte >= 48 && cbyte <= 127) { | |
138 | rlecnt += (cbyte - 47) * 8; | |
139 | d->byte_cnt++; | |
140 | } else if (cbyte >= 0x80) { //sample with possible rle | |
141 | rlecnt += (cbyte & 0x70) >> 4; | |
142 | if (rlecnt) { | |
143 | //On a value change, duplicate the previous values first. | |
144 | //The maximum value of one rle is 640. | |
145 | //To ensure we don't overflow the sample buffer but still send it large chunks of data | |
146 | //(to make the packet sends to the session efficient) only call process group after | |
147 | //a large number of samples have been seen. | |
148 | //Likely we could use the max rle value of 640 but 2048 gives some extra room. | |
149 | if ((rlecnt + d->cbuf_wrptr) > | |
150 | (d->sample_buf_size - 2048)) { | |
151 | //process_group is sent the number of slices which is just the cbufwrptr divided by the slice size | |
152 | //This modulo check should never happen as long the calculations for dig_sample_bytes etc are | |
153 | //correct, but it's a good cross check for code development. | |
154 | if ((d->cbuf_wrptr) % | |
155 | (d->dig_sample_bytes)) { | |
156 | sr_err | |
157 | ("Modulo fail %d %d ", | |
158 | d->cbuf_wrptr, | |
159 | d->dig_sample_bytes); | |
160 | } | |
161 | process_group(sdi, d, | |
162 | (d->cbuf_wrptr / | |
163 | d->dig_sample_bytes)); | |
164 | } | |
165 | rle_memset(d, rlecnt); | |
166 | rlecnt = 0; | |
167 | sampcnt = 0; | |
168 | } | |
169 | //Finally add in the new values | |
170 | cval = cbyte & 0xF; | |
171 | d->d_data_buf[d->cbuf_wrptr++] = cval; | |
172 | //pad in all other bytes since the sessions even wants disabled channels reported | |
173 | for (j = 1; j < d->dig_sample_bytes; j++) { | |
174 | d->d_data_buf[d->cbuf_wrptr++] = 0; | |
175 | } | |
176 | sampcnt++; | |
177 | d->byte_cnt++; | |
178 | sr_spew | |
179 | ("Dchan4 rdptr %d wrptr %d bytein 0x%X rle %d cval 0x%X\n", | |
180 | (d->ser_rdptr) - 1, d->cbuf_wrptr, cbyte, | |
181 | rlecnt, cval); | |
182 | rlecnt = 0; | |
183 | ||
184 | d->d_last[0] = cval; | |
185 | } | |
186 | //Any other character ends parsing - it could be a frame error or a start of the final byte cnt | |
187 | else { | |
188 | if (cbyte == '$') { | |
189 | sr_info | |
190 | ("D4 Data stream stops with cbyte %d char %c rdidx %d cnt %llu", | |
191 | cbyte, cbyte, d->ser_rdptr, | |
192 | d->byte_cnt); | |
193 | d->rxstate = RX_STOPPED; | |
194 | } else { | |
195 | sr_err | |
196 | ("D4 Data stream aborts with cbyte %d char %c rdidx %d cnt %llu", | |
197 | cbyte, cbyte, d->ser_rdptr, | |
198 | d->byte_cnt); | |
199 | d->rxstate = RX_ABORT; | |
200 | } | |
201 | break; //break from while loop | |
202 | } | |
203 | (d->ser_rdptr)++; | |
204 | } //while rdptr < wrptr | |
205 | sr_spew("D4 while done rdptr %d", d->ser_rdptr); | |
206 | //If we reach the end of the serial input stream send any remaining values or rles to the session | |
207 | /*this can also be skipped now the rle_memset handles cbufwrptr | |
208 | if(sampcnt){ | |
209 | process_group(sdi,d,sampcnt); | |
210 | sampcnt=0; | |
211 | } | |
212 | */ | |
213 | if (rlecnt) { | |
214 | sr_spew("Residual D4 slice rlecnt %d", rlecnt); | |
215 | rle_memset(d, rlecnt); | |
216 | } | |
217 | if (d->cbuf_wrptr) { | |
218 | sr_spew("Residual D4 data wrptr %d", d->cbuf_wrptr); | |
219 | process_group(sdi, d, d->cbuf_wrptr / d->dig_sample_bytes); | |
220 | ||
221 | } | |
222 | ||
223 | } //Process_D4 | |
bac2a8b8 A |
224 | |
225 | //Process incoming data stream and forward to trigger processing with process_group | |
226 | //The final value of ser_rdptr indicates how many bytes were processed. | |
227 | //This version handles all other enabled channel configurations that Process_D4 doesn't | |
ac132f83 A |
228 | void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc) |
229 | { | |
230 | int32_t i; | |
231 | uint32_t tmp32; | |
232 | uint8_t cbyte; | |
233 | uint32_t slices_avail = 0; | |
234 | uint32_t cword; | |
235 | uint32_t slice_bytes; //number of bytes that have legal slice values | |
236 | //Only process legal data values for this mode which are >=0x80 | |
237 | for (slice_bytes = 1; (slice_bytes < devc->bytes_avail) | |
238 | && (devc->buffer[slice_bytes - 1] >= 0x80); slice_bytes++); | |
239 | if (slice_bytes != devc->bytes_avail) { | |
240 | cbyte = devc->buffer[slice_bytes - 1]; | |
241 | slice_bytes--; //Don't process the ending character | |
242 | if (cbyte == '$') { | |
243 | sr_info | |
244 | ("Data stream stops with cbyte %d char %c rdidx %d sbytes %d cnt %llu", | |
245 | cbyte, cbyte, devc->ser_rdptr, slice_bytes, | |
246 | devc->byte_cnt); | |
247 | devc->rxstate = RX_STOPPED; | |
248 | } else { | |
249 | sr_err | |
250 | ("Data stream aborts with cbyte %d char %c rdidx %d sbytes %d cnt %llu", | |
251 | cbyte, cbyte, devc->ser_rdptr, slice_bytes, | |
252 | devc->byte_cnt); | |
253 | devc->rxstate = RX_ABORT; | |
254 | } | |
255 | } | |
256 | //If the wrptr is non-zero due to a residual from the previous serial transfer don't double count it towards byte_cnt | |
257 | devc->byte_cnt += slice_bytes - (devc->wrptr); | |
258 | sr_spew("process slice avail %d rdptr %d sb %d byte_cnt %lld", | |
259 | devc->bytes_avail, devc->ser_rdptr, slice_bytes, | |
260 | devc->byte_cnt); | |
261 | //Must have a full slice | |
262 | while ((devc->ser_rdptr + devc->bytes_per_slice) <= slice_bytes) { | |
263 | //The use of devc->cbuf_wrptr is different between analog and digital. | |
264 | //For analog it targets a float sized offset for that channel's buffer | |
265 | //For digital it targets a bit, so the 3 lsbs are bit offsets within a byte | |
266 | slices_avail++; | |
267 | cword = 0; | |
268 | //build up a word 7 bits at a time, using only enabled channels | |
269 | for (i = 0; i < devc->num_d_channels; i += 7) { | |
270 | if (((devc->d_chan_mask) >> i) & 0x7F) { | |
271 | cword |= | |
272 | ((devc->buffer[devc->ser_rdptr]) & | |
273 | 0x7F) << i; | |
274 | (devc->ser_rdptr)++; | |
275 | } | |
276 | } | |
277 | //and then distribute 8 bits at a time to all possible channels | |
278 | for (i = 0; i < devc->num_d_channels; i += 8) { | |
279 | uint32_t idx = | |
280 | ((devc->cbuf_wrptr) * devc->dig_sample_bytes) + | |
281 | (i >> 3); | |
282 | devc->d_data_buf[idx] = cword & 0xFF; | |
283 | sr_spew | |
284 | ("Dchan i %d wrptr %d idx %d char 0x%X cword 0x%X", | |
285 | i, devc->cbuf_wrptr, idx, | |
286 | devc->d_data_buf[idx], cword); | |
287 | cword >>= 8; | |
288 | } | |
289 | //Each analog value is a 7 bit value | |
290 | for (i = 0; i < devc->num_a_channels; i++) { | |
291 | if ((devc->a_chan_mask >> i) & 1) { | |
292 | //a_size is depracted and must always be 1B | |
293 | tmp32 = | |
294 | devc->buffer[devc->ser_rdptr] - 0x80; | |
295 | devc->a_data_bufs[i][devc->cbuf_wrptr] = | |
296 | ((float) tmp32 * devc->a_scale[i]) + | |
297 | devc->a_offset[i]; | |
298 | devc->a_last[i] = | |
299 | devc->a_data_bufs[i][devc->cbuf_wrptr]; | |
300 | sr_spew | |
301 | ("AChan %d t32 %d value %f wrptr %d rdptr %d sc %f off %f", | |
302 | i, tmp32, | |
303 | devc-> | |
304 | a_data_bufs[i][devc->cbuf_wrptr], | |
305 | devc->cbuf_wrptr, devc->ser_rdptr, | |
306 | devc->a_scale[i], devc->a_offset[i]); | |
307 | devc->ser_rdptr++; | |
308 | } //if channel enabled | |
309 | } //for num_a_channels | |
310 | devc->cbuf_wrptr++; | |
311 | } //While another slice available | |
312 | if (slices_avail) { | |
313 | process_group(sdi, devc, slices_avail); | |
314 | } | |
bac2a8b8 A |
315 | |
316 | } | |
ac132f83 | 317 | |
bac2a8b8 | 318 | //Send the processed analog values to the session |
ac132f83 A |
319 | int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc, |
320 | uint32_t num_samples, uint32_t offset) | |
321 | { | |
322 | struct sr_datafeed_packet packet; | |
323 | struct sr_datafeed_analog analog; | |
324 | struct sr_analog_encoding encoding; | |
325 | struct sr_analog_meaning meaning; | |
326 | struct sr_analog_spec spec; | |
327 | struct sr_channel *ch; | |
328 | uint32_t i; | |
329 | float *fptr; | |
330 | ||
331 | sr_analog_init(&analog, &encoding, &meaning, &spec, ANALOG_DIGITS); | |
332 | for (i = 0; i < devc->num_a_channels; i++) { | |
333 | if ((devc->a_chan_mask >> i) & 1) { | |
334 | ch = devc->analog_groups[i]->channels->data; | |
335 | analog.meaning->channels = | |
336 | g_slist_append(NULL, ch); | |
337 | analog.num_samples = num_samples; | |
338 | analog.data = (devc->a_data_bufs[i]) + offset; | |
339 | fptr = analog.data; | |
340 | sr_spew | |
341 | ("send analog num %d offset %d first %f 2 %f", | |
342 | num_samples, offset, *(devc->a_data_bufs[i]), | |
343 | *fptr); | |
344 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
345 | analog.meaning->unit = SR_UNIT_VOLT; | |
346 | analog.meaning->mqflags = 0; | |
347 | packet.type = SR_DF_ANALOG; | |
348 | packet.payload = &analog; | |
349 | sr_session_send(sdi, &packet); | |
350 | g_slist_free(analog.meaning->channels); | |
351 | } //if enabled | |
352 | } //for channels | |
353 | return 0; | |
bac2a8b8 A |
354 | |
355 | } | |
ac132f83 | 356 | |
bac2a8b8 A |
357 | //Send the ring buffer of pre-trigger analog samples. |
358 | // The entire buffer is sent (as long as it filled once), but need send two payloads split at the | |
359 | // the writeptr | |
ac132f83 A |
360 | int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc, |
361 | uint32_t num_samples) | |
362 | { | |
363 | struct sr_datafeed_packet packet; | |
364 | struct sr_datafeed_analog analog; | |
365 | struct sr_analog_encoding encoding; | |
366 | struct sr_analog_meaning meaning; | |
367 | struct sr_analog_spec spec; | |
368 | struct sr_channel *ch; | |
369 | int i; | |
370 | uint32_t num_pre, start_pre; | |
371 | uint32_t num_post, start_post; | |
372 | num_pre = | |
373 | (num_samples >= | |
374 | devc->pretrig_wr_ptr) ? devc->pretrig_wr_ptr : num_samples; | |
375 | start_pre = devc->pretrig_wr_ptr - num_pre; | |
376 | num_post = num_samples - num_pre; | |
377 | start_post = devc->pretrig_entries - num_post; | |
378 | sr_spew | |
379 | ("send_analog ring wrptr %u ns %d npre %u spre %u npost %u spost %u", | |
380 | devc->pretrig_wr_ptr, num_samples, num_pre, start_pre, | |
381 | num_post, start_post); | |
382 | float *fptr; | |
383 | sr_analog_init(&analog, &encoding, &meaning, &spec, ANALOG_DIGITS); | |
384 | for (i = 0; i < devc->num_a_channels; i++) { | |
385 | if ((devc->a_chan_mask >> i) & 1) { | |
386 | ch = devc->analog_groups[i]->channels->data; | |
387 | analog.meaning->channels = | |
388 | g_slist_append(NULL, ch); | |
389 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
390 | analog.meaning->unit = SR_UNIT_VOLT; | |
391 | analog.meaning->mqflags = 0; | |
392 | packet.type = SR_DF_ANALOG; | |
393 | packet.payload = &analog; | |
394 | //First send what is after the write pointer because it is oldest | |
395 | if (num_post) { | |
396 | analog.num_samples = num_post; | |
397 | analog.data = | |
398 | (devc->a_pretrig_bufs[i]) + start_post; | |
399 | //sr_spew("ring buf %d starts at %p",i,(void *) devc->a_pretrig_bufs[i]); | |
400 | //sr_spew("analog data %d starts at %p",i,(void *) analog.data); | |
401 | //sr_spew("Sending A%d ring buffer oldest ",i); | |
402 | for (uint32_t j = 0; | |
403 | j < analog.num_samples; j++) { | |
404 | fptr = | |
405 | analog.data + | |
406 | (j * sizeof(float)); | |
407 | //sr_spew("RNGDCT%d j %d %f %p",i,j,*fptr,(void *)fptr); | |
408 | } | |
409 | sr_session_send(sdi, &packet); | |
410 | } | |
411 | if (num_pre) { | |
412 | analog.num_samples = num_pre; | |
413 | analog.data = | |
414 | (devc->a_pretrig_bufs[i]) + start_pre; | |
415 | sr_dbg("Sending A%d ring buffer newest ", | |
416 | i); | |
417 | for (uint32_t j = 0; | |
418 | j < analog.num_samples; j++) { | |
419 | fptr = | |
420 | analog.data + | |
421 | (j * sizeof(float)); | |
422 | sr_spew("RNGDCW%d j %d %f %p", i, | |
423 | j, *fptr, (void *) fptr); | |
424 | } | |
425 | sr_session_send(sdi, &packet); | |
426 | } | |
427 | g_slist_free(analog.meaning->channels); | |
428 | sr_dbg("Sending A%d ring buffer done ", i); | |
429 | } //if enabled | |
430 | } //for channels | |
431 | return 0; | |
bac2a8b8 A |
432 | |
433 | } | |
434 | ||
435 | //Given a chunk of slices forward to trigger check or session as appropriate and update state | |
436 | //these could be real slices or those generated by rles | |
ac132f83 A |
437 | int process_group(struct sr_dev_inst *sdi, struct dev_context *devc, |
438 | uint32_t num_slices) | |
439 | { | |
440 | int trigger_offset; | |
441 | int pre_trigger_samples; | |
442 | //These are samples sent to session and are less than num_slices if we reach limit_samples | |
443 | size_t num_samples; | |
444 | struct sr_datafeed_logic logic; | |
445 | struct sr_datafeed_packet packet; | |
446 | int i; | |
447 | size_t cbuf_wrptr_cpy; | |
448 | cbuf_wrptr_cpy = devc->cbuf_wrptr; | |
449 | //regardless of whether we forward samples on or not (because we aren't triggered), always reset the | |
450 | //pointer into the device data buffers | |
451 | devc->cbuf_wrptr = 0; | |
452 | if (devc->trigger_fired) { //send directly to session | |
453 | if (devc->limit_samples && | |
454 | num_slices > | |
455 | devc->limit_samples - devc->sent_samples) { | |
456 | num_samples = | |
457 | devc->limit_samples - devc->sent_samples; | |
458 | } else { | |
459 | num_samples = num_slices; | |
460 | } | |
461 | if (num_samples > 0) { | |
462 | sr_spew | |
463 | ("Process_group sending %d post trig samples dsb %d", | |
464 | num_samples, devc->dig_sample_bytes); | |
465 | //for(int z=0;(z<num_samples);z+=2){ | |
466 | // sr_spew("0x%X ",devc->d_data_buf[z]); | |
467 | //} | |
468 | if (devc->num_d_channels) { | |
469 | packet.type = SR_DF_LOGIC; | |
470 | packet.payload = &logic; | |
471 | //Size the number of bytes required to fit all of the channels | |
472 | logic.unitsize = devc->dig_sample_bytes; | |
473 | //The total length of the array sent | |
474 | logic.length = | |
475 | num_samples * logic.unitsize; | |
476 | logic.data = devc->d_data_buf; | |
477 | sr_session_send(sdi, &packet); | |
478 | } | |
479 | send_analog(sdi, devc, num_samples, 0); | |
480 | } //num_sample>0 | |
481 | devc->sent_samples += num_samples; | |
482 | return 0; | |
483 | } //trigger_fired | |
484 | else { | |
485 | size_t num_ring_samples; | |
486 | size_t sptr; | |
487 | size_t eptr; | |
488 | size_t numtail; | |
489 | size_t numwrap; | |
490 | size_t srcptr; | |
491 | //sr_spew("Process_group check %d pre trig samples",num_slices); | |
492 | //The trigger_offset is -1 if no trigger is found, but if a trigger is found | |
493 | //then trigger_offset is the offset into the data buffer sent to it. | |
494 | //The pre_trigger_samples is the total number of samples before the trigger, but limited to | |
495 | //the size of the ring buffer set by the capture_ratio. So the pre_trigger_samples can include both the new samples | |
496 | //and the ring buffer, but trigger_offset is only in relation to the new samples | |
497 | trigger_offset = soft_trigger_logic_check(devc->stl, | |
498 | devc->d_data_buf, | |
499 | num_slices * | |
500 | devc->dig_sample_bytes, | |
501 | &pre_trigger_samples); | |
502 | //A trigger offset >=0 indicate a trigger was seen. The stl will isue the trigger to the session | |
503 | //and will forward all pre trigger logic samples, but we must send any post trigger logic | |
504 | //and all pre and post trigger analog signals | |
505 | // sr_dbg("trggr_off %d",trigger_offset); | |
506 | // sr_dbg("pre_samp %d",pre_trigger_samples); | |
507 | if (trigger_offset > -1) { | |
508 | devc->trigger_fired = TRUE; | |
509 | devc->sent_samples += pre_trigger_samples; | |
510 | packet.type = SR_DF_LOGIC; | |
511 | packet.payload = &logic; | |
512 | num_samples = num_slices - trigger_offset; | |
bac2a8b8 A |
513 | //Since we are in continuous mode for SW triggers it is possible to get more samples than limit_samples, so |
514 | //once the trigger fires make sure we don't get beyond limit samples. At this point sent_samples should | |
515 | //be equal to pre_trigger_samples (just added above) because without being triggered we'd never increment | |
516 | //sent_samples. | |
517 | //This number is the number of post trigger logic samples to send to the session, the number of floats | |
518 | //is larger because of the analog ring buffer we track. | |
ac132f83 A |
519 | if (devc->limit_samples && |
520 | num_samples > | |
521 | devc->limit_samples - devc->sent_samples) | |
522 | num_samples = | |
523 | devc->limit_samples - | |
524 | devc->sent_samples; | |
525 | //The soft trigger logic issues the trigger and sends packest for all logic data that was pretrigger | |
526 | //so only send what is left | |
527 | if (num_samples > 0) { | |
528 | sr_dbg | |
529 | ("Sending post trigger logical remainder of %d", | |
530 | num_samples); | |
531 | logic.length = | |
532 | num_samples * devc->dig_sample_bytes; | |
533 | logic.unitsize = devc->dig_sample_bytes; | |
534 | logic.data = | |
535 | devc->d_data_buf + | |
536 | (trigger_offset * | |
537 | devc->dig_sample_bytes); | |
538 | devc->sent_samples += num_samples; | |
539 | sr_session_send(sdi, &packet); | |
540 | } | |
541 | size_t new_start, new_end, new_samples, | |
542 | ring_samples; | |
543 | //Figure out the analog data to send. | |
544 | //We might need to send: | |
545 | //-some or all of incoming data | |
546 | //-all of incoming data and some of ring buffer | |
547 | //-all of incoming data and all of ring buffer (and still might be short) | |
548 | //We don't need to compare to limit_samples because pretrig_entries can never be more than limit_samples | |
549 | //trigger offset indicatese where in the new samples the trigger was, but we need to go back pretrig_entries before it | |
550 | new_start = | |
551 | (trigger_offset > | |
552 | (int) devc->pretrig_entries) ? trigger_offset | |
553 | - devc->pretrig_entries : 0; | |
554 | //Note that we might not have gotten all the pre triggerstore data we were looking for. In such a case the sw trigger | |
555 | //logic seems to fill up to the limit_samples and thus the ratio is off, but we get the full number of samples | |
556 | //The number of entries in the ring buffer is pre_trigger_samples-trigger_offset so subtract that from limit samples | |
557 | //as a threshold | |
558 | new_end = | |
559 | MIN(num_slices - 1, | |
560 | devc->limit_samples - | |
561 | (pre_trigger_samples - trigger_offset) - | |
562 | 1); | |
563 | //This includes pre and post trigger storage. | |
564 | new_samples = new_end - new_start + 1; | |
565 | //pre_trigger_samples can never be greater than trigger_offset by more than the ring buffer depth (pretrig entries) | |
566 | ring_samples = | |
567 | (pre_trigger_samples > | |
568 | trigger_offset) ? pre_trigger_samples - | |
569 | trigger_offset : 0; | |
570 | sr_spew | |
571 | ("SW trigger float info newstart %zu new_end %zu new_samp %zu ring_samp %zu", | |
572 | new_start, new_end, new_samples, | |
573 | ring_samples); | |
574 | if (ring_samples > 0) { | |
575 | send_analog_ring(sdi, devc, ring_samples); | |
576 | } | |
577 | if (new_samples) { | |
578 | send_analog(sdi, devc, new_samples, | |
579 | new_start); | |
580 | } | |
581 | ||
582 | } //if trigger_offset | |
583 | else { //We didn't trigger but need to copy to ring buffer | |
584 | if ((devc->a_chan_mask) && (devc->pretrig_entries)) { | |
585 | //The incoming data buffer could be much larger than the ring buffer, so never copy more than | |
586 | //the size of the ring buffer | |
587 | num_ring_samples = | |
588 | num_slices > | |
589 | devc-> | |
590 | pretrig_entries ? devc->pretrig_entries | |
591 | : num_slices; | |
592 | sptr = devc->pretrig_wr_ptr; //starting pointer to copy to | |
593 | //endptr can't go past the end | |
594 | eptr = | |
595 | (sptr + num_ring_samples) >= | |
596 | devc-> | |
597 | pretrig_entries ? devc->pretrig_entries | |
598 | - 1 : sptr + num_ring_samples - 1; | |
599 | numtail = (eptr - sptr) + 1; //number of samples to copy to the tail of ring buffer without wrapping | |
600 | numwrap = | |
601 | (num_ring_samples > | |
602 | numtail) ? num_ring_samples - | |
603 | numtail : 0; | |
604 | //cbuf_wrptr points to where the next write should go, not theactual write data | |
605 | srcptr = cbuf_wrptr_cpy - num_ring_samples; | |
606 | sr_spew("RNG num %zu sptr %zu eptr %zu ", | |
607 | num_ring_samples, sptr, eptr); | |
608 | //sr_spew("RNG srcptr %zu nt %zu nw %zu",srcptr,numtail,numwrap); | |
609 | for (i = 0; i < devc->num_a_channels; i++) { | |
610 | if ((devc->a_chan_mask >> i) & 1) { | |
611 | //copy tail | |
612 | for (uint32_t j = 0; | |
613 | j < numtail; j++) { | |
614 | devc->a_pretrig_bufs | |
615 | [i][sptr + j] = | |
616 | devc->a_data_bufs | |
617 | [i] | |
618 | [srcptr + j]; | |
619 | //sr_spew("RNGCpyT C%d src %zu dest %zu",i,srcptr+j,sptr+j); | |
620 | } //for j | |
621 | } //if chan_mask | |
622 | } //for channels | |
623 | //Copy wrap | |
624 | srcptr += numtail; | |
625 | for (i = 0; i < devc->num_a_channels; i++) { | |
626 | if ((devc->a_chan_mask >> i) & 1) { | |
627 | for (uint32_t j = 0; | |
628 | j < numwrap; j++) { | |
629 | devc->a_pretrig_bufs | |
630 | [i][j] = | |
631 | devc->a_data_bufs | |
632 | [i] | |
633 | [srcptr + j]; | |
634 | //sr_spew("RNGCpyW C%d src %zu dest %zu",i,srcptr+j,j); | |
635 | } //for j | |
636 | } //if chan_mask | |
637 | } //for channels | |
638 | devc->pretrig_wr_ptr = | |
639 | (numwrap) ? numwrap : (eptr + | |
640 | 1) % | |
641 | devc->pretrig_entries; | |
642 | //sr_dbg("RNG pwrptr new %u",devc->pretrig_wr_ptr); | |
643 | } //if any analog channel enabled and pretrig_entries | |
644 | } //else (trigger not detected) | |
645 | } //trigger not set on function entry | |
646 | return 0; | |
647 | } //process_group | |
bac2a8b8 A |
648 | |
649 | ||
650 | //Duplicate previous sample values | |
651 | //This function relies on the caller to ensure d_data_buf has samples to handle the full value of the rle | |
ac132f83 A |
652 | void rle_memset(struct dev_context *devc, uint32_t num_slices) |
653 | { | |
654 | uint32_t j, k; | |
655 | sr_spew("rle_memset val 0x%X,slices %d dsb %d\n", devc->d_last[0], | |
656 | num_slices, devc->dig_sample_bytes); | |
657 | //Even if a channel is disabled, PV expects the same location and size for the enabled | |
658 | // channels as if the channel were enabled. | |
659 | for (j = 0; j < num_slices; j++) { | |
660 | for (k = 0; k < devc->dig_sample_bytes; k++) { | |
661 | devc->d_data_buf[devc->cbuf_wrptr++] = | |
662 | devc->d_last[k]; | |
663 | //sr_spew("k %d j %d v 0x%X",k,j,devc->d_data_buf[(devc->cbuf_wrptr)-1]); | |
664 | } | |
665 | } | |
bac2a8b8 A |
666 | } |
667 | ||
668 | //This callback function is mapped from api.c with serial_source_add and is created after a capture | |
669 | //has been setup and is responsible for querying the device trigger status, downloading data | |
670 | //and forwarding packets | |
671 | SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data) | |
dc90146e | 672 | { |
ac132f83 A |
673 | struct sr_dev_inst *sdi; |
674 | struct dev_context *devc; | |
675 | struct sr_serial_dev_inst *serial; | |
676 | uint32_t i; | |
677 | int len; | |
678 | uint32_t bytes_rem; | |
679 | uint32_t residual_bytes; | |
680 | (void) fd; | |
681 | ||
682 | if (!(sdi = cb_data)) | |
683 | return TRUE; | |
684 | ||
685 | if (!(devc = sdi->priv)) | |
686 | return TRUE; | |
687 | if (devc->rxstate != RX_ACTIVE) { | |
688 | //This condition is normal operation and expected to happen | |
689 | //but printed as information | |
690 | sr_dbg("Reached non active state in receive %d", | |
691 | devc->rxstate); | |
692 | //don't return - we may be waiting for a final bytecnt | |
693 | //return TRUE; | |
694 | } | |
695 | if (devc->rxstate == RX_IDLE) { | |
696 | //This is the normal end condition where we do one more receive | |
697 | //to make sure we get the full byte_cnt | |
698 | sr_dbg("Reached idle state in receive %d", devc->rxstate); | |
699 | return FALSE; | |
700 | } | |
701 | ||
702 | serial = sdi->conn; | |
703 | //return true if it is some kind of event we don't handle | |
704 | if (!(revents == G_IO_IN || revents == 0)) | |
705 | return TRUE; | |
706 | //Fill the buffer, note the end may have partial slices | |
707 | bytes_rem = devc->serial_buffer_size - devc->wrptr; | |
708 | //Read one byte less so that we can null it and print as a string | |
709 | //Do a small 10ms timeout, if we get nothing, we'll always come back again | |
710 | len = | |
711 | serial_read_blocking(serial, &(devc->buffer[devc->wrptr]), | |
712 | bytes_rem - 1, 10); | |
713 | sr_spew("Entry wrptr %u bytes_rem %u len %d", devc->wrptr, | |
714 | bytes_rem, len); | |
715 | ||
716 | if (len > 0) { | |
717 | devc->buffer[devc->wrptr + len] = 0; | |
718 | //Add the "#" so that spaces are clearly seen | |
719 | sr_dbg("rx string %s#", devc->buffer); | |
720 | //This is not guaranteed to be a dataloss condition, but definitely indicates we are | |
721 | //processing data right at the incoming rate. | |
722 | //With the addition of the byte_cnt sent at the end we will detect any dataloss conditions | |
723 | //and thus this is disabled | |
724 | //if(len>=(int)bytes_rem-8){ | |
725 | // sr_err("ERROR: Serial buffer near or at max depth, data from device may have been lost"); | |
726 | //} | |
727 | devc->bytes_avail = (devc->wrptr + len); | |
728 | sr_spew | |
729 | ("rx len %d bytes_avail %ul sent_samples %ul wrptr %u", | |
730 | len, devc->bytes_avail, devc->sent_samples, | |
731 | devc->wrptr); | |
732 | //sr_err("rx len %d ",len); | |
733 | } else if (len == 0) { | |
734 | return TRUE; | |
735 | } else { | |
736 | sr_err("ERROR:Negative serial read code %d", len); | |
737 | sdi->driver->dev_acquisition_stop(sdi); | |
738 | return FALSE; | |
739 | } //len>0 | |
740 | //This can be used as a bit bucket to drop all samples to see how host processing time effects | |
741 | //the devices ability to send data. Obviously no data will be forwarded to the session so it will hang | |
742 | // return TRUE; | |
743 | ||
744 | //Process the serial read data | |
745 | devc->ser_rdptr = 0; | |
746 | if (devc->rxstate == RX_ACTIVE) { | |
747 | if ((devc->a_chan_mask == 0) | |
748 | && ((devc->d_chan_mask & 0xFFFFFFF0) == 0)) { | |
749 | process_D4(sdi, devc); | |
750 | } else { | |
751 | process_slice(sdi, devc); | |
752 | } | |
753 | } | |
754 | //process_slice/process_D4 increment ser_rdptr as bytes of the serial buffer are used | |
755 | //But they may not use all of it, and thus the residual unused bytes are shifted to the start of the buffer | |
756 | //for the next call. | |
757 | residual_bytes = devc->bytes_avail - devc->ser_rdptr; | |
758 | //sr_spew("Residuals resid %d avail %d rdptr %d wrptr %d\n",residual_bytes,devc->bytes_avail,devc->ser_rdptr,devc->wrptr); | |
759 | if (residual_bytes) { | |
760 | for (i = 0; i < residual_bytes; i++) { | |
761 | devc->buffer[i] = | |
762 | devc->buffer[i + devc->ser_rdptr]; | |
763 | } | |
764 | devc->ser_rdptr = 0; | |
765 | devc->wrptr = residual_bytes; | |
766 | sr_spew("Residual shift rdptr %u wrptr %u", | |
767 | devc->ser_rdptr, devc->wrptr); | |
768 | } else { | |
769 | //If there are no residuals shifted then zero the wrptr since all data is used | |
770 | devc->wrptr = 0; | |
771 | } | |
772 | //ABORT ends immediately | |
773 | if (devc->rxstate == RX_ABORT) { | |
774 | sr_err("Ending receive on abort"); | |
775 | sdi->driver->dev_acquisition_stop(sdi); | |
776 | return FALSE; // | |
777 | } | |
778 | //if stopped look for final '+' indicating the full byte_cnt is received | |
779 | if (devc->rxstate == RX_STOPPED) { | |
780 | sr_dbg("Stopped, checking byte_cnt"); | |
781 | if (devc->buffer[0] != '$') { | |
782 | //If this happens it means that we got a set of data that was not processed as | |
783 | //whole groups of slice bytes. So either we lost data or are not parsing it correctly. | |
784 | sr_err("ERROR: Stop marker should be byte zero"); | |
785 | devc->rxstate = RX_ABORT; | |
786 | sdi->driver->dev_acquisition_stop(sdi); | |
787 | return FALSE; | |
788 | } | |
789 | for (i = 1; i < devc->wrptr; i++) { | |
790 | if (devc->buffer[i] == '+') { | |
791 | devc->buffer[i] = 0; | |
792 | uint64_t rxbytecnt; | |
793 | rxbytecnt = atol((char *)&(devc->buffer[1])); | |
794 | sr_dbg | |
795 | ("Byte_cnt check device cnt %llu host cnt %llu", | |
796 | rxbytecnt, devc->byte_cnt); | |
797 | if (rxbytecnt != devc->byte_cnt) { | |
798 | sr_err | |
799 | ("ERROR: received %llu and counted %llu bytecnts don't match, data may be lost", | |
800 | rxbytecnt, devc->byte_cnt); | |
801 | } | |
802 | //Since we got the bytecnt we know the device is done sending data | |
803 | devc->rxstate = RX_IDLE; | |
804 | //We must always call acquisition_stop on all completed runs | |
805 | sdi->driver->dev_acquisition_stop(sdi); | |
806 | return TRUE; | |
807 | } | |
808 | } | |
809 | //It's possible we need one more serial transfer to get the byte_cnt, so print that here | |
810 | sr_dbg("Haven't seen byte_cnt + yet"); | |
811 | } //RX_STOPPED | |
812 | //If at the sample limit, send a "+" in case we are in continuous mode and need | |
813 | //to stop the device. Not that even in non continous mode there might be cases where get an extra | |
814 | //sample or two... | |
815 | ||
816 | if ((devc->sent_samples >= devc->limit_samples) | |
817 | && (devc->rxstate == RX_ACTIVE)) { | |
818 | sr_dbg | |
819 | ("Ending: sent %u of limit %llu samples byte_cnt %llu", | |
820 | devc->sent_samples, devc->limit_samples, | |
821 | devc->byte_cnt); | |
822 | send_serial_char(serial, '+'); | |
823 | ||
824 | } | |
825 | sr_spew | |
826 | ("Receive function done: sent %u limit %llu wrptr %u len %d", | |
827 | devc->sent_samples, devc->limit_samples, devc->wrptr, len); | |
828 | return TRUE; | |
829 | } //raspberrypi_pico_receive | |
bac2a8b8 A |
830 | |
831 | //Read device specific information from the device | |
832 | SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi) | |
833 | { | |
ac132f83 A |
834 | struct dev_context *devc; |
835 | struct sr_serial_dev_inst *serial; | |
836 | char *cmd, response[20]; | |
837 | gchar **tokens; | |
838 | unsigned int i; | |
839 | int ret, num_tokens; | |
840 | ||
841 | devc = sdi->priv; | |
842 | sr_dbg("At get_dev_cfg"); | |
843 | serial = sdi->conn; | |
844 | for (i = 0; i < devc->num_a_channels; i++) { | |
845 | cmd = g_strdup_printf("a%d\n", i); | |
846 | ret = send_serial_w_resp(serial, cmd, response, 20); | |
847 | if (ret <= 0) { | |
848 | sr_err | |
849 | ("ERROR:No response from device for analog channel query"); | |
850 | return SR_ERR; | |
851 | } | |
852 | //null end of string for strsplit | |
853 | response[ret] = 0; | |
854 | tokens = NULL; | |
855 | tokens = g_strsplit(response, "x", 0); | |
856 | num_tokens = g_strv_length(tokens); | |
857 | if (num_tokens == 2) { | |
858 | devc->a_scale[i] = | |
859 | ((float) atoi(tokens[0])) / 1000000.0; | |
860 | devc->a_offset[i] = | |
861 | ((float) atoi(tokens[1])) / 1000000.0; | |
862 | sr_dbg | |
863 | ("A%d scale %f offset %f response #%s# tokens #%s# #%s#\n", | |
864 | i, devc->a_scale[i], devc->a_offset[i], | |
865 | response, tokens[0], tokens[1]); | |
866 | } else { | |
867 | sr_err | |
868 | ("ERROR:Ascale read c%d got unparseable response %s tokens %d", | |
869 | i, response, num_tokens); | |
870 | //force a legal fixed value assuming a 3.3V scale | |
871 | //a failue in parsing the scale | |
872 | devc->a_scale[i] = 0.0257; | |
873 | devc->a_offset[i] = 0.0; | |
874 | } | |
875 | g_strfreev(tokens); | |
876 | g_free(cmd); | |
877 | } | |
dc90146e | 878 | |
bac2a8b8 | 879 | |
ac132f83 A |
880 | return SR_OK; |
881 | ||
882 | } |