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