]>
Commit | Line | Data |
---|---|---|
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> | |
45c59c8b BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
8c48f179 UH |
26 | |
27 | struct 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 | */ | |
42 | static 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 | */ | |
68 | static 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 | ||
84 | static int init(struct sr_output *o) | |
85 | { | |
86 | struct context *ctx; | |
87 | struct sr_probe *probe; | |
88 | GSList *l; | |
5c3c1241 | 89 | uint64_t *samplerate; |
8c48f179 UH |
90 | |
91 | if (!o) { | |
b08024a8 | 92 | sr_warn("la8 out: %s: o was NULL", __func__); |
8c48f179 UH |
93 | return SR_ERR_ARG; |
94 | } | |
95 | ||
5c3c1241 BV |
96 | if (!o->sdi) { |
97 | sr_warn("la8 out: %s: o->sdi was NULL", __func__); | |
8c48f179 UH |
98 | return SR_ERR_ARG; |
99 | } | |
100 | ||
5c3c1241 BV |
101 | if (!o->sdi->driver) { |
102 | sr_warn("la8 out: %s: o->sdi->driver was NULL", __func__); | |
8c48f179 UH |
103 | return SR_ERR_ARG; |
104 | } | |
105 | ||
133a37bf UH |
106 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { |
107 | sr_warn("la8 out: %s: ctx malloc failed", __func__); | |
8c48f179 UH |
108 | return SR_ERR_MALLOC; |
109 | } | |
110 | ||
111 | o->internal = ctx; | |
112 | ||
2285cf9b | 113 | /* Get the probe names and the unitsize. */ |
5c3c1241 | 114 | for (l = o->sdi->probes; l; l = l->next) { |
8c48f179 UH |
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 | ||
5c3c1241 BV |
123 | if (sr_dev_has_hwcap(o->sdi, SR_HWCAP_SAMPLERATE)) { |
124 | o->sdi->driver->info_get(SR_DI_CUR_SAMPLERATE, | |
125 | (const void **)&samplerate, o->sdi); | |
126 | ctx->samplerate = *samplerate; | |
127 | } else | |
128 | ctx->samplerate = 0; | |
8c48f179 | 129 | |
5c3c1241 | 130 | return SR_OK; |
8c48f179 UH |
131 | } |
132 | ||
054e6709 | 133 | static int event(struct sr_output *o, int event_type, uint8_t **data_out, |
8c48f179 UH |
134 | uint64_t *length_out) |
135 | { | |
136 | struct context *ctx; | |
054e6709 | 137 | uint8_t *outbuf; |
8c48f179 UH |
138 | |
139 | if (!o) { | |
b08024a8 | 140 | sr_warn("la8 out: %s: o was NULL", __func__); |
8c48f179 UH |
141 | return SR_ERR_ARG; |
142 | } | |
143 | ||
144 | if (!(ctx = o->internal)) { | |
b08024a8 | 145 | sr_warn("la8 out: %s: o->internal was NULL", __func__); |
8c48f179 UH |
146 | return SR_ERR_ARG; |
147 | } | |
148 | ||
149 | if (!data_out) { | |
b08024a8 | 150 | sr_warn("la8 out: %s: data_out was NULL", __func__); |
8c48f179 UH |
151 | return SR_ERR_ARG; |
152 | } | |
153 | ||
154 | switch (event_type) { | |
155 | case SR_DF_TRIGGER: | |
b08024a8 | 156 | sr_dbg("la8 out: %s: SR_DF_TRIGGER event", __func__); |
8c48f179 UH |
157 | /* Save the trigger point for later (SR_DF_END). */ |
158 | ctx->trigger_point = 0; /* TODO: Store _actual_ value. */ | |
159 | break; | |
160 | case SR_DF_END: | |
b08024a8 | 161 | sr_dbg("la8 out: %s: SR_DF_END event", __func__); |
133a37bf | 162 | if (!(outbuf = g_try_malloc(4 + 1))) { |
b08024a8 | 163 | sr_warn("la8 out: %s: outbuf malloc failed", __func__); |
8c48f179 UH |
164 | return SR_ERR_MALLOC; |
165 | } | |
166 | ||
167 | /* One byte for the 'divcount' value. */ | |
168 | outbuf[0] = samplerate_to_divcount(ctx->samplerate); | |
169 | // if (outbuf[0] == 0xff) { | |
b08024a8 | 170 | // sr_warn("la8 out: %s: invalid divcount", __func__); |
8c48f179 UH |
171 | // return SR_ERR; |
172 | // } | |
173 | ||
174 | /* Four bytes (little endian) for the trigger point. */ | |
175 | outbuf[1] = (ctx->trigger_point >> 0) & 0xff; | |
176 | outbuf[2] = (ctx->trigger_point >> 8) & 0xff; | |
177 | outbuf[3] = (ctx->trigger_point >> 16) & 0xff; | |
178 | outbuf[4] = (ctx->trigger_point >> 24) & 0xff; | |
179 | ||
180 | *data_out = outbuf; | |
181 | *length_out = 4 + 1; | |
133a37bf | 182 | g_free(o->internal); |
8c48f179 UH |
183 | o->internal = NULL; |
184 | break; | |
185 | default: | |
b08024a8 UH |
186 | sr_warn("la8 out: %s: unsupported event type: %d", __func__, |
187 | event_type); | |
8c48f179 UH |
188 | *data_out = NULL; |
189 | *length_out = 0; | |
190 | break; | |
191 | } | |
192 | ||
193 | return SR_OK; | |
194 | } | |
195 | ||
054e6709 UH |
196 | static int data(struct sr_output *o, const uint8_t *data_in, |
197 | uint64_t length_in, uint8_t **data_out, uint64_t *length_out) | |
8c48f179 UH |
198 | { |
199 | struct context *ctx; | |
054e6709 | 200 | uint8_t *outbuf; |
8c48f179 UH |
201 | |
202 | if (!o) { | |
b08024a8 | 203 | sr_warn("la8 out: %s: o was NULL", __func__); |
8c48f179 UH |
204 | return SR_ERR_ARG; |
205 | } | |
206 | ||
207 | if (!(ctx = o->internal)) { | |
b08024a8 | 208 | sr_warn("la8 out: %s: o->internal was NULL", __func__); |
8c48f179 UH |
209 | return SR_ERR_ARG; |
210 | } | |
211 | ||
212 | if (!data_in) { | |
b08024a8 | 213 | sr_warn("la8 out: %s: data_in was NULL", __func__); |
8c48f179 UH |
214 | return SR_ERR_ARG; |
215 | } | |
216 | ||
133a37bf UH |
217 | if (!(outbuf = g_try_malloc0(length_in))) { |
218 | sr_warn("la8 out: %s: outbuf malloc failed", __func__); | |
8c48f179 UH |
219 | return SR_ERR_MALLOC; |
220 | } | |
221 | ||
222 | memcpy(outbuf, data_in, length_in); | |
223 | ||
224 | *data_out = outbuf; | |
225 | *length_out = length_in; | |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
7c1d391c | 230 | SR_PRIV struct sr_output_format output_chronovu_la8 = { |
8c48f179 UH |
231 | .id = "chronovu-la8", |
232 | .description = "ChronoVu LA8", | |
233 | .df_type = SR_DF_LOGIC, | |
234 | .init = init, | |
235 | .data = data, | |
236 | .event = event, | |
237 | }; |