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 while (d->ser_rdptr < d->bytes_avail) {
134 cbyte = d->buffer[(d->ser_rdptr)];
136 if (cbyte >= 48 && cbyte <= 127) {
137 rlecnt += (cbyte - 47) * 8;
139 } else if (cbyte >= 0x80) { //sample with possible rle
140 rlecnt += (cbyte & 0x70) >> 4;
142 //On a value change, duplicate the previous values first.
143 rle_memset(d, rlecnt);
146 //Finally add in the new values
148 uint32_t didx=(d->cbuf_wrptr) * (d->dig_sample_bytes);
149 d->d_data_buf[didx] = cval;
150 //pad in all other bytes since the sessions even wants disabled channels reported
151 for (j = 1; j < d->dig_sample_bytes; j++) {
152 d->d_data_buf[didx+j] = 0;
156 ("Dchan4 rdptr %d wrptr %d bytein 0x%X rle %d cval 0x%X didx %d\n",
157 (d->ser_rdptr) - 1, d->cbuf_wrptr, cbyte,
163 //Any other character ends parsing - it could be a frame error or a start of the final byte cnt
167 ("D4 Data stream stops with cbyte %d char %c rdidx %d cnt %llu",
168 cbyte, cbyte, d->ser_rdptr,
170 d->rxstate = RX_STOPPED;
173 ("D4 Data stream aborts with cbyte %d char %c rdidx %d cnt %llu",
174 cbyte, cbyte, d->ser_rdptr,
176 d->rxstate = RX_ABORT;
178 break; //break from while loop
181 //To ensure we don't overflow the sample buffer, but still send it large chunks of data
182 //(to make the packet sends to the session efficient) only call process group after
183 //a large number of samples have been seen.
184 //cbuf_wrptr counts slices, so shift right by 2 to create a worst case x4 multiple ratio of
185 //cbuf_wrptr value to the depth of the sample buffer.
186 //Likely we could use the max rle value of 640 but 1024 gives some extra room.
187 //Also do a simple check of rlecnt>2000 since that is a reasonable minimal value to send to the session
189 ||((rlecnt + ((d->cbuf_wrptr)<<2))) > (d->sample_buf_size - 1024)) {
190 sr_spew("D4 preoverflow wrptr %d bufsize %d rlecnt %d\n\r",d->cbuf_wrptr,d->sample_buf_size,rlecnt);
191 rle_memset(d, rlecnt);
192 process_group(sdi, d, d->cbuf_wrptr);
196 }//while rdptr < wrptr
197 sr_spew("D4 while done rdptr %d", d->ser_rdptr);
198 //If we reach the end of the serial input stream send any remaining values or rles to the session
200 sr_spew("Residual D4 slice rlecnt %d", rlecnt);
201 rle_memset(d, rlecnt);
204 sr_spew("Residual D4 data wrptr %d", d->cbuf_wrptr);
205 process_group(sdi, d, d->cbuf_wrptr);
211 //Process incoming data stream and forward to trigger processing with process_group
212 //The final value of ser_rdptr indicates how many bytes were processed.
213 //This version handles all other enabled channel configurations that Process_D4 doesn't
214 void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc)
220 uint32_t slice_bytes; //number of bytes that have legal slice values including RLE
221 //Only process legal data values for this mode which are 0x32-0x7F for RLE and 0x80 to 0xFF for data
222 for (slice_bytes = 1; (slice_bytes < devc->bytes_avail)
223 && (devc->buffer[slice_bytes - 1] >= 0x30); slice_bytes++);
224 if (slice_bytes != devc->bytes_avail) {
225 cbyte = devc->buffer[slice_bytes - 1];
226 slice_bytes--; //Don't process the ending character
229 ("Data stream stops with cbyte %d char %c rdidx %d sbytes %d cnt %llu",
230 cbyte, cbyte, devc->ser_rdptr, slice_bytes,
232 devc->rxstate = RX_STOPPED;
235 ("Data stream aborts with cbyte %d char %c rdidx %d sbytes %d cnt %llu",
236 cbyte, cbyte, devc->ser_rdptr, slice_bytes,
238 devc->rxstate = RX_ABORT;
241 //If the wrptr is non-zero due to a residual from the previous serial transfer don't double count it towards byte_cnt
242 devc->byte_cnt += slice_bytes - (devc->wrptr);
243 sr_spew("process slice avail %d rdptr %d sb %d byte_cnt %lld",
244 devc->bytes_avail, devc->ser_rdptr, slice_bytes,
246 //Must have a full slice or one rle byte
247 while (((devc->ser_rdptr + devc->bytes_per_slice) <= slice_bytes)
248 ||((devc->ser_rdptr < slice_bytes)&&(devc->buffer[devc->ser_rdptr] < 0x80))) {
250 if(devc->buffer[devc->ser_rdptr] < 0x80){
252 if(devc->buffer[devc->ser_rdptr]<=79){
253 rlecnt=devc->buffer[devc->ser_rdptr]-47;
255 rlecnt=(devc->buffer[devc->ser_rdptr]-78)*32;
257 sr_info("RLEcnt of %d in %d",rlecnt,devc->buffer[devc->ser_rdptr]);
258 if((rlecnt < 1)||(rlecnt>1568)){
259 sr_err("Bad rlecnt val %d in %d",rlecnt,devc->buffer[devc->ser_rdptr]);
261 rle_memset(devc,rlecnt);
267 //build up a word 7 bits at a time, using only enabled channels
268 for (i = 0; i < devc->num_d_channels; i += 7) {
269 if (((devc->d_chan_mask) >> i) & 0x7F) {
271 ((devc->buffer[devc->ser_rdptr]) &
276 //and then distribute 8 bits at a time to all possible channels
277 //but first save of cword for rle
278 devc->d_last[0]=cword&0xFF;
279 devc->d_last[1]=(cword>>8)&0xFF;
280 devc->d_last[2]=(cword>>16)&0xFF;
281 devc->d_last[3]=(cword>>24)&0xFF;
282 for (i = 0; i < devc->num_d_channels; i += 8) {
284 ((devc->cbuf_wrptr) * devc->dig_sample_bytes) +
286 devc->d_data_buf[idx] = cword & 0xFF;
288 ("Dchan i %d wrptr %d idx %d char 0x%X cword 0x%X",
289 i, devc->cbuf_wrptr, idx,
290 devc->d_data_buf[idx], cword);
294 //Each analog value is a 7 bit value
295 for (i = 0; i < devc->num_a_channels; i++) {
296 if ((devc->a_chan_mask >> i) & 1) {
297 //a_size is depracted and must always be 1B
299 devc->buffer[devc->ser_rdptr] - 0x80;
300 devc->a_data_bufs[i][devc->cbuf_wrptr] =
301 ((float) tmp32 * devc->a_scale[i]) +
304 devc->a_data_bufs[i][devc->cbuf_wrptr];
306 ("AChan %d t32 %d value %f wrptr %d rdptr %d sc %f off %f",
309 a_data_bufs[i][devc->cbuf_wrptr],
310 devc->cbuf_wrptr, devc->ser_rdptr,
311 devc->a_scale[i], devc->a_offset[i]);
313 } //if channel enabled
314 } //for num_a_channels
317 //RLEs can create a large number of samples relative to the incoming serial buffer
318 //To prevent overflow of the sample data buffer we call process_group.
319 //cbuf_wrptr and sample_buf_size are both in terms of slices
320 //2048 is more than needed for a max rle of 1640 on the next incoming character
321 if((devc->cbuf_wrptr +2048) > devc->sample_buf_size){
322 sr_spew("Drain large buff %d %d\n\r",devc->cbuf_wrptr,devc->sample_buf_size);
323 process_group(sdi, devc, devc->cbuf_wrptr);
326 }//While another slice or RLE available
327 if (devc->cbuf_wrptr){
328 process_group(sdi, devc, devc->cbuf_wrptr);
333 //Send the processed analog values to the session
334 int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
335 uint32_t num_samples, uint32_t offset)
337 struct sr_datafeed_packet packet;
338 struct sr_datafeed_analog analog;
339 struct sr_analog_encoding encoding;
340 struct sr_analog_meaning meaning;
341 struct sr_analog_spec spec;
342 struct sr_channel *ch;
346 sr_analog_init(&analog, &encoding, &meaning, &spec, ANALOG_DIGITS);
347 for (i = 0; i < devc->num_a_channels; i++) {
348 if ((devc->a_chan_mask >> i) & 1) {
349 ch = devc->analog_groups[i]->channels->data;
350 analog.meaning->channels =
351 g_slist_append(NULL, ch);
352 analog.num_samples = num_samples;
353 analog.data = (devc->a_data_bufs[i]) + offset;
356 ("send analog num %d offset %d first %f 2 %f",
357 num_samples, offset, *(devc->a_data_bufs[i]),
359 analog.meaning->mq = SR_MQ_VOLTAGE;
360 analog.meaning->unit = SR_UNIT_VOLT;
361 analog.meaning->mqflags = 0;
362 packet.type = SR_DF_ANALOG;
363 packet.payload = &analog;
364 sr_session_send(sdi, &packet);
365 g_slist_free(analog.meaning->channels);
372 //Send the ring buffer of pre-trigger analog samples.
373 // The entire buffer is sent (as long as it filled once), but need send two payloads split at the
375 int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
376 uint32_t num_samples)
378 struct sr_datafeed_packet packet;
379 struct sr_datafeed_analog analog;
380 struct sr_analog_encoding encoding;
381 struct sr_analog_meaning meaning;
382 struct sr_analog_spec spec;
383 struct sr_channel *ch;
385 uint32_t num_pre, start_pre;
386 uint32_t num_post, start_post;
389 devc->pretrig_wr_ptr) ? devc->pretrig_wr_ptr : num_samples;
390 start_pre = devc->pretrig_wr_ptr - num_pre;
391 num_post = num_samples - num_pre;
392 start_post = devc->pretrig_entries - num_post;
394 ("send_analog ring wrptr %u ns %d npre %u spre %u npost %u spost %u",
395 devc->pretrig_wr_ptr, num_samples, num_pre, start_pre,
396 num_post, start_post);
398 sr_analog_init(&analog, &encoding, &meaning, &spec, ANALOG_DIGITS);
399 for (i = 0; i < devc->num_a_channels; i++) {
400 if ((devc->a_chan_mask >> i) & 1) {
401 ch = devc->analog_groups[i]->channels->data;
402 analog.meaning->channels =
403 g_slist_append(NULL, ch);
404 analog.meaning->mq = SR_MQ_VOLTAGE;
405 analog.meaning->unit = SR_UNIT_VOLT;
406 analog.meaning->mqflags = 0;
407 packet.type = SR_DF_ANALOG;
408 packet.payload = &analog;
409 //First send what is after the write pointer because it is oldest
411 analog.num_samples = num_post;
413 (devc->a_pretrig_bufs[i]) + start_post;
414 //sr_spew("ring buf %d starts at %p",i,(void *) devc->a_pretrig_bufs[i]);
415 //sr_spew("analog data %d starts at %p",i,(void *) analog.data);
416 //sr_spew("Sending A%d ring buffer oldest ",i);
418 j < analog.num_samples; j++) {
422 //sr_spew("RNGDCT%d j %d %f %p",i,j,*fptr,(void *)fptr);
424 sr_session_send(sdi, &packet);
427 analog.num_samples = num_pre;
429 (devc->a_pretrig_bufs[i]) + start_pre;
430 sr_dbg("Sending A%d ring buffer newest ",
433 j < analog.num_samples; j++) {
437 sr_spew("RNGDCW%d j %d %f %p", i,
438 j, *fptr, (void *) fptr);
440 sr_session_send(sdi, &packet);
442 g_slist_free(analog.meaning->channels);
443 sr_dbg("Sending A%d ring buffer done ", i);
450 //Given a chunk of slices forward to trigger check or session as appropriate and update state
451 //these could be real slices or those generated by rles
452 int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
456 int pre_trigger_samples;
457 //These are samples sent to session and are less than num_slices if we reach limit_samples
459 struct sr_datafeed_logic logic;
460 struct sr_datafeed_packet packet;
462 size_t cbuf_wrptr_cpy;
463 cbuf_wrptr_cpy = devc->cbuf_wrptr;
464 //regardless of whether we forward samples on or not (because we aren't triggered), always reset the
465 //pointer into the device data buffers
466 devc->cbuf_wrptr = 0;
467 if (devc->trigger_fired) { //send directly to session
468 if (devc->limit_samples &&
470 devc->limit_samples - devc->sent_samples) {
472 devc->limit_samples - devc->sent_samples;
474 num_samples = num_slices;
476 if (num_samples > 0) {
478 ("Process_group sending %d post trig samples dsb %d",
479 num_samples, devc->dig_sample_bytes);
480 //for(int z=0;(z<num_samples);z++){
481 // sr_spew("0x%X ",devc->d_data_buf[z]);
483 if (devc->num_d_channels) {
484 packet.type = SR_DF_LOGIC;
485 packet.payload = &logic;
486 //Size the number of bytes required to fit all of the channels
487 logic.unitsize = devc->dig_sample_bytes;
488 //The total length of the array sent
490 num_samples * logic.unitsize;
491 logic.data = devc->d_data_buf;
492 sr_session_send(sdi, &packet);
494 send_analog(sdi, devc, num_samples, 0);
496 devc->sent_samples += num_samples;
500 size_t num_ring_samples;
506 //sr_spew("Process_group check %d pre trig samples",num_slices);
507 //The trigger_offset is -1 if no trigger is found, but if a trigger is found
508 //then trigger_offset is the offset into the data buffer sent to it.
509 //The pre_trigger_samples is the total number of samples before the trigger, but limited to
510 //the size of the ring buffer set by the capture_ratio. So the pre_trigger_samples can include both the new samples
511 //and the ring buffer, but trigger_offset is only in relation to the new samples
512 trigger_offset = soft_trigger_logic_check(devc->stl,
515 devc->dig_sample_bytes,
516 &pre_trigger_samples);
517 //A trigger offset >=0 indicate a trigger was seen. The stl will isue the trigger to the session
518 //and will forward all pre trigger logic samples, but we must send any post trigger logic
519 //and all pre and post trigger analog signals
520 // sr_dbg("trggr_off %d",trigger_offset);
521 // sr_dbg("pre_samp %d",pre_trigger_samples);
522 if (trigger_offset > -1) {
523 devc->trigger_fired = TRUE;
524 devc->sent_samples += pre_trigger_samples;
525 packet.type = SR_DF_LOGIC;
526 packet.payload = &logic;
527 num_samples = num_slices - trigger_offset;
528 //Since we are in continuous mode for SW triggers it is possible to get more samples than limit_samples, so
529 //once the trigger fires make sure we don't get beyond limit samples. At this point sent_samples should
530 //be equal to pre_trigger_samples (just added above) because without being triggered we'd never increment
532 //This number is the number of post trigger logic samples to send to the session, the number of floats
533 //is larger because of the analog ring buffer we track.
534 if (devc->limit_samples &&
536 devc->limit_samples - devc->sent_samples)
538 devc->limit_samples -
540 //The soft trigger logic issues the trigger and sends packest for all logic data that was pretrigger
541 //so only send what is left
542 if (num_samples > 0) {
544 ("Sending post trigger logical remainder of %d",
547 num_samples * devc->dig_sample_bytes;
548 logic.unitsize = devc->dig_sample_bytes;
552 devc->dig_sample_bytes);
553 devc->sent_samples += num_samples;
554 sr_session_send(sdi, &packet);
556 size_t new_start, new_end, new_samples,
558 //Figure out the analog data to send.
559 //We might need to send:
560 //-some or all of incoming data
561 //-all of incoming data and some of ring buffer
562 //-all of incoming data and all of ring buffer (and still might be short)
563 //We don't need to compare to limit_samples because pretrig_entries can never be more than limit_samples
564 //trigger offset indicatese where in the new samples the trigger was, but we need to go back pretrig_entries before it
567 (int) devc->pretrig_entries) ? trigger_offset
568 - devc->pretrig_entries : 0;
569 //Note that we might not have gotten all the pre triggerstore data we were looking for. In such a case the sw trigger
570 //logic seems to fill up to the limit_samples and thus the ratio is off, but we get the full number of samples
571 //The number of entries in the ring buffer is pre_trigger_samples-trigger_offset so subtract that from limit samples
575 devc->limit_samples -
576 (pre_trigger_samples - trigger_offset) -
578 //This includes pre and post trigger storage.
579 new_samples = new_end - new_start + 1;
580 //pre_trigger_samples can never be greater than trigger_offset by more than the ring buffer depth (pretrig entries)
582 (pre_trigger_samples >
583 trigger_offset) ? pre_trigger_samples -
586 ("SW trigger float info newstart %zu new_end %zu new_samp %zu ring_samp %zu",
587 new_start, new_end, new_samples,
589 if (ring_samples > 0) {
590 send_analog_ring(sdi, devc, ring_samples);
593 send_analog(sdi, devc, new_samples,
597 } //if trigger_offset
598 else { //We didn't trigger but need to copy to ring buffer
599 if ((devc->a_chan_mask) && (devc->pretrig_entries)) {
600 //The incoming data buffer could be much larger than the ring buffer, so never copy more than
601 //the size of the ring buffer
605 pretrig_entries ? devc->pretrig_entries
607 sptr = devc->pretrig_wr_ptr; //starting pointer to copy to
608 //endptr can't go past the end
610 (sptr + num_ring_samples) >=
612 pretrig_entries ? devc->pretrig_entries
613 - 1 : sptr + num_ring_samples - 1;
614 numtail = (eptr - sptr) + 1; //number of samples to copy to the tail of ring buffer without wrapping
617 numtail) ? num_ring_samples -
619 //cbuf_wrptr points to where the next write should go, not theactual write data
620 srcptr = cbuf_wrptr_cpy - num_ring_samples;
621 sr_spew("RNG num %zu sptr %zu eptr %zu ",
622 num_ring_samples, sptr, eptr);
623 //sr_spew("RNG srcptr %zu nt %zu nw %zu",srcptr,numtail,numwrap);
624 for (i = 0; i < devc->num_a_channels; i++) {
625 if ((devc->a_chan_mask >> i) & 1) {
634 //sr_spew("RNGCpyT C%d src %zu dest %zu",i,srcptr+j,sptr+j);
640 for (i = 0; i < devc->num_a_channels; i++) {
641 if ((devc->a_chan_mask >> i) & 1) {
649 //sr_spew("RNGCpyW C%d src %zu dest %zu",i,srcptr+j,j);
653 devc->pretrig_wr_ptr =
654 (numwrap) ? numwrap : (eptr +
656 devc->pretrig_entries;
657 //sr_dbg("RNG pwrptr new %u",devc->pretrig_wr_ptr);
658 } //if any analog channel enabled and pretrig_entries
659 } //else (trigger not detected)
660 } //trigger not set on function entry
665 //Duplicate previous sample values
666 //This function relies on the caller to ensure d_data_buf has samples to handle the full value of the rle
667 void rle_memset(struct dev_context *devc, uint32_t num_slices)
670 sr_spew("rle_memset vals 0x%X, 0x%X, 0x%X slices %d dsb %d\n", devc->d_last[0],devc->d_last[1],devc->d_last[2],
671 num_slices, devc->dig_sample_bytes);
672 //Even if a channel is disabled, PV expects the same location and size for the enabled
673 // channels as if the channel were enabled.
674 for (j = 0; j < num_slices; j++) {
675 didx=devc->cbuf_wrptr*devc->dig_sample_bytes;
676 for (k = 0; k < devc->dig_sample_bytes; k++) {
677 devc->d_data_buf[didx + k] = devc->d_last[k];
678 // sr_spew("k %d j %d v 0x%X p %d didx %d",k,j,devc->d_data_buf[(devc->cbuf_wrptr)+k],(devc->cbuf_wrptr)+k,didx);
680 // cbuf_wrptr always counts slices/samples (and not the bytes in the buffer)
681 // regardless of mode
687 //This callback function is mapped from api.c with serial_source_add and is created after a capture
688 //has been setup and is responsible for querying the device trigger status, downloading data
689 //and forwarding packets
690 SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data)
692 struct sr_dev_inst *sdi;
693 struct dev_context *devc;
694 struct sr_serial_dev_inst *serial;
698 uint32_t residual_bytes;
701 if (!(sdi = cb_data))
704 if (!(devc = sdi->priv))
706 if (devc->rxstate != RX_ACTIVE) {
707 //This condition is normal operation and expected to happen
708 //but printed as information
709 sr_dbg("Reached non active state in receive %d",
711 //don't return - we may be waiting for a final bytecnt
714 if (devc->rxstate == RX_IDLE) {
715 //This is the normal end condition where we do one more receive
716 //to make sure we get the full byte_cnt
717 sr_dbg("Reached idle state in receive %d", devc->rxstate);
722 //return true if it is some kind of event we don't handle
723 if (!(revents == G_IO_IN || revents == 0))
725 //Fill the buffer, note the end may have partial slices
726 bytes_rem = devc->serial_buffer_size - devc->wrptr;
727 //Read one byte less so that we can null it and print as a string
728 //Do a small 10ms timeout, if we get nothing, we'll always come back again
730 serial_read_blocking(serial, &(devc->buffer[devc->wrptr]),
732 sr_spew("Entry wrptr %u bytes_rem %u len %d", devc->wrptr,
736 devc->buffer[devc->wrptr + len] = 0;
737 //Add the "#" so that spaces are clearly seen
738 sr_dbg("rx string %s#", devc->buffer);
739 //This is not guaranteed to be a dataloss condition, but definitely indicates we are
740 //processing data right at the incoming rate.
741 //With the addition of the byte_cnt sent at the end we will detect any dataloss conditions
742 //and thus this is disabled
743 //if(len>=(int)bytes_rem-8){
744 // sr_err("ERROR: Serial buffer near or at max depth, data from device may have been lost");
746 devc->bytes_avail = (devc->wrptr + len);
748 ("rx len %d bytes_avail %ul sent_samples %ul wrptr %u",
749 len, devc->bytes_avail, devc->sent_samples,
751 //sr_err("rx len %d ",len);
752 } else if (len == 0) {
755 sr_err("ERROR:Negative serial read code %d", len);
756 sdi->driver->dev_acquisition_stop(sdi);
759 //This can be used as a bit bucket to drop all samples to see how host processing time effects
760 //the devices ability to send data. Obviously no data will be forwarded to the session so it will hang
763 //Process the serial read data
765 if (devc->rxstate == RX_ACTIVE) {
766 if ((devc->a_chan_mask == 0)
767 && ((devc->d_chan_mask & 0xFFFFFFF0) == 0)) {
768 process_D4(sdi, devc);
770 process_slice(sdi, devc);
773 //process_slice/process_D4 increment ser_rdptr as bytes of the serial buffer are used
774 //But they may not use all of it, and thus the residual unused bytes are shifted to the start of the buffer
776 residual_bytes = devc->bytes_avail - devc->ser_rdptr;
777 //sr_spew("Residuals resid %d avail %d rdptr %d wrptr %d\n",residual_bytes,devc->bytes_avail,devc->ser_rdptr,devc->wrptr);
778 if (residual_bytes) {
779 for (i = 0; i < residual_bytes; i++) {
781 devc->buffer[i + devc->ser_rdptr];
784 devc->wrptr = residual_bytes;
785 sr_spew("Residual shift rdptr %u wrptr %u",
786 devc->ser_rdptr, devc->wrptr);
788 //If there are no residuals shifted then zero the wrptr since all data is used
791 //ABORT ends immediately
792 if (devc->rxstate == RX_ABORT) {
793 sr_err("Ending receive on abort");
794 sdi->driver->dev_acquisition_stop(sdi);
797 //if stopped look for final '+' indicating the full byte_cnt is received
798 if (devc->rxstate == RX_STOPPED) {
799 sr_dbg("Stopped, checking byte_cnt");
800 if (devc->buffer[0] != '$') {
801 //If this happens it means that we got a set of data that was not processed as
802 //whole groups of slice bytes. So either we lost data or are not parsing it correctly.
803 sr_err("ERROR: Stop marker should be byte zero");
804 devc->rxstate = RX_ABORT;
805 sdi->driver->dev_acquisition_stop(sdi);
808 for (i = 1; i < devc->wrptr; i++) {
809 if (devc->buffer[i] == '+') {
812 rxbytecnt = atol((char *)&(devc->buffer[1]));
814 ("Byte_cnt check device cnt %llu host cnt %llu",
815 rxbytecnt, devc->byte_cnt);
816 if (rxbytecnt != devc->byte_cnt) {
818 ("ERROR: received %llu and counted %llu bytecnts don't match, data may be lost",
819 rxbytecnt, devc->byte_cnt);
821 //Since we got the bytecnt we know the device is done sending data
822 devc->rxstate = RX_IDLE;
823 //We must always call acquisition_stop on all completed runs
824 sdi->driver->dev_acquisition_stop(sdi);
828 //It's possible we need one more serial transfer to get the byte_cnt, so print that here
829 sr_dbg("Haven't seen byte_cnt + yet");
831 //If at the sample limit, send a "+" in case we are in continuous mode and need
832 //to stop the device. Not that even in non continous mode there might be cases where get an extra
835 if ((devc->sent_samples >= devc->limit_samples)
836 && (devc->rxstate == RX_ACTIVE)) {
838 ("Ending: sent %u of limit %llu samples byte_cnt %llu",
839 devc->sent_samples, devc->limit_samples,
841 send_serial_char(serial, '+');
845 ("Receive function done: sent %u limit %llu wrptr %u len %d",
846 devc->sent_samples, devc->limit_samples, devc->wrptr, len);
848 } //raspberrypi_pico_receive
850 //Read device specific information from the device
851 SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi)
853 struct dev_context *devc;
854 struct sr_serial_dev_inst *serial;
855 char *cmd, response[20];
861 sr_dbg("At get_dev_cfg");
863 for (i = 0; i < devc->num_a_channels; i++) {
864 cmd = g_strdup_printf("a%d\n", i);
865 ret = send_serial_w_resp(serial, cmd, response, 20);
868 ("ERROR:No response from device for analog channel query");
871 //null end of string for strsplit
874 tokens = g_strsplit(response, "x", 0);
875 num_tokens = g_strv_length(tokens);
876 if (num_tokens == 2) {
878 ((float) atoi(tokens[0])) / 1000000.0;
880 ((float) atoi(tokens[1])) / 1000000.0;
882 ("A%d scale %f offset %f response #%s# tokens #%s# #%s#\n",
883 i, devc->a_scale[i], devc->a_offset[i],
884 response, tokens[0], tokens[1]);
887 ("ERROR:Ascale read c%d got unparseable response %s tokens %d",
888 i, response, num_tokens);
889 //force a legal fixed value assuming a 3.3V scale
890 //a failue in parsing the scale
891 devc->a_scale[i] = 0.0257;
892 devc->a_offset[i] = 0.0;