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