2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Soeren Apel <soeren@apelpie.net>
5 * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * This input module reads .ad files created using the
24 * following practice commands:
26 * I.SAVE <file> /NoCompress
27 * IPROBE.SAVE <file> /NoCompress
29 * It currently cannot make use of files that have been
30 * saved using /QuickCompress, /Compress or /ZIP.
31 * As a workaround you may load the file in PowerView
32 * using I.LOAD / IPROBE.LOAD and re-save using /NoCompress.
38 #include <sys/types.h>
43 #include <libsigrok/libsigrok.h>
44 #include "libsigrok-internal.h"
46 #define LOG_PREFIX "input/trace32_ad"
48 #define CHUNK_SIZE (4 * 1024 * 1024)
49 #define MAX_POD_COUNT 12
50 #define HEADER_SIZE 80
54 #define TRACE32 "trace32"
56 #define TIMESTAMP_RESOLUTION ((double)0.000000000078125) /* 0.078125 ns */
59 * The resolution equals a sampling freq of 12.8 GHz. That's a bit high
60 * for inter-record sample generation, so we scale it down to 200 MHz
61 * for now. That way, the scaling factor becomes 32.
63 #define DEFAULT_SAMPLERATE 200
66 AD_FORMAT_BINHDR = 1, /* Binary header, binary data, textual setup info */
67 AD_FORMAT_TXTHDR /* Textual header, binary data */
71 AD_DEVICE_PI = 1, /* Data recorded by LA-7940 PowerIntegrator or */
72 /* LA-394x PowerIntegrator II. */
73 AD_DEVICE_IPROBE /* Data recorded by LA-769x PowerTrace II IProbe. */
74 /* Missing file format info for LA-793x ICD PowerProbe */
75 /* Missing file format info for LA-4530 uTrace analog probe */
84 AD_COMPR_NONE = 0, /* File created with /NOCOMPRESS */
85 AD_COMPR_QCOMP = 6, /* File created with /COMPRESS or /QUICKCOMPRESS */
90 gboolean header_read, records_read, trigger_sent;
91 char format, device, record_mode, compression;
92 char pod_status[MAX_POD_COUNT];
93 struct sr_channel *channels[MAX_POD_COUNT][17]; /* 16 + CLK */
94 uint64_t trigger_timestamp;
95 uint32_t record_size, record_count, cur_record;
98 double timestamp_scale;
102 static int process_header(GString *buf, struct context *inc);
103 static void create_channels(struct sr_input *in);
105 /* Transform non-printable chars to '\xNN' presentation. */
106 static char *printable_name(const char *name)
114 s = g_malloc0(l * strlen("\\x00") + 1);
115 for (p = s, i = 0; i < l; i++) {
116 if (g_ascii_isprint(name[i])) {
119 snprintf(p, 5, "\\x%05x", name[i]);
120 p += strlen("\\x00");
128 static char get_pod_name_from_id(int id)
144 sr_err("get_pod_name_from_id() called with invalid ID %d!", id);
149 static int init(struct sr_input *in, GHashTable *options)
155 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
156 in->priv = g_malloc0(sizeof(struct context));
160 /* Calculate the desired timestamp scaling factor. */
161 inc->samplerate = 1000000 *
162 g_variant_get_uint32(g_hash_table_lookup(options, "samplerate"));
164 inc->timestamp_scale = ((1 / TIMESTAMP_RESOLUTION) / (double)inc->samplerate);
166 /* Enable the pods the user chose to see. */
167 for (pod = 0; pod < MAX_POD_COUNT; pod++) {
168 g_snprintf(id, sizeof(id), "pod%c", get_pod_name_from_id(pod));
169 if (g_variant_get_boolean(g_hash_table_lookup(options, id)))
170 inc->pod_status[pod] = 1;
174 if (g_slist_length(in->sdi->channels) == 0) {
175 sr_err("No pods were selected and thus no channels created, aborting.");
181 inc->out_buf = g_string_sized_new(CHUNK_SIZE);
186 static int format_match(GHashTable *metadata, unsigned int *confidence)
191 buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
192 rc = process_header(buf, NULL);
201 static int process_header(GString *buf, struct context *inc)
203 char *format_name, *format_name_sig;
206 int record_size, device_id;
209 * 00-31 (0x00-1F) file format name
210 * 32-39 (0x20-27) trigger timestamp u64
211 * 40-47 (0x28-2F) unused
212 * 48 (0x30) compression
214 * 50 (0x32) 0x00 (PI), 0x01 (iprobe)
215 * 54 (0x36) 0x08 (PI 250/500), 0x0A (iprobe 250)
216 * 55 (0x37) 0x00 (250), 0x01 (500)
217 * 56 (0x38) record size u8
218 * 57-59 (0x39-3B) const 0x00
219 * 60-63 (0x3C-3F) number of records u32
220 * 64-67 (0x40-43) id of last record s32
222 * 71 (0x47) const 0x80=128
223 * 72 (0x48) const 0x01
228 * Note: The routine is called from different contexts. Either
229 * to auto-detect the file format (format_match(), 'inc' is NULL),
230 * or to process the data during acquisition (receive(), 'inc'
231 * is a valid pointer). This header parse routine shall gracefully
232 * deal with unexpected or incorrect input data.
236 * Get up to the first 32 bytes of the file content. File format
237 * names end on SPACE or CTRL-Z (or NUL). Trim trailing SPACE
238 * before further processing.
240 format_name = g_strndup(buf->str, 32);
241 p = strchr(format_name, CTRLZ);
244 g_strchomp(format_name);
247 * File format names either start with the "trace32" literal,
248 * or with a digit and SPACE.
250 format_name_sig = g_strndup(format_name, strlen(TRACE32));
251 has_trace32 = g_strcmp0(format_name_sig, TRACE32) == 0;
252 g_free(format_name_sig);
255 /* Literal "trace32" leader, binary header follows. */
257 inc->format = AD_FORMAT_BINHDR;
258 } else if (g_ascii_isdigit(format_name[0]) && (format_name[1] == SPACE)) {
259 /* Digit and SPACE leader, currently unsupported text header. */
261 inc->format = AD_FORMAT_TXTHDR;
264 sr_err("This format isn't implemented yet, aborting.");
267 /* Unknown kind of format name. Unsupported. */
270 sr_err("Don't know this file format, aborting.");
274 p = printable_name(format_name);
275 sr_dbg("File says it's \"%s\"", p);
278 record_size = R8(buf->str + 56);
281 if (g_strcmp0(format_name, "trace32 power integrator data") == 0) {
282 if (record_size == 28 || record_size == 45)
283 device_id = AD_DEVICE_PI;
284 } else if (g_strcmp0(format_name, "trace32 iprobe data") == 0) {
285 if (record_size == 11)
286 device_id = AD_DEVICE_IPROBE;
291 sr_err("Don't know how to handle this file with record size %d.",
298 /* Stop processing the header if we just want to identify the file. */
302 inc->device = device_id;
303 inc->trigger_timestamp = RL64(buf->str + 32);
304 inc->compression = R8(buf->str + 48); /* Maps to the enum. */
305 inc->record_mode = R8(buf->str + 55); /* Maps to the enum. */
306 inc->record_size = record_size;
307 inc->record_count = RL32(buf->str + 60);
308 inc->last_record = RL32S(buf->str + 64);
310 sr_dbg("Trigger occured at %lf s.",
311 inc->trigger_timestamp * TIMESTAMP_RESOLUTION);
312 sr_dbg("File contains %d records: first one is %d, last one is %d.",
313 inc->record_count, (inc->last_record - inc->record_count + 1),
316 /* Check if we can work with this compression. */
317 if (inc->compression != AD_COMPR_NONE) {
318 sr_err("File uses unsupported compression (0x%02X), can't continue.",
323 inc->header_read = TRUE;
328 static void create_channels(struct sr_input *in)
331 int pod, channel, chan_id;
337 for (pod = 0; pod < MAX_POD_COUNT; pod++) {
338 if (!inc->pod_status[pod])
341 for (channel = 0; channel < 16; channel++) {
342 snprintf(name, sizeof(name), "%c%d", get_pod_name_from_id(pod), channel);
343 inc->channels[pod][channel] =
344 sr_channel_new(in->sdi, chan_id, SR_CHANNEL_LOGIC, TRUE, name);
348 snprintf(name, sizeof(name), "CLK%c", get_pod_name_from_id(pod));
349 inc->channels[pod][16] =
350 sr_channel_new(in->sdi, chan_id, SR_CHANNEL_LOGIC, TRUE, name);
355 static void send_metadata(struct sr_input *in)
357 struct sr_datafeed_packet packet;
358 struct sr_datafeed_meta meta;
359 struct sr_config *src;
364 packet.type = SR_DF_META;
365 packet.payload = &meta;
366 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
367 meta.config = g_slist_append(NULL, src);
368 sr_session_send(in->sdi, &packet);
369 g_slist_free(meta.config);
372 inc->meta_sent = TRUE;
375 static void flush_output_buffer(struct sr_input *in)
378 struct sr_datafeed_packet packet;
379 struct sr_datafeed_logic logic;
383 if (inc->out_buf->len) {
384 packet.type = SR_DF_LOGIC;
385 packet.payload = &logic;
386 logic.unitsize = (g_slist_length(in->sdi->channels) + 7) / 8;
387 logic.data = inc->out_buf->str;
388 logic.length = inc->out_buf->len;
389 sr_session_send(in->sdi, &packet);
391 g_string_truncate(inc->out_buf, 0);
395 static void process_record_pi(struct sr_input *in, gsize start)
397 struct sr_datafeed_packet packet;
399 uint64_t timestamp, next_timestamp;
401 char single_payload[12 * 3];
403 int i, pod_count, clk_offset, packet_count, pod;
404 int payload_bit, payload_len, value;
418 * 24-25 J15..0 Not present in 500MHz mode
419 * 26-27 K15..0 Not present in 500MHz mode
420 * 28-29 L15..0 Not present in 500MHz mode
421 * 30-31 M15..0 Not present in 500MHz mode
422 * 32-33 N15..0 Not present in 500MHz mode
423 * 34-35 O15..0 Not present in 500MHz mode
424 * 36-39 ?? Not present in 500MHz mode
425 * 40/24 CLKF..A (32=CLKF, .., 1=CLKA)
426 * 41 CLKO..J (32=CLKO, .., 1=CLKJ) Not present in 500MHz mode
432 timestamp = RL64(buf->str + start);
434 if (inc->record_mode == AD_MODE_500MHZ) {
444 single_payload[0] = 0;
446 for (pod = 0; pod < pod_count; pod++) {
447 if (!inc->pod_status[pod])
452 pod_data = RL16(buf->str + start + 8);
453 pod_data |= (RL16(buf->str + start + clk_offset) & 1) << 16;
456 pod_data = RL16(buf->str + start + 10);
457 pod_data |= (RL16(buf->str + start + clk_offset) & 2) << 15;
460 pod_data = RL16(buf->str + start + 12);
461 pod_data |= (RL16(buf->str + start + clk_offset) & 4) << 14;
464 pod_data = RL16(buf->str + start + 14);
465 pod_data |= (RL16(buf->str + start + clk_offset) & 8) << 13;
468 pod_data = RL16(buf->str + start + 16);
469 pod_data |= (RL16(buf->str + start + clk_offset) & 16) << 12;
472 pod_data = RL16(buf->str + start + 18);
473 pod_data |= (RL16(buf->str + start + clk_offset) & 32) << 11;
476 pod_data = RL16(buf->str + start + 24);
477 pod_data |= (RL16(buf->str + start + 41) & 1) << 16;
480 pod_data = RL16(buf->str + start + 26);
481 pod_data |= (RL16(buf->str + start + 41) & 2) << 15;
484 pod_data = RL16(buf->str + start + 28);
485 pod_data |= (RL16(buf->str + start + 41) & 4) << 14;
488 pod_data = RL16(buf->str + start + 30);
489 pod_data |= (RL16(buf->str + start + 41) & 8) << 13;
492 pod_data = RL16(buf->str + start + 32);
493 pod_data |= (RL16(buf->str + start + 41) & 16) << 12;
496 pod_data = RL16(buf->str + start + 34);
497 pod_data |= (RL16(buf->str + start + 41) & 32) << 11;
500 sr_err("Don't know how to obtain data for pod %d.", pod);
503 for (i = 0; i < 17; i++) {
504 value = (pod_data >> i) & 1;
505 single_payload[payload_len] |= value << payload_bit;
508 if (payload_bit > 7) {
511 single_payload[payload_len] = 0;
516 /* Make sure that payload_len accounts for any incomplete bytes used. */
520 i = (g_slist_length(in->sdi->channels) + 7) / 8;
521 if (payload_len != i) {
522 sr_err("Payload unit size is %d but should be %d!", payload_len, i);
526 if (timestamp == inc->trigger_timestamp && !inc->trigger_sent) {
527 sr_dbg("Trigger @%lf s, record #%d.",
528 timestamp * TIMESTAMP_RESOLUTION, inc->cur_record);
530 packet.type = SR_DF_TRIGGER;
531 packet.payload = NULL;
532 sr_session_send(in->sdi, &packet);
533 inc->trigger_sent = TRUE;
536 /* Is this the last record in the file? */
537 if (inc->cur_record == inc->record_count - 1) {
538 /* It is, so send the last sample data only once. */
539 g_string_append_len(inc->out_buf, single_payload, payload_len);
541 /* It's not, so fill the time gap by sending lots of data. */
542 next_timestamp = RL64(buf->str + start + inc->record_size);
543 packet_count = (int)(next_timestamp - timestamp) / inc->timestamp_scale;
545 /* Make sure we send at least one data set. */
546 if (packet_count == 0)
549 for (i = 0; i < packet_count; i++)
550 g_string_append_len(inc->out_buf, single_payload, payload_len);
553 if (inc->out_buf->len >= CHUNK_SIZE)
554 flush_output_buffer(in);
557 static void process_record_iprobe(struct sr_input *in, gsize start)
559 struct sr_datafeed_packet packet;
561 uint64_t timestamp, next_timestamp;
562 char single_payload[3];
563 int i, payload_len, packet_count;
573 timestamp = RL64(in->buf->str + start);
574 single_payload[0] = R8(in->buf->str + start + 8);
575 single_payload[1] = R8(in->buf->str + start + 9);
576 single_payload[2] = R8(in->buf->str + start + 10) & 1;
579 if (timestamp == inc->trigger_timestamp && !inc->trigger_sent) {
580 sr_dbg("Trigger @%lf s, record #%d.",
581 timestamp * TIMESTAMP_RESOLUTION, inc->cur_record);
583 packet.type = SR_DF_TRIGGER;
584 packet.payload = NULL;
585 sr_session_send(in->sdi, &packet);
586 inc->trigger_sent = TRUE;
589 /* Is this the last record in the file? */
590 if (inc->cur_record == inc->record_count - 1) {
591 /* It is, so send the last sample data only once. */
592 g_string_append_len(inc->out_buf, single_payload, payload_len);
594 /* It's not, so fill the time gap by sending lots of data. */
595 next_timestamp = RL64(in->buf->str + start + inc->record_size);
596 packet_count = (int)(next_timestamp - timestamp) / inc->timestamp_scale;
598 /* Make sure we send at least one data set. */
599 if (packet_count == 0)
602 for (i = 0; i < packet_count; i++)
603 g_string_append_len(inc->out_buf, single_payload, payload_len);
606 if (inc->out_buf->len >= CHUNK_SIZE)
607 flush_output_buffer(in);
610 static void process_practice_token(struct sr_input *in, char *cmd_token)
614 char chan_suffix[2], chan_name[33];
617 struct sr_channel *channel;
622 * Commands of interest (I may also be IPROBE):
627 * I.TYSNC.SELECT I.A0 HIGH
628 * NAME.SET <port.chan> <name> <+/-> ...
634 if (cmd_token[0] == 0)
637 tokens = g_strsplit(cmd_token, " ", 0);
642 if (g_strcmp0(tokens[0], "NAME.SET") == 0) {
643 /* Let the user know when the channel has been inverted. */
644 /* This *should* be token #3 but there's an additonal space, making it #4. */
648 if (tokens[4][0] == '-')
649 chan_suffix[0] = '-'; /* This is the way PowerView shows it. */
653 * Command is using structure "NAME.SET I.A00 I.XYZ" or
654 * "NAME.SET IP.00 IP.XYZ", depending on the device used.
655 * Let's get strings with the I./IP. from both tokens removed.
657 s1 = g_strstr_len(tokens[1], -1, ".") + 1;
658 s2 = g_strstr_len(tokens[2], -1, ".") + 1;
660 if (g_strcmp0(s1, "CLK") == 0) {
664 } else if ((strlen(s1) == 4) && g_ascii_isupper(s1[3])) {
665 /* CLKA/B/J/K for PowerIntegrator */
666 pod = s1[3] - (char)'A';
668 } else if (g_ascii_isupper(s1[0])) {
669 /* A00 for PowerIntegrator */
670 pod = s1[0] - (char)'A';
678 channel = inc->channels[pod][ch];
679 g_snprintf(chan_name, sizeof(chan_name), "%s%s", s2, chan_suffix);
681 sr_dbg("Changing channel name for %s to %s.", s1, chan_name);
682 sr_dev_channel_name_set(channel, chan_name);
688 static void process_practice(struct sr_input *in)
691 char **tokens, *token;
694 /* Gather all input data until we see the end marker. */
695 if (in->buf->str[in->buf->len - 1] != 0x29)
702 tokens = g_strsplit(in->buf->str, delimiter, 0);
704 /* Special case: first token contains the start marker, too. Skip it. */
706 for (i = 0; token[i]; i++) {
708 process_practice_token(in, token + i + 1);
711 for (i = 1; tokens[i]; i++)
712 process_practice_token(in, tokens[i]);
716 g_string_erase(in->buf, 0, in->buf->len);
719 static int process_buffer(struct sr_input *in)
722 int i, chunk_size, res;
726 if (!inc->header_read) {
727 res = process_header(in->buf, inc);
728 g_string_erase(in->buf, 0, HEADER_SIZE);
733 if (!inc->meta_sent) {
734 std_session_send_df_header(in->sdi);
738 if (!inc->records_read) {
739 /* Cut off at a multiple of the record size. */
740 chunk_size = ((in->buf->len) / inc->record_size) * inc->record_size;
742 /* There needs to be at least one more record process_record() can peek into. */
743 chunk_size -= inc->record_size;
745 for (i = 0; (i < chunk_size) && (!inc->records_read); i += inc->record_size) {
746 switch (inc->device) {
748 process_record_pi(in, i);
750 case AD_DEVICE_IPROBE:
751 process_record_iprobe(in, i);
754 sr_err("Trying to process records for unknown device!");
759 if (inc->cur_record == inc->record_count)
760 inc->records_read = TRUE;
763 g_string_erase(in->buf, 0, i);
766 if (inc->records_read) {
767 /* Read practice commands that configure the setup. */
768 process_practice(in);
774 static int receive(struct sr_input *in, GString *buf)
776 g_string_append_len(in->buf, buf->str, buf->len);
778 if (!in->sdi_ready) {
779 /* sdi is ready, notify frontend. */
780 in->sdi_ready = TRUE;
784 return process_buffer(in);
787 static int end(struct sr_input *in)
795 ret = process_buffer(in);
799 flush_output_buffer(in);
802 std_session_send_df_end(in->sdi);
807 static int reset(struct sr_input *in)
809 struct context *inc = in->priv;
811 inc->meta_sent = FALSE;
812 inc->header_read = FALSE;
813 inc->records_read = FALSE;
814 inc->trigger_sent = FALSE;
817 g_string_truncate(in->buf, 0);
822 static struct sr_option options[] = {
823 { "podA", "Import pod A / iprobe",
824 "Create channels and data for pod A / iprobe", NULL, NULL },
826 { "podB", "Import pod B", "Create channels and data for pod B", NULL, NULL },
827 { "podC", "Import pod C", "Create channels and data for pod C", NULL, NULL },
828 { "podD", "Import pod D", "Create channels and data for pod D", NULL, NULL },
829 { "podE", "Import pod E", "Create channels and data for pod E", NULL, NULL },
830 { "podF", "Import pod F", "Create channels and data for pod F", NULL, NULL },
831 { "podJ", "Import pod J", "Create channels and data for pod J", NULL, NULL },
832 { "podK", "Import pod K", "Create channels and data for pod K", NULL, NULL },
833 { "podL", "Import pod L", "Create channels and data for pod L", NULL, NULL },
834 { "podM", "Import pod M", "Create channels and data for pod M", NULL, NULL },
835 { "podN", "Import pod N", "Create channels and data for pod N", NULL, NULL },
836 { "podO", "Import pod O", "Create channels and data for pod O", NULL, NULL },
838 { "samplerate", "Reduced sample rate (MHz)", "Reduce the original sample rate of 12.8 GHz to the specified sample rate in MHz", NULL, NULL },
843 static const struct sr_option *get_options(void)
845 if (!options[0].def) {
846 options[0].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
847 options[1].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
848 options[2].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
849 options[3].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
850 options[4].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
851 options[5].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
852 options[6].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
853 options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
854 options[8].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
855 options[9].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
856 options[10].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
857 options[11].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
858 options[12].def = g_variant_ref_sink(g_variant_new_uint32(DEFAULT_SAMPLERATE));
864 SR_PRIV struct sr_input_module input_trace32_ad = {
866 .name = "Trace32_ad",
867 .desc = "Lauterbach Trace32 logic analyzer data",
868 .exts = (const char*[]){"ad", NULL},
869 .options = get_options,
870 .metadata = { SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
871 .format_match = format_match,