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