2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
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 2 of the License, or
9 * (at your option) any later version.
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.
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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "input/chronovu-la8"
30 #define DEFAULT_NUM_CHANNELS 8
31 #define DEFAULT_SAMPLERATE SR_MHZ(100)
32 #define CHUNK_SIZE (4 * 1024 * 1024)
36 * - Fixed size 8MiB data part at offset 0.
37 * - Either one byte per sample for LA8.
38 * - Or two bytes per sample for LA16, in little endian format.
39 * - Five byte "header" at offset 8MiB.
40 * - One "clock divider" byte. The byte value is the divider factor
41 * minus 1. Value 0xff is invalid. Base clock is 100MHz for LA8, or
43 * - Four bytes for the trigger position. This 32bit value is the
44 * sample number in little endian format, or 0 when unused.
46 #define CHRONOVU_LA8_DATASIZE (8 * 1024 * 1024)
47 #define CHRONOVU_LA8_HDRSIZE (sizeof(uint8_t) + sizeof(uint32_t))
48 #define CHRONOVU_LA8_FILESIZE (CHRONOVU_LA8_DATASIZE + CHRONOVU_LA8_HDRSIZE)
53 uint64_t samples_remain;
56 static int format_match(GHashTable *metadata, unsigned int *confidence)
61 * In the absence of a reliable condition like magic strings,
62 * we can only guess based on the file size. Since this is
63 * rather weak a condition, signal "little confidence" and
64 * optionally give precedence to better matches.
66 size = GPOINTER_TO_SIZE(g_hash_table_lookup(metadata,
67 GINT_TO_POINTER(SR_INPUT_META_FILESIZE)));
68 if (size != CHRONOVU_LA8_FILESIZE)
75 static int init(struct sr_input *in, GHashTable *options)
81 num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
82 if (num_channels < 1) {
83 sr_err("Invalid value for numchannels: must be at least 1.");
87 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
88 in->priv = inc = g_malloc0(sizeof(struct context));
90 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
92 for (i = 0; i < num_channels; i++) {
93 snprintf(name, sizeof(name), "%d", i);
94 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
100 static int process_buffer(struct sr_input *in)
102 struct sr_datafeed_packet packet;
103 struct sr_datafeed_meta meta;
104 struct sr_datafeed_logic logic;
105 struct sr_config *src;
112 unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
115 std_session_send_df_header(in->sdi);
117 if (inc->samplerate) {
118 packet.type = SR_DF_META;
119 packet.payload = &meta;
120 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
121 meta.config = g_slist_append(NULL, src);
122 sr_session_send(in->sdi, &packet);
123 g_slist_free(meta.config);
127 inc->samples_remain = CHRONOVU_LA8_DATASIZE;
128 inc->samples_remain /= unitsize;
133 packet.type = SR_DF_LOGIC;
134 packet.payload = &logic;
135 logic.unitsize = unitsize;
137 /* Cut off at multiple of unitsize. Avoid sending the "header". */
138 chunk_size = in->buf->len / logic.unitsize * logic.unitsize;
139 chunk_size = MIN(chunk_size, inc->samples_remain * unitsize);
141 for (i = 0; i < chunk_size; i += chunk) {
142 logic.data = in->buf->str + i;
143 chunk = MIN(CHUNK_SIZE, chunk_size - i);
145 logic.length = chunk;
146 sr_session_send(in->sdi, &packet);
147 inc->samples_remain -= chunk / unitsize;
150 g_string_erase(in->buf, 0, chunk_size);
155 static int receive(struct sr_input *in, GString *buf)
159 g_string_append_len(in->buf, buf->str, buf->len);
161 if (!in->sdi_ready) {
162 /* sdi is ready, notify frontend. */
163 in->sdi_ready = TRUE;
167 ret = process_buffer(in);
172 static int end(struct sr_input *in)
178 ret = process_buffer(in);
184 std_session_send_df_end(in->sdi);
189 static int reset(struct sr_input *in)
191 struct context *inc = in->priv;
193 inc->started = FALSE;
194 g_string_truncate(in->buf, 0);
199 static struct sr_option options[] = {
200 { "numchannels", "Number of logic channels", "The number of (logic) channels in the data", NULL, NULL },
201 { "samplerate", "Sample rate (Hz)", "The sample rate of the (logic) data in Hz", NULL, NULL },
205 static const struct sr_option *get_options(void)
207 if (!options[0].def) {
208 options[0].def = g_variant_ref_sink(g_variant_new_int32(DEFAULT_NUM_CHANNELS));
209 options[1].def = g_variant_ref_sink(g_variant_new_uint64(DEFAULT_SAMPLERATE));
215 SR_PRIV struct sr_input_module input_chronovu_la8 = {
216 .id = "chronovu-la8",
217 .name = "ChronoVu LA8",
218 .desc = "ChronoVu LA8 native file format data",
219 .exts = (const char*[]){"kdt", NULL},
220 .metadata = { SR_INPUT_META_FILESIZE | SR_INPUT_META_REQUIRED },
221 .options = get_options,
222 .format_match = format_match,