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