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