]> sigrok.org Git - libsigrok.git/blame - src/hardware/ikalogic-scanaplus/api.c
Simplify channel creation.
[libsigrok.git] / src / hardware / ikalogic-scanaplus / api.c
CommitLineData
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
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
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
f254bc4b 31static const uint32_t devopts[] = {
ab4bb6eb 32 SR_CONF_LOGIC_ANALYZER,
5827f61b
BV
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,
ab4bb6eb
UH
36};
37
ba7dd8bb
UH
38/* Channels are numbered 1-9. */
39static const char *channel_names[] = {
ab4bb6eb
UH
40 "1", "2", "3", "4", "5", "6", "7", "8", "9",
41 NULL,
42};
43
ce95428c 44/* Note: The IKALOGIC ScanaPLUS always samples at 100MHz. */
ab4bb6eb
UH
45static uint64_t samplerates[1] = { SR_MHZ(100) };
46
fdf4a1f5
UH
47SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info;
48static struct sr_dev_driver *di = &ikalogic_scanaplus_driver_info;
49
ab4bb6eb
UH
50static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
51
52static void clear_helper(void *priv)
53{
54 struct dev_context *devc;
55
56 devc = priv;
57
58 ftdi_free(devc->ftdic);
59 g_free(devc->compressed_buf);
60 g_free(devc->sample_buf);
f817f05a 61 g_free(devc);
ab4bb6eb
UH
62}
63
64static int dev_clear(void)
65{
66 return std_dev_clear(di, clear_helper);
67}
68
fdf4a1f5
UH
69static int init(struct sr_context *sr_ctx)
70{
ab4bb6eb 71 return std_init(sr_ctx, di, LOG_PREFIX);
fdf4a1f5
UH
72}
73
74static GSList *scan(GSList *options)
75{
ab4bb6eb 76 struct sr_dev_inst *sdi;
fdf4a1f5 77 struct drv_context *drvc;
ab4bb6eb 78 struct dev_context *devc;
fdf4a1f5 79 GSList *devices;
ab4bb6eb
UH
80 unsigned int i;
81 int ret;
fdf4a1f5
UH
82
83 (void)options;
84
fdf4a1f5 85 drvc = di->priv;
ab4bb6eb
UH
86
87 devices = NULL;
88
89 /* Allocate memory for our private device context. */
f57d8ffe 90 devc = g_malloc0(sizeof(struct dev_context));
ab4bb6eb
UH
91
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.");
95 goto err_free_devc;
96 }
97
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;
102 }
103
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;
108 }
109
110 /* Check for the device and temporarily open it. */
111 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
112 USB_IPRODUCT, NULL);
113 if (ret < 0) {
114 /* Log errors, except for -3 ("device not found"). */
115 if (ret != -3)
116 sr_err("Failed to open device (%d): %s", ret,
117 ftdi_get_error_string(devc->ftdic));
118 goto err_free_ftdic;
119 }
120
121 /* Register the device with libsigrok. */
aac29cc1 122 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
123 sdi->status = SR_ST_INITIALIZING;
124 sdi->vendor = g_strdup(USB_VENDOR_NAME);
125 sdi->model = g_strdup(USB_MODEL_NAME);
ab4bb6eb
UH
126 sdi->driver = di;
127 sdi->priv = devc;
128
5e23fcab
ML
129 for (i = 0; channel_names[i]; i++)
130 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
c368e6f3 131 channel_names[i]);
ab4bb6eb
UH
132
133 devices = g_slist_append(devices, sdi);
134 drvc->instances = g_slist_append(drvc->instances, sdi);
135
136 /* Close device. We'll reopen it again when we need it. */
137 scanaplus_close(devc);
fdf4a1f5
UH
138
139 return devices;
ab4bb6eb 140
ab4bb6eb
UH
141 scanaplus_close(devc);
142err_free_ftdic:
143 ftdi_free(devc->ftdic); /* NOT free() or g_free()! */
144err_free_sample_buf:
145 g_free(devc->sample_buf);
146err_free_compressed_buf:
147 g_free(devc->compressed_buf);
148err_free_devc:
149 g_free(devc);
ab4bb6eb
UH
150
151 return NULL;
fdf4a1f5
UH
152}
153
154static GSList *dev_list(void)
155{
156 return ((struct drv_context *)(di->priv))->instances;
157}
158
fdf4a1f5
UH
159static int dev_open(struct sr_dev_inst *sdi)
160{
ab4bb6eb
UH
161 struct dev_context *devc;
162 int ret;
163
164 devc = sdi->priv;
165
166 /* Select interface A, otherwise communication will fail. */
167 ret = ftdi_set_interface(devc->ftdic, INTERFACE_A);
168 if (ret < 0) {
169 sr_err("Failed to set FTDI interface A (%d): %s", ret,
170 ftdi_get_error_string(devc->ftdic));
171 return SR_ERR;
172 }
173 sr_dbg("FTDI chip interface A set successfully.");
174
175 /* Open the device. */
176 ret = ftdi_usb_open_desc(devc->ftdic, USB_VENDOR_ID, USB_DEVICE_ID,
177 USB_IPRODUCT, NULL);
178 if (ret < 0) {
179 sr_err("Failed to open device (%d): %s", ret,
180 ftdi_get_error_string(devc->ftdic));
181 return SR_ERR;
182 }
183 sr_dbg("FTDI device opened successfully.");
184
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;
190 }
191 sr_dbg("FTDI chip buffers purged successfully.");
192
193 /* Reset the FTDI bitmode. */
194 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_RESET);
195 if (ret < 0) {
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;
199 }
200 sr_dbg("FTDI chip bitmode reset successfully.");
201
202 /* Set FTDI bitmode to "sync FIFO". */
203 ret = ftdi_set_bitmode(devc->ftdic, 0xff, BITMODE_SYNCFF);
204 if (ret < 0) {
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;
208 }
209 sr_dbg("FTDI chip sync FIFO mode entered successfully.");
210
211 /* Set the FTDI latency timer to 2. */
212 ret = ftdi_set_latency_timer(devc->ftdic, 2);
213 if (ret < 0) {
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;
217 }
218 sr_dbg("FTDI chip latency timer set successfully.");
219
220 /* Set the FTDI read data chunk size to 64kB. */
221 ret = ftdi_read_data_set_chunksize(devc->ftdic, 64 * 1024);
222 if (ret < 0) {
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;
226 }
227 sr_dbg("FTDI chip read data chunk size set successfully.");
228
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;
233 }
234 sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
235 devc->devid[0], devc->devid[1], devc->devid[2]);
fdf4a1f5
UH
236
237 sdi->status = SR_ST_ACTIVE;
238
239 return SR_OK;
ab4bb6eb
UH
240
241err_dev_open_close_ftdic:
242 scanaplus_close(devc);
243 return SR_ERR;
fdf4a1f5
UH
244}
245
246static int dev_close(struct sr_dev_inst *sdi)
247{
ab4bb6eb
UH
248 int ret;
249 struct dev_context *devc;
250
251 ret = SR_OK;
252 devc = sdi->priv;
253
254 if (sdi->status == SR_ST_ACTIVE) {
255 sr_dbg("Status ACTIVE, closing device.");
256 ret = scanaplus_close(devc);
257 } else {
258 sr_spew("Status not ACTIVE, nothing to do.");
259 }
fdf4a1f5
UH
260
261 sdi->status = SR_ST_INACTIVE;
262
ab4bb6eb 263 return ret;
fdf4a1f5
UH
264}
265
266static int cleanup(void)
267{
268 return dev_clear();
269}
270
584560f1 271static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 272 const struct sr_channel_group *cg)
fdf4a1f5 273{
fdf4a1f5 274 (void)sdi;
53b4680f 275 (void)cg;
fdf4a1f5 276
584560f1 277 switch (key) {
ab4bb6eb
UH
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));
281 break;
fdf4a1f5
UH
282 default:
283 return SR_ERR_NA;
284 }
285
ab4bb6eb 286 return SR_OK;
fdf4a1f5
UH
287}
288
584560f1 289static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
53b4680f 290 const struct sr_channel_group *cg)
fdf4a1f5 291{
ab4bb6eb 292 struct dev_context *devc;
fdf4a1f5 293
53b4680f 294 (void)cg;
d3c74a6f 295
fdf4a1f5
UH
296 if (sdi->status != SR_ST_ACTIVE)
297 return SR_ERR_DEV_CLOSED;
298
ab4bb6eb
UH
299 devc = sdi->priv;
300
584560f1 301 switch (key) {
ab4bb6eb
UH
302 case SR_CONF_SAMPLERATE:
303 if (g_variant_get_uint64(data) != SR_MHZ(100)) {
304 sr_err("ScanaPLUS only supports samplerate = 100MHz.");
305 return SR_ERR_ARG;
306 }
307 /* Nothing to do, the ScanaPLUS samplerate is always 100MHz. */
308 break;
309 case SR_CONF_LIMIT_MSEC:
310 if (g_variant_get_uint64(data) == 0)
311 return SR_ERR_ARG;
312 devc->limit_msec = g_variant_get_uint64(data);
313 break;
314 case SR_CONF_LIMIT_SAMPLES:
315 if (g_variant_get_uint64(data) == 0)
316 return SR_ERR_ARG;
317 devc->limit_samples = g_variant_get_uint64(data);
318 break;
fdf4a1f5 319 default:
ab4bb6eb 320 return SR_ERR_NA;
fdf4a1f5
UH
321 }
322
ab4bb6eb 323 return SR_OK;
fdf4a1f5
UH
324}
325
584560f1 326static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
53b4680f 327 const struct sr_channel_group *cg)
fdf4a1f5 328{
ab4bb6eb
UH
329 GVariant *gvar;
330 GVariantBuilder gvb;
fdf4a1f5
UH
331
332 (void)sdi;
53b4680f 333 (void)cg;
fdf4a1f5 334
fdf4a1f5 335 switch (key) {
ab4bb6eb 336 case SR_CONF_DEVICE_OPTIONS:
584560f1 337 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
f254bc4b 338 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
ab4bb6eb
UH
339 break;
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),
344 sizeof(uint64_t));
345 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
346 *data = g_variant_builder_end(&gvb);
347 break;
fdf4a1f5
UH
348 default:
349 return SR_ERR_NA;
350 }
351
ab4bb6eb 352 return SR_OK;
fdf4a1f5
UH
353}
354
355static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
356{
ab4bb6eb
UH
357 int ret;
358 struct dev_context *devc;
fdf4a1f5
UH
359
360 if (sdi->status != SR_ST_ACTIVE)
361 return SR_ERR_DEV_CLOSED;
362
ab4bb6eb
UH
363 if (!(devc = sdi->priv))
364 return SR_ERR_BUG;
365
366 if (!devc->ftdic)
367 return SR_ERR_BUG;
368
ba7dd8bb 369 /* TODO: Configure channels later (thresholds etc.). */
ab4bb6eb
UH
370
371 devc->cb_data = cb_data;
372
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;
377
378 if ((ret = scanaplus_init(devc)) < 0)
379 return ret;
380
381 if ((ret = scanaplus_start_acquisition(devc)) < 0)
382 return ret;
383
384 /* Send header packet to the session bus. */
102f1239 385 std_session_send_df_header(sdi, LOG_PREFIX);
ab4bb6eb
UH
386
387 /* Hook up a dummy handler to receive data from the device. */
102f1239 388 sr_session_source_add(sdi->session, -1, G_IO_IN, 0, scanaplus_receive_data, (void *)sdi);
ab4bb6eb 389
fdf4a1f5
UH
390 return SR_OK;
391}
392
393static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
394{
ab4bb6eb 395 struct sr_datafeed_packet packet;
fdf4a1f5 396
102f1239 397 (void)cb_data;
ab4bb6eb
UH
398
399 sr_dbg("Stopping acquisition.");
102f1239 400 sr_session_source_remove(sdi->session, -1);
ab4bb6eb
UH
401
402 /* Send end packet to the session bus. */
403 sr_dbg("Sending SR_DF_END.");
404 packet.type = SR_DF_END;
102f1239 405 sr_session_send(sdi, &packet);
fdf4a1f5
UH
406
407 return SR_OK;
408}
409
410SR_PRIV struct sr_dev_driver ikalogic_scanaplus_driver_info = {
411 .name = "ikalogic-scanaplus",
ce95428c 412 .longname = "IKALOGIC ScanaPLUS",
fdf4a1f5
UH
413 .api_version = 1,
414 .init = init,
415 .cleanup = cleanup,
416 .scan = scan,
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,
426 .priv = NULL,
427};