]> sigrok.org Git - libsigrok.git/blame - hardware/chronovu-la8/driver.c
sr/drivers: obsolete SR_HWCAP_PROBECONFIG
[libsigrok.git] / hardware / chronovu-la8 / driver.c
CommitLineData
b908f067
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <ftdi.h>
22#include <glib.h>
45c59c8b
BV
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
b908f067
UH
25#include "driver.h"
26
27/* Probes are numbered 0-7. */
28SR_PRIV const char *probe_names[NUM_PROBES + 1] = {
29 "0",
30 "1",
31 "2",
32 "3",
33 "4",
34 "5",
35 "6",
36 "7",
37 NULL,
38};
39
6a2761fd 40/* This will be initialized via hw_info_get()/SR_DI_SAMPLERATES. */
b908f067
UH
41SR_PRIV uint64_t supported_samplerates[255 + 1] = { 0 };
42
43/*
44 * Min: 1 sample per 0.01us -> sample time is 0.084s, samplerate 100MHz
45 * Max: 1 sample per 2.55us -> sample time is 21.391s, samplerate 392.15kHz
46 */
47const struct sr_samplerates samplerates = {
48 .low = 0,
49 .high = 0,
50 .step = 0,
51 .list = supported_samplerates,
52};
53
54/* Note: Continuous sampling is not supported by the hardware. */
55SR_PRIV const int hwcaps[] = {
56 SR_HWCAP_LOGIC_ANALYZER,
57 SR_HWCAP_SAMPLERATE,
58 SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
59 SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
60 0,
61};
62
63SR_PRIV void fill_supported_samplerates_if_needed(void)
64{
65 int i;
66
67 /* Do nothing if supported_samplerates[] is already filled. */
68 if (supported_samplerates[0] != 0)
69 return;
70
71 /* Fill supported_samplerates[] with the proper values. */
72 for (i = 0; i < 255; i++)
73 supported_samplerates[254 - i] = SR_MHZ(100) / (i + 1);
74 supported_samplerates[255] = 0;
75}
76
77/**
78 * Check if the given samplerate is supported by the LA8 hardware.
79 *
80 * @param samplerate The samplerate (in Hz) to check.
81 * @return 1 if the samplerate is supported/valid, 0 otherwise.
82 */
83SR_PRIV int is_valid_samplerate(uint64_t samplerate)
84{
85 int i;
86
87 fill_supported_samplerates_if_needed();
88
89 for (i = 0; i < 255; i++) {
90 if (supported_samplerates[i] == samplerate)
91 return 1;
92 }
93
94 sr_err("la8: %s: invalid samplerate (%" PRIu64 "Hz)",
95 __func__, samplerate);
96
97 return 0;
98}
99
100/**
101 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
102 *
103 * LA8 hardware: sample period = (divcount + 1) * 10ns.
104 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
105 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
106 *
107 * @param samplerate The samplerate in Hz.
108 * @return The divcount value as needed by the hardware, or 0xff upon errors.
109 */
110SR_PRIV uint8_t samplerate_to_divcount(uint64_t samplerate)
111{
112 if (samplerate == 0) {
113 sr_err("la8: %s: samplerate was 0", __func__);
114 return 0xff;
115 }
116
117 if (!is_valid_samplerate(samplerate)) {
118 sr_err("la8: %s: can't get divcount, samplerate invalid",
119 __func__);
120 return 0xff;
121 }
122
123 return (SR_MHZ(100) / samplerate) - 1;
124}
125
126/**
127 * Write data of a certain length to the LA8's FTDI device.
128 *
1644fb24
BV
129 * @param devc The struct containing private per-device-instance data. Must not
130 * be NULL. devc->ftdic must not be NULL either.
b908f067
UH
131 * @param buf The buffer containing the data to write. Must not be NULL.
132 * @param size The number of bytes to write. Must be >= 0.
133 * @return The number of bytes written, or a negative value upon errors.
134 */
1644fb24 135SR_PRIV int la8_write(struct dev_context *devc, uint8_t *buf, int size)
b908f067
UH
136{
137 int bytes_written;
138
1644fb24 139 /* Note: Caller checked that devc and devc->ftdic != NULL. */
b908f067
UH
140
141 if (!buf) {
142 sr_err("la8: %s: buf was NULL", __func__);
143 return SR_ERR_ARG;
144 }
145
146 if (size < 0) {
147 sr_err("la8: %s: size was < 0", __func__);
148 return SR_ERR_ARG;
149 }
150
1644fb24 151 bytes_written = ftdi_write_data(devc->ftdic, buf, size);
b908f067
UH
152
153 if (bytes_written < 0) {
154 sr_err("la8: %s: ftdi_write_data: (%d) %s", __func__,
1644fb24
BV
155 bytes_written, ftdi_get_error_string(devc->ftdic));
156 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067
UH
157 } else if (bytes_written != size) {
158 sr_err("la8: %s: bytes to write: %d, bytes written: %d",
159 __func__, size, bytes_written);
1644fb24 160 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067
UH
161 }
162
163 return bytes_written;
164}
165
166/**
167 * Read a certain amount of bytes from the LA8's FTDI device.
168 *
1644fb24
BV
169 * @param devc The struct containing private per-device-instance data. Must not
170 * be NULL. devc->ftdic must not be NULL either.
b908f067
UH
171 * @param buf The buffer where the received data will be stored. Must not
172 * be NULL.
173 * @param size The number of bytes to read. Must be >= 1.
174 * @return The number of bytes read, or a negative value upon errors.
175 */
1644fb24 176SR_PRIV int la8_read(struct dev_context *devc, uint8_t *buf, int size)
b908f067
UH
177{
178 int bytes_read;
179
1644fb24 180 /* Note: Caller checked that devc and devc->ftdic != NULL. */
b908f067
UH
181
182 if (!buf) {
183 sr_err("la8: %s: buf was NULL", __func__);
184 return SR_ERR_ARG;
185 }
186
187 if (size <= 0) {
188 sr_err("la8: %s: size was <= 0", __func__);
189 return SR_ERR_ARG;
190 }
191
1644fb24 192 bytes_read = ftdi_read_data(devc->ftdic, buf, size);
b908f067
UH
193
194 if (bytes_read < 0) {
195 sr_err("la8: %s: ftdi_read_data: (%d) %s", __func__,
1644fb24 196 bytes_read, ftdi_get_error_string(devc->ftdic));
b908f067
UH
197 } else if (bytes_read != size) {
198 // sr_err("la8: %s: bytes to read: %d, bytes read: %d",
199 // __func__, size, bytes_read);
200 }
201
202 return bytes_read;
203}
204
1644fb24 205SR_PRIV int la8_close(struct dev_context *devc)
b908f067
UH
206{
207 int ret;
208
1644fb24
BV
209 if (!devc) {
210 sr_err("la8: %s: devc was NULL", __func__);
b908f067
UH
211 return SR_ERR_ARG;
212 }
213
1644fb24
BV
214 if (!devc->ftdic) {
215 sr_err("la8: %s: devc->ftdic was NULL", __func__);
b908f067
UH
216 return SR_ERR_ARG;
217 }
218
1644fb24 219 if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
b908f067 220 sr_err("la8: %s: ftdi_usb_close: (%d) %s",
1644fb24 221 __func__, ret, ftdi_get_error_string(devc->ftdic));
b908f067
UH
222 }
223
224 return ret;
225}
226
227/**
228 * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
229 *
1644fb24 230 * @param devc The struct containing private per-device-instance data.
b908f067
UH
231 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
232 */
1644fb24 233SR_PRIV int la8_close_usb_reset_sequencer(struct dev_context *devc)
b908f067
UH
234{
235 /* Magic sequence of bytes for resetting the LA8 sequencer logic. */
236 uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
237 int ret;
238
1644fb24
BV
239 if (!devc) {
240 sr_err("la8: %s: devc was NULL", __func__);
b908f067
UH
241 return SR_ERR_ARG;
242 }
243
1644fb24
BV
244 if (!devc->ftdic) {
245 sr_err("la8: %s: devc->ftdic was NULL", __func__);
b908f067
UH
246 return SR_ERR_ARG;
247 }
248
1644fb24 249 if (devc->ftdic->usb_dev) {
b908f067
UH
250 /* Reset the LA8 sequencer logic, then wait 100ms. */
251 sr_dbg("la8: Resetting sequencer logic.");
1644fb24 252 (void) la8_write(devc, buf, 8); /* Ignore errors. */
b908f067
UH
253 g_usleep(100 * 1000);
254
255 /* Purge FTDI buffers, then reset and close the FTDI device. */
256 sr_dbg("la8: Purging buffers, resetting+closing FTDI device.");
257
258 /* Log errors, but ignore them (i.e., don't abort). */
1644fb24 259 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0)
b908f067 260 sr_err("la8: %s: ftdi_usb_purge_buffers: (%d) %s",
1644fb24
BV
261 __func__, ret, ftdi_get_error_string(devc->ftdic));
262 if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
b908f067 263 sr_err("la8: %s: ftdi_usb_reset: (%d) %s", __func__,
1644fb24
BV
264 ret, ftdi_get_error_string(devc->ftdic));
265 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
b908f067 266 sr_err("la8: %s: ftdi_usb_close: (%d) %s", __func__,
1644fb24 267 ret, ftdi_get_error_string(devc->ftdic));
b908f067
UH
268 }
269
270 /* Close USB device, deinitialize and free the FTDI context. */
1644fb24
BV
271 ftdi_free(devc->ftdic); /* Returns void. */
272 devc->ftdic = NULL;
b908f067
UH
273
274 return SR_OK;
275}
276
277/**
278 * Reset the ChronoVu LA8.
279 *
280 * The LA8 must be reset after a failed read/write operation or upon timeouts.
281 *
1644fb24 282 * @param devc The struct containing private per-device-instance data.
b908f067
UH
283 * @return SR_OK upon success, SR_ERR upon failure.
284 */
1644fb24 285SR_PRIV int la8_reset(struct dev_context *devc)
b908f067
UH
286{
287 uint8_t buf[BS];
288 time_t done, now;
289 int bytes_read;
290
1644fb24
BV
291 if (!devc) {
292 sr_err("la8: %s: devc was NULL", __func__);
b908f067
UH
293 return SR_ERR_ARG;
294 }
295
1644fb24
BV
296 if (!devc->ftdic) {
297 sr_err("la8: %s: devc->ftdic was NULL", __func__);
b908f067
UH
298 return SR_ERR_ARG;
299 }
300
301 sr_dbg("la8: Resetting the device.");
302
303 /*
304 * Purge pending read data from the FTDI hardware FIFO until
305 * no more data is left, or a timeout occurs (after 20s).
306 */
307 done = 20 + time(NULL);
308 do {
309 /* TODO: Ignore errors? Check for < 0 at least! */
1644fb24 310 bytes_read = la8_read(devc, (uint8_t *)&buf, BS);
b908f067
UH
311 now = time(NULL);
312 } while ((done > now) && (bytes_read > 0));
313
314 /* Reset the LA8 sequencer logic and close the USB port. */
1644fb24 315 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
b908f067
UH
316
317 sr_dbg("la8: Device reset finished.");
318
319 return SR_OK;
320}
321
014359e3 322SR_PRIV int configure_probes(const struct sr_dev_inst *sdi)
b908f067 323{
014359e3 324 struct dev_context *devc;
b908f067
UH
325 const struct sr_probe *probe;
326 const GSList *l;
327 uint8_t probe_bit;
328 char *tc;
329
014359e3 330 devc = sdi->priv;
1644fb24
BV
331 devc->trigger_pattern = 0;
332 devc->trigger_mask = 0; /* Default to "don't care" for all probes. */
b908f067 333
014359e3 334 for (l = sdi->probes; l; l = l->next) {
b908f067
UH
335 probe = (struct sr_probe *)l->data;
336
337 if (!probe) {
338 sr_err("la8: %s: probe was NULL", __func__);
339 return SR_ERR;
340 }
341
342 /* Skip disabled probes. */
343 if (!probe->enabled)
344 continue;
345
346 /* Skip (enabled) probes with no configured trigger. */
347 if (!probe->trigger)
348 continue;
349
350 /* Note: Must only be run if probe->trigger != NULL. */
351 if (probe->index < 0 || probe->index > 7) {
352 sr_err("la8: %s: invalid probe index %d, must be "
353 "between 0 and 7", __func__, probe->index);
354 return SR_ERR;
355 }
356
b35c8293 357 probe_bit = (1 << (probe->index));
b908f067
UH
358
359 /* Configure the probe's trigger mask and trigger pattern. */
360 for (tc = probe->trigger; tc && *tc; tc++) {
1644fb24 361 devc->trigger_mask |= probe_bit;
b908f067
UH
362
363 /* Sanity check, LA8 only supports low/high trigger. */
364 if (*tc != '0' && *tc != '1') {
365 sr_err("la8: %s: invalid trigger '%c', only "
366 "'0'/'1' supported", __func__, *tc);
367 return SR_ERR;
368 }
369
370 if (*tc == '1')
1644fb24 371 devc->trigger_pattern |= probe_bit;
b908f067
UH
372 }
373 }
374
375 sr_dbg("la8: trigger_mask = 0x%x, trigger_pattern = 0x%x",
1644fb24 376 devc->trigger_mask, devc->trigger_pattern);
b908f067
UH
377
378 return SR_OK;
379}
380
6f4b1868 381SR_PRIV int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
b908f067 382{
1644fb24 383 struct dev_context *devc;
b908f067
UH
384
385 /* Note: Caller checked that sdi and sdi->priv != NULL. */
386
1644fb24 387 devc = sdi->priv;
b908f067
UH
388
389 sr_spew("la8: Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
390
391 fill_supported_samplerates_if_needed();
392
393 /* Check if this is a samplerate supported by the hardware. */
394 if (!is_valid_samplerate(samplerate))
395 return SR_ERR;
396
397 /* Set the new samplerate. */
1644fb24 398 devc->cur_samplerate = samplerate;
b908f067 399
1644fb24 400 sr_dbg("la8: Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate);
b908f067
UH
401
402 return SR_OK;
403}
404
405/**
406 * Get a block of data from the LA8.
407 *
1644fb24
BV
408 * @param devc The struct containing private per-device-instance data. Must not
409 * be NULL. devc->ftdic must not be NULL either.
b908f067
UH
410 * @return SR_OK upon success, or SR_ERR upon errors.
411 */
1644fb24 412SR_PRIV int la8_read_block(struct dev_context *devc)
b908f067
UH
413{
414 int i, byte_offset, m, mi, p, index, bytes_read;
415 time_t now;
416
1644fb24 417 /* Note: Caller checked that devc and devc->ftdic != NULL. */
b908f067 418
1644fb24 419 sr_spew("la8: Reading block %d.", devc->block_counter);
b908f067 420
1644fb24 421 bytes_read = la8_read(devc, devc->mangled_buf, BS);
b908f067
UH
422
423 /* If first block read got 0 bytes, retry until success or timeout. */
1644fb24 424 if ((bytes_read == 0) && (devc->block_counter == 0)) {
b908f067
UH
425 do {
426 sr_spew("la8: Reading block 0 (again).");
1644fb24 427 bytes_read = la8_read(devc, devc->mangled_buf, BS);
b908f067
UH
428 /* TODO: How to handle read errors here? */
429 now = time(NULL);
1644fb24 430 } while ((devc->done > now) && (bytes_read == 0));
b908f067
UH
431 }
432
433 /* Check if block read was successful or a timeout occured. */
434 if (bytes_read != BS) {
435 sr_err("la8: Trigger timed out. Bytes read: %d.", bytes_read);
1644fb24 436 (void) la8_reset(devc); /* Ignore errors. */
b908f067
UH
437 return SR_ERR;
438 }
439
440 /* De-mangle the data. */
1644fb24
BV
441 sr_spew("la8: Demangling block %d.", devc->block_counter);
442 byte_offset = devc->block_counter * BS;
b908f067
UH
443 m = byte_offset / (1024 * 1024);
444 mi = m * (1024 * 1024);
445 for (i = 0; i < BS; i++) {
446 p = i & (1 << 0);
447 index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
1644fb24
BV
448 index += (devc->divcount == 0) ? p : (1 - p);
449 devc->final_buf[index] = devc->mangled_buf[i];
b908f067
UH
450 }
451
452 return SR_OK;
453}
454
1644fb24 455SR_PRIV void send_block_to_session_bus(struct dev_context *devc, int block)
b908f067
UH
456{
457 int i;
458 uint8_t sample, expected_sample;
459 struct sr_datafeed_packet packet;
460 struct sr_datafeed_logic logic;
461 int trigger_point; /* Relative trigger point (in this block). */
462
1644fb24 463 /* Note: No sanity checks on devc/block, caller is responsible. */
b908f067
UH
464
465 /* Check if we can find the trigger condition in this block. */
466 trigger_point = -1;
1644fb24 467 expected_sample = devc->trigger_pattern & devc->trigger_mask;
b908f067
UH
468 for (i = 0; i < BS; i++) {
469 /* Don't continue if the trigger was found previously. */
1644fb24 470 if (devc->trigger_found)
b908f067
UH
471 break;
472
473 /*
474 * Also, don't continue if triggers are "don't care", i.e. if
475 * no trigger conditions were specified by the user. In that
476 * case we don't want to send an SR_DF_TRIGGER packet at all.
477 */
1644fb24 478 if (devc->trigger_mask == 0x00)
b908f067
UH
479 break;
480
1644fb24 481 sample = *(devc->final_buf + (block * BS) + i);
b908f067 482
1644fb24 483 if ((sample & devc->trigger_mask) == expected_sample) {
b908f067 484 trigger_point = i;
1644fb24 485 devc->trigger_found = 1;
b908f067
UH
486 break;
487 }
488 }
489
490 /* If no trigger was found, send one SR_DF_LOGIC packet. */
491 if (trigger_point == -1) {
492 /* Send an SR_DF_LOGIC packet to the session bus. */
493 sr_spew("la8: sending SR_DF_LOGIC packet (%d bytes) for "
494 "block %d", BS, block);
495 packet.type = SR_DF_LOGIC;
496 packet.payload = &logic;
497 logic.length = BS;
498 logic.unitsize = 1;
1644fb24
BV
499 logic.data = devc->final_buf + (block * BS);
500 sr_session_send(devc->session_dev_id, &packet);
b908f067
UH
501 return;
502 }
503
504 /*
505 * We found the trigger, so some special handling is needed. We have
506 * to send an SR_DF_LOGIC packet with the samples before the trigger
507 * (if any), then the SD_DF_TRIGGER packet itself, then another
508 * SR_DF_LOGIC packet with the samples after the trigger (if any).
509 */
510
511 /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
512
513 /* If at least one sample is located before the trigger... */
514 if (trigger_point > 0) {
515 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
516 sr_spew("la8: sending pre-trigger SR_DF_LOGIC packet, "
517 "start = %d, length = %d", block * BS, trigger_point);
518 packet.type = SR_DF_LOGIC;
519 packet.payload = &logic;
520 logic.length = trigger_point;
521 logic.unitsize = 1;
1644fb24
BV
522 logic.data = devc->final_buf + (block * BS);
523 sr_session_send(devc->session_dev_id, &packet);
b908f067
UH
524 }
525
526 /* Send the SR_DF_TRIGGER packet to the session bus. */
527 sr_spew("la8: sending SR_DF_TRIGGER packet, sample = %d",
528 (block * BS) + trigger_point);
529 packet.type = SR_DF_TRIGGER;
530 packet.payload = NULL;
1644fb24 531 sr_session_send(devc->session_dev_id, &packet);
b908f067
UH
532
533 /* If at least one sample is located after the trigger... */
534 if (trigger_point < (BS - 1)) {
535 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
536 sr_spew("la8: sending post-trigger SR_DF_LOGIC packet, "
537 "start = %d, length = %d",
538 (block * BS) + trigger_point, BS - trigger_point);
539 packet.type = SR_DF_LOGIC;
540 packet.payload = &logic;
541 logic.length = BS - trigger_point;
542 logic.unitsize = 1;
1644fb24
BV
543 logic.data = devc->final_buf + (block * BS) + trigger_point;
544 sr_session_send(devc->session_dev_id, &packet);
b908f067
UH
545 }
546}