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
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
28 #define NUM_PACKETS 2048
29 #define PACKET_SIZE 4096
30 #define DEFAULT_NUM_PROBES 8
33 * Convert the LA8 'divcount' value to the respective samplerate (in Hz).
35 * LA8 hardware: sample period = (divcount + 1) * 10ns.
36 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
37 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
39 * @param divcount The divcount value as needed by the hardware.
41 * @return The samplerate in Hz, or 0xffffffffffffffff upon errors.
43 static uint64_t divcount_to_samplerate(uint8_t divcount)
46 return 0xffffffffffffffffULL;
48 return SR_MHZ(100) / (divcount + 1);
51 static int format_match(const char *filename)
57 sr_err("la8 in: %s: filename was NULL", __func__);
58 // return SR_ERR; /* FIXME */
62 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
63 sr_err("la8 in: %s: input file '%s' does not exist",
65 // return SR_ERR; /* FIXME */
69 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
70 sr_err("la8 in: %s: input file '%s' not a regular file",
72 // return SR_ERR; /* FIXME */
76 /* Only accept files of length 8MB + 5 bytes. */
77 ret = stat(filename, &stat_buf);
79 sr_err("la8 in: %s: Error getting file size of '%s'",
83 if (stat_buf.st_size != (8 * 1024 * 1024 + 5)) {
84 sr_dbg("la8 in: %s: File size must be exactly 8388613 bytes ("
85 "it actually is %d bytes in size), so this is not a "
86 "ChronoVu LA8 file.", __func__, stat_buf.st_size);
90 /* TODO: Check for divcount != 0xff. */
95 static int init(struct sr_input *in)
98 char name[SR_MAX_PROBENAME_LEN + 1];
101 num_probes = DEFAULT_NUM_PROBES;
104 param = g_hash_table_lookup(in->param, "numprobes");
106 num_probes = strtoul(param, NULL, 10);
107 if (num_probes < 1) {
108 sr_err("la8 in: %s: strtoul failed", __func__);
114 /* Create a virtual device. */
115 in->vdev = sr_dev_new(NULL, 0);
117 for (i = 0; i < num_probes; i++) {
118 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
119 /* TODO: Check return value. */
120 sr_dev_probe_add(in->vdev, name);
126 static int loadfile(struct sr_input *in, const char *filename)
128 struct sr_datafeed_header header;
129 struct sr_datafeed_packet packet;
130 struct sr_datafeed_meta_logic meta;
131 struct sr_datafeed_logic logic;
132 uint8_t buf[PACKET_SIZE], divcount;
133 int i, fd, size, num_probes;
136 /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */
137 if ((fd = open(filename, O_RDONLY)) == -1) {
138 sr_err("la8 in: %s: file open failed", __func__);
142 num_probes = g_slist_length(in->vdev->probes);
144 /* Seek to the end of the file, and read the divcount byte. */
145 divcount = 0x00; /* TODO: Don't hardcode! */
147 /* Convert the divcount value to a samplerate. */
148 samplerate = divcount_to_samplerate(divcount);
149 if (samplerate == 0xffffffffffffffffULL) {
150 close(fd); /* FIXME */
153 sr_dbg("la8 in: %s: samplerate is %" PRIu64, __func__, samplerate);
155 /* Send header packet to the session bus. */
156 sr_dbg("la8 in: %s: sending SR_DF_HEADER packet", __func__);
157 packet.type = SR_DF_HEADER;
158 packet.payload = &header;
159 header.feed_version = 1;
160 gettimeofday(&header.starttime, NULL);
161 sr_session_send(in->vdev, &packet);
163 /* Send metadata about the SR_DF_LOGIC packets to come. */
164 packet.type = SR_DF_META_LOGIC;
165 packet.payload = &meta;
166 meta.samplerate = samplerate;
167 meta.num_probes = num_probes;
168 sr_session_send(in->vdev, &packet);
170 /* TODO: Handle trigger point. */
172 /* Send data packets to the session bus. */
173 sr_dbg("la8 in: %s: sending SR_DF_LOGIC data packets", __func__);
174 packet.type = SR_DF_LOGIC;
175 packet.payload = &logic;
176 logic.unitsize = (num_probes + 7) / 8;
179 /* Send 8MB of total data to the session bus in small chunks. */
180 for (i = 0; i < NUM_PACKETS; i++) {
181 /* TODO: Handle errors, handle incomplete reads. */
182 size = read(fd, buf, PACKET_SIZE);
184 sr_session_send(in->vdev, &packet);
186 close(fd); /* FIXME */
188 /* Send end packet to the session bus. */
189 sr_dbg("la8 in: %s: sending SR_DF_END", __func__);
190 packet.type = SR_DF_END;
191 packet.payload = NULL;
192 sr_session_send(in->vdev, &packet);
197 SR_PRIV struct sr_input_format input_chronovu_la8 = {
198 .id = "chronovu-la8",
199 .description = "ChronoVu LA8",
200 .format_match = format_match,
202 .loadfile = loadfile,