]> sigrok.org Git - libsigrok.git/blob - hardware/openbench-logic-sniffer/protocol.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / openbench-logic-sniffer / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "protocol.h"
21 #include <libserialport.h>
22
23 extern SR_PRIV struct sr_dev_driver ols_driver_info;
24 static struct sr_dev_driver *di = &ols_driver_info;
25
26 SR_PRIV int send_shortcommand(struct sr_serial_dev_inst *serial,
27                 uint8_t command)
28 {
29         char buf[1];
30
31         sr_dbg("Sending cmd 0x%.2x.", command);
32         buf[0] = command;
33         if (serial_write_blocking(serial, buf, 1) != 1)
34                 return SR_ERR;
35
36         return SR_OK;
37 }
38
39 SR_PRIV int send_longcommand(struct sr_serial_dev_inst *serial,
40                 uint8_t command, uint8_t *data)
41 {
42         char buf[5];
43
44         sr_dbg("Sending cmd 0x%.2x data 0x%.2x%.2x%.2x%.2x.", command,
45                         data[0], data[1], data[2], data[3]);
46         buf[0] = command;
47         buf[1] = data[0];
48         buf[2] = data[1];
49         buf[3] = data[2];
50         buf[4] = data[3];
51         if (serial_write_blocking(serial, buf, 5) != 5)
52                 return SR_ERR;
53
54         return SR_OK;
55 }
56
57 SR_PRIV int ols_configure_channels(const struct sr_dev_inst *sdi)
58 {
59         struct dev_context *devc;
60         const struct sr_channel *ch;
61         const GSList *l;
62         int channel_bit, stage, i;
63         char *tc;
64
65         devc = sdi->priv;
66
67         devc->channel_mask = 0;
68         for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
69                 devc->trigger_mask[i] = 0;
70                 devc->trigger_value[i] = 0;
71         }
72
73         devc->num_stages = 0;
74         for (l = sdi->channels; l; l = l->next) {
75                 ch = (const struct sr_channel *)l->data;
76                 if (!ch->enabled)
77                         continue;
78
79                 if (ch->index >= devc->max_channels) {
80                         sr_err("Channels over the limit of %d\n", devc->max_channels);
81                         return SR_ERR;
82                 }
83
84                 /*
85                  * Set up the channel mask for later configuration into the
86                  * flag register.
87                  */
88                 channel_bit = 1 << (ch->index);
89                 devc->channel_mask |= channel_bit;
90
91                 if (!ch->trigger)
92                         continue;
93
94                 /* Configure trigger mask and value. */
95                 stage = 0;
96                 for (tc = ch->trigger; tc && *tc; tc++) {
97                         devc->trigger_mask[stage] |= channel_bit;
98                         if (*tc == '1')
99                                 devc->trigger_value[stage] |= channel_bit;
100                         stage++;
101                         /* Only supporting parallel mode, with up to 4 stages. */
102                         if (stage > 4)
103                                 return SR_ERR;
104                 }
105                 if (stage > devc->num_stages)
106                         devc->num_stages = stage - 1;
107         }
108
109         return SR_OK;
110 }
111
112 SR_PRIV struct dev_context *ols_dev_new(void)
113 {
114         struct dev_context *devc;
115
116         if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
117                 sr_err("Device context malloc failed.");
118                 return NULL;
119         }
120
121         /* Device-specific settings */
122         devc->max_samples = devc->max_samplerate = devc->protocol_version = 0;
123
124         /* Acquisition settings */
125         devc->limit_samples = devc->capture_ratio = 0;
126         devc->trigger_at = -1;
127         devc->channel_mask = 0xffffffff;
128         devc->flag_reg = 0;
129
130         return devc;
131 }
132
133 SR_PRIV struct sr_dev_inst *get_metadata(struct sr_serial_dev_inst *serial)
134 {
135         struct sr_dev_inst *sdi;
136         struct dev_context *devc;
137         struct sr_channel *ch;
138         uint32_t tmp_int, ui;
139         uint8_t key, type, token;
140         GString *tmp_str, *devname, *version;
141         guchar tmp_c;
142
143         sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
144         sdi->driver = di;
145         devc = ols_dev_new();
146         sdi->priv = devc;
147
148         devname = g_string_new("");
149         version = g_string_new("");
150
151         key = 0xff;
152         while (key) {
153                 if (serial_read_blocking(serial, &key, 1) != 1)
154                         break;
155                 if (key == 0x00) {
156                         sr_dbg("Got metadata key 0x00, metadata ends.");
157                         break;
158                 }
159                 type = key >> 5;
160                 token = key & 0x1f;
161                 switch (type) {
162                 case 0:
163                         /* NULL-terminated string */
164                         tmp_str = g_string_new("");
165                         while (serial_read_blocking(serial, &tmp_c, 1) == 1 && tmp_c != '\0')
166                                 g_string_append_c(tmp_str, tmp_c);
167                         sr_dbg("Got metadata key 0x%.2x value '%s'.",
168                                key, tmp_str->str);
169                         switch (token) {
170                         case 0x01:
171                                 /* Device name */
172                                 devname = g_string_append(devname, tmp_str->str);
173                                 break;
174                         case 0x02:
175                                 /* FPGA firmware version */
176                                 if (version->len)
177                                         g_string_append(version, ", ");
178                                 g_string_append(version, "FPGA version ");
179                                 g_string_append(version, tmp_str->str);
180                                 break;
181                         case 0x03:
182                                 /* Ancillary version */
183                                 if (version->len)
184                                         g_string_append(version, ", ");
185                                 g_string_append(version, "Ancillary version ");
186                                 g_string_append(version, tmp_str->str);
187                                 break;
188                         default:
189                                 sr_info("ols: unknown token 0x%.2x: '%s'",
190                                         token, tmp_str->str);
191                                 break;
192                         }
193                         g_string_free(tmp_str, TRUE);
194                         break;
195                 case 1:
196                         /* 32-bit unsigned integer */
197                         if (serial_read_blocking(serial, &tmp_int, 4) != 4)
198                                 break;
199                         tmp_int = RB32(&tmp_int);
200                         sr_dbg("Got metadata key 0x%.2x value 0x%.8x.",
201                                key, tmp_int);
202                         switch (token) {
203                         case 0x00:
204                                 /* Number of usable channels */
205                                 for (ui = 0; ui < tmp_int; ui++) {
206                                         if (!(ch = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
207                                                         ols_channel_names[ui])))
208                                                 return 0;
209                                         sdi->channels = g_slist_append(sdi->channels, ch);
210                                 }
211                                 break;
212                         case 0x01:
213                                 /* Amount of sample memory available (bytes) */
214                                 devc->max_samples = tmp_int;
215                                 break;
216                         case 0x02:
217                                 /* Amount of dynamic memory available (bytes) */
218                                 /* what is this for? */
219                                 break;
220                         case 0x03:
221                                 /* Maximum sample rate (hz) */
222                                 devc->max_samplerate = tmp_int;
223                                 break;
224                         case 0x04:
225                                 /* protocol version */
226                                 devc->protocol_version = tmp_int;
227                                 break;
228                         default:
229                                 sr_info("Unknown token 0x%.2x: 0x%.8x.",
230                                         token, tmp_int);
231                                 break;
232                         }
233                         break;
234                 case 2:
235                         /* 8-bit unsigned integer */
236                         if (serial_read_blocking(serial, &tmp_c, 1) != 1)
237                                 break;
238                         sr_dbg("Got metadata key 0x%.2x value 0x%.2x.",
239                                key, tmp_c);
240                         switch (token) {
241                         case 0x00:
242                                 /* Number of usable channels */
243                                 for (ui = 0; ui < tmp_c; ui++) {
244                                         if (!(ch = sr_probe_new(ui, SR_PROBE_LOGIC, TRUE,
245                                                         ols_channel_names[ui])))
246                                                 return 0;
247                                         sdi->channels = g_slist_append(sdi->channels, ch);
248                                 }
249                                 break;
250                         case 0x01:
251                                 /* protocol version */
252                                 devc->protocol_version = tmp_c;
253                                 break;
254                         default:
255                                 sr_info("Unknown token 0x%.2x: 0x%.2x.",
256                                         token, tmp_c);
257                                 break;
258                         }
259                         break;
260                 default:
261                         /* unknown type */
262                         break;
263                 }
264         }
265
266         sdi->model = devname->str;
267         sdi->version = version->str;
268         g_string_free(devname, FALSE);
269         g_string_free(version, FALSE);
270
271         return sdi;
272 }
273
274 SR_PRIV int ols_set_samplerate(const struct sr_dev_inst *sdi,
275                 const uint64_t samplerate)
276 {
277         struct dev_context *devc;
278
279         devc = sdi->priv;
280         if (devc->max_samplerate && samplerate > devc->max_samplerate)
281                 return SR_ERR_SAMPLERATE;
282
283         if (samplerate > CLOCK_RATE) {
284                 sr_info("Enabling demux mode.");
285                 devc->flag_reg |= FLAG_DEMUX;
286                 devc->flag_reg &= ~FLAG_FILTER;
287                 devc->max_channels = NUM_PROBES / 2;
288                 devc->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
289         } else {
290                 sr_info("Disabling demux mode.");
291                 devc->flag_reg &= ~FLAG_DEMUX;
292                 devc->flag_reg |= FLAG_FILTER;
293                 devc->max_channels = NUM_PROBES;
294                 devc->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
295         }
296
297         /* Calculate actual samplerate used and complain if it is different
298          * from the requested.
299          */
300         devc->cur_samplerate = CLOCK_RATE / (devc->cur_samplerate_divider + 1);
301         if (devc->flag_reg & FLAG_DEMUX)
302                 devc->cur_samplerate *= 2;
303         if (devc->cur_samplerate != samplerate)
304                 sr_info("Can't match samplerate %" PRIu64 ", using %"
305                        PRIu64 ".", samplerate, devc->cur_samplerate);
306
307         return SR_OK;
308 }
309
310 SR_PRIV void abort_acquisition(const struct sr_dev_inst *sdi)
311 {
312         struct sr_datafeed_packet packet;
313         struct sr_serial_dev_inst *serial;
314
315         serial = sdi->conn;
316         serial_source_remove(serial);
317
318         /* Terminate session */
319         packet.type = SR_DF_END;
320         sr_session_send(sdi, &packet);
321 }
322
323 SR_PRIV int ols_receive_data(int fd, int revents, void *cb_data)
324 {
325         struct dev_context *devc;
326         struct sr_dev_inst *sdi;
327         struct sr_serial_dev_inst *serial;
328         struct sr_datafeed_packet packet;
329         struct sr_datafeed_logic logic;
330         uint32_t sample;
331         int num_channels, offset, j;
332         unsigned int i;
333         unsigned char byte;
334
335         (void)fd;
336
337         sdi = cb_data;
338         serial = sdi->conn;
339         devc = sdi->priv;
340
341         if (devc->num_transfers++ == 0) {
342                 /*
343                  * First time round, means the device started sending data,
344                  * and will not stop until done. If it stops sending for
345                  * longer than it takes to send a byte, that means it's
346                  * finished. We'll double that to 30ms to be sure...
347                  */
348                 serial_source_remove(serial);
349                 serial_source_add(serial, G_IO_IN, 30, ols_receive_data, cb_data);
350                 devc->raw_sample_buf = g_try_malloc(devc->limit_samples * 4);
351                 if (!devc->raw_sample_buf) {
352                         sr_err("Sample buffer malloc failed.");
353                         return FALSE;
354                 }
355                 /* fill with 1010... for debugging */
356                 memset(devc->raw_sample_buf, 0x82, devc->limit_samples * 4);
357         }
358
359         num_channels = 0;
360         for (i = NUM_PROBES; i > 0x02; i /= 2) {
361                 if ((devc->flag_reg & i) == 0) {
362                         num_channels++;
363                 }
364         }
365
366         if (revents == G_IO_IN && devc->num_samples < devc->limit_samples) {
367                 if (serial_read_nonblocking(serial, &byte, 1) != 1)
368                         return FALSE;
369                 devc->cnt_bytes++;
370
371                 /* Ignore it if we've read enough. */
372                 if (devc->num_samples >= devc->limit_samples)
373                         return TRUE;
374
375                 devc->sample[devc->num_bytes++] = byte;
376                 sr_spew("Received byte 0x%.2x.", byte);
377                 if (devc->num_bytes == num_channels) {
378                         devc->cnt_samples++;
379                         devc->cnt_samples_rle++;
380                         /*
381                          * Got a full sample. Convert from the OLS's little-endian
382                          * sample to the local format.
383                          */
384                         sample = devc->sample[0] | (devc->sample[1] << 8) \
385                                         | (devc->sample[2] << 16) | (devc->sample[3] << 24);
386                         sr_dbg("Received sample 0x%.*x.", devc->num_bytes * 2, sample);
387                         if (devc->flag_reg & FLAG_RLE) {
388                                 /*
389                                  * In RLE mode the high bit of the sample is the
390                                  * "count" flag, meaning this sample is the number
391                                  * of times the previous sample occurred.
392                                  */
393                                 if (devc->sample[devc->num_bytes - 1] & 0x80) {
394                                         /* Clear the high bit. */
395                                         sample &= ~(0x80 << (devc->num_bytes - 1) * 8);
396                                         devc->rle_count = sample;
397                                         devc->cnt_samples_rle += devc->rle_count;
398                                         sr_dbg("RLE count: %u.", devc->rle_count);
399                                         devc->num_bytes = 0;
400                                         return TRUE;
401                                 }
402                         }
403                         devc->num_samples += devc->rle_count + 1;
404                         if (devc->num_samples > devc->limit_samples) {
405                                 /* Save us from overrunning the buffer. */
406                                 devc->rle_count -= devc->num_samples - devc->limit_samples;
407                                 devc->num_samples = devc->limit_samples;
408                         }
409
410                         if (num_channels < 4) {
411                                 /*
412                                  * Some channel groups may have been turned
413                                  * off, to speed up transfer between the
414                                  * hardware and the PC. Expand that here before
415                                  * submitting it over the session bus --
416                                  * whatever is listening on the bus will be
417                                  * expecting a full 32-bit sample, based on
418                                  * the number of channels.
419                                  */
420                                 j = 0;
421                                 memset(devc->tmp_sample, 0, 4);
422                                 for (i = 0; i < 4; i++) {
423                                         if (((devc->flag_reg >> 2) & (1 << i)) == 0) {
424                                                 /*
425                                                  * This channel group was
426                                                  * enabled, copy from received
427                                                  * sample.
428                                                  */
429                                                 devc->tmp_sample[i] = devc->sample[j++];
430                                         } else if (devc->flag_reg & FLAG_DEMUX && (i > 2)) {
431                                                 /* group 2 & 3 get added to 0 & 1 */
432                                                 devc->tmp_sample[i - 2] = devc->sample[j++];
433                                         }
434                                 }
435                                 memcpy(devc->sample, devc->tmp_sample, 4);
436                                 sr_spew("Expanded sample: 0x%.8x.", sample);
437                         }
438
439                         /*
440                          * the OLS sends its sample buffer backwards.
441                          * store it in reverse order here, so we can dump
442                          * this on the session bus later.
443                          */
444                         offset = (devc->limit_samples - devc->num_samples) * 4;
445                         for (i = 0; i <= devc->rle_count; i++) {
446                                 memcpy(devc->raw_sample_buf + offset + (i * 4),
447                                        devc->sample, 4);
448                         }
449                         memset(devc->sample, 0, 4);
450                         devc->num_bytes = 0;
451                         devc->rle_count = 0;
452                 }
453         } else {
454                 /*
455                  * This is the main loop telling us a timeout was reached, or
456                  * we've acquired all the samples we asked for -- we're done.
457                  * Send the (properly-ordered) buffer to the frontend.
458                  */
459                 sr_dbg("Received %d bytes, %d samples, %d decompressed samples.",
460                                 devc->cnt_bytes, devc->cnt_samples,
461                                 devc->cnt_samples_rle);
462                 if (devc->trigger_at != -1) {
463                         /*
464                          * A trigger was set up, so we need to tell the frontend
465                          * about it.
466                          */
467                         if (devc->trigger_at > 0) {
468                                 /* There are pre-trigger samples, send those first. */
469                                 packet.type = SR_DF_LOGIC;
470                                 packet.payload = &logic;
471                                 logic.length = devc->trigger_at * 4;
472                                 logic.unitsize = 4;
473                                 logic.data = devc->raw_sample_buf +
474                                         (devc->limit_samples - devc->num_samples) * 4;
475                                 sr_session_send(cb_data, &packet);
476                         }
477
478                         /* Send the trigger. */
479                         packet.type = SR_DF_TRIGGER;
480                         sr_session_send(cb_data, &packet);
481
482                         /* Send post-trigger samples. */
483                         packet.type = SR_DF_LOGIC;
484                         packet.payload = &logic;
485                         logic.length = (devc->num_samples * 4) - (devc->trigger_at * 4);
486                         logic.unitsize = 4;
487                         logic.data = devc->raw_sample_buf + devc->trigger_at * 4 +
488                                 (devc->limit_samples - devc->num_samples) * 4;
489                         sr_session_send(cb_data, &packet);
490                 } else {
491                         /* no trigger was used */
492                         packet.type = SR_DF_LOGIC;
493                         packet.payload = &logic;
494                         logic.length = devc->num_samples * 4;
495                         logic.unitsize = 4;
496                         logic.data = devc->raw_sample_buf +
497                                 (devc->limit_samples - devc->num_samples) * 4;
498                         sr_session_send(cb_data, &packet);
499                 }
500                 g_free(devc->raw_sample_buf);
501
502                 serial_flush(serial);
503                 abort_acquisition(sdi);
504         }
505
506         return TRUE;
507 }