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