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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
27 #define LOG_PREFIX "output/chronovu-la8"
30 unsigned int num_enabled_probes;
31 unsigned int unitsize;
32 uint64_t trigger_point;
37 * Check if the given samplerate is supported by the LA8 hardware.
39 * @param samplerate The samplerate (in Hz) to check.
41 * @return 1 if the samplerate is supported/valid, 0 otherwise.
43 static int is_valid_samplerate(uint64_t samplerate)
47 for (i = 0; i < 255; i++) {
48 if (samplerate == (SR_MHZ(100) / (i + 1)))
52 sr_warn("%s: invalid samplerate (%" PRIu64 "Hz)",
53 __func__, samplerate);
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) {
72 sr_warn("%s: samplerate was 0", __func__);
76 if (!is_valid_samplerate(samplerate)) {
77 sr_warn("%s: can't get divcount, samplerate invalid", __func__);
81 return (SR_MHZ(100) / samplerate) - 1;
84 static int init(struct sr_output *o)
87 struct sr_probe *probe;
92 sr_warn("%s: o was NULL", __func__);
97 sr_warn("%s: o->sdi was NULL", __func__);
101 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
102 sr_warn("%s: ctx malloc failed", __func__);
103 return SR_ERR_MALLOC;
108 /* Get the unitsize. */
109 for (l = o->sdi->probes; l; l = l->next) {
113 ctx->num_enabled_probes++;
115 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
117 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
119 ctx->samplerate = g_variant_get_uint64(gvar);
120 g_variant_unref(gvar);
127 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
128 uint64_t *length_out)
134 sr_warn("%s: o was NULL", __func__);
138 if (!(ctx = o->internal)) {
139 sr_warn("%s: o->internal was NULL", __func__);
144 sr_warn("%s: data_out was NULL", __func__);
148 switch (event_type) {
150 sr_dbg("%s: SR_DF_TRIGGER event", __func__);
151 /* Save the trigger point for later (SR_DF_END). */
152 ctx->trigger_point = 0; /* TODO: Store _actual_ value. */
155 sr_dbg("%s: SR_DF_END event", __func__);
156 if (!(outbuf = g_try_malloc(4 + 1))) {
157 sr_warn("la8 out: %s: outbuf malloc failed", __func__);
158 return SR_ERR_MALLOC;
161 /* One byte for the 'divcount' value. */
162 outbuf[0] = samplerate_to_divcount(ctx->samplerate);
163 // if (outbuf[0] == 0xff) {
164 // sr_warn("%s: invalid divcount", __func__);
168 /* Four bytes (little endian) for the trigger point. */
169 outbuf[1] = (ctx->trigger_point >> 0) & 0xff;
170 outbuf[2] = (ctx->trigger_point >> 8) & 0xff;
171 outbuf[3] = (ctx->trigger_point >> 16) & 0xff;
172 outbuf[4] = (ctx->trigger_point >> 24) & 0xff;
180 sr_warn("%s: unsupported event type: %d", __func__,
190 static int data(struct sr_output *o, const uint8_t *data_in,
191 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
197 sr_warn("%s: o was NULL", __func__);
201 if (!(ctx = o->internal)) {
202 sr_warn("%s: o->internal was NULL", __func__);
207 sr_warn("%s: data_in was NULL", __func__);
211 if (!(outbuf = g_try_malloc0(length_in))) {
212 sr_warn("%s: outbuf malloc failed", __func__);
213 return SR_ERR_MALLOC;
216 memcpy(outbuf, data_in, length_in);
219 *length_out = length_in;
224 SR_PRIV struct sr_output_format output_chronovu_la8 = {
225 .id = "chronovu-la8",
226 .description = "ChronoVu LA8",
227 .df_type = SR_DF_LOGIC,