]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <glib.h> | |
25e7d9b1 | 24 | #include <sigrok.h> |
a1bb33af | 25 | |
02076d69 UH |
26 | #define DEFAULT_BPL_BITS 64 |
27 | #define DEFAULT_BPL_HEX 256 | |
a1bb33af UH |
28 | |
29 | struct context { | |
afc8e4de | 30 | unsigned int num_enabled_probes; |
a1bb33af | 31 | int samples_per_line; |
afc8e4de | 32 | unsigned int unitsize; |
a1bb33af UH |
33 | int line_offset; |
34 | int linebuf_len; | |
35 | char *probelist[65]; | |
36 | char *linebuf; | |
37 | int spl_cnt; | |
38 | uint8_t *linevalues; | |
39 | char *header; | |
5045c217 | 40 | int mark_trigger; |
a1bb33af UH |
41 | }; |
42 | ||
afc8e4de | 43 | static void flush_linebufs(struct context *ctx, char *outbuf) |
a1bb33af UH |
44 | { |
45 | static int max_probename_len = 0; | |
46 | int len, i; | |
47 | ||
99c1fc59 | 48 | if (ctx->linebuf[0] == 0) |
a1bb33af UH |
49 | return; |
50 | ||
99c1fc59 UH |
51 | if (max_probename_len == 0) { |
52 | /* First time through... */ | |
53 | for (i = 0; ctx->probelist[i]; i++) { | |
a1bb33af | 54 | len = strlen(ctx->probelist[i]); |
99c1fc59 | 55 | if (len > max_probename_len) |
a1bb33af UH |
56 | max_probename_len = len; |
57 | } | |
58 | } | |
59 | ||
99c1fc59 UH |
60 | for (i = 0; ctx->probelist[i]; i++) { |
61 | sprintf(outbuf + strlen(outbuf), "%*s:%s\n", max_probename_len, | |
62 | ctx->probelist[i], ctx->linebuf + i * ctx->linebuf_len); | |
a1bb33af | 63 | } |
5045c217 HE |
64 | |
65 | /* Mark trigger with ^ */ | |
66 | if (ctx->mark_trigger != -1) | |
67 | sprintf(outbuf + strlen(outbuf), "T:%*s^\n", | |
68 | ctx->mark_trigger + (ctx->mark_trigger / 8), ""); | |
69 | ||
a1bb33af | 70 | memset(ctx->linebuf, 0, i * ctx->linebuf_len); |
a1bb33af UH |
71 | } |
72 | ||
5a8fda15 | 73 | static int init(struct output *o, int default_spl) |
a1bb33af UH |
74 | { |
75 | struct context *ctx; | |
76 | struct probe *probe; | |
77 | GSList *l; | |
78 | uint64_t samplerate; | |
79 | int num_probes; | |
25e7d9b1 | 80 | char *samplerate_s; |
a1bb33af UH |
81 | |
82 | ctx = malloc(sizeof(struct context)); | |
83 | o->internal = ctx; | |
84 | ctx->num_enabled_probes = 0; | |
99c1fc59 UH |
85 | |
86 | for (l = o->device->probes; l; l = l->next) { | |
a1bb33af | 87 | probe = l->data; |
99c1fc59 | 88 | if (probe->enabled) |
a1bb33af UH |
89 | ctx->probelist[ctx->num_enabled_probes++] = probe->name; |
90 | } | |
99c1fc59 | 91 | |
a1bb33af UH |
92 | ctx->probelist[ctx->num_enabled_probes] = 0; |
93 | ctx->unitsize = (ctx->num_enabled_probes + 7) / 8; | |
94 | ctx->line_offset = 0; | |
95 | ctx->spl_cnt = 0; | |
5045c217 | 96 | ctx->mark_trigger = -1; |
99c1fc59 UH |
97 | |
98 | if (o->param && o->param[0]) | |
a1bb33af UH |
99 | ctx->samples_per_line = strtoul(o->param, NULL, 10); |
100 | else | |
101 | ctx->samples_per_line = default_spl; | |
102 | ||
103 | ctx->header = malloc(512); | |
104 | num_probes = g_slist_length(o->device->probes); | |
99c1fc59 UH |
105 | samplerate = *((uint64_t *) o->device->plugin->get_device_info( |
106 | o->device->plugin_index, DI_CUR_SAMPLERATE)); | |
107 | snprintf(ctx->header, 512, "Acquisition with %d/%d probes at ", | |
108 | ctx->num_enabled_probes, num_probes); | |
25e7d9b1 UH |
109 | |
110 | if ((samplerate_s = sigrok_samplerate_string(samplerate)) == NULL) | |
99c1fc59 | 111 | return -1; /* FIXME */ |
25e7d9b1 UH |
112 | snprintf(ctx->header + strlen(ctx->header), 512, "%s\n", samplerate_s); |
113 | free(samplerate_s); | |
a1bb33af UH |
114 | |
115 | ctx->linebuf_len = ctx->samples_per_line * 2; | |
116 | ctx->linebuf = calloc(1, num_probes * ctx->linebuf_len); | |
117 | ctx->linevalues = calloc(1, num_probes); | |
118 | ||
5a8fda15 | 119 | return 0; |
a1bb33af UH |
120 | } |
121 | ||
99c1fc59 UH |
122 | static int event(struct output *o, int event_type, char **data_out, |
123 | uint64_t *length_out) | |
a1bb33af UH |
124 | { |
125 | struct context *ctx; | |
126 | int outsize; | |
127 | char *outbuf; | |
128 | ||
129 | ctx = o->internal; | |
99c1fc59 | 130 | switch (event_type) { |
a1bb33af | 131 | case DF_TRIGGER: |
5045c217 | 132 | ctx->mark_trigger = ctx->spl_cnt; |
a1bb33af UH |
133 | break; |
134 | case DF_END: | |
99c1fc59 UH |
135 | outsize = ctx->num_enabled_probes |
136 | * (ctx->samples_per_line + 20) + 512; | |
a1bb33af | 137 | outbuf = calloc(1, outsize); |
afc8e4de | 138 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
139 | *data_out = outbuf; |
140 | *length_out = strlen(outbuf); | |
141 | free(o->internal); | |
142 | o->internal = NULL; | |
143 | break; | |
144 | } | |
145 | ||
146 | return SIGROK_OK; | |
147 | } | |
148 | ||
02076d69 | 149 | static int init_bits(struct output *o) |
a1bb33af | 150 | { |
02076d69 | 151 | return init(o, DEFAULT_BPL_BITS); |
a1bb33af UH |
152 | } |
153 | ||
99c1fc59 UH |
154 | static int data_bits(struct output *o, char *data_in, uint64_t length_in, |
155 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
156 | { |
157 | struct context *ctx; | |
afc8e4de | 158 | unsigned int outsize, offset, p; |
a1bb33af UH |
159 | uint64_t sample; |
160 | char *outbuf; | |
161 | ||
162 | ctx = o->internal; | |
99c1fc59 UH |
163 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * |
164 | ctx->samples_per_line + 512; | |
165 | outbuf = calloc(1, outsize + 1); | |
166 | if (ctx->header) { | |
167 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
168 | strncpy(outbuf, ctx->header, outsize); |
169 | free(ctx->header); | |
170 | ctx->header = NULL; | |
99c1fc59 | 171 | } else |
a1bb33af UH |
172 | outbuf[0] = 0; |
173 | ||
99c1fc59 UH |
174 | if (length_in >= ctx->unitsize) { |
175 | for (offset = 0; offset <= length_in - ctx->unitsize; | |
176 | offset += ctx->unitsize) { | |
a1bb33af | 177 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 UH |
178 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
179 | if (sample & ((uint64_t) 1 << p)) | |
180 | ctx->linebuf[p * ctx->linebuf_len + | |
181 | ctx->line_offset] = '1'; | |
a1bb33af | 182 | else |
99c1fc59 UH |
183 | ctx->linebuf[p * ctx->linebuf_len + |
184 | ctx->line_offset] = '0'; | |
a1bb33af UH |
185 | } |
186 | ctx->line_offset++; | |
187 | ctx->spl_cnt++; | |
188 | ||
99c1fc59 UH |
189 | /* Add a space every 8th bit. */ |
190 | if ((ctx->spl_cnt & 7) == 0) { | |
191 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
192 | ctx->linebuf[p * ctx->linebuf_len + | |
193 | ctx->line_offset] = ' '; | |
a1bb33af UH |
194 | ctx->line_offset++; |
195 | } | |
196 | ||
99c1fc59 UH |
197 | /* End of line. */ |
198 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 199 | flush_linebufs(ctx, outbuf); |
a1bb33af | 200 | ctx->line_offset = ctx->spl_cnt = 0; |
5045c217 | 201 | ctx->mark_trigger = -1; |
a1bb33af UH |
202 | } |
203 | } | |
204 | } else | |
99c1fc59 | 205 | g_message("short buffer (length_in=%" PRIu64 ")", length_in); |
a1bb33af UH |
206 | |
207 | *data_out = outbuf; | |
208 | *length_out = strlen(outbuf); | |
209 | ||
210 | return SIGROK_OK; | |
211 | } | |
212 | ||
5a8fda15 | 213 | static int init_hex(struct output *o) |
a1bb33af | 214 | { |
02076d69 | 215 | return init(o, DEFAULT_BPL_BITS); |
a1bb33af UH |
216 | } |
217 | ||
99c1fc59 UH |
218 | static int data_hex(struct output *o, char *data_in, uint64_t length_in, |
219 | char **data_out, uint64_t *length_out) | |
a1bb33af UH |
220 | { |
221 | struct context *ctx; | |
afc8e4de | 222 | unsigned int outsize, offset, p; |
a1bb33af UH |
223 | uint64_t sample; |
224 | char *outbuf; | |
225 | ||
226 | ctx = o->internal; | |
99c1fc59 UH |
227 | outsize = length_in / ctx->unitsize * ctx->num_enabled_probes * |
228 | ctx->samples_per_line + 512; | |
229 | outbuf = calloc(1, outsize + 1); | |
230 | if (ctx->header) { | |
231 | /* The header is still here, this must be the first packet. */ | |
a1bb33af UH |
232 | strncpy(outbuf, ctx->header, outsize); |
233 | free(ctx->header); | |
234 | ctx->header = NULL; | |
99c1fc59 | 235 | } else |
a1bb33af UH |
236 | outbuf[0] = 0; |
237 | ||
238 | ctx->line_offset = 0; | |
99c1fc59 UH |
239 | for (offset = 0; offset <= length_in - ctx->unitsize; |
240 | offset += ctx->unitsize) { | |
a1bb33af | 241 | memcpy(&sample, data_in + offset, ctx->unitsize); |
99c1fc59 | 242 | for (p = 0; p < ctx->num_enabled_probes; p++) { |
a1bb33af | 243 | ctx->linevalues[p] <<= 1; |
99c1fc59 UH |
244 | if (sample & ((uint64_t) 1 << p)) |
245 | ctx->linevalues[p] |= 1; | |
246 | sprintf(ctx->linebuf + (p * ctx->linebuf_len) + | |
247 | ctx->line_offset, "%.2x", ctx->linevalues[p]); | |
a1bb33af UH |
248 | } |
249 | ctx->spl_cnt++; | |
250 | ||
99c1fc59 UH |
251 | /* Add a space after every complete hex byte. */ |
252 | if ((ctx->spl_cnt & 7) == 0) { | |
253 | for (p = 0; p < ctx->num_enabled_probes; p++) | |
254 | ctx->linebuf[p * ctx->linebuf_len + | |
255 | ctx->line_offset + 2] = ' '; | |
a1bb33af UH |
256 | ctx->line_offset += 3; |
257 | } | |
258 | ||
99c1fc59 UH |
259 | /* End of line. */ |
260 | if (ctx->spl_cnt >= ctx->samples_per_line) { | |
afc8e4de | 261 | flush_linebufs(ctx, outbuf); |
a1bb33af UH |
262 | ctx->line_offset = ctx->spl_cnt = 0; |
263 | } | |
264 | } | |
265 | ||
266 | *data_out = outbuf; | |
267 | *length_out = strlen(outbuf); | |
268 | ||
269 | return SIGROK_OK; | |
270 | } | |
271 | ||
02076d69 | 272 | struct output_format output_text_bits = { |
1c5b9d30 UH |
273 | "bits", |
274 | "Text (bits)", | |
02076d69 UH |
275 | init_bits, |
276 | data_bits, | |
99c1fc59 | 277 | event, |
a1bb33af UH |
278 | }; |
279 | ||
a1bb33af UH |
280 | struct output_format output_text_hex = { |
281 | "hex", | |
282 | "Text (hexadecimal)", | |
283 | init_hex, | |
284 | data_hex, | |
99c1fc59 | 285 | event, |
a1bb33af | 286 | }; |