2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
5 * Copyright (C) 2011 HÃ¥vard Espeland <gus@ping.uio.no>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #define DEFAULT_BPL_BITS 64
29 #define DEFAULT_BPL_HEX 192
30 #define DEFAULT_BPL_ASCII 74
33 unsigned int num_enabled_probes;
35 unsigned int unitsize;
47 static void flush_linebufs(struct context *ctx, char *outbuf)
49 static int max_probename_len = 0;
52 if (ctx->linebuf[0] == 0)
55 if (max_probename_len == 0) {
56 /* First time through... */
57 for (i = 0; ctx->probelist[i]; i++) {
58 len = strlen(ctx->probelist[i]);
59 if (len > max_probename_len)
60 max_probename_len = len;
64 for (i = 0; ctx->probelist[i]; i++) {
65 sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
66 ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
69 /* Mark trigger with a ^ character. */
70 if (ctx->mark_trigger != -1)
71 sprintf(outbuf + strlen(outbuf), "T:%*s^\n",
72 ctx->mark_trigger + (ctx->mark_trigger / 8), "");
74 memset(ctx->linebuf, 0, i * ctx->linebuf_len);
77 static int init(struct output *o, int default_spl)
86 if (!(ctx = calloc(1, sizeof(struct context))))
87 return SIGROK_ERR_MALLOC;
90 ctx->num_enabled_probes = 0;
92 for (l = o->device->probes; l; l = l->next) {
96 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
99 ctx->probelist[ctx->num_enabled_probes] = 0;
100 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
101 ctx->line_offset = 0;
103 ctx->mark_trigger = -1;
105 if (o->param && o->param[0]) {
106 ctx->samples_per_line = strtoul(o->param, NULL, 10);
107 if (ctx->samples_per_line < 1)
110 ctx->samples_per_line = default_spl;
112 if (!(ctx->header = malloc(512))) {
114 return SIGROK_ERR_MALLOC;
117 snprintf(ctx->header, 511, "%s\n", PACKAGE_STRING);
118 num_probes = g_slist_length(o->device->probes);
119 if (o->device->plugin) {
120 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
121 o->device->plugin_index, DI_CUR_SAMPLERATE));
122 if (!(samplerate_s = sigrok_samplerate_string(samplerate))) {
127 snprintf(ctx->header + strlen(ctx->header),
128 511 - strlen(ctx->header),
129 "Acquisition with %d/%d probes at %s\n",
130 ctx->num_enabled_probes, num_probes, samplerate_s);
134 ctx->linebuf_len = ctx->samples_per_line * 2 + 4;
135 if (!(ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len))) {
138 return SIGROK_ERR_MALLOC;
140 if (!(ctx->linevalues = calloc(1, num_probes))) {
143 return SIGROK_ERR_MALLOC;
149 static int event(struct output *o, int event_type, char **data_out,
150 uint64_t *length_out)
157 switch (event_type) {
159 ctx->mark_trigger = ctx->spl_cnt;
164 outsize = ctx->num_enabled_probes
165 * (ctx->samples_per_line + 20) + 512;
166 if (!(outbuf = calloc(1, outsize)))
167 return SIGROK_ERR_MALLOC;
168 flush_linebufs(ctx, outbuf);
170 *length_out = strlen(outbuf);
183 static int init_bits(struct output *o)
185 return init(o, DEFAULT_BPL_BITS);
188 static int data_bits(struct output *o, char *data_in, uint64_t length_in,
189 char **data_out, uint64_t *length_out)
192 unsigned int outsize, offset, p;
198 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
199 + ctx->samples_per_line / 8;
201 * Calculate space needed for probes. Set aside 512 bytes for
202 * extra output, e.g. trigger.
204 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
205 * (ctx->num_enabled_probes * max_linelen);
207 if (!(outbuf = calloc(1, outsize + 1)))
208 return SIGROK_ERR_MALLOC;
212 /* The header is still here, this must be the first packet. */
213 strncpy(outbuf, ctx->header, outsize);
217 /* Ensure first transition. */
218 memcpy(&ctx->prevsample, data_in, ctx->unitsize);
219 ctx->prevsample = ~ctx->prevsample;
222 if (length_in >= ctx->unitsize) {
223 for (offset = 0; offset <= length_in - ctx->unitsize;
224 offset += ctx->unitsize) {
225 memcpy(&sample, data_in + offset, ctx->unitsize);
226 for (p = 0; p < ctx->num_enabled_probes; p++) {
227 c = (sample & ((uint64_t) 1 << p)) ? '1' : '0';
228 ctx->linebuf[p * ctx->linebuf_len +
229 ctx->line_offset] = c;
234 /* Add a space every 8th bit. */
235 if ((ctx->spl_cnt & 7) == 0) {
236 for (p = 0; p < ctx->num_enabled_probes; p++)
237 ctx->linebuf[p * ctx->linebuf_len +
238 ctx->line_offset] = ' ';
243 if (ctx->spl_cnt >= ctx->samples_per_line) {
244 flush_linebufs(ctx, outbuf);
245 ctx->line_offset = ctx->spl_cnt = 0;
246 ctx->mark_trigger = -1;
250 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
254 *length_out = strlen(outbuf);
259 static int init_hex(struct output *o)
261 return init(o, DEFAULT_BPL_HEX);
264 static int data_hex(struct output *o, char *data_in, uint64_t length_in,
265 char **data_out, uint64_t *length_out)
268 unsigned int outsize, offset, p;
274 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
275 + ctx->samples_per_line / 2;
276 outsize = length_in / ctx->unitsize * ctx->num_enabled_probes
277 / ctx->samples_per_line * max_linelen + 512;
279 if (!(outbuf = calloc(1, outsize + 1)))
280 return SIGROK_ERR_MALLOC;
284 /* The header is still here, this must be the first packet. */
285 strncpy(outbuf, ctx->header, outsize);
290 ctx->line_offset = 0;
291 for (offset = 0; offset <= length_in - ctx->unitsize;
292 offset += ctx->unitsize) {
293 memcpy(&sample, data_in + offset, ctx->unitsize);
294 for (p = 0; p < ctx->num_enabled_probes; p++) {
295 ctx->linevalues[p] <<= 1;
296 if (sample & ((uint64_t) 1 << p))
297 ctx->linevalues[p] |= 1;
298 sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
299 ctx->line_offset, "%.2x", ctx->linevalues[p]);
303 /* Add a space after every complete hex byte. */
304 if ((ctx->spl_cnt & 7) == 0) {
305 for (p = 0; p < ctx->num_enabled_probes; p++)
306 ctx->linebuf[p * ctx->linebuf_len +
307 ctx->line_offset + 2] = ' ';
308 ctx->line_offset += 3;
312 if (ctx->spl_cnt >= ctx->samples_per_line) {
313 flush_linebufs(ctx, outbuf);
314 ctx->line_offset = ctx->spl_cnt = 0;
319 *length_out = strlen(outbuf);
324 static int init_ascii(struct output *o)
326 return init(o, DEFAULT_BPL_ASCII);
329 static int data_ascii(struct output *o, char *data_in, uint64_t length_in,
330 char **data_out, uint64_t *length_out)
333 unsigned int outsize, offset, p;
339 max_linelen = MAX_PROBENAME_LEN + 3 + ctx->samples_per_line
340 + ctx->samples_per_line / 8;
342 * Calculate space needed for probes. Set aside 512 bytes for
343 * extra output, e.g. trigger.
345 outsize = 512 + (1 + (length_in / ctx->unitsize) / ctx->samples_per_line)
346 * (ctx->num_enabled_probes * max_linelen);
348 if (!(outbuf = calloc(1, outsize + 1)))
349 return SIGROK_ERR_MALLOC;
353 /* The header is still here, this must be the first packet. */
354 strncpy(outbuf, ctx->header, outsize);
359 if (length_in >= ctx->unitsize) {
360 for (offset = 0; offset <= length_in - ctx->unitsize;
361 offset += ctx->unitsize) {
362 memcpy(&sample, data_in + offset, ctx->unitsize);
364 char tmpval[ctx->num_enabled_probes];
366 for (p = 0; p < ctx->num_enabled_probes; p++) {
367 uint64_t curbit = (sample & ((uint64_t) 1 << p));
368 uint64_t prevbit = (ctx->prevsample &
369 ((uint64_t) 1 << p));
371 if (curbit < prevbit && ctx->line_offset > 0) {
372 ctx->linebuf[p * ctx->linebuf_len +
373 ctx->line_offset-1] = '\\';
376 if (curbit > prevbit) {
387 if (ctx->spl_cnt >= ctx->samples_per_line) {
388 flush_linebufs(ctx, outbuf);
389 ctx->line_offset = ctx->spl_cnt = 0;
390 ctx->mark_trigger = -1;
393 for (p = 0; p < ctx->num_enabled_probes; p++) {
394 ctx->linebuf[p * ctx->linebuf_len +
395 ctx->line_offset] = tmpval[p];
401 ctx->prevsample = sample;
404 g_message("short buffer (length_in=%" PRIu64 ")", length_in);
408 *length_out = strlen(outbuf);
414 struct output_format output_text_bits = {
416 "Bits (takes argument, default 64)",
423 struct output_format output_text_hex = {
425 "Hexadecimal (takes argument, default 192)",
432 struct output_format output_text_ascii = {
434 "ASCII (takes argument, default 74)",