2010-04-02 18:18:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
2010-04-07 17:43:41 +00:00
|
|
|
#include <sigrok.h>
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-04-12 21:21:12 +00:00
|
|
|
#define DEFAULT_BPL_BITS 64
|
|
|
|
#define DEFAULT_BPL_HEX 256
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
struct context {
|
2010-04-09 20:18:46 +00:00
|
|
|
unsigned int num_enabled_probes;
|
2010-04-02 18:18:27 +00:00
|
|
|
int samples_per_line;
|
2010-04-09 20:18:46 +00:00
|
|
|
unsigned int unitsize;
|
2010-04-02 18:18:27 +00:00
|
|
|
int line_offset;
|
|
|
|
int linebuf_len;
|
|
|
|
char *probelist[65];
|
|
|
|
char *linebuf;
|
|
|
|
int spl_cnt;
|
|
|
|
uint8_t *linevalues;
|
|
|
|
char *header;
|
|
|
|
};
|
|
|
|
|
2010-04-09 20:18:46 +00:00
|
|
|
static void flush_linebufs(struct context *ctx, char *outbuf)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
static int max_probename_len = 0;
|
|
|
|
int len, i;
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
if (ctx->linebuf[0] == 0)
|
2010-04-02 18:18:27 +00:00
|
|
|
return;
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
if (max_probename_len == 0) {
|
|
|
|
/* First time through... */
|
|
|
|
for (i = 0; ctx->probelist[i]; i++) {
|
2010-04-02 18:18:27 +00:00
|
|
|
len = strlen(ctx->probelist[i]);
|
2010-04-15 18:36:04 +00:00
|
|
|
if (len > max_probename_len)
|
2010-04-02 18:18:27 +00:00
|
|
|
max_probename_len = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
for (i = 0; ctx->probelist[i]; i++) {
|
|
|
|
sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len,
|
|
|
|
ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
memset(ctx->linebuf, 0, i * ctx->linebuf_len);
|
|
|
|
}
|
|
|
|
|
2010-04-05 14:20:09 +00:00
|
|
|
static int init(struct output *o, int default_spl)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
|
|
|
struct probe *probe;
|
|
|
|
GSList *l;
|
|
|
|
uint64_t samplerate;
|
|
|
|
int num_probes;
|
2010-04-07 17:43:41 +00:00
|
|
|
char *samplerate_s;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
ctx = malloc(sizeof(struct context));
|
|
|
|
o->internal = ctx;
|
|
|
|
ctx->num_enabled_probes = 0;
|
2010-04-15 18:36:04 +00:00
|
|
|
|
|
|
|
for (l = o->device->probes; l; l = l->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
probe = l->data;
|
2010-04-15 18:36:04 +00:00
|
|
|
if (probe->enabled)
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->probelist[ctx->num_enabled_probes++] = probe->name;
|
|
|
|
}
|
2010-04-15 18:36:04 +00:00
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->probelist[ctx->num_enabled_probes] = 0;
|
|
|
|
ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
|
|
|
|
ctx->line_offset = 0;
|
|
|
|
ctx->spl_cnt = 0;
|
2010-04-15 18:36:04 +00:00
|
|
|
|
|
|
|
if (o->param && o->param[0])
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->samples_per_line = strtoul(o->param, NULL, 10);
|
|
|
|
else
|
|
|
|
ctx->samples_per_line = default_spl;
|
|
|
|
|
|
|
|
ctx->header = malloc(512);
|
|
|
|
num_probes = g_slist_length(o->device->probes);
|
2010-04-15 18:36:04 +00:00
|
|
|
samplerate = *((uint64_t *) o->device->plugin->get_device_info(
|
|
|
|
o->device->plugin_index, DI_CUR_SAMPLERATE));
|
|
|
|
snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ",
|
|
|
|
ctx->num_enabled_probes, num_probes);
|
2010-04-07 17:43:41 +00:00
|
|
|
|
|
|
|
if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL)
|
2010-04-15 18:36:04 +00:00
|
|
|
return -1; /* FIXME */
|
2010-04-07 17:43:41 +00:00
|
|
|
snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s);
|
|
|
|
free(samplerate_s);
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
ctx->linebuf_len = ctx->samples_per_line * 2;
|
|
|
|
ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len);
|
|
|
|
ctx->linevalues = calloc(1, num_probes);
|
|
|
|
|
2010-04-05 14:20:09 +00:00
|
|
|
return 0;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
static int event(struct output *o, int event_type, char **data_out,
|
|
|
|
uint64_t *length_out)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
|
|
|
int outsize;
|
|
|
|
char *outbuf;
|
|
|
|
|
|
|
|
ctx = o->internal;
|
2010-04-15 18:36:04 +00:00
|
|
|
switch (event_type) {
|
2010-04-02 18:18:27 +00:00
|
|
|
case DF_TRIGGER:
|
|
|
|
break;
|
|
|
|
case DF_END:
|
2010-04-15 18:36:04 +00:00
|
|
|
outsize = ctx->num_enabled_probes
|
|
|
|
* (ctx->samples_per_line + 20) + 512;
|
2010-04-02 18:18:27 +00:00
|
|
|
outbuf = calloc(1, outsize);
|
2010-04-09 20:18:46 +00:00
|
|
|
flush_linebufs(ctx, outbuf);
|
2010-04-02 18:18:27 +00:00
|
|
|
*data_out = outbuf;
|
|
|
|
*length_out = strlen(outbuf);
|
|
|
|
free(o->internal);
|
|
|
|
o->internal = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SIGROK_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-12 21:21:12 +00:00
|
|
|
static int init_bits(struct output *o)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2010-04-12 21:21:12 +00:00
|
|
|
return init(o, DEFAULT_BPL_BITS);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
static int data_bits(struct output *o, char *data_in, uint64_t length_in,
|
|
|
|
char **data_out, uint64_t *length_out)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
2010-04-09 20:18:46 +00:00
|
|
|
unsigned int outsize, offset, p;
|
2010-04-02 18:18:27 +00:00
|
|
|
uint64_t sample;
|
|
|
|
char *outbuf;
|
|
|
|
|
|
|
|
ctx = o->internal;
|
2010-04-15 18:36:04 +00:00
|
|
|
outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
|
|
|
|
ctx->samples_per_line + 512;
|
|
|
|
outbuf = calloc(1, outsize + 1);
|
|
|
|
if (ctx->header) {
|
|
|
|
/* The header is still here, this must be the first packet. */
|
2010-04-02 18:18:27 +00:00
|
|
|
strncpy(outbuf, ctx->header, outsize);
|
|
|
|
free(ctx->header);
|
|
|
|
ctx->header = NULL;
|
2010-04-15 18:36:04 +00:00
|
|
|
} else
|
2010-04-02 18:18:27 +00:00
|
|
|
outbuf[0] = 0;
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
if (length_in >= ctx->unitsize) {
|
|
|
|
for (offset = 0; offset <= length_in - ctx->unitsize;
|
|
|
|
offset += ctx->unitsize) {
|
2010-04-02 18:18:27 +00:00
|
|
|
memcpy(&sample, data_in + offset, ctx->unitsize);
|
2010-04-15 18:36:04 +00:00
|
|
|
for (p = 0; p < ctx->num_enabled_probes; p++) {
|
|
|
|
if (sample & ((uint64_t) 1 << p))
|
|
|
|
ctx->linebuf[p * ctx->linebuf_len +
|
|
|
|
ctx->line_offset] = '1';
|
2010-04-02 18:18:27 +00:00
|
|
|
else
|
2010-04-15 18:36:04 +00:00
|
|
|
ctx->linebuf[p * ctx->linebuf_len +
|
|
|
|
ctx->line_offset] = '0';
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
ctx->line_offset++;
|
|
|
|
ctx->spl_cnt++;
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
/* Add a space every 8th bit. */
|
|
|
|
if ((ctx->spl_cnt & 7) == 0) {
|
|
|
|
for (p = 0; p < ctx->num_enabled_probes; p++)
|
|
|
|
ctx->linebuf[p * ctx->linebuf_len +
|
|
|
|
ctx->line_offset] = ' ';
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->line_offset++;
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
/* End of line. */
|
|
|
|
if (ctx->spl_cnt >= ctx->samples_per_line) {
|
2010-04-09 20:18:46 +00:00
|
|
|
flush_linebufs(ctx, outbuf);
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->line_offset = ctx->spl_cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2010-04-15 18:36:04 +00:00
|
|
|
g_message("short buffer (length_in=%" PRIu64 ")", length_in);
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
*data_out = outbuf;
|
|
|
|
*length_out = strlen(outbuf);
|
|
|
|
|
|
|
|
return SIGROK_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-05 14:20:09 +00:00
|
|
|
static int init_hex(struct output *o)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2010-04-12 21:21:12 +00:00
|
|
|
return init(o, DEFAULT_BPL_BITS);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
static int data_hex(struct output *o, char *data_in, uint64_t length_in,
|
|
|
|
char **data_out, uint64_t *length_out)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
struct context *ctx;
|
2010-04-09 20:18:46 +00:00
|
|
|
unsigned int outsize, offset, p;
|
2010-04-02 18:18:27 +00:00
|
|
|
uint64_t sample;
|
|
|
|
char *outbuf;
|
|
|
|
|
|
|
|
ctx = o->internal;
|
2010-04-15 18:36:04 +00:00
|
|
|
outsize = length_in / ctx->unitsize * ctx->num_enabled_probes *
|
|
|
|
ctx->samples_per_line + 512;
|
|
|
|
outbuf = calloc(1, outsize + 1);
|
|
|
|
if (ctx->header) {
|
|
|
|
/* The header is still here, this must be the first packet. */
|
2010-04-02 18:18:27 +00:00
|
|
|
strncpy(outbuf, ctx->header, outsize);
|
|
|
|
free(ctx->header);
|
|
|
|
ctx->header = NULL;
|
2010-04-15 18:36:04 +00:00
|
|
|
} else
|
2010-04-02 18:18:27 +00:00
|
|
|
outbuf[0] = 0;
|
|
|
|
|
|
|
|
ctx->line_offset = 0;
|
2010-04-15 18:36:04 +00:00
|
|
|
for (offset = 0; offset <= length_in - ctx->unitsize;
|
|
|
|
offset += ctx->unitsize) {
|
2010-04-02 18:18:27 +00:00
|
|
|
memcpy(&sample, data_in + offset, ctx->unitsize);
|
2010-04-15 18:36:04 +00:00
|
|
|
for (p = 0; p < ctx->num_enabled_probes; p++) {
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->linevalues[p] <<= 1;
|
2010-04-15 18:36:04 +00:00
|
|
|
if (sample & ((uint64_t) 1 << p))
|
|
|
|
ctx->linevalues[p] |= 1;
|
|
|
|
sprintf(ctx->linebuf + (p * ctx->linebuf_len) +
|
|
|
|
ctx->line_offset, "%.2x", ctx->linevalues[p]);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
ctx->spl_cnt++;
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
/* Add a space after every complete hex byte. */
|
|
|
|
if ((ctx->spl_cnt & 7) == 0) {
|
|
|
|
for (p = 0; p < ctx->num_enabled_probes; p++)
|
|
|
|
ctx->linebuf[p * ctx->linebuf_len +
|
|
|
|
ctx->line_offset + 2] = ' ';
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->line_offset += 3;
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:36:04 +00:00
|
|
|
/* End of line. */
|
|
|
|
if (ctx->spl_cnt >= ctx->samples_per_line) {
|
2010-04-09 20:18:46 +00:00
|
|
|
flush_linebufs(ctx, outbuf);
|
2010-04-02 18:18:27 +00:00
|
|
|
ctx->line_offset = ctx->spl_cnt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_out = outbuf;
|
|
|
|
*length_out = strlen(outbuf);
|
|
|
|
|
|
|
|
return SIGROK_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-12 21:21:12 +00:00
|
|
|
struct output_format output_text_bits = {
|
2010-04-12 19:22:58 +00:00
|
|
|
"bits",
|
|
|
|
"Text (bits)",
|
2010-04-12 21:21:12 +00:00
|
|
|
init_bits,
|
|
|
|
data_bits,
|
2010-04-15 18:36:04 +00:00
|
|
|
event,
|
2010-04-02 18:18:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct output_format output_text_hex = {
|
|
|
|
"hex",
|
|
|
|
"Text (hexadecimal)",
|
|
|
|
init_hex,
|
|
|
|
data_hex,
|
2010-04-15 18:36:04 +00:00
|
|
|
event,
|
2010-04-02 18:18:27 +00:00
|
|
|
};
|