]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 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 "protocol.h" | |
22 | ||
23 | /* | |
24 | * Logic level thresholds. | |
25 | * | |
26 | * For each of the two channel groups (1-4 and 5-9), the logic level | |
27 | * threshold can be set independently. | |
28 | * | |
29 | * The threshold can be set to values that are usable for systems with | |
30 | * different voltage levels, e.g. for 1.8V or 3.3V systems. | |
31 | * | |
32 | * The actual threshold value is always the middle of the values below. | |
33 | * E.g. for a system voltage level of 1.8V, the threshold is at 0.9V. That | |
34 | * means that values <= 0.9V are considered to be a logic 0/low, and | |
35 | * values > 0.9V are considered to be a logic 1/high. | |
36 | * | |
37 | * - 1.2V system: threshold = 0.6V | |
38 | * - 1.5V system: threshold = 0.75V | |
39 | * - 1.8V system: threshold = 0.9V | |
40 | * - 2.8V system: threshold = 1.4V | |
41 | * - 3.3V system: threshold = 1.65V | |
42 | */ | |
43 | #define THRESHOLD_1_2V_SYSTEM 0x2e | |
44 | #define THRESHOLD_1_5V_SYSTEM 0x39 | |
45 | #define THRESHOLD_1_8V_SYSTEM 0x45 | |
46 | #define THRESHOLD_2_8V_SYSTEM 0x6c | |
47 | #define THRESHOLD_3_3V_SYSTEM 0x7f | |
48 | ||
49 | static int scanaplus_write(struct dev_context *devc, uint8_t *buf, int size) | |
50 | { | |
51 | int i, bytes_written; | |
52 | GString *s; | |
53 | ||
54 | /* Note: Caller checks devc, devc->ftdic, buf, size. */ | |
55 | ||
56 | s = g_string_sized_new(100); | |
57 | g_string_printf(s, "Writing %d bytes: ", size); | |
58 | for (i = 0; i < size; i++) | |
59 | g_string_append_printf(s, "0x%02x ", buf[i]); | |
60 | sr_spew("%s", s->str); | |
61 | g_string_free(s, TRUE); | |
62 | ||
63 | bytes_written = ftdi_write_data(devc->ftdic, buf, size); | |
64 | if (bytes_written < 0) { | |
65 | sr_err("Failed to write FTDI data (%d): %s.", | |
66 | bytes_written, ftdi_get_error_string(devc->ftdic)); | |
67 | } else if (bytes_written != size) { | |
68 | sr_err("FTDI write error, only %d/%d bytes written: %s.", | |
69 | bytes_written, size, ftdi_get_error_string(devc->ftdic)); | |
70 | } | |
71 | ||
72 | return bytes_written; | |
73 | } | |
74 | ||
75 | SR_PRIV int scanaplus_close(struct dev_context *devc) | |
76 | { | |
77 | int ret; | |
78 | ||
79 | /* Note: Caller checks devc and devc->ftdic. */ | |
80 | ||
81 | if ((ret = ftdi_usb_close(devc->ftdic)) < 0) { | |
82 | sr_err("Failed to close FTDI device (%d): %s.", | |
83 | ret, ftdi_get_error_string(devc->ftdic)); | |
84 | return SR_ERR; | |
85 | } | |
86 | ||
87 | return SR_OK; | |
88 | } | |
89 | ||
90 | static void scanaplus_uncompress_block(struct dev_context *devc, | |
91 | uint64_t num_bytes) | |
92 | { | |
93 | uint64_t i, j; | |
94 | uint8_t num_samples, low, high; | |
95 | ||
96 | for (i = 0; i < num_bytes; i += 2) { | |
97 | num_samples = devc->compressed_buf[i + 0] >> 1; | |
98 | ||
99 | low = devc->compressed_buf[i + 0] & (1 << 0); | |
100 | high = devc->compressed_buf[i + 1]; | |
101 | ||
102 | for (j = 0; j < num_samples; j++) { | |
103 | devc->sample_buf[devc->bytes_received++] = high; | |
104 | devc->sample_buf[devc->bytes_received++] = low; | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
109 | static void send_samples(struct dev_context *devc, uint64_t samples_to_send) | |
110 | { | |
111 | struct sr_datafeed_packet packet; | |
112 | struct sr_datafeed_logic logic; | |
113 | ||
114 | sr_spew("Sending %" PRIu64 " samples.", samples_to_send); | |
115 | ||
116 | packet.type = SR_DF_LOGIC; | |
117 | packet.payload = &logic; | |
118 | logic.length = samples_to_send * 2; | |
119 | logic.unitsize = 2; /* We need 2 bytes for 9 channels. */ | |
120 | logic.data = devc->sample_buf; | |
121 | sr_session_send(devc->cb_data, &packet); | |
122 | ||
123 | devc->samples_sent += samples_to_send; | |
124 | devc->bytes_received -= samples_to_send * 2; | |
125 | } | |
126 | ||
127 | SR_PRIV int scanaplus_get_device_id(struct dev_context *devc) | |
128 | { | |
129 | int ret; | |
130 | uint16_t val1, val2; | |
131 | ||
132 | /* FTDI EEPROM indices 16+17 contain the 3 device ID bytes. */ | |
133 | if ((ret = ftdi_read_eeprom_location(devc->ftdic, 16, &val1)) < 0) { | |
134 | sr_err("Failed to read EEPROM index 16 (%d): %s.", | |
135 | ret, ftdi_get_error_string(devc->ftdic)); | |
136 | return SR_ERR; | |
137 | } | |
138 | if ((ret = ftdi_read_eeprom_location(devc->ftdic, 17, &val2)) < 0) { | |
139 | sr_err("Failed to read EEPROM index 17 (%d): %s.", | |
140 | ret, ftdi_get_error_string(devc->ftdic)); | |
141 | return SR_ERR; | |
142 | } | |
143 | ||
144 | /* | |
145 | * Note: Bit 7 of the three bytes must not be used, apparently. | |
146 | * | |
147 | * Even though the three bits can be either 0 or 1 (we've seen both | |
148 | * in actual ScanaPLUS devices), the device ID as sent to the FPGA | |
149 | * has bit 7 of each byte zero'd out. | |
150 | * | |
151 | * It is unknown whether bit 7 of these bytes has any meaning, | |
152 | * whether it's used somewhere, or whether it can be simply ignored. | |
153 | */ | |
154 | devc->devid[0] = ((val1 >> 0) & 0xff) & ~(1 << 7); | |
155 | devc->devid[1] = ((val1 >> 8) & 0xff) & ~(1 << 7); | |
156 | devc->devid[2] = ((val2 >> 0) & 0xff) & ~(1 << 7); | |
157 | ||
158 | return SR_OK; | |
159 | } | |
160 | ||
161 | static int scanaplus_clear_device_id(struct dev_context *devc) | |
162 | { | |
163 | uint8_t buf[2]; | |
164 | ||
165 | buf[0] = 0x8c; | |
166 | buf[1] = 0x00; | |
167 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
168 | return SR_ERR; | |
169 | ||
170 | buf[0] = 0x8e; | |
171 | buf[1] = 0x00; | |
172 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
173 | return SR_ERR; | |
174 | ||
175 | buf[0] = 0x8f; | |
176 | buf[1] = 0x00; | |
177 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
178 | return SR_ERR; | |
179 | ||
180 | return SR_OK; | |
181 | } | |
182 | ||
183 | static int scanaplus_send_device_id(struct dev_context *devc) | |
184 | { | |
185 | uint8_t buf[2]; | |
186 | ||
187 | buf[0] = 0x8c; | |
188 | buf[1] = devc->devid[0]; | |
189 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
190 | return SR_ERR; | |
191 | ||
192 | buf[0] = 0x8e; | |
193 | buf[1] = devc->devid[1]; | |
194 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
195 | return SR_ERR; | |
196 | ||
197 | buf[0] = 0x8f; | |
198 | buf[1] = devc->devid[2]; | |
199 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
200 | return SR_ERR; | |
201 | ||
202 | return SR_OK; | |
203 | } | |
204 | ||
205 | SR_PRIV int scanaplus_init(struct dev_context *devc) | |
206 | { | |
207 | int i; | |
208 | uint8_t buf[8]; | |
209 | ||
210 | buf[0] = 0x88; | |
211 | buf[1] = 0x41; | |
212 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
213 | return SR_ERR; | |
214 | ||
215 | buf[0] = 0x89; | |
216 | buf[1] = 0x64; | |
217 | buf[2] = 0x8a; | |
218 | buf[3] = 0x64; | |
219 | if (scanaplus_write(devc, (uint8_t *)&buf, 4) < 0) | |
220 | return SR_ERR; | |
221 | ||
222 | buf[0] = 0x88; | |
223 | buf[1] = 0x41; | |
224 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
225 | return SR_ERR; | |
226 | ||
227 | buf[0] = 0x88; | |
228 | buf[1] = 0x40; | |
229 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
230 | return SR_ERR; | |
231 | ||
232 | buf[0] = 0x8d; | |
233 | buf[1] = 0x01; | |
234 | buf[2] = 0x8d; | |
235 | buf[3] = 0x05; | |
236 | buf[4] = 0x8d; | |
237 | buf[5] = 0x01; | |
238 | buf[6] = 0x8d; | |
239 | buf[7] = 0x02; | |
240 | if (scanaplus_write(devc, (uint8_t *)&buf, 8) < 0) | |
241 | return SR_ERR; | |
242 | ||
243 | for (i = 0; i < 57; i++) { | |
244 | buf[0] = 0x8d; | |
245 | buf[1] = 0x06; | |
246 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
247 | return SR_ERR; | |
248 | ||
249 | buf[0] = 0x8d; | |
250 | buf[1] = 0x02; | |
251 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
252 | return SR_ERR; | |
253 | } | |
254 | ||
255 | if (scanaplus_send_device_id(devc) < 0) | |
256 | return SR_ERR; | |
257 | ||
258 | buf[0] = 0x88; | |
259 | buf[1] = 0x40; | |
260 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
261 | return SR_ERR; | |
262 | ||
263 | return SR_OK; | |
264 | } | |
265 | ||
266 | SR_PRIV int scanaplus_start_acquisition(struct dev_context *devc) | |
267 | { | |
268 | uint8_t buf[4]; | |
269 | ||
270 | /* Threshold and differential channel settings not yet implemented. */ | |
271 | ||
272 | buf[0] = 0x89; | |
273 | buf[1] = 0x7f; /* Logic level threshold for channels 1-4. */ | |
274 | buf[2] = 0x8a; | |
275 | buf[3] = 0x7f; /* Logic level threshold for channels 5-9. */ | |
276 | if (scanaplus_write(devc, (uint8_t *)&buf, 4) < 0) | |
277 | return SR_ERR; | |
278 | ||
279 | buf[0] = 0x88; | |
280 | buf[1] = 0x40; /* Special config of channels 5/6 and 7/8. */ | |
281 | /* 0x40: normal, 0x50: ch56 diff, 0x48: ch78 diff, 0x58: ch5678 diff */ | |
282 | if (scanaplus_write(devc, (uint8_t *)&buf, 2) < 0) | |
283 | return SR_ERR; | |
284 | ||
285 | if (scanaplus_clear_device_id(devc) < 0) | |
286 | return SR_ERR; | |
287 | ||
288 | if (scanaplus_send_device_id(devc) < 0) | |
289 | return SR_ERR; | |
290 | ||
291 | return SR_OK; | |
292 | } | |
293 | ||
294 | SR_PRIV int scanaplus_receive_data(int fd, int revents, void *cb_data) | |
295 | { | |
296 | int bytes_read; | |
297 | struct sr_dev_inst *sdi; | |
298 | struct dev_context *devc; | |
299 | uint64_t max, n; | |
300 | ||
301 | (void)fd; | |
302 | (void)revents; | |
303 | ||
304 | if (!(sdi = cb_data)) | |
305 | return TRUE; | |
306 | ||
307 | if (!(devc = sdi->priv)) | |
308 | return TRUE; | |
309 | ||
310 | if (!devc->ftdic) | |
311 | return TRUE; | |
312 | ||
313 | /* Get a block of data. */ | |
314 | bytes_read = ftdi_read_data(devc->ftdic, devc->compressed_buf, | |
315 | COMPRESSED_BUF_SIZE); | |
316 | if (bytes_read < 0) { | |
317 | sr_err("Failed to read FTDI data (%d): %s.", | |
318 | bytes_read, ftdi_get_error_string(devc->ftdic)); | |
319 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
320 | return FALSE; | |
321 | } | |
322 | if (bytes_read == 0) { | |
323 | sr_spew("Received 0 bytes, nothing to do."); | |
324 | return TRUE; | |
325 | } | |
326 | ||
327 | /* | |
328 | * After a ScanaPLUS acquisition starts, a bunch of samples will be | |
329 | * returned as all-zero, no matter which signals are actually present | |
330 | * on the channels. This is probably due to the FPGA reconfiguring some | |
331 | * of its internal state/config during this time. | |
332 | * | |
333 | * As far as we know there is apparently no way for the PC-side to | |
334 | * know when this "reconfiguration" starts or ends. The FTDI chip | |
335 | * will return all-zero "dummy" samples during this time, which is | |
336 | * indistinguishable from actual all-zero samples. | |
337 | * | |
338 | * We currently simply ignore the first 64kB of data after an | |
339 | * acquisition starts. Empirical tests have shown that the | |
340 | * "reconfigure" time is a lot less than that usually. | |
341 | */ | |
342 | if (devc->compressed_bytes_ignored < COMPRESSED_BUF_SIZE) { | |
343 | /* Ignore the first 64kB of data of every acquisition. */ | |
344 | sr_spew("Ignoring first 64kB chunk of data."); | |
345 | devc->compressed_bytes_ignored += COMPRESSED_BUF_SIZE; | |
346 | return TRUE; | |
347 | } | |
348 | ||
349 | /* TODO: Handle bytes_read which is not a multiple of 2? */ | |
350 | scanaplus_uncompress_block(devc, bytes_read); | |
351 | ||
352 | n = devc->samples_sent + (devc->bytes_received / 2); | |
353 | max = (SR_MHZ(100) / 1000) * devc->limit_msec; | |
354 | ||
355 | if (devc->limit_samples && (n >= devc->limit_samples)) { | |
356 | send_samples(devc, devc->limit_samples - devc->samples_sent); | |
357 | sr_info("Requested number of samples reached."); | |
358 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
359 | return TRUE; | |
360 | } else if (devc->limit_msec && (n >= max)) { | |
361 | send_samples(devc, max - devc->samples_sent); | |
362 | sr_info("Requested time limit reached."); | |
363 | sdi->driver->dev_acquisition_stop(sdi, cb_data); | |
364 | return TRUE; | |
365 | } else { | |
366 | send_samples(devc, devc->bytes_received / 2); | |
367 | } | |
368 | ||
369 | return TRUE; | |
370 | } |