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