2 * This file is part of the libsigrok project.
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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 2 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/>.
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
28 #define LOG_PREFIX "output/chronovu-la8"
31 unsigned int num_enabled_channels;
40 * Check if the given samplerate is supported by the LA8 hardware.
42 * @param samplerate The samplerate (in Hz) to check.
44 * @return 1 if the samplerate is supported/valid, 0 otherwise.
46 static gboolean is_valid_samplerate(uint64_t samplerate)
50 for (i = 0; i < 255; i++) {
51 if (samplerate == (SR_MHZ(100) / (i + 1)))
59 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
61 * LA8 hardware: sample period = (divcount + 1) * 10ns.
62 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
63 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
65 * @param samplerate The samplerate in Hz.
67 * @return The divcount value as needed by the hardware, or 0xff upon errors.
69 static uint8_t samplerate_to_divcount(uint64_t samplerate)
71 if (samplerate == 0 || !is_valid_samplerate(samplerate)) {
72 sr_warn("Invalid samplerate (%" PRIu64 "Hz)", samplerate);
76 return (SR_MHZ(100) / samplerate) - 1;
79 static int init(struct sr_output *o, GHashTable *options)
82 struct sr_channel *ch;
90 ctx = g_malloc0(sizeof(struct context));
93 for (l = o->sdi->channels; l; l = l->next) {
95 if (ch->type != SR_CHANNEL_LOGIC)
99 ctx->num_enabled_channels++;
101 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
102 ctx->pretrig_buf = g_string_sized_new(1024);
107 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
110 const struct sr_datafeed_logic *logic;
119 if (!(ctx = o->priv))
122 switch (packet->type) {
124 /* One byte for the 'divcount' value. */
125 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
127 samplerate = g_variant_get_uint64(gvar);
128 g_variant_unref(gvar);
131 c[0] = samplerate_to_divcount(samplerate);
132 *out = g_string_new_len(c, 1);
133 ctx->triggered = FALSE;
136 /* Four bytes (little endian) for the trigger point. */
137 c[0] = ctx->samplecount & 0xff;
138 c[1] = (ctx->samplecount >> 8) & 0xff;
139 c[2] = (ctx->samplecount >> 16) & 0xff;
140 c[3] = (ctx->samplecount >> 24) & 0xff;
141 *out = g_string_new_len(c, 4);
142 /* Flush the pre-trigger buffer. */
143 if (ctx->pretrig_buf->len)
144 g_string_append_len(*out, ctx->pretrig_buf->str,
145 ctx->pretrig_buf->len);
146 ctx->triggered = TRUE;
149 logic = packet->payload;
151 g_string_append_len(ctx->pretrig_buf, logic->data, logic->length);
153 *out = g_string_new_len(logic->data, logic->length);
154 ctx->samplecount += logic->length / logic->unitsize;
157 if (!ctx->triggered && ctx->pretrig_buf->len) {
158 /* We never got a trigger, submit an empty one. */
159 *out = g_string_sized_new(ctx->pretrig_buf->len + 4);
160 g_string_append_len(*out, "\x00\x00\x00\x00", 4);
161 g_string_append_len(*out, ctx->pretrig_buf->str, ctx->pretrig_buf->len);
169 static int cleanup(struct sr_output *o)
178 g_string_free(ctx->pretrig_buf, TRUE);
179 g_free(ctx->channel_index);
187 SR_PRIV struct sr_output_module output_chronovu_la8 = {
188 .id = "chronovu-la8",
189 .name = "ChronoVu LA8",
190 .desc = "ChronoVu LA8 native file format",
191 .exts = (const char*[]){"kdt", NULL},