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 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "input/chronovu-la8: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
37 #define NUM_PACKETS 2048
38 #define PACKET_SIZE 4096
39 #define DEFAULT_NUM_PROBES 8
42 * Convert the LA8 'divcount' value to the respective samplerate (in Hz).
44 * LA8 hardware: sample period = (divcount + 1) * 10ns.
45 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
46 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
48 * @param divcount The divcount value as needed by the hardware.
50 * @return The samplerate in Hz, or 0xffffffffffffffff upon errors.
52 static uint64_t divcount_to_samplerate(uint8_t divcount)
55 return 0xffffffffffffffffULL;
57 return SR_MHZ(100) / (divcount + 1);
60 static int format_match(const char *filename)
66 sr_err("%s: filename was NULL", __func__);
67 // return SR_ERR; /* FIXME */
71 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
72 sr_err("%s: input file '%s' does not exist",
74 // return SR_ERR; /* FIXME */
78 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
79 sr_err("%s: input file '%s' not a regular file",
81 // return SR_ERR; /* FIXME */
85 /* Only accept files of length 8MB + 5 bytes. */
86 ret = stat(filename, &stat_buf);
88 sr_err("%s: Error getting file size of '%s'",
92 if (stat_buf.st_size != (8 * 1024 * 1024 + 5)) {
93 sr_dbg("%s: File size must be exactly 8388613 bytes ("
94 "it actually is %d bytes in size), so this is not a "
95 "ChronoVu LA8 file.", __func__, stat_buf.st_size);
99 /* TODO: Check for divcount != 0xff. */
104 static int init(struct sr_input *in)
106 struct sr_probe *probe;
108 char name[SR_MAX_PROBENAME_LEN + 1];
111 num_probes = DEFAULT_NUM_PROBES;
114 param = g_hash_table_lookup(in->param, "numprobes");
116 num_probes = strtoul(param, NULL, 10);
117 if (num_probes < 1) {
118 sr_err("%s: strtoul failed", __func__);
124 /* Create a virtual device. */
125 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
127 for (i = 0; i < num_probes; i++) {
128 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
129 /* TODO: Check return value. */
130 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
132 in->sdi->probes = g_slist_append(in->sdi->probes, probe);
138 static int loadfile(struct sr_input *in, const char *filename)
140 struct sr_datafeed_header header;
141 struct sr_datafeed_packet packet;
142 struct sr_datafeed_meta_logic meta;
143 struct sr_datafeed_logic logic;
144 uint8_t buf[PACKET_SIZE], divcount;
145 int i, fd, size, num_probes;
148 /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */
149 if ((fd = open(filename, O_RDONLY)) == -1) {
150 sr_err("%s: file open failed", __func__);
154 num_probes = g_slist_length(in->sdi->probes);
156 /* Seek to the end of the file, and read the divcount byte. */
157 divcount = 0x00; /* TODO: Don't hardcode! */
159 /* Convert the divcount value to a samplerate. */
160 samplerate = divcount_to_samplerate(divcount);
161 if (samplerate == 0xffffffffffffffffULL) {
162 close(fd); /* FIXME */
165 sr_dbg("%s: samplerate is %" PRIu64, __func__, samplerate);
167 /* Send header packet to the session bus. */
168 sr_dbg("%s: sending SR_DF_HEADER packet", __func__);
169 packet.type = SR_DF_HEADER;
170 packet.payload = &header;
171 header.feed_version = 1;
172 gettimeofday(&header.starttime, NULL);
173 sr_session_send(in->sdi, &packet);
175 /* Send metadata about the SR_DF_LOGIC packets to come. */
176 packet.type = SR_DF_META_LOGIC;
177 packet.payload = &meta;
178 meta.samplerate = samplerate;
179 meta.num_probes = num_probes;
180 sr_session_send(in->sdi, &packet);
182 /* TODO: Handle trigger point. */
184 /* Send data packets to the session bus. */
185 sr_dbg("%s: sending SR_DF_LOGIC data packets", __func__);
186 packet.type = SR_DF_LOGIC;
187 packet.payload = &logic;
188 logic.unitsize = (num_probes + 7) / 8;
191 /* Send 8MB of total data to the session bus in small chunks. */
192 for (i = 0; i < NUM_PACKETS; i++) {
193 /* TODO: Handle errors, handle incomplete reads. */
194 size = read(fd, buf, PACKET_SIZE);
196 sr_session_send(in->sdi, &packet);
198 close(fd); /* FIXME */
200 /* Send end packet to the session bus. */
201 sr_dbg("%s: sending SR_DF_END", __func__);
202 packet.type = SR_DF_END;
203 packet.payload = NULL;
204 sr_session_send(in->sdi, &packet);
209 SR_PRIV struct sr_input_format input_chronovu_la8 = {
210 .id = "chronovu-la8",
211 .description = "ChronoVu LA8",
212 .format_match = format_match,
214 .loadfile = loadfile,