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