2 * This file is part of the sigrok 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
27 unsigned int num_enabled_probes;
28 unsigned int unitsize;
29 char *probelist[SR_MAX_NUM_PROBES + 1];
30 uint64_t trigger_point;
35 * Check if the given samplerate is supported by the LA8 hardware.
37 * @param samplerate The samplerate (in Hz) to check.
38 * @return 1 if the samplerate is supported/valid, 0 otherwise.
40 static int is_valid_samplerate(uint64_t samplerate)
44 for (i = 0; i < 255; i++) {
45 if (samplerate == (SR_MHZ(100) / (i + 1)))
49 g_warning("la8 out: %s: invalid samplerate (%" PRIu64 "Hz)",
50 __func__, samplerate);
56 * Convert a samplerate (in Hz) to the 'divcount' value the LA8 wants.
58 * LA8 hardware: sample period = (divcount + 1) * 10ns.
59 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
60 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
62 * @param samplerate The samplerate in Hz.
63 * @return The divcount value as needed by the hardware, or 0xff upon errors.
65 static uint8_t samplerate_to_divcount(uint64_t samplerate)
67 if (samplerate == 0) {
68 g_warning("la8 out: %s: samplerate was 0", __func__);
72 if (!is_valid_samplerate(samplerate)) {
73 g_warning("la8 out: %s: can't get divcount, samplerate invalid",
78 return (SR_MHZ(100) / samplerate) - 1;
81 static int init(struct sr_output *o)
84 struct sr_probe *probe;
90 g_warning("la8 out: %s: o was NULL", __func__);
95 g_warning("la8 out: %s: o->device was NULL", __func__);
99 if (!o->device->plugin) {
100 g_warning("la8 out: %s: o->device->plugin was NULL", __func__);
104 if (!(ctx = calloc(1, sizeof(struct context)))) {
105 g_warning("la8 out: %s: ctx calloc failed", __func__);
106 return SR_ERR_MALLOC;
111 /* Get the number of probes, their names, and the unitsize. */
112 /* TODO: Error handling. */
113 for (l = o->device->probes; l; l = l->next) {
117 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
119 ctx->probelist[ctx->num_enabled_probes] = 0;
120 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
122 num_probes = g_slist_length(o->device->probes);
124 if (sr_device_has_hwcap(o->device, SR_HWCAP_SAMPLERATE)) {
125 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
126 o->device->plugin_index, SR_DI_CUR_SAMPLERATE));
127 /* TODO: Error checks. */
129 samplerate = 0; /* TODO: Error or set some value? */
131 ctx->samplerate = samplerate;
133 return 0; /* TODO: SR_OK? */
136 static int event(struct sr_output *o, int event_type, char **data_out,
137 uint64_t *length_out)
143 g_warning("la8 out: %s: o was NULL", __func__);
147 if (!(ctx = o->internal)) {
148 g_warning("la8 out: %s: o->internal was NULL", __func__);
153 g_warning("la8 out: %s: data_out was NULL", __func__);
157 switch (event_type) {
159 g_debug("la8 out: %s: SR_DF_TRIGGER event", __func__);
160 /* Save the trigger point for later (SR_DF_END). */
161 ctx->trigger_point = 0; /* TODO: Store _actual_ value. */
164 g_debug("la8 out: %s: SR_DF_END event", __func__);
165 if (!(outbuf = malloc(4 + 1))) {
166 g_warning("la8 out: %s: outbuf malloc failed",
168 return SR_ERR_MALLOC;
171 /* One byte for the 'divcount' value. */
172 outbuf[0] = samplerate_to_divcount(ctx->samplerate);
173 // if (outbuf[0] == 0xff) {
174 // g_warning("la8 out: %s: invalid divcount", __func__);
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;
190 g_warning("la8 out: %s: unsupported event type: %d", __func__,
200 static int data(struct sr_output *o, const char *data_in, uint64_t length_in,
201 char **data_out, uint64_t *length_out)
207 g_warning("la8 out: %s: o was NULL", __func__);
211 if (!(ctx = o->internal)) {
212 g_warning("la8 out: %s: o->internal was NULL", __func__);
217 g_warning("la8 out: %s: data_in was NULL", __func__);
221 if (!(outbuf = calloc(1, length_in))) {
222 g_warning("la8 out: %s: outbuf calloc failed", __func__);
223 return SR_ERR_MALLOC;
226 memcpy(outbuf, data_in, length_in);
229 *length_out = length_in;
234 struct sr_output_format output_chronovu_la8 = {
235 .id = "chronovu-la8",
236 .description = "ChronoVu LA8",
237 .df_type = SR_DF_LOGIC,