2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
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"
29 #define SAMPLE_BUF_SIZE (8 * 1024 * 1024)
31 static const uint32_t devopts[] = {
32 SR_CONF_LOGIC_ANALYZER,
33 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
34 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
35 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 /* Channels are numbered 1-9. */
39 static const char *channel_names[] = {
40 "1", "2", "3", "4", "5", "6", "7", "8", "9",
44 /* Note: The IKALOGIC ScanaPLUS always samples at 100MHz. */
45 static uint64_t samplerates[1] = { SR_MHZ(100) };
47 SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info;
48 static struct sr_dev_driver *di = &ikalogic_scanaplus_driver_info;
50 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
52 static void clear_helper(void *priv)
54 struct dev_context *devc;
58 ftdi_free(devc->ftdic);
59 g_free(devc->compressed_buf);
60 g_free(devc->sample_buf);
64 static int dev_clear(void)
66 return std_dev_clear(di, clear_helper);
69 static int init(struct sr_context *sr_ctx)
71 return std_init(sr_ctx, di, LOG_PREFIX);
74 static GSList *scan(GSList *options)
76 struct sr_dev_inst *sdi;
77 struct drv_context *drvc;
78 struct dev_context *devc;
89 /* Allocate memory for our private device context. */
90 devc = g_malloc0(sizeof(struct dev_context));
92 /* Allocate memory for the incoming compressed samples. */
93 if (!(devc->compressed_buf = g_try_malloc0(COMPRESSED_BUF_SIZE))) {
94 sr_err("compressed_buf malloc failed.");
98 /* Allocate memory for the uncompressed samples. */
99 if (!(devc->sample_buf = g_try_malloc0(SAMPLE_BUF_SIZE))) {
100 sr_err("sample_buf malloc failed.");
101 goto err_free_compressed_buf;
104 /* Allocate memory for the FTDI context (ftdic) and initialize it. */
105 if (!(devc->ftdic = ftdi_new())) {
106 sr_err("Failed to initialize libftdi.");
107 goto err_free_sample_buf;
110 /* Check for the device and temporarily open it. */
111 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
114 /* Log errors, except for -3 ("device not found"). */
116 sr_err("Failed to open device (%d): %s", ret,
117 ftdi_get_error_string(devc->ftdic));
121 /* Register the device with libsigrok. */
122 sdi = g_malloc0(sizeof(struct sr_dev_inst));
123 sdi->status = SR_ST_INITIALIZING;
124 sdi->vendor = g_strdup(USB_VENDOR_NAME);
125 sdi->model = g_strdup(USB_MODEL_NAME);
129 for (i = 0; channel_names[i]; i++)
130 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
133 devices = g_slist_append(devices, sdi);
134 drvc->instances = g_slist_append(drvc->instances, sdi);
136 /* Close device. We'll reopen it again when we need it. */
137 scanaplus_close(devc);
141 scanaplus_close(devc);
143 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
145 g_free(devc->sample_buf);
146 err_free_compressed_buf:
147 g_free(devc->compressed_buf);
154 static GSList *dev_list(void)
156 return ((struct drv_context *)(di->priv))->instances;
159 static int dev_open(struct sr_dev_inst *sdi)
161 struct dev_context *devc;
166 /* Select interface A, otherwise communication will fail. */
167 ret = ftdi_set_interface(devc->ftdic, INTERFACE_A);
169 sr_err("Failed to set FTDI interface A (%d): %s", ret,
170 ftdi_get_error_string(devc->ftdic));
173 sr_dbg("FTDI chip interface A set successfully.");
175 /* Open the device. */
176 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
179 sr_err("Failed to open device (%d): %s", ret,
180 ftdi_get_error_string(devc->ftdic));
183 sr_dbg("FTDI device opened successfully.");
185 /* Purge RX/TX buffers in the FTDI chip. */
186 if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) {
187 sr_err("Failed to purge FTDI RX/TX buffers (%d): %s.",
188 ret, ftdi_get_error_string(devc->ftdic));
189 goto err_dev_open_close_ftdic;
191 sr_dbg("FTDI chip buffers purged successfully.");
193 /* Reset the FTDI bitmode. */
194 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
196 sr_err("Failed to reset the FTDI chip bitmode (%d): %s.",
197 ret, ftdi_get_error_string(devc->ftdic));
198 goto err_dev_open_close_ftdic;
200 sr_dbg("FTDI chip bitmode reset successfully.");
202 /* Set FTDI bitmode to "sync FIFO". */
203 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF);
205 sr_err("Failed to put FTDI chip into sync FIFO mode (%d): %s.",
206 ret, ftdi_get_error_string(devc->ftdic));
207 goto err_dev_open_close_ftdic;
209 sr_dbg("FTDI chip sync FIFO mode entered successfully.");
211 /* Set the FTDI latency timer to 2. */
212 ret = ftdi_set_latency_timer(devc->ftdic, 2);
214 sr_err("Failed to set FTDI latency timer (%d): %s.",
215 ret, ftdi_get_error_string(devc->ftdic));
216 goto err_dev_open_close_ftdic;
218 sr_dbg("FTDI chip latency timer set successfully.");
220 /* Set the FTDI read data chunk size to 64kB. */
221 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
223 sr_err("Failed to set FTDI read data chunk size (%d): %s.",
224 ret, ftdi_get_error_string(devc->ftdic));
225 goto err_dev_open_close_ftdic;
227 sr_dbg("FTDI chip read data chunk size set successfully.");
229 /* Get the ScanaPLUS device ID from the FTDI EEPROM. */
230 if ((ret = scanaplus_get_device_id(devc)) < 0) {
231 sr_err("Failed to get ScanaPLUS device ID: %d.", ret);
232 goto err_dev_open_close_ftdic;
234 sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
235 devc->devid[0], devc->devid[1], devc->devid[2]);
237 sdi->status = SR_ST_ACTIVE;
241 err_dev_open_close_ftdic:
242 scanaplus_close(devc);
246 static int dev_close(struct sr_dev_inst *sdi)
249 struct dev_context *devc;
254 if (sdi->status == SR_ST_ACTIVE) {
255 sr_dbg("Status ACTIVE, closing device.");
256 ret = scanaplus_close(devc);
258 sr_spew("Status not ACTIVE, nothing to do.");
261 sdi->status = SR_ST_INACTIVE;
266 static int cleanup(void)
271 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
272 const struct sr_channel_group *cg)
278 case SR_CONF_SAMPLERATE:
279 /* The ScanaPLUS samplerate is 100MHz and can't be changed. */
280 *data = g_variant_new_uint64(SR_MHZ(100));
289 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
290 const struct sr_channel_group *cg)
292 struct dev_context *devc;
296 if (sdi->status != SR_ST_ACTIVE)
297 return SR_ERR_DEV_CLOSED;
302 case SR_CONF_SAMPLERATE:
303 if (g_variant_get_uint64(data) != SR_MHZ(100)) {
304 sr_err("ScanaPLUS only supports samplerate = 100MHz.");
307 /* Nothing to do, the ScanaPLUS samplerate is always 100MHz. */
309 case SR_CONF_LIMIT_MSEC:
310 if (g_variant_get_uint64(data) == 0)
312 devc->limit_msec = g_variant_get_uint64(data);
314 case SR_CONF_LIMIT_SAMPLES:
315 if (g_variant_get_uint64(data) == 0)
317 devc->limit_samples = g_variant_get_uint64(data);
326 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
327 const struct sr_channel_group *cg)
336 case SR_CONF_DEVICE_OPTIONS:
337 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
338 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
340 case SR_CONF_SAMPLERATE:
341 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
342 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
343 samplerates, ARRAY_SIZE(samplerates),
345 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
346 *data = g_variant_builder_end(&gvb);
355 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
358 struct dev_context *devc;
360 if (sdi->status != SR_ST_ACTIVE)
361 return SR_ERR_DEV_CLOSED;
363 if (!(devc = sdi->priv))
369 /* TODO: Configure channels later (thresholds etc.). */
371 devc->cb_data = cb_data;
373 /* Properly reset internal variables before every new acquisition. */
374 devc->compressed_bytes_ignored = 0;
375 devc->samples_sent = 0;
376 devc->bytes_received = 0;
378 if ((ret = scanaplus_init(devc)) < 0)
381 if ((ret = scanaplus_start_acquisition(devc)) < 0)
384 /* Send header packet to the session bus. */
385 std_session_send_df_header(sdi, LOG_PREFIX);
387 /* Hook up a dummy handler to receive data from the device. */
388 sr_session_source_add(sdi->session, -1, G_IO_IN, 0, scanaplus_receive_data, (void *)sdi);
393 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
395 struct sr_datafeed_packet packet;
399 sr_dbg("Stopping acquisition.");
400 sr_session_source_remove(sdi->session, -1);
402 /* Send end packet to the session bus. */
403 sr_dbg("Sending SR_DF_END.");
404 packet.type = SR_DF_END;
405 sr_session_send(sdi, &packet);
410 SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info = {
411 .name = "ikalogic-scanaplus",
412 .longname = "IKALOGIC ScanaPLUS",
417 .dev_list = dev_list,
418 .dev_clear = dev_clear,
419 .config_get = config_get,
420 .config_set = config_set,
421 .config_list = config_list,
422 .dev_open = dev_open,
423 .dev_close = dev_close,
424 .dev_acquisition_start = dev_acquisition_start,
425 .dev_acquisition_stop = dev_acquisition_stop,