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