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