]> sigrok.org Git - libsigrok.git/blame - src/output/chronovu_la8.c
uni-t-ut181a: silence compiler warning, use of uninitialized variable
[libsigrok.git] / src / output / chronovu_la8.c
CommitLineData
8c48f179 1/*
50985c20 2 * This file is part of the libsigrok project.
8c48f179
UH
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
44559b2c 5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
8c48f179
UH
6 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
44559b2c 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
8c48f179
UH
19 */
20
6ec6c43b 21#include <config.h>
8c48f179
UH
22#include <stdlib.h>
23#include <string.h>
24#include <glib.h>
c1aae900 25#include <libsigrok/libsigrok.h>
45c59c8b 26#include "libsigrok-internal.h"
8c48f179 27
3544f848 28#define LOG_PREFIX "output/chronovu-la8"
a944a84b 29
8c48f179 30struct context {
ba7dd8bb 31 unsigned int num_enabled_channels;
d42eb9d8 32 gboolean triggered;
8c48f179 33 uint64_t samplerate;
d42eb9d8
BV
34 uint64_t samplecount;
35 int *channel_index;
36 GString *pretrig_buf;
8c48f179
UH
37};
38
39/**
40 * Check if the given samplerate is supported by the LA8 hardware.
41 *
42 * @param samplerate The samplerate (in Hz) to check.
44dae539 43 *
8c48f179
UH
44 * @return 1 if the samplerate is supported/valid, 0 otherwise.
45 */
d42eb9d8 46static gboolean is_valid_samplerate(uint64_t samplerate)
8c48f179
UH
47{
48 unsigned int i;
49
50 for (i = 0; i < 255; i++) {
51 if (samplerate == (SR_MHZ(100) / (i + 1)))
d42eb9d8 52 return TRUE;
8c48f179
UH
53 }
54
d42eb9d8 55 return FALSE;
8c48f179
UH
56}
57
58/**
59 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
60 *
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).
64 *
65 * @param samplerate The samplerate in Hz.
44dae539 66 *
8c48f179
UH
67 * @return The divcount value as needed by the hardware, or 0xff upon errors.
68 */
69static uint8_t samplerate_to_divcount(uint64_t samplerate)
70{
d42eb9d8
BV
71 if (samplerate == 0 || !is_valid_samplerate(samplerate)) {
72 sr_warn("Invalid samplerate (%" PRIu64 "Hz)", samplerate);
8c48f179
UH
73 return 0xff;
74 }
75
76 return (SR_MHZ(100) / samplerate) - 1;
77}
78
a755b0e1 79static int init(struct sr_output *o, GHashTable *options)
8c48f179
UH
80{
81 struct context *ctx;
ba7dd8bb 82 struct sr_channel *ch;
8c48f179 83 GSList *l;
8c48f179 84
a755b0e1
BV
85 (void)options;
86
d42eb9d8 87 if (!o || !o->sdi)
8c48f179 88 return SR_ERR_ARG;
8c48f179 89
d42eb9d8 90 ctx = g_malloc0(sizeof(struct context));
d686c5ec 91 o->priv = ctx;
8c48f179 92
ba7dd8bb
UH
93 for (l = o->sdi->channels; l; l = l->next) {
94 ch = l->data;
3f239f08 95 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 96 continue;
ba7dd8bb 97 if (!ch->enabled)
8c48f179 98 continue;
ba7dd8bb 99 ctx->num_enabled_channels++;
8c48f179 100 }
d42eb9d8
BV
101 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
102 ctx->pretrig_buf = g_string_sized_new(1024);
8c48f179 103
5c3c1241 104 return SR_OK;
8c48f179
UH
105}
106
a755b0e1 107static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 108 GString **out)
8c48f179 109{
d42eb9d8 110 const struct sr_datafeed_logic *logic;
8c48f179 111 struct context *ctx;
d42eb9d8
BV
112 GVariant *gvar;
113 uint64_t samplerate;
114 gchar c[4];
8c48f179 115
d42eb9d8
BV
116 *out = NULL;
117 if (!o || !o->sdi)
8c48f179 118 return SR_ERR_ARG;
d686c5ec 119 if (!(ctx = o->priv))
8c48f179 120 return SR_ERR_ARG;
8c48f179 121
d42eb9d8
BV
122 switch (packet->type) {
123 case SR_DF_HEADER:
124 /* One byte for the 'divcount' value. */
125 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
126 &gvar) == SR_OK) {
127 samplerate = g_variant_get_uint64(gvar);
128 g_variant_unref(gvar);
129 } else
130 samplerate = 0;
131 c[0] = samplerate_to_divcount(samplerate);
132 *out = g_string_new_len(c, 1);
133 ctx->triggered = FALSE;
134 break;
8c48f179 135 case SR_DF_TRIGGER:
d42eb9d8
BV
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;
147 break;
148 case SR_DF_LOGIC:
149 logic = packet->payload;
150 if (!ctx->triggered)
151 g_string_append_len(ctx->pretrig_buf, logic->data, logic->length);
152 else
153 *out = g_string_new_len(logic->data, logic->length);
154 ctx->samplecount += logic->length / logic->unitsize;
8c48f179
UH
155 break;
156 case SR_DF_END:
d42eb9d8
BV
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);
8c48f179 162 }
8c48f179
UH
163 break;
164 }
165
166 return SR_OK;
167}
168
d42eb9d8 169static int cleanup(struct sr_output *o)
8c48f179
UH
170{
171 struct context *ctx;
8c48f179 172
d42eb9d8 173 if (!o || !o->sdi)
8c48f179 174 return SR_ERR_ARG;
8c48f179 175
d686c5ec
BV
176 if (o->priv) {
177 ctx = o->priv;
d42eb9d8
BV
178 g_string_free(ctx->pretrig_buf, TRUE);
179 g_free(ctx->channel_index);
d686c5ec
BV
180 g_free(o->priv);
181 o->priv = NULL;
8c48f179
UH
182 }
183
8c48f179
UH
184 return SR_OK;
185}
186
a755b0e1 187SR_PRIV struct sr_output_module output_chronovu_la8 = {
8c48f179 188 .id = "chronovu-la8",
a755b0e1 189 .name = "ChronoVu LA8",
b20eb520 190 .desc = "ChronoVu LA8 native file format data",
8a174d23 191 .exts = (const char*[]){"kdt", NULL},
3cd4b381 192 .flags = 0,
a755b0e1 193 .options = NULL,
8c48f179 194 .init = init,
d42eb9d8
BV
195 .receive = receive,
196 .cleanup = cleanup,
8c48f179 197};