]>
Commit | Line | Data |
---|---|---|
83e9d586 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
83e9d586 UH |
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 <fcntl.h> | |
23 | #include <unistd.h> | |
36423d04 | 24 | #include <sys/stat.h> |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
83e9d586 | 27 | |
b95dd761 | 28 | #define LOG_PREFIX "input/chronovu-la8" |
8e7f1cfd | 29 | |
83e9d586 UH |
30 | #define NUM_PACKETS 2048 |
31 | #define PACKET_SIZE 4096 | |
32 | #define DEFAULT_NUM_PROBES 8 | |
33 | ||
34 | /** | |
35 | * Convert the LA8 'divcount' value to the respective samplerate (in Hz). | |
36 | * | |
37 | * LA8 hardware: sample period = (divcount + 1) * 10ns. | |
38 | * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate). | |
39 | * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate). | |
40 | * | |
41 | * @param divcount The divcount value as needed by the hardware. | |
44dae539 | 42 | * |
83e9d586 UH |
43 | * @return The samplerate in Hz, or 0xffffffffffffffff upon errors. |
44 | */ | |
45 | static uint64_t divcount_to_samplerate(uint8_t divcount) | |
46 | { | |
47 | if (divcount == 0xff) | |
48 | return 0xffffffffffffffffULL; | |
49 | ||
50 | return SR_MHZ(100) / (divcount + 1); | |
51 | } | |
52 | ||
53 | static int format_match(const char *filename) | |
54 | { | |
3217b032 UH |
55 | struct stat stat_buf; |
56 | int ret; | |
57 | ||
83e9d586 | 58 | if (!filename) { |
8e7f1cfd | 59 | sr_err("%s: filename was NULL", __func__); |
83e9d586 UH |
60 | // return SR_ERR; /* FIXME */ |
61 | return FALSE; | |
62 | } | |
63 | ||
64 | if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
8e7f1cfd | 65 | sr_err("%s: input file '%s' does not exist", |
133a37bf | 66 | __func__, filename); |
83e9d586 UH |
67 | // return SR_ERR; /* FIXME */ |
68 | return FALSE; | |
69 | } | |
70 | ||
71 | if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { | |
8e7f1cfd | 72 | sr_err("%s: input file '%s' not a regular file", |
133a37bf | 73 | __func__, filename); |
83e9d586 UH |
74 | // return SR_ERR; /* FIXME */ |
75 | return FALSE; | |
76 | } | |
77 | ||
3217b032 UH |
78 | /* Only accept files of length 8MB + 5 bytes. */ |
79 | ret = stat(filename, &stat_buf); | |
80 | if (ret != 0) { | |
8e7f1cfd | 81 | sr_err("%s: Error getting file size of '%s'", |
3217b032 UH |
82 | __func__, filename); |
83 | return FALSE; | |
84 | } | |
85 | if (stat_buf.st_size != (8 * 1024 * 1024 + 5)) { | |
8e7f1cfd | 86 | sr_dbg("%s: File size must be exactly 8388613 bytes (" |
3217b032 UH |
87 | "it actually is %d bytes in size), so this is not a " |
88 | "ChronoVu LA8 file.", __func__, stat_buf.st_size); | |
89 | return FALSE; | |
90 | } | |
83e9d586 UH |
91 | |
92 | /* TODO: Check for divcount != 0xff. */ | |
93 | ||
94 | return TRUE; | |
95 | } | |
96 | ||
543d45c5 | 97 | static int init(struct sr_input *in, const char *filename) |
83e9d586 | 98 | { |
5c3c1241 | 99 | struct sr_probe *probe; |
464d12c7 | 100 | int num_probes, i; |
c37d2b1b | 101 | char name[SR_MAX_PROBENAME_LEN + 1]; |
c506a6a6 TŠ |
102 | char *param; |
103 | ||
543d45c5 BV |
104 | (void)filename; |
105 | ||
c506a6a6 TŠ |
106 | num_probes = DEFAULT_NUM_PROBES; |
107 | ||
108 | if (in->param) { | |
109 | param = g_hash_table_lookup(in->param, "numprobes"); | |
110 | if (param) { | |
111 | num_probes = strtoul(param, NULL, 10); | |
112 | if (num_probes < 1) { | |
8e7f1cfd | 113 | sr_err("%s: strtoul failed", __func__); |
c506a6a6 TŠ |
114 | return SR_ERR; |
115 | } | |
83e9d586 | 116 | } |
83e9d586 UH |
117 | } |
118 | ||
119 | /* Create a virtual device. */ | |
5c3c1241 | 120 | in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL); |
464d12c7 KS |
121 | |
122 | for (i = 0; i < num_probes; i++) { | |
123 | snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i); | |
c37d2b1b | 124 | /* TODO: Check return value. */ |
5c3c1241 BV |
125 | if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name))) |
126 | return SR_ERR; | |
127 | in->sdi->probes = g_slist_append(in->sdi->probes, probe); | |
464d12c7 | 128 | } |
83e9d586 UH |
129 | |
130 | return SR_OK; | |
131 | } | |
132 | ||
133 | static int loadfile(struct sr_input *in, const char *filename) | |
134 | { | |
83e9d586 | 135 | struct sr_datafeed_packet packet; |
2df1e819 | 136 | struct sr_datafeed_meta meta; |
9c939c51 | 137 | struct sr_datafeed_logic logic; |
2df1e819 | 138 | struct sr_config *src; |
83e9d586 UH |
139 | uint8_t buf[PACKET_SIZE], divcount; |
140 | int i, fd, size, num_probes; | |
141 | uint64_t samplerate; | |
142 | ||
143 | /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */ | |
144 | if ((fd = open(filename, O_RDONLY)) == -1) { | |
8e7f1cfd | 145 | sr_err("%s: file open failed", __func__); |
83e9d586 UH |
146 | return SR_ERR; |
147 | } | |
148 | ||
5c3c1241 | 149 | num_probes = g_slist_length(in->sdi->probes); |
83e9d586 UH |
150 | |
151 | /* Seek to the end of the file, and read the divcount byte. */ | |
152 | divcount = 0x00; /* TODO: Don't hardcode! */ | |
153 | ||
154 | /* Convert the divcount value to a samplerate. */ | |
155 | samplerate = divcount_to_samplerate(divcount); | |
156 | if (samplerate == 0xffffffffffffffffULL) { | |
157 | close(fd); /* FIXME */ | |
158 | return SR_ERR; | |
159 | } | |
8e7f1cfd | 160 | sr_dbg("%s: samplerate is %" PRIu64, __func__, samplerate); |
83e9d586 UH |
161 | |
162 | /* Send header packet to the session bus. */ | |
29a27196 | 163 | std_session_send_df_header(in->sdi, LOG_PREFIX); |
f366e86c BV |
164 | |
165 | /* Send metadata about the SR_DF_LOGIC packets to come. */ | |
2df1e819 | 166 | packet.type = SR_DF_META; |
f366e86c | 167 | packet.payload = &meta; |
ec4063b8 | 168 | src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate)); |
2df1e819 | 169 | meta.config = g_slist_append(NULL, src); |
5c3c1241 | 170 | sr_session_send(in->sdi, &packet); |
ec4063b8 | 171 | sr_config_free(src); |
83e9d586 UH |
172 | |
173 | /* TODO: Handle trigger point. */ | |
174 | ||
175 | /* Send data packets to the session bus. */ | |
8e7f1cfd | 176 | sr_dbg("%s: sending SR_DF_LOGIC data packets", __func__); |
83e9d586 | 177 | packet.type = SR_DF_LOGIC; |
9c939c51 BV |
178 | packet.payload = &logic; |
179 | logic.unitsize = (num_probes + 7) / 8; | |
180 | logic.data = buf; | |
83e9d586 UH |
181 | |
182 | /* Send 8MB of total data to the session bus in small chunks. */ | |
183 | for (i = 0; i < NUM_PACKETS; i++) { | |
184 | /* TODO: Handle errors, handle incomplete reads. */ | |
185 | size = read(fd, buf, PACKET_SIZE); | |
2285cf9b | 186 | logic.length = size; |
5c3c1241 | 187 | sr_session_send(in->sdi, &packet); |
83e9d586 UH |
188 | } |
189 | close(fd); /* FIXME */ | |
190 | ||
191 | /* Send end packet to the session bus. */ | |
8e7f1cfd | 192 | sr_dbg("%s: sending SR_DF_END", __func__); |
83e9d586 | 193 | packet.type = SR_DF_END; |
83e9d586 | 194 | packet.payload = NULL; |
5c3c1241 | 195 | sr_session_send(in->sdi, &packet); |
83e9d586 UH |
196 | |
197 | return SR_OK; | |
198 | } | |
199 | ||
7c1d391c | 200 | SR_PRIV struct sr_input_format input_chronovu_la8 = { |
83e9d586 UH |
201 | .id = "chronovu-la8", |
202 | .description = "ChronoVu LA8", | |
203 | .format_match = format_match, | |
204 | .init = init, | |
205 | .loadfile = loadfile, | |
206 | }; |