]>
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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | #define USB_VENDOR_ID 0x0403 | |
24 | #define USB_DEVICE_ID 0x6014 | |
25 | #define USB_IPRODUCT "SCANAPLUS" | |
26 | ||
27 | #define SAMPLE_BUF_SIZE (8 * 1024 * 1024) | |
28 | ||
29 | static const uint32_t drvopts[] = { | |
30 | SR_CONF_LOGIC_ANALYZER, | |
31 | }; | |
32 | ||
33 | static const uint32_t devopts[] = { | |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
35 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
36 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
37 | }; | |
38 | ||
39 | static const char *channel_names[] = { | |
40 | "1", "2", "3", "4", "5", "6", "7", "8", "9", | |
41 | }; | |
42 | ||
43 | /* Note: The IKALOGIC ScanaPLUS always samples at 100MHz. */ | |
44 | static const uint64_t samplerates[1] = { SR_MHZ(100) }; | |
45 | ||
46 | static void clear_helper(struct dev_context *devc) | |
47 | { | |
48 | ftdi_free(devc->ftdic); | |
49 | g_free(devc->compressed_buf); | |
50 | g_free(devc->sample_buf); | |
51 | } | |
52 | ||
53 | static int dev_clear(const struct sr_dev_driver *di) | |
54 | { | |
55 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); | |
56 | } | |
57 | ||
58 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
59 | { | |
60 | struct sr_dev_inst *sdi; | |
61 | struct dev_context *devc; | |
62 | unsigned int i; | |
63 | int ret; | |
64 | ||
65 | (void)options; | |
66 | ||
67 | devc = g_malloc0(sizeof(struct dev_context)); | |
68 | ||
69 | /* Allocate memory for the incoming compressed samples. */ | |
70 | if (!(devc->compressed_buf = g_try_malloc0(COMPRESSED_BUF_SIZE))) { | |
71 | sr_err("compressed_buf malloc failed."); | |
72 | goto err_free_devc; | |
73 | } | |
74 | ||
75 | /* Allocate memory for the uncompressed samples. */ | |
76 | if (!(devc->sample_buf = g_try_malloc0(SAMPLE_BUF_SIZE))) { | |
77 | sr_err("sample_buf malloc failed."); | |
78 | goto err_free_compressed_buf; | |
79 | } | |
80 | ||
81 | if (!(devc->ftdic = ftdi_new())) { | |
82 | sr_err("Failed to initialize libftdi."); | |
83 | goto err_free_sample_buf; | |
84 | } | |
85 | ||
86 | ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, | |
87 | USB_IPRODUCT, NULL); | |
88 | if (ret < 0) { | |
89 | /* Log errors, except for -3 ("device not found"). */ | |
90 | if (ret != -3) | |
91 | sr_err("Failed to open device (%d): %s", ret, | |
92 | ftdi_get_error_string(devc->ftdic)); | |
93 | goto err_free_ftdic; | |
94 | } | |
95 | ||
96 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
97 | sdi->status = SR_ST_INACTIVE; | |
98 | sdi->vendor = g_strdup("IKALOGIC"); | |
99 | sdi->model = g_strdup("ScanaPLUS"); | |
100 | sdi->priv = devc; | |
101 | ||
102 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) | |
103 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]); | |
104 | ||
105 | scanaplus_close(devc); | |
106 | ||
107 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
108 | ||
109 | scanaplus_close(devc); | |
110 | err_free_ftdic: | |
111 | ftdi_free(devc->ftdic); | |
112 | err_free_sample_buf: | |
113 | g_free(devc->sample_buf); | |
114 | err_free_compressed_buf: | |
115 | g_free(devc->compressed_buf); | |
116 | err_free_devc: | |
117 | g_free(devc); | |
118 | ||
119 | return NULL; | |
120 | } | |
121 | ||
122 | static int dev_open(struct sr_dev_inst *sdi) | |
123 | { | |
124 | struct dev_context *devc; | |
125 | int ret; | |
126 | ||
127 | devc = sdi->priv; | |
128 | ||
129 | ret = ftdi_set_interface(devc->ftdic, INTERFACE_A); | |
130 | if (ret < 0) { | |
131 | sr_err("Failed to set FTDI interface A (%d): %s", ret, | |
132 | ftdi_get_error_string(devc->ftdic)); | |
133 | return SR_ERR; | |
134 | } | |
135 | ||
136 | ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID, | |
137 | USB_IPRODUCT, NULL); | |
138 | if (ret < 0) { | |
139 | sr_err("Failed to open device (%d): %s", ret, | |
140 | ftdi_get_error_string(devc->ftdic)); | |
141 | return SR_ERR; | |
142 | } | |
143 | ||
144 | if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) { | |
145 | sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.", | |
146 | ret, ftdi_get_error_string(devc->ftdic)); | |
147 | goto err_dev_open_close_ftdic; | |
148 | } | |
149 | ||
150 | ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET); | |
151 | if (ret < 0) { | |
152 | sr_err("Failed to reset the FTDI chip bitmode (%d): %s.", | |
153 | ret, ftdi_get_error_string(devc->ftdic)); | |
154 | goto err_dev_open_close_ftdic; | |
155 | } | |
156 | ||
157 | ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF); | |
158 | if (ret < 0) { | |
159 | sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.", | |
160 | ret, ftdi_get_error_string(devc->ftdic)); | |
161 | goto err_dev_open_close_ftdic; | |
162 | } | |
163 | ||
164 | ret = ftdi_set_latency_timer(devc->ftdic, 2); | |
165 | if (ret < 0) { | |
166 | sr_err("Failed to set FTDI latency timer (%d): %s.", | |
167 | ret, ftdi_get_error_string(devc->ftdic)); | |
168 | goto err_dev_open_close_ftdic; | |
169 | } | |
170 | ||
171 | ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024); | |
172 | if (ret < 0) { | |
173 | sr_err("Failed to set FTDI read data chunk size (%d): %s.", | |
174 | ret, ftdi_get_error_string(devc->ftdic)); | |
175 | goto err_dev_open_close_ftdic; | |
176 | } | |
177 | ||
178 | /* Get the ScanaPLUS device ID from the FTDI EEPROM. */ | |
179 | if ((ret = scanaplus_get_device_id(devc)) < 0) { | |
180 | sr_err("Failed to get ScanaPLUS device ID: %d.", ret); | |
181 | goto err_dev_open_close_ftdic; | |
182 | } | |
183 | sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.", | |
184 | devc->devid[0], devc->devid[1], devc->devid[2]); | |
185 | ||
186 | return SR_OK; | |
187 | ||
188 | err_dev_open_close_ftdic: | |
189 | scanaplus_close(devc); | |
190 | ||
191 | return SR_ERR; | |
192 | } | |
193 | ||
194 | static int dev_close(struct sr_dev_inst *sdi) | |
195 | { | |
196 | struct dev_context *devc; | |
197 | ||
198 | devc = sdi->priv; | |
199 | ||
200 | return scanaplus_close(devc); | |
201 | } | |
202 | ||
203 | static int config_get(uint32_t key, GVariant **data, | |
204 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
205 | { | |
206 | (void)sdi; | |
207 | (void)cg; | |
208 | ||
209 | switch (key) { | |
210 | case SR_CONF_SAMPLERATE: | |
211 | /* The ScanaPLUS samplerate is 100MHz and can't be changed. */ | |
212 | *data = g_variant_new_uint64(SR_MHZ(100)); | |
213 | break; | |
214 | default: | |
215 | return SR_ERR_NA; | |
216 | } | |
217 | ||
218 | return SR_OK; | |
219 | } | |
220 | ||
221 | static int config_set(uint32_t key, GVariant *data, | |
222 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
223 | { | |
224 | struct dev_context *devc; | |
225 | ||
226 | (void)cg; | |
227 | ||
228 | devc = sdi->priv; | |
229 | ||
230 | switch (key) { | |
231 | case SR_CONF_SAMPLERATE: | |
232 | if (g_variant_get_uint64(data) != SR_MHZ(100)) { | |
233 | sr_err("ScanaPLUS only supports samplerate = 100MHz."); | |
234 | return SR_ERR_ARG; | |
235 | } | |
236 | /* Nothing to do, the ScanaPLUS samplerate is always 100MHz. */ | |
237 | break; | |
238 | case SR_CONF_LIMIT_MSEC: | |
239 | devc->limit_msec = g_variant_get_uint64(data); | |
240 | break; | |
241 | case SR_CONF_LIMIT_SAMPLES: | |
242 | devc->limit_samples = g_variant_get_uint64(data); | |
243 | break; | |
244 | default: | |
245 | return SR_ERR_NA; | |
246 | } | |
247 | ||
248 | return SR_OK; | |
249 | } | |
250 | ||
251 | static int config_list(uint32_t key, GVariant **data, | |
252 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
253 | { | |
254 | switch (key) { | |
255 | case SR_CONF_DEVICE_OPTIONS: | |
256 | return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts); | |
257 | case SR_CONF_SAMPLERATE: | |
258 | *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates)); | |
259 | break; | |
260 | default: | |
261 | return SR_ERR_NA; | |
262 | } | |
263 | ||
264 | return SR_OK; | |
265 | } | |
266 | ||
267 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
268 | { | |
269 | int ret; | |
270 | struct dev_context *devc; | |
271 | ||
272 | devc = sdi->priv; | |
273 | ||
274 | if (!devc->ftdic) | |
275 | return SR_ERR_BUG; | |
276 | ||
277 | /* TODO: Configure channels later (thresholds etc.). */ | |
278 | ||
279 | /* Properly reset internal variables before every new acquisition. */ | |
280 | devc->compressed_bytes_ignored = 0; | |
281 | devc->samples_sent = 0; | |
282 | devc->bytes_received = 0; | |
283 | ||
284 | if ((ret = scanaplus_init(devc)) < 0) | |
285 | return ret; | |
286 | ||
287 | if ((ret = scanaplus_start_acquisition(devc)) < 0) | |
288 | return ret; | |
289 | ||
290 | std_session_send_df_header(sdi); | |
291 | ||
292 | /* Hook up a dummy handler to receive data from the device. */ | |
293 | sr_session_source_add(sdi->session, -1, 0, 0, scanaplus_receive_data, (void *)sdi); | |
294 | ||
295 | return SR_OK; | |
296 | } | |
297 | ||
298 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
299 | { | |
300 | sr_session_source_remove(sdi->session, -1); | |
301 | std_session_send_df_end(sdi); | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
306 | static struct sr_dev_driver ikalogic_scanaplus_driver_info = { | |
307 | .name = "ikalogic-scanaplus", | |
308 | .longname = "IKALOGIC ScanaPLUS", | |
309 | .api_version = 1, | |
310 | .init = std_init, | |
311 | .cleanup = std_cleanup, | |
312 | .scan = scan, | |
313 | .dev_list = std_dev_list, | |
314 | .dev_clear = dev_clear, | |
315 | .config_get = config_get, | |
316 | .config_set = config_set, | |
317 | .config_list = config_list, | |
318 | .dev_open = dev_open, | |
319 | .dev_close = dev_close, | |
320 | .dev_acquisition_start = dev_acquisition_start, | |
321 | .dev_acquisition_stop = dev_acquisition_stop, | |
322 | .context = NULL, | |
323 | }; | |
324 | SR_REGISTER_DEV_DRIVER(ikalogic_scanaplus_driver_info); |