]> sigrok.org Git - libsigrok.git/blame - src/hardware/raspberrypi-pico/api.c
raspberrypi-pico: Cleanup whitespace, add a serial_close to api.c scan function
[libsigrok.git] / src / hardware / raspberrypi-pico / api.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 */
bac2a8b8 19//debug print levels are err/warn/info/dbg/spew
dc90146e 20#include <config.h>
bac2a8b8
A
21#include <fcntl.h>
22#include <glib.h>
23#include <math.h>
24#include <stdlib.h>
25#include <string.h>
26#include <strings.h>
27#include <unistd.h>
28#include <libsigrok/libsigrok.h>
29#include "libsigrok-internal.h"
dc90146e
A
30#include "protocol.h"
31
dc90146e 32
bac2a8b8 33#define SERIALCOMM "115200/8n1"
dc90146e 34
bac2a8b8 35static const uint32_t scanopts[] = {
ac132f83
A
36 SR_CONF_CONN, //Required OS name for the port, i.e. /dev/ttyACM0
37 SR_CONF_SERIALCOMM, //Optional config of the port, i.e. 115200/8n1
bac2a8b8 38};
dc90146e 39
bac2a8b8
A
40//PulseView reads a sample rate config list as a min, max and step.
41//If step is 1 then it creates a 1,2,5,10 set of selects, as well as the max.
42//If step is not 1, then it gives a place to enter any value, which gives the greatest flexibility
43static const uint64_t samplerates[] = {
ac132f83
A
44 SR_HZ(10),
45 SR_MHZ(120),
46 SR_HZ(2),
bac2a8b8 47};
dc90146e 48
bac2a8b8 49static const uint32_t drvopts[] = {
ac132f83
A
50 SR_CONF_OSCILLOSCOPE,
51 SR_CONF_LOGIC_ANALYZER,
bac2a8b8 52};
ac132f83 53
bac2a8b8
A
54//SW trigger requires this
55static const int32_t trigger_matches[] = {
ac132f83
A
56 SR_TRIGGER_ZERO,
57 SR_TRIGGER_ONE,
58 SR_TRIGGER_RISING,
59 SR_TRIGGER_FALLING,
60 SR_TRIGGER_EDGE,
bac2a8b8 61};
dc90146e 62
dc90146e 63
bac2a8b8
A
64static const uint32_t devopts[] = {
65//CLI prefers LIMIT_SAMPLES to be a list of high,low
ac132f83
A
66 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
67 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
68 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
bac2a8b8 69//pulseview needs a list return to allow sample rate setting
ac132f83 70 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
bac2a8b8 71};
dc90146e 72
bac2a8b8 73static struct sr_dev_driver raspberrypi_pico_driver_info;
dc90146e 74
dc90146e 75
ac132f83 76static GSList *scan(struct sr_dev_driver *di, GSList * options)
dc90146e 77{
ac132f83
A
78 struct sr_config *src;
79 struct sr_dev_inst *sdi;
80 struct sr_serial_dev_inst *serial;
81 struct dev_context *devc;
82 struct sr_channel *ch;
83 GSList *l;
84 int num_read;
85 unsigned int i;
86 const char *conn, *serialcomm;
87 char buf[32];
88 int len;
89 uint8_t num_a, num_d, a_size;
90 gchar *channel_name;
91
92 conn = serialcomm = NULL;
93 for (l = options; l; l = l->next) {
94 src = l->data;
95 switch (src->key) {
96 case SR_CONF_CONN:
97 conn = g_variant_get_string(src->data, NULL);
98 break;
99 case SR_CONF_SERIALCOMM:
100 serialcomm = g_variant_get_string(src->data, NULL);
101 break;
102 }
103 }
104 if (!conn)
105 return NULL;
106
107 if (!serialcomm)
108 serialcomm = SERIALCOMM;
109
110 serial = sr_serial_dev_inst_new(conn, serialcomm);
111 sr_info("Opening %s.", conn);
112 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
113 sr_err("1st serial open fail");
114 return NULL;
115 }
116
117 sr_info("Reseting device with *s at %s.", conn);
118 send_serial_char(serial, '*');
119 g_usleep(10000);
120 //drain any inflight data
121 do {
122 sr_warn("Drain reads");
123 len = serial_read_blocking(serial, buf, 32, 100);
124 sr_warn("Drain reads done");
125 if (len)
126 sr_dbg("Dropping in flight serial data");
127 } while (len > 0);
128
129
130 //Send identify
131 num_read = send_serial_w_resp(serial, "i\n", buf, 17);
132 if (num_read < 16) {
133 sr_err("1st identify failed");
134 serial_close(serial);
135 g_usleep(100000);
136 if (serial_open(serial, SERIAL_RDWR) != SR_OK) {
137 sr_err("2st serial open fail");
138 return NULL;
139 }
140 g_usleep(100000);
141 sr_err("Send second *");
142 send_serial_char(serial, '*');
143 g_usleep(100000);
144 num_read = send_serial_w_resp(serial, "i\n", buf, 17);
145 if (num_read < 10) {
146 sr_err("Second attempt failed");
147 return NULL;
148 }
149 }
150 //Expected ID response is SRPICO,AxxyDzz,VV
151 //where xx are number of analog channels, y is bytes per analog sample
152 //and zz is number of digital channels, and VV is two digit version# which must be 00
153 if ((num_read < 16)
154 || (strncmp(buf, "SRPICO,A", 8))
155 || (buf[11] != 'D')
156 || (buf[15] != '0')
157 || (buf[16] != '0')) {
158 sr_err("ERROR:Bad response string %s %d", buf, num_read);
159 return NULL;
160 }
161 a_size = buf[10] - '0';
162 buf[10] = '\0'; //Null to end the str for atois
163 buf[14] = '\0'; //Null to end the str for atois
164 num_a = atoi(&buf[8]);
165 num_d = atoi(&buf[12]);
166
167 sdi = g_malloc0(sizeof(struct sr_dev_inst));
168 sdi->status = SR_ST_INACTIVE;
169 sdi->vendor = g_strdup("Raspberry Pi");
170 sdi->model = g_strdup("PICO");
171 sdi->version = g_strdup("00");
172 sdi->conn = serial;
173 sdi->driver = &raspberrypi_pico_driver_info;
174 sdi->inst_type = SR_INST_SERIAL;
175 sdi->serial_num = g_strdup("N/A");
176 if (((num_a == 0) && (num_d == 0))
177 || (num_a > MAX_ANALOG_CHANNELS)
178 || (num_d > MAX_DIGITAL_CHANNELS)
179 || (a_size < 1)
180 || (a_size > 4)) {
181 sr_err("ERROR: invalid channel config a %d d %d asz %d",
182 num_a, num_d, a_size);
183 return NULL;
184 }
185 devc = g_malloc0(sizeof(struct dev_context));
186 devc->a_size = a_size;
187 //multiple bytes per analog sample not supported
188 if ((num_a > 0) && (devc->a_size != 1)) {
189 sr_err("Only Analog Size of 1 supported\n\r");
190 return NULL;
191 }
192 devc->num_a_channels = num_a;
193 devc->num_d_channels = num_d;
194 devc->a_chan_mask = ((1 << num_a) - 1);
195 devc->d_chan_mask = ((1 << num_d) - 1);
bac2a8b8
A
196//The number of bytes that each digital sample in the buffers sent to the session.
197//All logical channels are packed together, where a slice of N channels takes roundup(N/8) bytes
198//This never changes even if channels are disabled because PV expects disabled channels to still
199//be accounted for in the packing
ac132f83
A
200 devc->dig_sample_bytes = ((devc->num_d_channels + 7) / 8);
201 //These are the slice sizes of the data on the wire
202 //1 7 bit field per byte
203 devc->bytes_per_slice = (devc->num_a_channels * devc->a_size);
204 if (devc->num_d_channels > 0) {
205 // logic sent in groups of 7
206 devc->bytes_per_slice += (devc->num_d_channels + 6) / 7;
207 }
208 sr_dbg("num channels a %d d %d bps %d dsb %d", num_a, num_d,
209 devc->bytes_per_slice, devc->dig_sample_bytes);
bac2a8b8
A
210//Each analog channel is it's own group
211//Digital are just channels
212//Grouping of channels is rather arbitrary as parameters like sample rate and number of samples
213//apply to all changes. Analog channels do have a scale and offset, but that is applied
214//without involvement of the session.
ac132f83
A
215 devc->analog_groups = g_malloc0(sizeof(struct sr_channel_group *) *
216 devc->num_a_channels);
217 for (i = 0; i < devc->num_a_channels; i++) {
218 channel_name = g_strdup_printf("A%d", i);
219 //sdi, index, type, enabled,name
220 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
221 channel_name);
222 devc->analog_groups[i] =
223 g_malloc0(sizeof(struct sr_channel_group));
224 devc->analog_groups[i]->name = channel_name;
225 devc->analog_groups[i]->channels =
226 g_slist_append(NULL, ch);
227 sdi->channel_groups =
228 g_slist_append(sdi->channel_groups,
229 devc->analog_groups[i]);
230 }
231
232 if (devc->num_d_channels > 0) {
233 for (i = 0; i < devc->num_d_channels; i++) {
234 //Name digital channels starting at D2 to match pico board pin names
235 channel_name = g_strdup_printf("D%d", i + 2);
236 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
237 channel_name);
238 g_free(channel_name);
239 }
240
241 }
242 //In large sample usages we get the call to receive with large transfers.
243 //Since the CDC serial implemenation can silenty lose data as it gets close to full, allocate
244 //storage for a half buffer which in a worst case scenario has 2x ratio of transmitted bytes
245 // to storage bytes.
246 //Note: The intent of making this buffer large is to prevent CDC serial buffer overflows.
247 //However, it is likely that if the host is running slow (i.e. it's a raspberry pi model 3) that it becomes
248 //compute bound and doesn't service CDC serial responses in time to not overflow the internal CDC buffers.
249 //And thus no serial buffer is large enough. But, it's only 256K....
250 devc->serial_buffer_size = 256000;
251 devc->buffer = NULL;
252 sr_dbg("Setting serial buffer size: %i.",
253 devc->serial_buffer_size);
254 devc->cbuf_wrptr = 0;
255 //While slices are sent as a group of one sample across all channels, sigrok wants analog
256 //channel data sent as separate packets.
257 //Logical trace values are packed together.
258 //A serial byte in normal mode never represent more than one sample so a 2x multiplier is plenty.
259 //In D4 mode a serial byte can represents 100s of samples due to RLE, but process_D4 ensures that
260 //it breaks up the rle_memset calls to prevent overflowing the sample buffer.
261 //that it doesn't overflow the sample buffers.
262 devc->sample_buf_size = devc->serial_buffer_size * 2;
263 for (i = 0; i < devc->num_a_channels; i++) {
264 devc->a_data_bufs[i] = NULL;
265 devc->a_pretrig_bufs[i] = NULL;
266 }
267 devc->d_data_buf = NULL;
268 devc->sample_rate = 5000;
269 devc->capture_ratio = 10;
270 devc->rxstate = RX_IDLE;
271 sdi->priv = devc;
272 //Set an initial value as various code relies on an inital value.
273 devc->limit_samples = 1000;
274
275 if (raspberrypi_pico_get_dev_cfg(sdi) != SR_OK) {
276 return NULL;
277 };
278
279 sr_err("sr_err level logging enabled");
280 sr_warn("sr_warn level logging enabled");
281 sr_info("sr_info level logging enabled");
282 sr_dbg("sr_dbg level logging enabled");
283 sr_spew("sr_spew level logging enabled");
284 serial_close(serial);
285 return std_scan_complete(di, g_slist_append(NULL, sdi));
dc90146e 286
dc90146e
A
287}
288
dc90146e 289
dc90146e 290
bac2a8b8
A
291//Note that on the initial driver load we pull all values into local storage.
292//Thus gets can return local data, but sets have to issue commands to device.
ac132f83
A
293static int config_set(uint32_t key, GVariant * data,
294 const struct sr_dev_inst *sdi,
295 const struct sr_channel_group *cg)
dc90146e 296{
ac132f83
A
297 struct dev_context *devc;
298 int ret;
299 (void) cg;
300 if (!sdi)
301 return SR_ERR_ARG;
302 devc = sdi->priv;
303 ret = SR_OK;
304 sr_dbg("Got config_set key %d \n", key);
305 switch (key) {
306 case SR_CONF_SAMPLERATE:
307 devc->sample_rate = g_variant_get_uint64(data);
308 sr_dbg("config_set sr %llu\n", devc->sample_rate);
309 break;
310 case SR_CONF_LIMIT_SAMPLES:
311 devc->limit_samples = g_variant_get_uint64(data);
312 sr_dbg("config_set slimit %lld\n", devc->limit_samples);
313 break;
314 case SR_CONF_CAPTURE_RATIO:
315 devc->capture_ratio = g_variant_get_uint64(data);
316 break;
317
318 default:
319 sr_err("ERROR:config_set undefine %d\n", key);
320 ret = SR_ERR_NA;
321 }
322
323 return ret;
bac2a8b8 324}
dc90146e 325
ac132f83
A
326static int config_get(uint32_t key, GVariant ** data,
327 const struct sr_dev_inst *sdi,
328 const struct sr_channel_group *cg)
bac2a8b8 329{
ac132f83
A
330 struct dev_context *devc;
331 sr_dbg("at config_get key %d", key);
332 (void) cg;
333 if (!sdi)
334 return SR_ERR_ARG;
335
336 devc = sdi->priv;
337 switch (key) {
338 case SR_CONF_SAMPLERATE:
339 *data = g_variant_new_uint64(devc->sample_rate);
340 sr_spew("sample rate get of %lld", devc->sample_rate);
341 break;
342 case SR_CONF_CAPTURE_RATIO:
343 if (!sdi)
344 return SR_ERR;
345 devc = sdi->priv;
346 *data = g_variant_new_uint64(devc->capture_ratio);
347 break;
348 case SR_CONF_LIMIT_SAMPLES:
349 sr_spew("config_get limit_samples of %llu",
350 devc->limit_samples);
351 *data = g_variant_new_uint64(devc->limit_samples);
352 break;
353 default:
354 sr_spew("unsupported cfg_get key %d", key);
355 return SR_ERR_NA;
356 }
357 return SR_OK;
dc90146e
A
358}
359
ac132f83
A
360static int config_list(uint32_t key, GVariant ** data,
361 const struct sr_dev_inst *sdi,
362 const struct sr_channel_group *cg)
dc90146e 363{
ac132f83
A
364 (void) cg;
365 //scan or device options are the only ones that can be called without a defined instance
366 if ((key == SR_CONF_SCAN_OPTIONS)
367 || (key == SR_CONF_DEVICE_OPTIONS)) {
368 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts,
369 drvopts, devopts);
370 }
371 if (!sdi) {
372 sr_err
373 ("ERROR:\n\r\n\r\n\r Call to config list with null sdi\n\r\n\r");
374 return SR_ERR_ARG;
375 }
376 sr_dbg("start config_list with key %X\n", key);
377 switch (key) {
bac2a8b8
A
378//Pulseview in pulseview/pv/toolbars/mainbar.cpp requires list support for frequencies as a triple
379//of min,max,step. If step is 1, then it proves a 1,2,5,10 select, but if not 1 it allows a spin box
ac132f83
A
380 case SR_CONF_SAMPLERATE:
381 sr_dbg("Return sample rate list");
382 *data =
383 std_gvar_samplerates_steps(ARRAY_AND_SIZE
384 (samplerates));
385 break;
bac2a8b8 386//This must be set to get SW trigger support
ac132f83
A
387 case SR_CONF_TRIGGER_MATCH:
388 *data =
389 std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
390 break;
391 case SR_CONF_LIMIT_SAMPLES:
392 //Really this limit is up to the memory capacity of the host,
393 //and users that pick huge values deserve what they get.
394 //But setting this limit to prevent really crazy things.
395 *data = std_gvar_tuple_u64(1LL, 1000000000LL);
396 sr_dbg("sr_config_list limit samples ");
397 break;
398 default:
399 sr_dbg("reached default statement of config_list");
400
401 return SR_ERR_NA;
402 }
403
404 return SR_OK;
dc90146e
A
405}
406
407static int dev_acquisition_start(const struct sr_dev_inst *sdi)
408{
ac132f83
A
409 struct sr_serial_dev_inst *serial;
410 struct dev_context *devc;
411 struct sr_channel *ch;
412 struct sr_trigger *trigger;
413 char tmpstr[20];
414 GSList *l;
415 int a_enabled = 0, d_enabled = 0, len;
416 serial = sdi->conn;
417 int i;
418 devc = sdi->priv;
419 sr_dbg("Enter acq start");
420 sr_dbg("dsbstart %d", devc->dig_sample_bytes);
421 devc->buffer = g_malloc(devc->serial_buffer_size);
422 if (!(devc->buffer)) {
423 sr_err("ERROR:serial buffer malloc fail");
424 return SR_ERR_MALLOC;
425 }
426 //Get device in idle state
427 if (serial_drain(serial) != SR_OK) {
428 sr_err("Initial Drain Failed\n\r");
429 return SR_ERR;
430 }
431 send_serial_char(serial, '*');
432 if (serial_drain(serial) != SR_OK) {
433 sr_err("Second Drain Failed\n\r");
434 return SR_ERR;
435 }
436
437 for (l = sdi->channels; l; l = l->next) {
438 ch = l->data;
439 sr_dbg("c %d enabled %d name %s\n", ch->index, ch->enabled,
440 ch->name);
441
442 if (ch->name[0] == 'A') {
443 devc->a_chan_mask &= ~(1 << ch->index);
444 if (ch->enabled) {
445 devc->a_chan_mask |=
446 (ch->enabled << ch->index);
447 a_enabled++;
448 }
bac2a8b8 449// sr_dbg("A%d en %d mask 0x%X",ch->index,ch->enabled,devc->a_chan_mask);
ac132f83
A
450
451 }
452 if (ch->name[0] == 'D') {
453 devc->d_chan_mask &= ~(1 << ch->index);
454 if (ch->enabled) {
455 devc->d_chan_mask |=
456 (ch->enabled << ch->index);
457 d_enabled++;
458 // sr_dbg("D%d en %d mask 0x%X",ch->index,ch->enabled,devc->d_chan_mask);
459 }
460 }
461 sr_info("Channel enable masks D 0x%X A 0x%X",
462 devc->d_chan_mask, devc->a_chan_mask);
463 sprintf(tmpstr, "%c%d%d\n", ch->name[0], ch->enabled,
464 ch->index);
465 if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
466 sr_err("ERROR:Channel enable fail");
467 return SR_ERR;
468 } else {
469
470 }
471 } //for all channels
472 //ensure data channels are continuous
473 int invalid = 0;
474 for (i = 0; i < 32; i++) {
475 if ((devc->d_chan_mask >> i) & 1) {
476 if (invalid) {
477 sr_err
478 ("Digital channel mask 0x%X not continous\n\r",
479 devc->d_chan_mask);
480 return SR_ERR;
481 }
482 } else {
483 invalid = 1;
484 }
485 }
486 //recalculate bytes_per_slice.
487 devc->bytes_per_slice = (a_enabled * devc->a_size);
488
489 for (i = 0; i < devc->num_d_channels; i += 7) {
490 if (((devc->d_chan_mask) >> i) & (0x7F)) {
491 (devc->bytes_per_slice)++;
492 }
493 }
494 if ((a_enabled == 0) && (d_enabled == 0)) {
495 sr_err("ERROR:No channels enabled");
496 return SR_ERR;
497 }
498 sr_dbg("bps %d\n", devc->bytes_per_slice);
499
500 //Apply sample rate limits
501 //Save off the lower rate values which are hacked way of getting configs to the device
502 uint8_t cfg_bits;
503 cfg_bits = (devc->sample_rate % 10 & 0x6); //Only bits 2&1 are used as cfg_bits
504 devc->sample_rate -= cfg_bits;
505 sr_warn("Capture device cfg_bits of 0x%X from sample rate %lld",
506 cfg_bits, devc->sample_rate);
507 if ((a_enabled == 3) && (devc->sample_rate > 166660)) {
508 sr_err
509 ("ERROR:3 channel ADC sample rate dropped to 166.660khz");
510 devc->sample_rate = 166660;
511 }
512 if ((a_enabled == 2) && (devc->sample_rate > 250000)) {
513 sr_err
514 ("ERROR:2 channel ADC sample rate dropped to 250khz");
515 devc->sample_rate = 250000;
516 }
517 if ((a_enabled == 1) && (devc->sample_rate > 500000)) {
518 sr_err
519 ("ERROR:1 channel ADC sample rate dropped to 500khz");
520 devc->sample_rate = 500000;
521 }
522 //Depending on channel configs, rates below 5ksps are possible
523 //but such a low rate can easily stream and this eliminates a lot
524 //of special cases.
525 if (devc->sample_rate < 5000) {
526 sr_err("Sample rate override to min of 5ksps");
527 devc->sample_rate = 5000;
528 }
529 if (devc->sample_rate > 120000000) {
530 sr_err("Sample rate override to max of 120Msps");
531 devc->sample_rate = 12000000;
532 }
533 //It may take a very large number of samples to notice, but if digital and analog are enabled
534 //and either PIO or ADC are fractional the samples will skew over time.
535 //24Mhz is the max common divisor to the 120Mhz and 48Mhz ADC clock
536 //so force an integer divisor to it.
537 if ((a_enabled > 0) && (d_enabled > 0)) {
538 if (24000000ULL % (devc->sample_rate)) {
539 uint32_t commondivint =
540 24000000ULL / (devc->sample_rate);
541 //Always increment the divisor so that we go down in frequency to avoid max sample rate issues
542 commondivint++;
543 devc->sample_rate = 24000000ULL / commondivint;
544 //While the common divisor is an integer, that does not mean the resulting sample rate is, and
545 //we want to keep the sample_rate divisible by 10 to support the cfg_bits
546 while ((devc->sample_rate % 10)
547 && (commondivint < 4800)) {
548 commondivint++;
549 devc->sample_rate =
550 24000000ULL / commondivint;
551 //sr_err(" sample rate of %llu div %u\n\r",devc->sample_rate,commondivint);
552 }
553 //Make sure the divisor increement didn't make use go too low.
554 if (devc->sample_rate < 5000) {
555 devc->sample_rate = 50000;
556 }
557 sr_err
558 ("WARN: Forcing common integer divisor sample rate of %llu div %u\n\r",
559 devc->sample_rate, commondivint);
560 }
561
562 }
563 //If we are only digital only or only analog print a warning that the
564 //fractional divisors aren't a true PLL fractional feedback loop and thus
565 //could have sample to sample variation.
566 if (a_enabled > 0) {
567 if (48000000ULL % (devc->sample_rate * a_enabled)) {
568 sr_warn
569 ("WARN: Non integer ADC divisor of 48Mhz clock for sample rate %llu may cause sample to sample variability.",
570 devc->sample_rate);
571 }
572 }
573 if (d_enabled > 0) {
574 if (120000000ULL % (devc->sample_rate)) {
575 sr_warn
576 ("WARN: Non integer PIO divisor of 120Mhz for sample rate %llu may cause sample to sample variability.",
577 devc->sample_rate);
578 }
579 }
580
581 //modulo 10 to add cfg_bits back in
582 //All code above should create overrides that are multiples of 10, but add a check just in case.
583 if (devc->sample_rate % 10) {
584 sr_err("Output sample rate %llu not mod 10",
585 devc->sample_rate);
586 devc->sample_rate = (devc->sample_rate / 10) * 10;
587 }
588
589 devc->sample_rate += cfg_bits;
590 if (cfg_bits) {
591 sr_warn
592 ("Embedding cfg_bits of 0x%X in sample_rate %lld\n\r",
593 cfg_bits, devc->sample_rate);
594 }
595 sprintf(&tmpstr[0], "R%llu\n", devc->sample_rate);
596 if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
597 sr_err("Sample rate to device failed");
598 return SR_ERR;
599 }
600 sprintf(tmpstr, "L%lld\n", devc->limit_samples);
601 if (send_serial_w_ack(serial, tmpstr) != SR_OK) {
602 sr_err("Sample limit to device failed");
603 return SR_ERR;
604 }
605
606
607 devc->sent_samples = 0;
608 devc->byte_cnt = 0;
609 devc->bytes_avail = 0;
610 devc->wrptr = 0;
611 devc->cbuf_wrptr = 0;
612 len =
613 serial_read_blocking(serial, devc->buffer,
614 devc->serial_buffer_size,
615 serial_timeout(serial, 4));
616 if (len > 0) {
617 sr_info("Pre-ARM drain had %d characters:", len);
618 devc->buffer[len] = 0;
619 sr_info("%s", devc->buffer);
620 }
621
622 for (i = 0; i < devc->num_a_channels; i++) {
623 devc->a_data_bufs[i] =
624 g_malloc(devc->sample_buf_size * sizeof(float));
625 if (!(devc->a_data_bufs[i])) {
626 sr_err("ERROR:analog buffer malloc fail");
627 return SR_ERR_MALLOC;
628 }
629 }
630 if (devc->num_d_channels > 0) {
631 devc->d_data_buf =
632 g_malloc(devc->sample_buf_size *
633 devc->dig_sample_bytes);
634 if (!(devc->d_data_buf)) {
635 sr_err("ERROR:logic buffer malloc fail");
636 return SR_ERR_MALLOC;
637 }
638 }
639
640 if ((trigger = sr_session_trigger_get(sdi->session))) {
641 devc->pretrig_entries =
642 (devc->capture_ratio * devc->limit_samples) / 100;
643 devc->stl =
644 soft_trigger_logic_new(sdi, trigger,
645 devc->pretrig_entries);
646 if (!devc->stl)
647 return SR_ERR_MALLOC;
648 devc->trigger_fired = FALSE;
649 if (devc->pretrig_entries > 0) {
650 sr_dbg("Allocating pretrig buffers size %d",
651 devc->pretrig_entries);
652 for (i = 0; i < devc->num_a_channels; i++) {
653 if ((devc->a_chan_mask >> i) & 1) {
654 devc->a_pretrig_bufs[i] =
655 g_malloc0(sizeof(float) *
656 devc->
657 pretrig_entries);
658 if (!devc->a_pretrig_bufs[i]) {
659 sr_err
660 ("ERROR:Analog pretrigger buffer malloc failure, disabling");
661 devc->trigger_fired = TRUE;
662 }
663 } //if chan_mask
664 } //for num_a_channels
665 } //if pre_trigger
666 sr_info("Entering sw triggered mode");
667 //post the receive before starting the device to ensure we are ready to receive data ASAP
668 serial_source_add(sdi->session, serial, G_IO_IN, 200,
669 raspberrypi_pico_receive, (void *) sdi);
670 sprintf(tmpstr, "C\n");
671 if (send_serial_str(serial, tmpstr) != SR_OK)
672 return SR_ERR;
673
674 } else {
675 devc->trigger_fired = TRUE;
676 devc->pretrig_entries = 0;
677 sr_info("Entering fixed sample mode");
678 serial_source_add(sdi->session, serial, G_IO_IN, 200,
679 raspberrypi_pico_receive, (void *) sdi);
680 sprintf(tmpstr, "F\n");
681 if (send_serial_str(serial, tmpstr) != SR_OK)
682 return SR_ERR;
683 }
684 std_session_send_df_header(sdi);
685
686 sr_dbg("dsbstartend %d", devc->dig_sample_bytes);
687
688 if (devc->trigger_fired)
689 std_session_send_df_trigger(sdi);
690 //Keep this at the end as we don't want to be RX_ACTIVE unless everything is ok
691 devc->rxstate = RX_ACTIVE;
692
693 return SR_OK;
dc90146e 694}
ac132f83 695
bac2a8b8
A
696//This function is called either by the protocol code if we reached all of the samples
697//or an error condition, and also by the user clicking stop in pulseview.
698//It must always be called for any acquistion that was started to free memory.
dc90146e
A
699static int dev_acquisition_stop(struct sr_dev_inst *sdi)
700{
ac132f83
A
701 struct dev_context *devc;
702 struct sr_serial_dev_inst *serial;
703 sr_dbg("****at dev_acquisition_stop");
704 int len;
705 devc = sdi->priv;
706 serial = sdi->conn;
707
708 std_session_send_df_end(sdi);
709 //If we reached this while still active it is likely because the stop button was pushed
710 //in pulseview.
711 //That is generally some kind of error condition, so we don't try to check the bytenct
712 if (devc->rxstate == RX_ACTIVE) {
713 sr_err("Reached dev_acquisition_stop in RX_ACTIVE");
714 }
715 if (devc->rxstate != RX_IDLE) {
716 sr_err("Sending plus to stop device stream\n\r");
717 send_serial_char(serial, '+');
718 }
719 //In case we get calls to receive force it to exit
720 devc->rxstate = RX_IDLE;
721 //drain data from device so that it doesn't confuse subsequent commands
722 do {
723 len =
724 serial_read_blocking(serial, devc->buffer,
725 devc->serial_buffer_size, 100);
726 if (len)
727 sr_err("Dropping %d device bytes\n\r", len);
728 } while (len > 0);
729
730
731
732 if (devc->buffer) {
733 g_free(devc->buffer);
734 devc->buffer = NULL;
735 }
736
737 for (int i = 0; i < devc->num_a_channels; i++) {
738 if (devc->a_data_bufs[i]) {
739 g_free(devc->a_data_bufs[i]);
740 devc->a_data_bufs[i] = NULL;
741 }
742 }
743
744 if (devc->d_data_buf) {
745 g_free(devc->d_data_buf);
746 devc->d_data_buf = NULL;
747 }
748 for (int i = 0; i < devc->num_a_channels; i++) {
749 if (devc->a_pretrig_bufs[i])
750 g_free(devc->a_pretrig_bufs[i]);
751 devc->a_pretrig_bufs[i] = NULL;
752 }
753
754 serial = sdi->conn;
755 serial_source_remove(sdi->session, serial);
756
757 return SR_OK;
dc90146e
A
758}
759
760static struct sr_dev_driver raspberrypi_pico_driver_info = {
ac132f83
A
761 .name = "raspberrypi-pico",
762 .longname = "RaspberryPI PICO",
763 .api_version = 1,
764 .init = std_init,
765 .cleanup = std_cleanup,
766 .scan = scan,
767 .dev_list = std_dev_list,
768 .dev_clear = std_dev_clear,
769 .config_get = config_get,
770 .config_set = config_set,
771 .config_list = config_list,
772 .dev_open = std_serial_dev_open,
773 .dev_close = std_serial_dev_close,
774 .dev_acquisition_start = dev_acquisition_start,
775 .dev_acquisition_stop = dev_acquisition_stop,
776 .context = NULL,
dc90146e 777};
ac132f83 778
dc90146e 779SR_REGISTER_DEV_DRIVER(raspberrypi_pico_driver_info);