]>
Commit | Line | Data |
---|---|---|
fdf4a1f5 UH |
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 | |
2ea1fdf1 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
fdf4a1f5 UH |
18 | */ |
19 | ||
6ec6c43b | 20 | #include <config.h> |
fdf4a1f5 UH |
21 | #include "protocol.h" |
22 | ||
ab4bb6eb UH |
23 | #define USB_VENDOR_ID 0x0403 |
24 | #define USB_DEVICE_ID 0x6014 | |
ce95428c | 25 | #define USB_VENDOR_NAME "IKALOGIC" |
ab4bb6eb UH |
26 | #define USB_MODEL_NAME "ScanaPLUS" |
27 | #define USB_IPRODUCT "SCANAPLUS" | |
28 | ||
29 | #define SAMPLE_BUF_SIZE (8 * 1024 * 1024) | |
30 | ||
05199c0a | 31 | static const uint32_t drvopts[] = { |
ab4bb6eb | 32 | SR_CONF_LOGIC_ANALYZER, |
05199c0a UH |
33 | }; |
34 | ||
35 | static const uint32_t devopts[] = { | |
5827f61b BV |
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, | |
ab4bb6eb UH |
39 | }; |
40 | ||
ba7dd8bb | 41 | static const char *channel_names[] = { |
ab4bb6eb | 42 | "1", "2", "3", "4", "5", "6", "7", "8", "9", |
ab4bb6eb UH |
43 | }; |
44 | ||
ce95428c | 45 | /* Note: The IKALOGIC ScanaPLUS always samples at 100MHz. */ |
329733d9 | 46 | static const uint64_t samplerates[1] = { SR_MHZ(100) }; |
ab4bb6eb | 47 | |
3553451f | 48 | static void clear_helper(struct dev_context *devc) |
ab4bb6eb | 49 | { |
ab4bb6eb UH |
50 | ftdi_free(devc->ftdic); |
51 | g_free(devc->compressed_buf); | |
52 | g_free(devc->sample_buf); | |
53 | } | |
54 | ||
4f840ce9 | 55 | static int dev_clear(const struct sr_dev_driver *di) |
ab4bb6eb | 56 | { |
3553451f | 57 | return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); |
ab4bb6eb UH |
58 | } |
59 | ||
4f840ce9 | 60 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
fdf4a1f5 | 61 | { |
ab4bb6eb | 62 | struct sr_dev_inst *sdi; |
ab4bb6eb | 63 | struct dev_context *devc; |
ab4bb6eb UH |
64 | unsigned int i; |
65 | int ret; | |
fdf4a1f5 UH |
66 | |
67 | (void)options; | |
68 | ||
f57d8ffe | 69 | devc = g_malloc0(sizeof(struct dev_context)); |
ab4bb6eb UH |
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 | ||
ab4bb6eb UH |
83 | if (!(devc->ftdic = ftdi_new())) { |
84 | sr_err("Failed to initialize libftdi."); | |
85 | goto err_free_sample_buf; | |
86 | } | |
87 | ||
ab4bb6eb UH |
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 | ||
aac29cc1 | 98 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
45884333 | 99 | sdi->status = SR_ST_INACTIVE; |
0af636be UH |
100 | sdi->vendor = g_strdup(USB_VENDOR_NAME); |
101 | sdi->model = g_strdup(USB_MODEL_NAME); | |
ab4bb6eb UH |
102 | sdi->priv = devc; |
103 | ||
0f34cb47 UH |
104 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) |
105 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]); | |
ab4bb6eb | 106 | |
ab4bb6eb | 107 | scanaplus_close(devc); |
fdf4a1f5 | 108 | |
43376f33 | 109 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
ab4bb6eb | 110 | |
ab4bb6eb UH |
111 | scanaplus_close(devc); |
112 | err_free_ftdic: | |
ca314e06 | 113 | ftdi_free(devc->ftdic); |
ab4bb6eb UH |
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); | |
ab4bb6eb UH |
120 | |
121 | return NULL; | |
fdf4a1f5 UH |
122 | } |
123 | ||
fdf4a1f5 UH |
124 | static int dev_open(struct sr_dev_inst *sdi) |
125 | { | |
ab4bb6eb UH |
126 | struct dev_context *devc; |
127 | int ret; | |
128 | ||
129 | devc = sdi->priv; | |
130 | ||
ab4bb6eb UH |
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 | } | |
ab4bb6eb | 137 | |
ab4bb6eb UH |
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 | } | |
ab4bb6eb | 145 | |
ab4bb6eb UH |
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 | } | |
ab4bb6eb | 151 | |
ab4bb6eb UH |
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 | } | |
ab4bb6eb | 158 | |
ab4bb6eb UH |
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 | } | |
ab4bb6eb | 165 | |
ab4bb6eb UH |
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 | } | |
ab4bb6eb | 172 | |
ab4bb6eb UH |
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 | } | |
ab4bb6eb UH |
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]); | |
fdf4a1f5 | 187 | |
fdf4a1f5 | 188 | return SR_OK; |
ab4bb6eb UH |
189 | |
190 | err_dev_open_close_ftdic: | |
191 | scanaplus_close(devc); | |
7e463623 | 192 | |
ab4bb6eb | 193 | return SR_ERR; |
fdf4a1f5 UH |
194 | } |
195 | ||
196 | static int dev_close(struct sr_dev_inst *sdi) | |
197 | { | |
ab4bb6eb UH |
198 | struct dev_context *devc; |
199 | ||
ab4bb6eb UH |
200 | devc = sdi->priv; |
201 | ||
093e1cba | 202 | return scanaplus_close(devc); |
fdf4a1f5 UH |
203 | } |
204 | ||
584560f1 | 205 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 206 | const struct sr_channel_group *cg) |
fdf4a1f5 | 207 | { |
fdf4a1f5 | 208 | (void)sdi; |
53b4680f | 209 | (void)cg; |
fdf4a1f5 | 210 | |
584560f1 | 211 | switch (key) { |
ab4bb6eb UH |
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; | |
fdf4a1f5 UH |
216 | default: |
217 | return SR_ERR_NA; | |
218 | } | |
219 | ||
ab4bb6eb | 220 | return SR_OK; |
fdf4a1f5 UH |
221 | } |
222 | ||
584560f1 | 223 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 224 | const struct sr_channel_group *cg) |
fdf4a1f5 | 225 | { |
ab4bb6eb | 226 | struct dev_context *devc; |
fdf4a1f5 | 227 | |
53b4680f | 228 | (void)cg; |
d3c74a6f | 229 | |
ab4bb6eb UH |
230 | devc = sdi->priv; |
231 | ||
584560f1 | 232 | switch (key) { |
ab4bb6eb UH |
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; | |
fdf4a1f5 | 250 | default: |
ab4bb6eb | 251 | return SR_ERR_NA; |
fdf4a1f5 UH |
252 | } |
253 | ||
ab4bb6eb | 254 | return SR_OK; |
fdf4a1f5 UH |
255 | } |
256 | ||
584560f1 | 257 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 258 | const struct sr_channel_group *cg) |
fdf4a1f5 | 259 | { |
fdf4a1f5 | 260 | switch (key) { |
ab4bb6eb | 261 | case SR_CONF_DEVICE_OPTIONS: |
05199c0a | 262 | return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); |
ab4bb6eb | 263 | case SR_CONF_SAMPLERATE: |
463160cb | 264 | *data = std_gvar_samplerates(samplerates, ARRAY_SIZE(samplerates)); |
ab4bb6eb | 265 | break; |
fdf4a1f5 UH |
266 | default: |
267 | return SR_ERR_NA; | |
268 | } | |
269 | ||
ab4bb6eb | 270 | return SR_OK; |
fdf4a1f5 UH |
271 | } |
272 | ||
695dc859 | 273 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
fdf4a1f5 | 274 | { |
ab4bb6eb UH |
275 | int ret; |
276 | struct dev_context *devc; | |
fdf4a1f5 | 277 | |
208c1d35 | 278 | devc = sdi->priv; |
ab4bb6eb UH |
279 | |
280 | if (!devc->ftdic) | |
281 | return SR_ERR_BUG; | |
282 | ||
ba7dd8bb | 283 | /* TODO: Configure channels later (thresholds etc.). */ |
ab4bb6eb | 284 | |
ab4bb6eb UH |
285 | /* Properly reset internal variables before every new acquisition. */ |
286 | devc->compressed_bytes_ignored = 0; | |
287 | devc->samples_sent = 0; | |
288 | devc->bytes_received = 0; | |
289 | ||
290 | if ((ret = scanaplus_init(devc)) < 0) | |
291 | return ret; | |
292 | ||
293 | if ((ret = scanaplus_start_acquisition(devc)) < 0) | |
294 | return ret; | |
295 | ||
bee2b016 | 296 | std_session_send_df_header(sdi); |
ab4bb6eb UH |
297 | |
298 | /* Hook up a dummy handler to receive data from the device. */ | |
c650d3ec | 299 | sr_session_source_add(sdi->session, -1, 0, 0, scanaplus_receive_data, (void *)sdi); |
ab4bb6eb | 300 | |
fdf4a1f5 UH |
301 | return SR_OK; |
302 | } | |
303 | ||
695dc859 | 304 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
fdf4a1f5 | 305 | { |
102f1239 | 306 | sr_session_source_remove(sdi->session, -1); |
bee2b016 | 307 | std_session_send_df_end(sdi); |
fdf4a1f5 UH |
308 | |
309 | return SR_OK; | |
310 | } | |
311 | ||
dd5c48a6 | 312 | static struct sr_dev_driver ikalogic_scanaplus_driver_info = { |
fdf4a1f5 | 313 | .name = "ikalogic-scanaplus", |
ce95428c | 314 | .longname = "IKALOGIC ScanaPLUS", |
fdf4a1f5 | 315 | .api_version = 1, |
c2fdcc25 | 316 | .init = std_init, |
700d6b64 | 317 | .cleanup = std_cleanup, |
fdf4a1f5 | 318 | .scan = scan, |
c01bf34c | 319 | .dev_list = std_dev_list, |
fdf4a1f5 UH |
320 | .dev_clear = dev_clear, |
321 | .config_get = config_get, | |
322 | .config_set = config_set, | |
323 | .config_list = config_list, | |
324 | .dev_open = dev_open, | |
325 | .dev_close = dev_close, | |
326 | .dev_acquisition_start = dev_acquisition_start, | |
327 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 328 | .context = NULL, |
fdf4a1f5 | 329 | }; |
dd5c48a6 | 330 | SR_REGISTER_DEV_DRIVER(ikalogic_scanaplus_driver_info); |