]> sigrok.org Git - libsigrok.git/blob - hardware/chronovu-la8/protocol.c
Shorten probe_names[] arrays everywhere.
[libsigrok.git] / hardware / chronovu-la8 / protocol.c
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>
23 #include "libsigrok.h"
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26
27 /* Probes are numbered 0-7. */
28 SR_PRIV const char *probe_names[NUM_PROBES + 1] = {
29         "0", "1", "2", "3", "4", "5", "6", "7",
30         NULL,
31 };
32
33 /* This will be initialized via hw_info_get()/SR_DI_SAMPLERATES. */
34 SR_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  */
40 const 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. */
48 SR_PRIV const int hwcaps[] = {
49         SR_HWCAP_LOGIC_ANALYZER,
50         SR_HWCAP_SAMPLERATE,
51         SR_HWCAP_LIMIT_MSEC, /* TODO: Not yet implemented. */
52         SR_HWCAP_LIMIT_SAMPLES, /* TODO: Not yet implemented. */
53         0,
54 };
55
56 SR_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  */
76 SR_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
87         sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate);
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  */
102 SR_PRIV uint8_t samplerate_to_divcount(uint64_t samplerate)
103 {
104         if (samplerate == 0) {
105                 sr_err("%s: samplerate was 0.", __func__);
106                 return 0xff;
107         }
108
109         if (!is_valid_samplerate(samplerate)) {
110                 sr_err("%s: Can't get divcount, samplerate invalid.", __func__);
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  *
120  * @param devc The struct containing private per-device-instance data. Must not
121  *            be NULL. devc->ftdic must not be NULL either.
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  */
126 SR_PRIV int la8_write(struct dev_context *devc, uint8_t *buf, int size)
127 {
128         int bytes_written;
129
130         /* Note: Caller checked that devc and devc->ftdic != NULL. */
131
132         if (!buf) {
133                 sr_err("%s: buf was NULL.", __func__);
134                 return SR_ERR_ARG;
135         }
136
137         if (size < 0) {
138                 sr_err("%s: size was < 0.", __func__);
139                 return SR_ERR_ARG;
140         }
141
142         bytes_written = ftdi_write_data(devc->ftdic, buf, size);
143
144         if (bytes_written < 0) {
145                 sr_err("%s: ftdi_write_data: (%d) %s.", __func__,
146                        bytes_written, ftdi_get_error_string(devc->ftdic));
147                 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
148         } else if (bytes_written != size) {
149                 sr_err("%s: bytes to write: %d, bytes written: %d.",
150                        __func__, size, bytes_written);
151                 (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
152         }
153
154         return bytes_written;
155 }
156
157 /**
158  * Read a certain amount of bytes from the LA8's FTDI device.
159  *
160  * @param devc The struct containing private per-device-instance data. Must not
161  *            be NULL. devc->ftdic must not be NULL either.
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  */
167 SR_PRIV int la8_read(struct dev_context *devc, uint8_t *buf, int size)
168 {
169         int bytes_read;
170
171         /* Note: Caller checked that devc and devc->ftdic != NULL. */
172
173         if (!buf) {
174                 sr_err("%s: buf was NULL.", __func__);
175                 return SR_ERR_ARG;
176         }
177
178         if (size <= 0) {
179                 sr_err("%s: size was <= 0.", __func__);
180                 return SR_ERR_ARG;
181         }
182
183         bytes_read = ftdi_read_data(devc->ftdic, buf, size);
184
185         if (bytes_read < 0) {
186                 sr_err("%s: ftdi_read_data: (%d) %s.", __func__,
187                        bytes_read, ftdi_get_error_string(devc->ftdic));
188         } else if (bytes_read != size) {
189                 // sr_err("%s: Bytes to read: %d, bytes read: %d.",
190                 //        __func__, size, bytes_read);
191         }
192
193         return bytes_read;
194 }
195
196 SR_PRIV int la8_close(struct dev_context *devc)
197 {
198         int ret;
199
200         if (!devc) {
201                 sr_err("%s: devc was NULL.", __func__);
202                 return SR_ERR_ARG;
203         }
204
205         if (!devc->ftdic) {
206                 sr_err("%s: devc->ftdic was NULL.", __func__);
207                 return SR_ERR_ARG;
208         }
209
210         if ((ret = ftdi_usb_close(devc->ftdic)) < 0) {
211                 sr_err("%s: ftdi_usb_close: (%d) %s.",
212                        __func__, ret, ftdi_get_error_string(devc->ftdic));
213         }
214
215         return ret;
216 }
217
218 /**
219  * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
220  *
221  * @param devc The struct containing private per-device-instance data.
222  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
223  */
224 SR_PRIV int la8_close_usb_reset_sequencer(struct dev_context *devc)
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
230         if (!devc) {
231                 sr_err("%s: devc was NULL.", __func__);
232                 return SR_ERR_ARG;
233         }
234
235         if (!devc->ftdic) {
236                 sr_err("%s: devc->ftdic was NULL.", __func__);
237                 return SR_ERR_ARG;
238         }
239
240         if (devc->ftdic->usb_dev) {
241                 /* Reset the LA8 sequencer logic, then wait 100ms. */
242                 sr_dbg("Resetting sequencer logic.");
243                 (void) la8_write(devc, buf, 8); /* Ignore errors. */
244                 g_usleep(100 * 1000);
245
246                 /* Purge FTDI buffers, then reset and close the FTDI device. */
247                 sr_dbg("Purging buffers, resetting+closing FTDI device.");
248
249                 /* Log errors, but ignore them (i.e., don't abort). */
250                 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0)
251                         sr_err("%s: ftdi_usb_purge_buffers: (%d) %s.",
252                             __func__, ret, ftdi_get_error_string(devc->ftdic));
253                 if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
254                         sr_err("%s: ftdi_usb_reset: (%d) %s.", __func__,
255                                ret, ftdi_get_error_string(devc->ftdic));
256                 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
257                         sr_err("%s: ftdi_usb_close: (%d) %s.", __func__,
258                                ret, ftdi_get_error_string(devc->ftdic));
259         }
260
261         /* Close USB device, deinitialize and free the FTDI context. */
262         ftdi_free(devc->ftdic); /* Returns void. */
263         devc->ftdic = NULL;
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  *
273  * @param devc The struct containing private per-device-instance data.
274  * @return SR_OK upon success, SR_ERR upon failure.
275  */
276 SR_PRIV int la8_reset(struct dev_context *devc)
277 {
278         uint8_t buf[BS];
279         time_t done, now;
280         int bytes_read;
281
282         if (!devc) {
283                 sr_err("%s: devc was NULL.", __func__);
284                 return SR_ERR_ARG;
285         }
286
287         if (!devc->ftdic) {
288                 sr_err("%s: devc->ftdic was NULL.", __func__);
289                 return SR_ERR_ARG;
290         }
291
292         sr_dbg("Resetting the device.");
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! */
301                 bytes_read = la8_read(devc, (uint8_t *)&buf, BS);
302                 now = time(NULL);
303         } while ((done > now) && (bytes_read > 0));
304
305         /* Reset the LA8 sequencer logic and close the USB port. */
306         (void) la8_close_usb_reset_sequencer(devc); /* Ignore errors. */
307
308         sr_dbg("Device reset finished.");
309
310         return SR_OK;
311 }
312
313 SR_PRIV int configure_probes(const struct sr_dev_inst *sdi)
314 {
315         struct dev_context *devc;
316         const struct sr_probe *probe;
317         const GSList *l;
318         uint8_t probe_bit;
319         char *tc;
320
321         devc = sdi->priv;
322         devc->trigger_pattern = 0;
323         devc->trigger_mask = 0; /* Default to "don't care" for all probes. */
324
325         for (l = sdi->probes; l; l = l->next) {
326                 probe = (struct sr_probe *)l->data;
327
328                 if (!probe) {
329                         sr_err("%s: probe was NULL.", __func__);
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) {
343                         sr_err("%s: Invalid probe index %d, must be "
344                                "between 0 and 7.", __func__, probe->index);
345                         return SR_ERR;
346                 }
347
348                 probe_bit = (1 << (probe->index));
349
350                 /* Configure the probe's trigger mask and trigger pattern. */
351                 for (tc = probe->trigger; tc && *tc; tc++) {
352                         devc->trigger_mask |= probe_bit;
353
354                         /* Sanity check, LA8 only supports low/high trigger. */
355                         if (*tc != '0' && *tc != '1') {
356                                 sr_err("%s: Invalid trigger '%c', only "
357                                        "'0'/'1' supported.", __func__, *tc);
358                                 return SR_ERR;
359                         }
360
361                         if (*tc == '1')
362                                 devc->trigger_pattern |= probe_bit;
363                 }
364         }
365
366         sr_dbg("Trigger mask = 0x%x, trigger pattern = 0x%x.",
367                devc->trigger_mask, devc->trigger_pattern);
368
369         return SR_OK;
370 }
371
372 SR_PRIV int set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
373 {
374         struct dev_context *devc;
375
376         /* Note: Caller checked that sdi and sdi->priv != NULL. */
377
378         devc = sdi->priv;
379
380         sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
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. */
389         devc->cur_samplerate = samplerate;
390
391         sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate);
392
393         return SR_OK;
394 }
395
396 /**
397  * Get a block of data from the LA8.
398  *
399  * @param devc The struct containing private per-device-instance data. Must not
400  *            be NULL. devc->ftdic must not be NULL either.
401  * @return SR_OK upon success, or SR_ERR upon errors.
402  */
403 SR_PRIV int la8_read_block(struct dev_context *devc)
404 {
405         int i, byte_offset, m, mi, p, index, bytes_read;
406         time_t now;
407
408         /* Note: Caller checked that devc and devc->ftdic != NULL. */
409
410         sr_spew("Reading block %d.", devc->block_counter);
411
412         bytes_read = la8_read(devc, devc->mangled_buf, BS);
413
414         /* If first block read got 0 bytes, retry until success or timeout. */
415         if ((bytes_read == 0) && (devc->block_counter == 0)) {
416                 do {
417                         sr_spew("Reading block 0 (again).");
418                         bytes_read = la8_read(devc, devc->mangled_buf, BS);
419                         /* TODO: How to handle read errors here? */
420                         now = time(NULL);
421                 } while ((devc->done > now) && (bytes_read == 0));
422         }
423
424         /* Check if block read was successful or a timeout occured. */
425         if (bytes_read != BS) {
426                 sr_err("Trigger timed out. Bytes read: %d.", bytes_read);
427                 (void) la8_reset(devc); /* Ignore errors. */
428                 return SR_ERR;
429         }
430
431         /* De-mangle the data. */
432         sr_spew("Demangling block %d.", devc->block_counter);
433         byte_offset = devc->block_counter * BS;
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;
439                 index += (devc->divcount == 0) ? p : (1 - p);
440                 devc->final_buf[index] = devc->mangled_buf[i];
441         }
442
443         return SR_OK;
444 }
445
446 SR_PRIV void send_block_to_session_bus(struct dev_context *devc, int block)
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
454         /* Note: No sanity checks on devc/block, caller is responsible. */
455
456         /* Check if we can find the trigger condition in this block. */
457         trigger_point = -1;
458         expected_sample = devc->trigger_pattern & devc->trigger_mask;
459         for (i = 0; i < BS; i++) {
460                 /* Don't continue if the trigger was found previously. */
461                 if (devc->trigger_found)
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                  */
469                 if (devc->trigger_mask == 0x00)
470                         break;
471
472                 sample = *(devc->final_buf + (block * BS) + i);
473
474                 if ((sample & devc->trigger_mask) == expected_sample) {
475                         trigger_point = i;
476                         devc->trigger_found = 1;
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. */
484                 sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for "
485                         "block %d.", BS, block);
486                 packet.type = SR_DF_LOGIC;
487                 packet.payload = &logic;
488                 logic.length = BS;
489                 logic.unitsize = 1;
490                 logic.data = devc->final_buf + (block * BS);
491                 sr_session_send(devc->session_dev_id, &packet);
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. */
507                 sr_spew("Sending pre-trigger SR_DF_LOGIC packet, "
508                         "start = %d, length = %d.", block * BS, trigger_point);
509                 packet.type = SR_DF_LOGIC;
510                 packet.payload = &logic;
511                 logic.length = trigger_point;
512                 logic.unitsize = 1;
513                 logic.data = devc->final_buf + (block * BS);
514                 sr_session_send(devc->session_dev_id, &packet);
515         }
516
517         /* Send the SR_DF_TRIGGER packet to the session bus. */
518         sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.",
519                 (block * BS) + trigger_point);
520         packet.type = SR_DF_TRIGGER;
521         packet.payload = NULL;
522         sr_session_send(devc->session_dev_id, &packet);
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. */
527                 sr_spew("Sending post-trigger SR_DF_LOGIC packet, "
528                         "start = %d, length = %d.",
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;
534                 logic.data = devc->final_buf + (block * BS) + trigger_point;
535                 sr_session_send(devc->session_dev_id, &packet);
536         }
537 }