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