]> sigrok.org Git - libsigrok.git/blame - output/chronovu_la8.c
GPL headers: Use correct project name.
[libsigrok.git] / 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>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
45c59c8b
BV
24#include "libsigrok.h"
25#include "libsigrok-internal.h"
8c48f179 26
a944a84b
UH
27/* Message logging helpers with driver-specific prefix string. */
28#define DRIVER_LOG_DOMAIN "output/chronovu-la8: "
29#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
30#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
31#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
32#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
33#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
34#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
35
8c48f179
UH
36struct context {
37 unsigned int num_enabled_probes;
38 unsigned int unitsize;
39 char *probelist[SR_MAX_NUM_PROBES + 1];
40 uint64_t trigger_point;
41 uint64_t samplerate;
42};
43
44/**
45 * Check if the given samplerate is supported by the LA8 hardware.
46 *
47 * @param samplerate The samplerate (in Hz) to check.
44dae539 48 *
8c48f179
UH
49 * @return 1 if the samplerate is supported/valid, 0 otherwise.
50 */
51static int is_valid_samplerate(uint64_t samplerate)
52{
53 unsigned int i;
54
55 for (i = 0; i < 255; i++) {
56 if (samplerate == (SR_MHZ(100) / (i + 1)))
57 return 1;
58 }
59
a944a84b 60 sr_warn("%s: invalid samplerate (%" PRIu64 "Hz)",
b08024a8 61 __func__, samplerate);
8c48f179
UH
62
63 return 0;
64}
65
66/**
67 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
68 *
69 * LA8 hardware: sample period = (divcount + 1) * 10ns.
70 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
71 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
72 *
73 * @param samplerate The samplerate in Hz.
44dae539 74 *
8c48f179
UH
75 * @return The divcount value as needed by the hardware, or 0xff upon errors.
76 */
77static uint8_t samplerate_to_divcount(uint64_t samplerate)
78{
79 if (samplerate == 0) {
a944a84b 80 sr_warn("%s: samplerate was 0", __func__);
8c48f179
UH
81 return 0xff;
82 }
83
84 if (!is_valid_samplerate(samplerate)) {
a944a84b 85 sr_warn("%s: can't get divcount, samplerate invalid", __func__);
8c48f179
UH
86 return 0xff;
87 }
88
89 return (SR_MHZ(100) / samplerate) - 1;
90}
91
92static int init(struct sr_output *o)
93{
94 struct context *ctx;
95 struct sr_probe *probe;
96 GSList *l;
ec4063b8 97 GVariant *gvar;
8c48f179
UH
98
99 if (!o) {
a944a84b 100 sr_warn("%s: o was NULL", __func__);
8c48f179
UH
101 return SR_ERR_ARG;
102 }
103
5c3c1241 104 if (!o->sdi) {
a944a84b 105 sr_warn("%s: o->sdi was NULL", __func__);
8c48f179
UH
106 return SR_ERR_ARG;
107 }
108
5c3c1241 109 if (!o->sdi->driver) {
a944a84b 110 sr_warn("%s: o->sdi->driver was NULL", __func__);
8c48f179
UH
111 return SR_ERR_ARG;
112 }
113
133a37bf 114 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 115 sr_warn("%s: ctx malloc failed", __func__);
8c48f179
UH
116 return SR_ERR_MALLOC;
117 }
118
119 o->internal = ctx;
120
2285cf9b 121 /* Get the probe names and the unitsize. */
5c3c1241 122 for (l = o->sdi->probes; l; l = l->next) {
8c48f179
UH
123 probe = l->data;
124 if (!probe->enabled)
125 continue;
126 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
127 }
128 ctx->probelist[ctx->num_enabled_probes] = 0;
129 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
130
4d15e5c9 131 if (sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
ec4063b8
BV
132 o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
133 ctx->samplerate = g_variant_get_uint64(gvar);
134 g_variant_unref(gvar);
5c3c1241
BV
135 } else
136 ctx->samplerate = 0;
8c48f179 137
5c3c1241 138 return SR_OK;
8c48f179
UH
139}
140
054e6709 141static int event(struct sr_output *o, int event_type, uint8_t **data_out,
8c48f179
UH
142 uint64_t *length_out)
143{
144 struct context *ctx;
054e6709 145 uint8_t *outbuf;
8c48f179
UH
146
147 if (!o) {
a944a84b 148 sr_warn("%s: o was NULL", __func__);
8c48f179
UH
149 return SR_ERR_ARG;
150 }
151
152 if (!(ctx = o->internal)) {
a944a84b 153 sr_warn("%s: o->internal was NULL", __func__);
8c48f179
UH
154 return SR_ERR_ARG;
155 }
156
157 if (!data_out) {
a944a84b 158 sr_warn("%s: data_out was NULL", __func__);
8c48f179
UH
159 return SR_ERR_ARG;
160 }
161
162 switch (event_type) {
163 case SR_DF_TRIGGER:
a944a84b 164 sr_dbg("%s: SR_DF_TRIGGER event", __func__);
8c48f179
UH
165 /* Save the trigger point for later (SR_DF_END). */
166 ctx->trigger_point = 0; /* TODO: Store _actual_ value. */
167 break;
168 case SR_DF_END:
a944a84b 169 sr_dbg("%s: SR_DF_END event", __func__);
133a37bf 170 if (!(outbuf = g_try_malloc(4 + 1))) {
b08024a8 171 sr_warn("la8 out: %s: outbuf malloc failed", __func__);
8c48f179
UH
172 return SR_ERR_MALLOC;
173 }
174
175 /* One byte for the 'divcount' value. */
176 outbuf[0] = samplerate_to_divcount(ctx->samplerate);
177 // if (outbuf[0] == 0xff) {
a944a84b 178 // sr_warn("%s: invalid divcount", __func__);
8c48f179
UH
179 // return SR_ERR;
180 // }
181
182 /* Four bytes (little endian) for the trigger point. */
183 outbuf[1] = (ctx->trigger_point >> 0) & 0xff;
184 outbuf[2] = (ctx->trigger_point >> 8) & 0xff;
185 outbuf[3] = (ctx->trigger_point >> 16) & 0xff;
186 outbuf[4] = (ctx->trigger_point >> 24) & 0xff;
187
188 *data_out = outbuf;
189 *length_out = 4 + 1;
133a37bf 190 g_free(o->internal);
8c48f179
UH
191 o->internal = NULL;
192 break;
193 default:
a944a84b 194 sr_warn("%s: unsupported event type: %d", __func__,
b08024a8 195 event_type);
8c48f179
UH
196 *data_out = NULL;
197 *length_out = 0;
198 break;
199 }
200
201 return SR_OK;
202}
203
054e6709
UH
204static int data(struct sr_output *o, const uint8_t *data_in,
205 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
8c48f179
UH
206{
207 struct context *ctx;
054e6709 208 uint8_t *outbuf;
8c48f179
UH
209
210 if (!o) {
a944a84b 211 sr_warn("%s: o was NULL", __func__);
8c48f179
UH
212 return SR_ERR_ARG;
213 }
214
215 if (!(ctx = o->internal)) {
a944a84b 216 sr_warn("%s: o->internal was NULL", __func__);
8c48f179
UH
217 return SR_ERR_ARG;
218 }
219
220 if (!data_in) {
a944a84b 221 sr_warn("%s: data_in was NULL", __func__);
8c48f179
UH
222 return SR_ERR_ARG;
223 }
224
133a37bf 225 if (!(outbuf = g_try_malloc0(length_in))) {
a944a84b 226 sr_warn("%s: outbuf malloc failed", __func__);
8c48f179
UH
227 return SR_ERR_MALLOC;
228 }
229
230 memcpy(outbuf, data_in, length_in);
231
232 *data_out = outbuf;
233 *length_out = length_in;
234
235 return SR_OK;
236}
237
7c1d391c 238SR_PRIV struct sr_output_format output_chronovu_la8 = {
8c48f179
UH
239 .id = "chronovu-la8",
240 .description = "ChronoVu LA8",
241 .df_type = SR_DF_LOGIC,
242 .init = init,
243 .data = data,
244 .event = event,
245};