]> sigrok.org Git - libsigrok.git/blob - src/hardware/chronovu-la/protocol.c
dev_acquisition_{start,stop}(): Drop duplicate 'cb_data' parameter.
[libsigrok.git] / src / hardware / chronovu-la / protocol.c
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 <config.h>
22 #include "protocol.h"
23
24 SR_PRIV const struct cv_profile cv_profiles[] = {
25         { CHRONOVU_LA8,  "LA8",  "ChronoVu LA8",  8,  SR_MHZ(100), 2, 0.8388608 },
26         { CHRONOVU_LA16, "LA16", "ChronoVu LA16", 16, SR_MHZ(200), 4, 0.042 },
27         ALL_ZERO
28 };
29
30 /* LA8: channels are numbered 0-7. LA16: channels are numbered 0-15. */
31 SR_PRIV const char *cv_channel_names[] = {
32         "0", "1", "2", "3", "4", "5", "6", "7",
33         "8", "9", "10", "11", "12", "13", "14", "15",
34 };
35
36 static int close_usb_reset_sequencer(struct dev_context *devc);
37
38 SR_PRIV void cv_fill_samplerates_if_needed(const struct sr_dev_inst *sdi)
39 {
40         int i;
41         struct dev_context *devc;
42
43         devc = sdi->priv;
44
45         if (devc->samplerates[0] != 0)
46                 return;
47
48         for (i = 0; i < 255; i++)
49                 devc->samplerates[254 - i] = devc->prof->max_samplerate / (i + 1);
50 }
51
52 /**
53  * Check if the given samplerate is supported by the hardware.
54  *
55  * @param sdi Device instance.
56  * @param samplerate The samplerate (in Hz) to check.
57  *
58  * @return 1 if the samplerate is supported/valid, 0 otherwise.
59  */
60 static int is_valid_samplerate(const struct sr_dev_inst *sdi,
61                                uint64_t samplerate)
62 {
63         int i;
64         struct dev_context *devc;
65
66         devc = sdi->priv;
67
68         cv_fill_samplerates_if_needed(sdi);
69
70         for (i = 0; i < 255; i++) {
71                 if (devc->samplerates[i] == samplerate)
72                         return 1;
73         }
74
75         sr_err("Invalid samplerate (%" PRIu64 "Hz).", samplerate);
76
77         return 0;
78 }
79
80 /**
81  * Convert a samplerate (in Hz) to the 'divcount' value the device wants.
82  *
83  * The divcount value can be 0x00 - 0xfe (0xff is not valid).
84  *
85  * LA8:
86  * sample period = (divcount + 1) * 10ns.
87  * divcount = 0x00: 10ns period, 100MHz samplerate.
88  * divcount = 0xfe: 2550ns period, 392.15kHz samplerate.
89  *
90  * LA16:
91  * sample period = (divcount + 1) * 5ns.
92  * divcount = 0x00: 5ns period, 200MHz samplerate.
93  * divcount = 0xfe: 1275ns period, ~784.31kHz samplerate.
94  *
95  * @param sdi Device instance.
96  * @param samplerate The samplerate in Hz.
97  *
98  * @return The divcount value as needed by the hardware, or 0xff upon errors.
99  */
100 SR_PRIV uint8_t cv_samplerate_to_divcount(const struct sr_dev_inst *sdi,
101                                           uint64_t samplerate)
102 {
103         struct dev_context *devc;
104
105         devc = sdi->priv;
106
107         if (samplerate == 0) {
108                 sr_err("Can't convert invalid samplerate of 0 Hz.");
109                 return 0xff;
110         }
111
112         if (!is_valid_samplerate(sdi, samplerate)) {
113                 sr_err("Can't get divcount, samplerate invalid.");
114                 return 0xff;
115         }
116
117         return (devc->prof->max_samplerate / samplerate) - 1;
118 }
119
120 /**
121  * Write data of a certain length to the FTDI device.
122  *
123  * @param devc The struct containing private per-device-instance data. Must not
124  *             be NULL. devc->ftdic must not be NULL either.
125  * @param buf The buffer containing the data to write. Must not be NULL.
126  * @param size The number of bytes to write. Must be > 0.
127  *
128  * @return The number of bytes written, or a negative value upon errors.
129  */
130 SR_PRIV int cv_write(struct dev_context *devc, uint8_t *buf, int size)
131 {
132         int bytes_written;
133
134         /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */
135
136         bytes_written = ftdi_write_data(devc->ftdic, buf, size);
137
138         if (bytes_written < 0) {
139                 sr_err("Failed to write data (%d): %s.",
140                        bytes_written, ftdi_get_error_string(devc->ftdic));
141                 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
142         } else if (bytes_written != size) {
143                 sr_err("Failed to write data, only %d/%d bytes written.",
144                        size, bytes_written);
145                 (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
146         }
147
148         return bytes_written;
149 }
150
151 /**
152  * Read a certain amount of bytes from the FTDI device.
153  *
154  * @param devc The struct containing private per-device-instance data. Must not
155  *             be NULL. devc->ftdic must not be NULL either.
156  * @param buf The buffer where the received data will be stored. Must not
157  *            be NULL.
158  * @param size The number of bytes to read. Must be >= 1.
159  *
160  * @return The number of bytes read, or a negative value upon errors.
161  */
162 static int cv_read(struct dev_context *devc, uint8_t *buf, int size)
163 {
164         int bytes_read;
165
166         /* Note: Caller ensures devc/devc->ftdic/buf != NULL and size > 0. */
167
168         bytes_read = ftdi_read_data(devc->ftdic, buf, size);
169
170         if (bytes_read < 0) {
171                 sr_err("Failed to read data (%d): %s.",
172                        bytes_read, ftdi_get_error_string(devc->ftdic));
173         } else if (bytes_read != size) {
174                 // sr_err("Failed to read data, only %d/%d bytes read.",
175                 //        bytes_read, size);
176         }
177
178         return bytes_read;
179 }
180
181 /**
182  * Close the USB port and reset the sequencer logic.
183  *
184  * @param devc The struct containing private per-device-instance data.
185  *
186  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
187  */
188 static int close_usb_reset_sequencer(struct dev_context *devc)
189 {
190         /* Magic sequence of bytes for resetting the sequencer logic. */
191         uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
192         int ret;
193
194         /* Note: Caller checked that devc and devc->ftdic != NULL. */
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("Failed to purge FTDI buffers (%d): %s.",
208                                ret, ftdi_get_error_string(devc->ftdic));
209                 if ((ret = ftdi_usb_reset(devc->ftdic)) < 0)
210                         sr_err("Failed to reset FTDI device (%d): %s.",
211                                ret, ftdi_get_error_string(devc->ftdic));
212                 if ((ret = ftdi_usb_close(devc->ftdic)) < 0)
213                         sr_err("Failed to close FTDI device (%d): %s.",
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);
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  *
231  * @return SR_OK upon success, SR_ERR upon failure.
232  */
233 static int reset_device(struct dev_context *devc)
234 {
235         uint8_t buf[BS];
236         gint64 done, now;
237         int bytes_read;
238
239         /* Note: Caller checked that devc and devc->ftdic != NULL. */
240
241         sr_dbg("Resetting the device.");
242
243         /*
244          * Purge pending read data from the FTDI hardware FIFO until
245          * no more data is left, or a timeout occurs (after 20s).
246          */
247         done = (20 * G_TIME_SPAN_SECOND) + g_get_monotonic_time();
248         do {
249                 /* Try to read bytes until none are left (or errors occur). */
250                 bytes_read = cv_read(devc, (uint8_t *)&buf, BS);
251                 now = g_get_monotonic_time();
252         } while ((done > now) && (bytes_read > 0));
253
254         /* Reset the sequencer logic and close the USB port. */
255         (void) close_usb_reset_sequencer(devc); /* Ignore errors. */
256
257         sr_dbg("Device reset finished.");
258
259         return SR_OK;
260 }
261
262 SR_PRIV int cv_convert_trigger(const struct sr_dev_inst *sdi)
263 {
264         struct dev_context *devc;
265         struct sr_trigger *trigger;
266         struct sr_trigger_stage *stage;
267         struct sr_trigger_match *match;
268         const GSList *l, *m;
269         uint16_t channel_bit;
270
271         devc = sdi->priv;
272         devc->trigger_pattern = 0x0000; /* Default to "low" trigger. */
273         devc->trigger_mask = 0x0000; /* Default to "don't care". */
274         devc->trigger_edgemask = 0x0000; /* Default to "state triggered". */
275
276         if (!(trigger = sr_session_trigger_get(sdi->session)))
277                 return SR_OK;
278
279         if (g_slist_length(trigger->stages) > 1) {
280                 sr_err("This device only supports 1 trigger stage.");
281                 return SR_ERR;
282         }
283
284         for (l = trigger->stages; l; l = l->next) {
285                 stage = l->data;
286                 for (m = stage->matches; m; m = m->next) {
287                         match = m->data;
288                         if (!match->channel->enabled)
289                                 /* Ignore disabled channels with a trigger. */
290                                 continue;
291                         if (devc->prof->model == CHRONOVU_LA8 &&
292                                         (match->match == SR_TRIGGER_RISING
293                                         || match->match == SR_TRIGGER_FALLING)) {
294                                 sr_err("This model supports only simple triggers.");
295                                 return SR_ERR;
296                         }
297                         channel_bit = (1 << (match->channel->index));
298
299                         /* state: 1 == high, edge: 1 == rising edge. */
300                         if (match->match == SR_TRIGGER_ONE
301                                         || match->match == SR_TRIGGER_RISING)
302                                 devc->trigger_pattern |= channel_bit;
303
304                         /* LA16 (but not LA8) supports edge triggering. */
305                         if ((devc->prof->model == CHRONOVU_LA16)) {
306                                 if (match->match == SR_TRIGGER_RISING
307                                                 || match->match == SR_TRIGGER_FALLING)
308                                                 devc->trigger_edgemask |= channel_bit;
309                         }
310                 }
311         }
312
313         sr_dbg("Trigger pattern/mask/edgemask = 0x%04x / 0x%04x / 0x%04x.",
314                         devc->trigger_pattern, devc->trigger_mask,
315                         devc->trigger_edgemask);
316
317         return SR_OK;
318 }
319
320 SR_PRIV int cv_set_samplerate(const struct sr_dev_inst *sdi, uint64_t samplerate)
321 {
322         struct dev_context *devc;
323
324         /* Note: Caller checked that sdi and sdi->priv != NULL. */
325
326         devc = sdi->priv;
327
328         sr_spew("Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
329
330         cv_fill_samplerates_if_needed(sdi);
331
332         /* Check if this is a samplerate supported by the hardware. */
333         if (!is_valid_samplerate(sdi, samplerate)) {
334                 sr_dbg("Failed to set invalid samplerate (%" PRIu64 "Hz).",
335                        samplerate);
336                 return SR_ERR;
337         }
338
339         devc->cur_samplerate = samplerate;
340
341         sr_dbg("Samplerate set to %" PRIu64 "Hz.", devc->cur_samplerate);
342
343         return SR_OK;
344 }
345
346 /**
347  * Get a block of data from the device.
348  *
349  * @param devc The struct containing private per-device-instance data. Must not
350  *             be NULL. devc->ftdic must not be NULL either.
351  *
352  * @return SR_OK upon success, or SR_ERR upon errors.
353  */
354 SR_PRIV int cv_read_block(struct dev_context *devc)
355 {
356         int i, byte_offset, m, mi, p, q, index, bytes_read;
357         gint64 now;
358
359         /* Note: Caller checked that devc and devc->ftdic != NULL. */
360
361         sr_spew("Reading block %d.", devc->block_counter);
362
363         bytes_read = cv_read(devc, devc->mangled_buf, BS);
364
365         /* If first block read got 0 bytes, retry until success or timeout. */
366         if ((bytes_read == 0) && (devc->block_counter == 0)) {
367                 do {
368                         sr_spew("Reading block 0 (again).");
369                         /* Note: If bytes_read < 0 cv_read() will log errors. */
370                         bytes_read = cv_read(devc, devc->mangled_buf, BS);
371                         now = g_get_monotonic_time();
372                 } while ((devc->done > now) && (bytes_read == 0));
373         }
374
375         /* Check if block read was successful or a timeout occurred. */
376         if (bytes_read != BS) {
377                 sr_err("Trigger timed out. Bytes read: %d.", bytes_read);
378                 (void) reset_device(devc); /* Ignore errors. */
379                 return SR_ERR;
380         }
381
382         /* De-mangle the data. */
383         sr_spew("Demangling block %d.", devc->block_counter);
384         byte_offset = devc->block_counter * BS;
385         m = byte_offset / (1024 * 1024);
386         mi = m * (1024 * 1024);
387         for (i = 0; i < BS; i++) {
388                 if (devc->prof->model == CHRONOVU_LA8) {
389                         p = i & (1 << 0);
390                         index = m * 2 + (((byte_offset + i) - mi) / 2) * 16;
391                         index += (devc->divcount == 0) ? p : (1 - p);
392                 } else {
393                         p = i & (1 << 0);
394                         q = i & (1 << 1);
395                         index = m * 4 + (((byte_offset + i) - mi) / 4) * 32;
396                         index += q + (1 - p);
397                 }
398                 devc->final_buf[index] = devc->mangled_buf[i];
399         }
400
401         return SR_OK;
402 }
403
404 SR_PRIV void cv_send_block_to_session_bus(const struct sr_dev_inst *sdi, int block)
405 {
406         int i, idx;
407         uint8_t sample, expected_sample, tmp8;
408         struct sr_datafeed_packet packet;
409         struct sr_datafeed_logic logic;
410         int trigger_point; /* Relative trigger point (in this block). */
411         struct dev_context *devc;
412
413         /* Note: Caller ensures devc/devc->ftdic != NULL and block > 0. */
414
415         devc = sdi->priv;
416
417         /* TODO: Implement/test proper trigger support for the LA16. */
418
419         /* Check if we can find the trigger condition in this block. */
420         trigger_point = -1;
421         expected_sample = devc->trigger_pattern & devc->trigger_mask;
422         for (i = 0; i < BS; i++) {
423                 /* Don't continue if the trigger was found previously. */
424                 if (devc->trigger_found)
425                         break;
426
427                 /*
428                  * Also, don't continue if triggers are "don't care", i.e. if
429                  * no trigger conditions were specified by the user. In that
430                  * case we don't want to send an SR_DF_TRIGGER packet at all.
431                  */
432                 if (devc->trigger_mask == 0x0000)
433                         break;
434
435                 sample = *(devc->final_buf + (block * BS) + i);
436
437                 if ((sample & devc->trigger_mask) == expected_sample) {
438                         trigger_point = i;
439                         devc->trigger_found = 1;
440                         break;
441                 }
442         }
443
444         /* Swap low and high bytes of the 16-bit LA16 samples. */
445         if (devc->prof->model == CHRONOVU_LA16) {
446                 for (i = 0; i < BS; i += 2) {
447                         idx = (block * BS) + i;
448                         tmp8 = devc->final_buf[idx];
449                         devc->final_buf[idx] = devc->final_buf[idx + 1];
450                         devc->final_buf[idx + 1] = tmp8;
451                 }
452         }
453
454         /* If no trigger was found, send one SR_DF_LOGIC packet. */
455         if (trigger_point == -1) {
456                 /* Send an SR_DF_LOGIC packet to the session bus. */
457                 sr_spew("Sending SR_DF_LOGIC packet (%d bytes) for "
458                         "block %d.", BS, block);
459                 packet.type = SR_DF_LOGIC;
460                 packet.payload = &logic;
461                 logic.length = BS;
462                 logic.unitsize = devc->prof->num_channels / 8;
463                 logic.data = devc->final_buf + (block * BS);
464                 sr_session_send(sdi, &packet);
465                 return;
466         }
467
468         /*
469          * We found the trigger, so some special handling is needed. We have
470          * to send an SR_DF_LOGIC packet with the samples before the trigger
471          * (if any), then the SD_DF_TRIGGER packet itself, then another
472          * SR_DF_LOGIC packet with the samples after the trigger (if any).
473          */
474
475         /* TODO: Send SR_DF_TRIGGER packet before or after the actual sample? */
476
477         /* If at least one sample is located before the trigger... */
478         if (trigger_point > 0) {
479                 /* Send pre-trigger SR_DF_LOGIC packet to the session bus. */
480                 sr_spew("Sending pre-trigger SR_DF_LOGIC packet, "
481                         "start = %d, length = %d.", block * BS, trigger_point);
482                 packet.type = SR_DF_LOGIC;
483                 packet.payload = &logic;
484                 logic.length = trigger_point;
485                 logic.unitsize = devc->prof->num_channels / 8;
486                 logic.data = devc->final_buf + (block * BS);
487                 sr_session_send(sdi, &packet);
488         }
489
490         /* Send the SR_DF_TRIGGER packet to the session bus. */
491         sr_spew("Sending SR_DF_TRIGGER packet, sample = %d.",
492                 (block * BS) + trigger_point);
493         packet.type = SR_DF_TRIGGER;
494         packet.payload = NULL;
495         sr_session_send(sdi, &packet);
496
497         /* If at least one sample is located after the trigger... */
498         if (trigger_point < (BS - 1)) {
499                 /* Send post-trigger SR_DF_LOGIC packet to the session bus. */
500                 sr_spew("Sending post-trigger SR_DF_LOGIC packet, "
501                         "start = %d, length = %d.",
502                         (block * BS) + trigger_point, BS - trigger_point);
503                 packet.type = SR_DF_LOGIC;
504                 packet.payload = &logic;
505                 logic.length = BS - trigger_point;
506                 logic.unitsize = devc->prof->num_channels / 8;
507                 logic.data = devc->final_buf + (block * BS) + trigger_point;
508                 sr_session_send(sdi, &packet);
509         }
510 }