]> sigrok.org Git - libsigrok.git/blame - src/hardware/raspberrypi-pico/protocol.c
raspberrypi-pico: Rev2 updates
[libsigrok.git] / src / hardware / raspberrypi-pico / protocol.c
CommitLineData
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
35SR_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
52SR_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
64int 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
94SR_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
127void 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;
ac132f83
A
133 while (d->ser_rdptr < d->bytes_avail) {
134 cbyte = d->buffer[(d->ser_rdptr)];
135 //RLE only byte
136 if (cbyte >= 48 && cbyte <= 127) {
137 rlecnt += (cbyte - 47) * 8;
138 d->byte_cnt++;
139 } else if (cbyte >= 0x80) { //sample with possible rle
140 rlecnt += (cbyte & 0x70) >> 4;
141 if (rlecnt) {
142 //On a value change, duplicate the previous values first.
ac132f83
A
143 rle_memset(d, rlecnt);
144 rlecnt = 0;
ac132f83
A
145 }
146 //Finally add in the new values
147 cval = cbyte & 0xF;
0c792900
A
148 uint32_t didx=(d->cbuf_wrptr) * (d->dig_sample_bytes);
149 d->d_data_buf[didx] = cval;
ac132f83
A
150 //pad in all other bytes since the sessions even wants disabled channels reported
151 for (j = 1; j < d->dig_sample_bytes; j++) {
0c792900 152 d->d_data_buf[didx+j] = 0;
ac132f83 153 }
ac132f83
A
154 d->byte_cnt++;
155 sr_spew
0c792900 156 ("Dchan4 rdptr %d wrptr %d bytein 0x%X rle %d cval 0x%X didx %d\n",
ac132f83 157 (d->ser_rdptr) - 1, d->cbuf_wrptr, cbyte,
0c792900
A
158 rlecnt, cval,didx);
159 d->cbuf_wrptr++;
ac132f83 160 rlecnt = 0;
ac132f83
A
161 d->d_last[0] = cval;
162 }
163 //Any other character ends parsing - it could be a frame error or a start of the final byte cnt
164 else {
165 if (cbyte == '$') {
166 sr_info
167 ("D4 Data stream stops with cbyte %d char %c rdidx %d cnt %llu",
168 cbyte, cbyte, d->ser_rdptr,
169 d->byte_cnt);
170 d->rxstate = RX_STOPPED;
171 } else {
172 sr_err
173 ("D4 Data stream aborts with cbyte %d char %c rdidx %d cnt %llu",
174 cbyte, cbyte, d->ser_rdptr,
175 d->byte_cnt);
176 d->rxstate = RX_ABORT;
177 }
178 break; //break from while loop
179 }
180 (d->ser_rdptr)++;
0c792900
A
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
188 if ((rlecnt>=2000)
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);
193 rlecnt=0;
194 }
195
196 }//while rdptr < wrptr
ac132f83
A
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
ac132f83
A
199 if (rlecnt) {
200 sr_spew("Residual D4 slice rlecnt %d", rlecnt);
201 rle_memset(d, rlecnt);
202 }
203 if (d->cbuf_wrptr) {
204 sr_spew("Residual D4 data wrptr %d", d->cbuf_wrptr);
0c792900 205 process_group(sdi, d, d->cbuf_wrptr);
ac132f83
A
206
207 }
208
0c792900 209} //Process_D4
bac2a8b8
A
210
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
ac132f83
A
214void process_slice(struct sr_dev_inst *sdi, struct dev_context *devc)
215{
216 int32_t i;
217 uint32_t tmp32;
218 uint8_t cbyte;
ac132f83 219 uint32_t cword;
0c792900
A
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
ac132f83 222 for (slice_bytes = 1; (slice_bytes < devc->bytes_avail)
0c792900 223 && (devc->buffer[slice_bytes - 1] >= 0x30); slice_bytes++);
ac132f83
A
224 if (slice_bytes != devc->bytes_avail) {
225 cbyte = devc->buffer[slice_bytes - 1];
226 slice_bytes--; //Don't process the ending character
227 if (cbyte == '$') {
228 sr_info
229 ("Data stream stops with cbyte %d char %c rdidx %d sbytes %d cnt %llu",
230 cbyte, cbyte, devc->ser_rdptr, slice_bytes,
231 devc->byte_cnt);
232 devc->rxstate = RX_STOPPED;
233 } else {
234 sr_err
235 ("Data stream aborts with cbyte %d char %c rdidx %d sbytes %d cnt %llu",
236 cbyte, cbyte, devc->ser_rdptr, slice_bytes,
237 devc->byte_cnt);
238 devc->rxstate = RX_ABORT;
239 }
240 }
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,
245 devc->byte_cnt);
0c792900
A
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))) {
249
250 if(devc->buffer[devc->ser_rdptr] < 0x80){
251 int16_t rlecnt;
252 if(devc->buffer[devc->ser_rdptr]<=79){
253 rlecnt=devc->buffer[devc->ser_rdptr]-47;
254 }else{
255 rlecnt=(devc->buffer[devc->ser_rdptr]-78)*32;
256 }
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]);
260 }else{
261 rle_memset(devc,rlecnt);
262 }
263 devc->ser_rdptr++;
264
265 }else{
ac132f83
A
266 cword = 0;
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) {
270 cword |=
271 ((devc->buffer[devc->ser_rdptr]) &
272 0x7F) << i;
273 (devc->ser_rdptr)++;
274 }
275 }
276 //and then distribute 8 bits at a time to all possible channels
0c792900
A
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;
ac132f83
A
282 for (i = 0; i < devc->num_d_channels; i += 8) {
283 uint32_t idx =
284 ((devc->cbuf_wrptr) * devc->dig_sample_bytes) +
285 (i >> 3);
286 devc->d_data_buf[idx] = cword & 0xFF;
287 sr_spew
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);
291 cword >>= 8;
292 }
0c792900 293
ac132f83
A
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
298 tmp32 =
299 devc->buffer[devc->ser_rdptr] - 0x80;
300 devc->a_data_bufs[i][devc->cbuf_wrptr] =
301 ((float) tmp32 * devc->a_scale[i]) +
302 devc->a_offset[i];
303 devc->a_last[i] =
304 devc->a_data_bufs[i][devc->cbuf_wrptr];
305 sr_spew
306 ("AChan %d t32 %d value %f wrptr %d rdptr %d sc %f off %f",
307 i, tmp32,
308 devc->
309 a_data_bufs[i][devc->cbuf_wrptr],
310 devc->cbuf_wrptr, devc->ser_rdptr,
311 devc->a_scale[i], devc->a_offset[i]);
312 devc->ser_rdptr++;
313 } //if channel enabled
314 } //for num_a_channels
315 devc->cbuf_wrptr++;
0c792900
A
316 }//Not an RLE
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);
324
325 }
326 }//While another slice or RLE available
327 if (devc->cbuf_wrptr){
328 process_group(sdi, devc, devc->cbuf_wrptr);
ac132f83 329 }
bac2a8b8
A
330
331}
ac132f83 332
bac2a8b8 333//Send the processed analog values to the session
ac132f83
A
334int send_analog(struct sr_dev_inst *sdi, struct dev_context *devc,
335 uint32_t num_samples, uint32_t offset)
336{
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;
343 uint32_t i;
344 float *fptr;
345
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;
354 fptr = analog.data;
355 sr_spew
356 ("send analog num %d offset %d first %f 2 %f",
357 num_samples, offset, *(devc->a_data_bufs[i]),
358 *fptr);
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);
366 } //if enabled
367 } //for channels
368 return 0;
bac2a8b8
A
369
370}
ac132f83 371
bac2a8b8
A
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
374// the writeptr
ac132f83
A
375int send_analog_ring(struct sr_dev_inst *sdi, struct dev_context *devc,
376 uint32_t num_samples)
377{
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;
384 int i;
385 uint32_t num_pre, start_pre;
386 uint32_t num_post, start_post;
387 num_pre =
388 (num_samples >=
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;
393 sr_spew
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);
397 float *fptr;
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
410 if (num_post) {
411 analog.num_samples = num_post;
412 analog.data =
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);
417 for (uint32_t j = 0;
418 j < analog.num_samples; j++) {
419 fptr =
420 analog.data +
421 (j * sizeof(float));
422 //sr_spew("RNGDCT%d j %d %f %p",i,j,*fptr,(void *)fptr);
423 }
424 sr_session_send(sdi, &packet);
425 }
426 if (num_pre) {
427 analog.num_samples = num_pre;
428 analog.data =
429 (devc->a_pretrig_bufs[i]) + start_pre;
430 sr_dbg("Sending A%d ring buffer newest ",
431 i);
432 for (uint32_t j = 0;
433 j < analog.num_samples; j++) {
434 fptr =
435 analog.data +
436 (j * sizeof(float));
437 sr_spew("RNGDCW%d j %d %f %p", i,
438 j, *fptr, (void *) fptr);
439 }
440 sr_session_send(sdi, &packet);
441 }
442 g_slist_free(analog.meaning->channels);
443 sr_dbg("Sending A%d ring buffer done ", i);
444 } //if enabled
445 } //for channels
446 return 0;
bac2a8b8
A
447
448}
449
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
ac132f83
A
452int process_group(struct sr_dev_inst *sdi, struct dev_context *devc,
453 uint32_t num_slices)
454{
455 int trigger_offset;
456 int pre_trigger_samples;
457 //These are samples sent to session and are less than num_slices if we reach limit_samples
458 size_t num_samples;
459 struct sr_datafeed_logic logic;
460 struct sr_datafeed_packet packet;
461 int i;
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 &&
469 num_slices >
470 devc->limit_samples - devc->sent_samples) {
471 num_samples =
472 devc->limit_samples - devc->sent_samples;
473 } else {
474 num_samples = num_slices;
475 }
476 if (num_samples > 0) {
477 sr_spew
478 ("Process_group sending %d post trig samples dsb %d",
479 num_samples, devc->dig_sample_bytes);
0c792900
A
480 //for(int z=0;(z<num_samples);z++){
481 // sr_spew("0x%X ",devc->d_data_buf[z]);
ac132f83
A
482 //}
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
489 logic.length =
490 num_samples * logic.unitsize;
491 logic.data = devc->d_data_buf;
492 sr_session_send(sdi, &packet);
493 }
494 send_analog(sdi, devc, num_samples, 0);
495 } //num_sample>0
496 devc->sent_samples += num_samples;
497 return 0;
498 } //trigger_fired
499 else {
500 size_t num_ring_samples;
501 size_t sptr;
502 size_t eptr;
503 size_t numtail;
504 size_t numwrap;
505 size_t srcptr;
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,
513 devc->d_data_buf,
514 num_slices *
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;
bac2a8b8
A
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
531//sent_samples.
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.
ac132f83
A
534 if (devc->limit_samples &&
535 num_samples >
536 devc->limit_samples - devc->sent_samples)
537 num_samples =
538 devc->limit_samples -
539 devc->sent_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) {
543 sr_dbg
544 ("Sending post trigger logical remainder of %d",
545 num_samples);
546 logic.length =
547 num_samples * devc->dig_sample_bytes;
548 logic.unitsize = devc->dig_sample_bytes;
549 logic.data =
550 devc->d_data_buf +
551 (trigger_offset *
552 devc->dig_sample_bytes);
553 devc->sent_samples += num_samples;
554 sr_session_send(sdi, &packet);
555 }
556 size_t new_start, new_end, new_samples,
557 ring_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
565 new_start =
566 (trigger_offset >
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
572 //as a threshold
573 new_end =
574 MIN(num_slices - 1,
575 devc->limit_samples -
576 (pre_trigger_samples - trigger_offset) -
577 1);
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)
581 ring_samples =
582 (pre_trigger_samples >
583 trigger_offset) ? pre_trigger_samples -
584 trigger_offset : 0;
585 sr_spew
586 ("SW trigger float info newstart %zu new_end %zu new_samp %zu ring_samp %zu",
587 new_start, new_end, new_samples,
588 ring_samples);
589 if (ring_samples > 0) {
590 send_analog_ring(sdi, devc, ring_samples);
591 }
592 if (new_samples) {
593 send_analog(sdi, devc, new_samples,
594 new_start);
595 }
596
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
602 num_ring_samples =
603 num_slices >
604 devc->
605 pretrig_entries ? devc->pretrig_entries
606 : num_slices;
607 sptr = devc->pretrig_wr_ptr; //starting pointer to copy to
608 //endptr can't go past the end
609 eptr =
610 (sptr + num_ring_samples) >=
611 devc->
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
615 numwrap =
616 (num_ring_samples >
617 numtail) ? num_ring_samples -
618 numtail : 0;
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) {
626 //copy tail
627 for (uint32_t j = 0;
628 j < numtail; j++) {
629 devc->a_pretrig_bufs
630 [i][sptr + j] =
631 devc->a_data_bufs
632 [i]
633 [srcptr + j];
634 //sr_spew("RNGCpyT C%d src %zu dest %zu",i,srcptr+j,sptr+j);
635 } //for j
636 } //if chan_mask
637 } //for channels
638 //Copy wrap
639 srcptr += numtail;
640 for (i = 0; i < devc->num_a_channels; i++) {
641 if ((devc->a_chan_mask >> i) & 1) {
642 for (uint32_t j = 0;
643 j < numwrap; j++) {
644 devc->a_pretrig_bufs
645 [i][j] =
646 devc->a_data_bufs
647 [i]
648 [srcptr + j];
649 //sr_spew("RNGCpyW C%d src %zu dest %zu",i,srcptr+j,j);
650 } //for j
651 } //if chan_mask
652 } //for channels
653 devc->pretrig_wr_ptr =
654 (numwrap) ? numwrap : (eptr +
655 1) %
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
661 return 0;
662} //process_group
bac2a8b8
A
663
664
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
ac132f83
A
667void rle_memset(struct dev_context *devc, uint32_t num_slices)
668{
0c792900
A
669 uint32_t j, k,didx;
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],
ac132f83
A
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++) {
0c792900 675 didx=devc->cbuf_wrptr*devc->dig_sample_bytes;
ac132f83 676 for (k = 0; k < devc->dig_sample_bytes; k++) {
0c792900
A
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);
ac132f83 679 }
0c792900
A
680 // cbuf_wrptr always counts slices/samples (and not the bytes in the buffer)
681 // regardless of mode
682 devc->cbuf_wrptr++;
ac132f83 683 }
0c792900 684
bac2a8b8
A
685}
686
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
690SR_PRIV int raspberrypi_pico_receive(int fd, int revents, void *cb_data)
dc90146e 691{
ac132f83
A
692 struct sr_dev_inst *sdi;
693 struct dev_context *devc;
694 struct sr_serial_dev_inst *serial;
695 uint32_t i;
696 int len;
697 uint32_t bytes_rem;
698 uint32_t residual_bytes;
699 (void) fd;
700
701 if (!(sdi = cb_data))
702 return TRUE;
703
704 if (!(devc = sdi->priv))
705 return TRUE;
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",
710 devc->rxstate);
711 //don't return - we may be waiting for a final bytecnt
712 //return TRUE;
713 }
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);
718 return FALSE;
719 }
720
721 serial = sdi->conn;
722 //return true if it is some kind of event we don't handle
723 if (!(revents == G_IO_IN || revents == 0))
724 return TRUE;
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
729 len =
730 serial_read_blocking(serial, &(devc->buffer[devc->wrptr]),
731 bytes_rem - 1, 10);
732 sr_spew("Entry wrptr %u bytes_rem %u len %d", devc->wrptr,
733 bytes_rem, len);
734
735 if (len > 0) {
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");
745 //}
746 devc->bytes_avail = (devc->wrptr + len);
747 sr_spew
748 ("rx len %d bytes_avail %ul sent_samples %ul wrptr %u",
749 len, devc->bytes_avail, devc->sent_samples,
750 devc->wrptr);
751 //sr_err("rx len %d ",len);
752 } else if (len == 0) {
753 return TRUE;
754 } else {
755 sr_err("ERROR:Negative serial read code %d", len);
756 sdi->driver->dev_acquisition_stop(sdi);
757 return FALSE;
758 } //len>0
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
761 // return TRUE;
762
763 //Process the serial read data
764 devc->ser_rdptr = 0;
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);
769 } else {
770 process_slice(sdi, devc);
771 }
772 }
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
775 //for the next call.
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++) {
780 devc->buffer[i] =
781 devc->buffer[i + devc->ser_rdptr];
782 }
783 devc->ser_rdptr = 0;
784 devc->wrptr = residual_bytes;
785 sr_spew("Residual shift rdptr %u wrptr %u",
786 devc->ser_rdptr, devc->wrptr);
787 } else {
788 //If there are no residuals shifted then zero the wrptr since all data is used
789 devc->wrptr = 0;
790 }
791 //ABORT ends immediately
792 if (devc->rxstate == RX_ABORT) {
793 sr_err("Ending receive on abort");
794 sdi->driver->dev_acquisition_stop(sdi);
795 return FALSE; //
796 }
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);
806 return FALSE;
807 }
808 for (i = 1; i < devc->wrptr; i++) {
809 if (devc->buffer[i] == '+') {
810 devc->buffer[i] = 0;
811 uint64_t rxbytecnt;
812 rxbytecnt = atol((char *)&(devc->buffer[1]));
813 sr_dbg
814 ("Byte_cnt check device cnt %llu host cnt %llu",
815 rxbytecnt, devc->byte_cnt);
816 if (rxbytecnt != devc->byte_cnt) {
817 sr_err
818 ("ERROR: received %llu and counted %llu bytecnts don't match, data may be lost",
819 rxbytecnt, devc->byte_cnt);
820 }
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);
825 return TRUE;
826 }
827 }
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");
830 } //RX_STOPPED
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
833 //sample or two...
834
835 if ((devc->sent_samples >= devc->limit_samples)
836 && (devc->rxstate == RX_ACTIVE)) {
837 sr_dbg
838 ("Ending: sent %u of limit %llu samples byte_cnt %llu",
839 devc->sent_samples, devc->limit_samples,
840 devc->byte_cnt);
841 send_serial_char(serial, '+');
842
843 }
844 sr_spew
845 ("Receive function done: sent %u limit %llu wrptr %u len %d",
846 devc->sent_samples, devc->limit_samples, devc->wrptr, len);
847 return TRUE;
848} //raspberrypi_pico_receive
bac2a8b8
A
849
850//Read device specific information from the device
851SR_PRIV int raspberrypi_pico_get_dev_cfg(const struct sr_dev_inst *sdi)
852{
ac132f83
A
853 struct dev_context *devc;
854 struct sr_serial_dev_inst *serial;
855 char *cmd, response[20];
856 gchar **tokens;
857 unsigned int i;
858 int ret, num_tokens;
859
860 devc = sdi->priv;
861 sr_dbg("At get_dev_cfg");
862 serial = sdi->conn;
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);
866 if (ret <= 0) {
867 sr_err
868 ("ERROR:No response from device for analog channel query");
869 return SR_ERR;
870 }
871 //null end of string for strsplit
872 response[ret] = 0;
873 tokens = NULL;
874 tokens = g_strsplit(response, "x", 0);
875 num_tokens = g_strv_length(tokens);
876 if (num_tokens == 2) {
877 devc->a_scale[i] =
878 ((float) atoi(tokens[0])) / 1000000.0;
879 devc->a_offset[i] =
880 ((float) atoi(tokens[1])) / 1000000.0;
881 sr_dbg
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]);
885 } else {
886 sr_err
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;
893 }
894 g_strfreev(tokens);
895 g_free(cmd);
896 }
dc90146e 897
bac2a8b8 898
ac132f83
A
899 return SR_OK;
900
901}