]> sigrok.org Git - libsigrok.git/blame - output/chronovu_la8.c
Replace 'probe' with 'channel' in most places.
[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
3544f848 27#define LOG_PREFIX "output/chronovu-la8"
a944a84b 28
8c48f179 29struct context {
ba7dd8bb 30 unsigned int num_enabled_channels;
8c48f179 31 unsigned int unitsize;
8c48f179
UH
32 uint64_t trigger_point;
33 uint64_t samplerate;
34};
35
36/**
37 * Check if the given samplerate is supported by the LA8 hardware.
38 *
39 * @param samplerate The samplerate (in Hz) to check.
44dae539 40 *
8c48f179
UH
41 * @return 1 if the samplerate is supported/valid, 0 otherwise.
42 */
43static int is_valid_samplerate(uint64_t samplerate)
44{
45 unsigned int i;
46
47 for (i = 0; i < 255; i++) {
48 if (samplerate == (SR_MHZ(100) / (i + 1)))
49 return 1;
50 }
51
a944a84b 52 sr_warn("%s: invalid samplerate (%" PRIu64 "Hz)",
b08024a8 53 __func__, samplerate);
8c48f179
UH
54
55 return 0;
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{
71 if (samplerate == 0) {
a944a84b 72 sr_warn("%s: samplerate was 0", __func__);
8c48f179
UH
73 return 0xff;
74 }
75
76 if (!is_valid_samplerate(samplerate)) {
a944a84b 77 sr_warn("%s: can't get divcount, samplerate invalid", __func__);
8c48f179
UH
78 return 0xff;
79 }
80
81 return (SR_MHZ(100) / samplerate) - 1;
82}
83
84static int init(struct sr_output *o)
85{
86 struct context *ctx;
ba7dd8bb 87 struct sr_channel *ch;
8c48f179 88 GSList *l;
ec4063b8 89 GVariant *gvar;
8c48f179
UH
90
91 if (!o) {
a944a84b 92 sr_warn("%s: o was NULL", __func__);
8c48f179
UH
93 return SR_ERR_ARG;
94 }
95
5c3c1241 96 if (!o->sdi) {
a944a84b 97 sr_warn("%s: o->sdi was NULL", __func__);
8c48f179
UH
98 return SR_ERR_ARG;
99 }
100
133a37bf 101 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 102 sr_warn("%s: ctx malloc failed", __func__);
8c48f179
UH
103 return SR_ERR_MALLOC;
104 }
105
106 o->internal = ctx;
107
91a44f50 108 /* Get the unitsize. */
ba7dd8bb
UH
109 for (l = o->sdi->channels; l; l = l->next) {
110 ch = l->data;
111 if (ch->type != SR_PROBE_LOGIC)
3699a8a1 112 continue;
ba7dd8bb 113 if (!ch->enabled)
8c48f179 114 continue;
ba7dd8bb 115 ctx->num_enabled_channels++;
8c48f179 116 }
ba7dd8bb 117 ctx->unitsize = (ctx->num_enabled_channels + 7) / 8;
8c48f179 118
d3c74a6f
BV
119 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
120 &gvar) == SR_OK) {
ec4063b8
BV
121 ctx->samplerate = g_variant_get_uint64(gvar);
122 g_variant_unref(gvar);
5c3c1241
BV
123 } else
124 ctx->samplerate = 0;
8c48f179 125
5c3c1241 126 return SR_OK;
8c48f179
UH
127}
128
054e6709 129static int event(struct sr_output *o, int event_type, uint8_t **data_out,
8c48f179
UH
130 uint64_t *length_out)
131{
132 struct context *ctx;
054e6709 133 uint8_t *outbuf;
8c48f179
UH
134
135 if (!o) {
a944a84b 136 sr_warn("%s: o was NULL", __func__);
8c48f179
UH
137 return SR_ERR_ARG;
138 }
139
140 if (!(ctx = o->internal)) {
a944a84b 141 sr_warn("%s: o->internal was NULL", __func__);
8c48f179
UH
142 return SR_ERR_ARG;
143 }
144
145 if (!data_out) {
a944a84b 146 sr_warn("%s: data_out was NULL", __func__);
8c48f179
UH
147 return SR_ERR_ARG;
148 }
149
150 switch (event_type) {
151 case SR_DF_TRIGGER:
a944a84b 152 sr_dbg("%s: SR_DF_TRIGGER event", __func__);
8c48f179
UH
153 /* Save the trigger point for later (SR_DF_END). */
154 ctx->trigger_point = 0; /* TODO: Store _actual_ value. */
155 break;
156 case SR_DF_END:
a944a84b 157 sr_dbg("%s: SR_DF_END event", __func__);
133a37bf 158 if (!(outbuf = g_try_malloc(4 + 1))) {
b08024a8 159 sr_warn("la8 out: %s: outbuf malloc failed", __func__);
8c48f179
UH
160 return SR_ERR_MALLOC;
161 }
162
163 /* One byte for the 'divcount' value. */
164 outbuf[0] = samplerate_to_divcount(ctx->samplerate);
165 // if (outbuf[0] == 0xff) {
a944a84b 166 // sr_warn("%s: invalid divcount", __func__);
8c48f179
UH
167 // return SR_ERR;
168 // }
169
170 /* Four bytes (little endian) for the trigger point. */
171 outbuf[1] = (ctx->trigger_point >> 0) & 0xff;
172 outbuf[2] = (ctx->trigger_point >> 8) & 0xff;
173 outbuf[3] = (ctx->trigger_point >> 16) & 0xff;
174 outbuf[4] = (ctx->trigger_point >> 24) & 0xff;
175
176 *data_out = outbuf;
177 *length_out = 4 + 1;
133a37bf 178 g_free(o->internal);
8c48f179
UH
179 o->internal = NULL;
180 break;
181 default:
a944a84b 182 sr_warn("%s: unsupported event type: %d", __func__,
b08024a8 183 event_type);
8c48f179
UH
184 *data_out = NULL;
185 *length_out = 0;
186 break;
187 }
188
189 return SR_OK;
190}
191
054e6709
UH
192static int data(struct sr_output *o, const uint8_t *data_in,
193 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
8c48f179
UH
194{
195 struct context *ctx;
054e6709 196 uint8_t *outbuf;
8c48f179
UH
197
198 if (!o) {
a944a84b 199 sr_warn("%s: o was NULL", __func__);
8c48f179
UH
200 return SR_ERR_ARG;
201 }
202
203 if (!(ctx = o->internal)) {
a944a84b 204 sr_warn("%s: o->internal was NULL", __func__);
8c48f179
UH
205 return SR_ERR_ARG;
206 }
207
208 if (!data_in) {
a944a84b 209 sr_warn("%s: data_in was NULL", __func__);
8c48f179
UH
210 return SR_ERR_ARG;
211 }
212
133a37bf 213 if (!(outbuf = g_try_malloc0(length_in))) {
a944a84b 214 sr_warn("%s: outbuf malloc failed", __func__);
8c48f179
UH
215 return SR_ERR_MALLOC;
216 }
217
218 memcpy(outbuf, data_in, length_in);
219
220 *data_out = outbuf;
221 *length_out = length_in;
222
223 return SR_OK;
224}
225
7c1d391c 226SR_PRIV struct sr_output_format output_chronovu_la8 = {
8c48f179
UH
227 .id = "chronovu-la8",
228 .description = "ChronoVu LA8",
229 .df_type = SR_DF_LOGIC,
230 .init = init,
231 .data = data,
232 .event = event,
233};