2 * This file is part of the libsigrok project.
4 * Copyright (C) 2022 Shawn Walker <ac0bi00@gmail.com>
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.
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.
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/>.
31 #include <libsigrok/libsigrok.h>
32 #include "libsigrok-internal.h"
35 SR_PRIV int send_serial_str(struct sr_serial_dev_inst *serial, char *str)
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);
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");
52 SR_PRIV int send_serial_char(struct sr_serial_dev_inst *serial, char ch)
56 if (serial_write_blocking(serial, buf, 1, 100) != 1) { //100ms
57 sr_err("ERROR:Serial char write failed");
63 //Issue a command that expects a string return, return length of string
64 int send_serial_w_resp(struct sr_serial_dev_inst *serial, char *str,
65 char *resp, size_t cnt)
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);
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.
81 serial_read_blocking(serial, &(resp[num_read]), cnt - num_read,
83 //sr_spew("rwrsp2 nr %d",num_read);
85 if ((num_read < 1) || (num_read > 30)) {
86 sr_err("ERROR:Serial_w_resp failed (%d).", num_read);
93 //Issue a command that expects a single char ack
94 SR_PRIV int send_serial_w_ack(struct sr_serial_dev_inst *serial, char *str)
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]);
102 send_serial_str(serial, str);
104 num_read = serial_read_blocking(serial, buf, 1, 1000);
105 if ((num_read == 1) && (buf[0] == '*')) {
108 sr_err("ERROR:Serial_w_ack %s failed (%d).", str,
111 sr_err("ack resp char %c d %d\n\r", buf[0],
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.
127 void process_D4(struct sr_dev_inst *sdi, struct dev_context *d)
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)];
137 if (cbyte >= 48 && cbyte <= 127) {
138 rlecnt += (cbyte - 47) * 8;
140 } else if (cbyte >= 0x80) { //sample with possible rle
141 rlecnt += (cbyte & 0x70) >> 4;
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)) {
157 ("Modulo fail %d %d ",
159 d->dig_sample_bytes);
161 process_group(sdi, d,
163 d->dig_sample_bytes));
165 rle_memset(d, rlecnt);
169 //Finally add in the new values
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;
179 ("Dchan4 rdptr %d wrptr %d bytein 0x%X rle %d cval 0x%X\n",
180 (d->ser_rdptr) - 1, d->cbuf_wrptr, cbyte,
186 //Any other character ends parsing - it could be a frame error or a start of the final byte cnt
190 ("D4 Data stream stops with cbyte %d char %c rdidx %d cnt %llu",
191 cbyte, cbyte, d->ser_rdptr,
193 d->rxstate = RX_STOPPED;
196 ("D4 Data stream aborts with cbyte %d char %c rdidx %d cnt %llu",
197 cbyte, cbyte, d->ser_rdptr,
199 d->rxstate = RX_ABORT;
201 break; //break from while loop
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
209 process_group(sdi,d,sampcnt);
214 sr_spew("Residual D4 slice rlecnt %d", rlecnt);
215 rle_memset(d, rlecnt);
218 sr_spew("Residual D4 data wrptr %d", d->cbuf_wrptr);
219 process_group(sdi, d, d->cbuf_wrptr / d->dig_sample_bytes);
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
228 void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc)
233 uint32_t slices_avail = 0;
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
244 ("Data stream stops with cbyte %d char %c rdidx %d sbytes %d cnt %llu",
245 cbyte, cbyte, devc->ser_rdptr, slice_bytes,
247 devc->rxstate = RX_STOPPED;
250 ("Data stream aborts with cbyte %d char %c rdidx %d sbytes %d cnt %llu",
251 cbyte, cbyte, devc->ser_rdptr, slice_bytes,
253 devc->rxstate = RX_ABORT;
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,
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
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) {
272 ((devc->buffer[devc->ser_rdptr]) &
277 //and then distribute 8 bits at a time to all possible channels
278 for (i = 0; i < devc->num_d_channels; i += 8) {
280 ((devc->cbuf_wrptr) * devc->dig_sample_bytes) +
282 devc->d_data_buf[idx] = cword & 0xFF;
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);
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
294 devc->buffer[devc->ser_rdptr] - 0x80;
295 devc->a_data_bufs[i][devc->cbuf_wrptr] =
296 ((float) tmp32 * devc->a_scale[i]) +
299 devc->a_data_bufs[i][devc->cbuf_wrptr];
301 ("AChan %d t32 %d value %f wrptr %d rdptr %d sc %f off %f",
304 a_data_bufs[i][devc->cbuf_wrptr],
305 devc->cbuf_wrptr, devc->ser_rdptr,
306 devc->a_scale[i], devc->a_offset[i]);
308 } //if channel enabled
309 } //for num_a_channels
311 } //While another slice available
313 process_group(sdi, devc, slices_avail);
318 //Send the processed analog values to the session
319 int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
320 uint32_t num_samples, uint32_t offset)
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;
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;
341 ("send analog num %d offset %d first %f 2 %f",
342 num_samples, offset, *(devc->a_data_bufs[i]),
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);
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
360 int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
361 uint32_t num_samples)
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;
370 uint32_t num_pre, start_pre;
371 uint32_t num_post, start_post;
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;
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);
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
396 analog.num_samples = num_post;
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);
403 j < analog.num_samples; j++) {
407 //sr_spew("RNGDCT%d j %d %f %p",i,j,*fptr,(void *)fptr);
409 sr_session_send(sdi, &packet);
412 analog.num_samples = num_pre;
414 (devc->a_pretrig_bufs[i]) + start_pre;
415 sr_dbg("Sending A%d ring buffer newest ",
418 j < analog.num_samples; j++) {
422 sr_spew("RNGDCW%d j %d %f %p", i,
423 j, *fptr, (void *) fptr);
425 sr_session_send(sdi, &packet);
427 g_slist_free(analog.meaning->channels);
428 sr_dbg("Sending A%d ring buffer done ", i);
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
437 int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
441 int pre_trigger_samples;
442 //These are samples sent to session and are less than num_slices if we reach limit_samples
444 struct sr_datafeed_logic logic;
445 struct sr_datafeed_packet packet;
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 &&
455 devc->limit_samples - devc->sent_samples) {
457 devc->limit_samples - devc->sent_samples;
459 num_samples = num_slices;
461 if (num_samples > 0) {
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]);
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
475 num_samples * logic.unitsize;
476 logic.data = devc->d_data_buf;
477 sr_session_send(sdi, &packet);
479 send_analog(sdi, devc, num_samples, 0);
481 devc->sent_samples += num_samples;
485 size_t num_ring_samples;
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,
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;
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
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.
519 if (devc->limit_samples &&
521 devc->limit_samples - devc->sent_samples)
523 devc->limit_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) {
529 ("Sending post trigger logical remainder of %d",
532 num_samples * devc->dig_sample_bytes;
533 logic.unitsize = devc->dig_sample_bytes;
537 devc->dig_sample_bytes);
538 devc->sent_samples += num_samples;
539 sr_session_send(sdi, &packet);
541 size_t new_start, new_end, new_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
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
560 devc->limit_samples -
561 (pre_trigger_samples - trigger_offset) -
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)
567 (pre_trigger_samples >
568 trigger_offset) ? pre_trigger_samples -
571 ("SW trigger float info newstart %zu new_end %zu new_samp %zu ring_samp %zu",
572 new_start, new_end, new_samples,
574 if (ring_samples > 0) {
575 send_analog_ring(sdi, devc, ring_samples);
578 send_analog(sdi, devc, new_samples,
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
590 pretrig_entries ? devc->pretrig_entries
592 sptr = devc->pretrig_wr_ptr; //starting pointer to copy to
593 //endptr can't go past the end
595 (sptr + num_ring_samples) >=
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
602 numtail) ? num_ring_samples -
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) {
619 //sr_spew("RNGCpyT C%d src %zu dest %zu",i,srcptr+j,sptr+j);
625 for (i = 0; i < devc->num_a_channels; i++) {
626 if ((devc->a_chan_mask >> i) & 1) {
634 //sr_spew("RNGCpyW C%d src %zu dest %zu",i,srcptr+j,j);
638 devc->pretrig_wr_ptr =
639 (numwrap) ? numwrap : (eptr +
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
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
652 void rle_memset(struct dev_context *devc, uint32_t num_slices)
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++] =
663 //sr_spew("k %d j %d v 0x%X",k,j,devc->d_data_buf[(devc->cbuf_wrptr)-1]);
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)
673 struct sr_dev_inst *sdi;
674 struct dev_context *devc;
675 struct sr_serial_dev_inst *serial;
679 uint32_t residual_bytes;
682 if (!(sdi = cb_data))
685 if (!(devc = sdi->priv))
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",
692 //don't return - we may be waiting for a final bytecnt
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);
703 //return true if it is some kind of event we don't handle
704 if (!(revents == G_IO_IN || revents == 0))
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
711 serial_read_blocking(serial, &(devc->buffer[devc->wrptr]),
713 sr_spew("Entry wrptr %u bytes_rem %u len %d", devc->wrptr,
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");
727 devc->bytes_avail = (devc->wrptr + len);
729 ("rx len %d bytes_avail %ul sent_samples %ul wrptr %u",
730 len, devc->bytes_avail, devc->sent_samples,
732 //sr_err("rx len %d ",len);
733 } else if (len == 0) {
736 sr_err("ERROR:Negative serial read code %d", len);
737 sdi->driver->dev_acquisition_stop(sdi);
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
744 //Process the serial read data
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);
751 process_slice(sdi, devc);
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
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++) {
762 devc->buffer[i + devc->ser_rdptr];
765 devc->wrptr = residual_bytes;
766 sr_spew("Residual shift rdptr %u wrptr %u",
767 devc->ser_rdptr, devc->wrptr);
769 //If there are no residuals shifted then zero the wrptr since all data is used
772 //ABORT ends immediately
773 if (devc->rxstate == RX_ABORT) {
774 sr_err("Ending receive on abort");
775 sdi->driver->dev_acquisition_stop(sdi);
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);
789 for (i = 1; i < devc->wrptr; i++) {
790 if (devc->buffer[i] == '+') {
793 rxbytecnt = atol((char *)&(devc->buffer[1]));
795 ("Byte_cnt check device cnt %llu host cnt %llu",
796 rxbytecnt, devc->byte_cnt);
797 if (rxbytecnt != devc->byte_cnt) {
799 ("ERROR: received %llu and counted %llu bytecnts don't match, data may be lost",
800 rxbytecnt, devc->byte_cnt);
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);
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");
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
816 if ((devc->sent_samples >= devc->limit_samples)
817 && (devc->rxstate == RX_ACTIVE)) {
819 ("Ending: sent %u of limit %llu samples byte_cnt %llu",
820 devc->sent_samples, devc->limit_samples,
822 send_serial_char(serial, '+');
826 ("Receive function done: sent %u limit %llu wrptr %u len %d",
827 devc->sent_samples, devc->limit_samples, devc->wrptr, len);
829 } //raspberrypi_pico_receive
831 //Read device specific information from the device
832 SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi)
834 struct dev_context *devc;
835 struct sr_serial_dev_inst *serial;
836 char *cmd, response[20];
842 sr_dbg("At get_dev_cfg");
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);
849 ("ERROR:No response from device for analog channel query");
852 //null end of string for strsplit
855 tokens = g_strsplit(response, "x", 0);
856 num_tokens = g_strv_length(tokens);
857 if (num_tokens == 2) {
859 ((float) atoi(tokens[0])) / 1000000.0;
861 ((float) atoi(tokens[1])) / 1000000.0;
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]);
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;