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