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