aboutsummaryrefslogtreecommitdiff
path: root/src/hardware/beaglelogic/api.c
blob: 6deafb8102f93234eb12e342a4b1870f9a2d1b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * This file is part of the libsigrok project.
 *
 * Copyright (C) 2014 Kumar Abhishek <abhishek@theembeddedkitchen.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include "protocol.h"
#include "beaglelogic.h"

/* Scan options */
static const uint32_t scanopts[] = {
	SR_CONF_NUM_LOGIC_CHANNELS,
};

/* Hardware capabilities */
static const uint32_t devopts[] = {
	SR_CONF_LOGIC_ANALYZER,
	SR_CONF_CONTINUOUS,
	SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
	SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET,
	SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
	SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
	SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_GET,
};

/* Trigger matching capabilities */
static const int32_t soft_trigger_matches[] = {
	SR_TRIGGER_ZERO,
	SR_TRIGGER_ONE,
	SR_TRIGGER_RISING,
	SR_TRIGGER_FALLING,
	SR_TRIGGER_EDGE,
};

SR_PRIV const char *channel_names[] = {
	"P8_45", "P8_46", "P8_43", "P8_44", "P8_41", "P8_42", "P8_39",
	"P8_40", "P8_27", "P8_29", "P8_28", "P8_30", "P8_21", "P8_20",
};

/* Possible sample rates : 10 Hz to 100 MHz = (100 / x) MHz */
static const uint64_t samplerates[] = {
	SR_HZ(10),
	SR_MHZ(100),
	SR_HZ(1),
};

static struct dev_context *beaglelogic_devc_alloc(void)
{
	struct dev_context *devc;

	devc = g_malloc0(sizeof(struct dev_context));

	/* Default non-zero values (if any) */
	devc->fd = -1;
	devc->limit_samples = (uint64_t)-1;

	return devc;
}

static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
	struct drv_context *drvc;
	GSList *devices, *l;
	struct sr_config *src;
	struct sr_dev_inst *sdi;
	struct dev_context *devc;
	int i, maxch;

	devices = NULL;
	drvc = di->context;
	drvc->instances = NULL;

	/* Probe for /dev/beaglelogic */
	if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
		return NULL;

	sdi = g_malloc0(sizeof(struct sr_dev_inst));
	sdi->status = SR_ST_INACTIVE;
	sdi->model = g_strdup("BeagleLogic");
	sdi->version = g_strdup("1.0");
	sdi->driver = di;

	/* Unless explicitly specified, keep max channels to 8 only */
	maxch = 8;
	for (l = options; l; l = l->next) {
		src = l->data;
		if (src->key == SR_CONF_NUM_LOGIC_CHANNELS)
			maxch = g_variant_get_int32(src->data);
	}

	/* We need to test for number of channels by opening the node */
	devc = beaglelogic_devc_alloc();

	if (beaglelogic_open_nonblock(devc) != SR_OK) {
		g_free(devc);
		sr_dev_inst_free(sdi);

		return NULL;
	}

	if (maxch > 8) {
		maxch = NUM_CHANNELS;
		devc->sampleunit = BL_SAMPLEUNIT_16_BITS;
	} else {
		maxch = 8;
		devc->sampleunit = BL_SAMPLEUNIT_8_BITS;
	}

	beaglelogic_set_sampleunit(devc);
	beaglelogic_close(devc);

	/* Signal */
	sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);

	/* Fill the channels */
	for (i = 0; i < maxch; i++)
		sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
				channel_names[i]);

	sdi->priv = devc;
	drvc->instances = g_slist_append(drvc->instances, sdi);
	devices = g_slist_append(devices, sdi);

	return devices;
}

static int dev_open(struct sr_dev_inst *sdi)
{
	struct dev_context *devc = sdi->priv;

	/* Open BeagleLogic */
	if (beaglelogic_open_nonblock(devc))
		return SR_ERR;

	/* Set fd and local attributes */
	devc->pollfd.fd = devc->fd;
	devc->pollfd.events = G_IO_IN;
	devc->pollfd.revents = 0;

	/* Get the default attributes */
	beaglelogic_get_samplerate(devc);
	beaglelogic_get_sampleunit(devc);
	beaglelogic_get_triggerflags(devc);
	beaglelogic_get_buffersize(devc);
	beaglelogic_get_bufunitsize(devc);

	/* Map the kernel capture FIFO for reads, saves 1 level of memcpy */
	if (beaglelogic_mmap(devc) != SR_OK) {
		sr_err("Unable to map capture buffer");
		beaglelogic_close(devc);
		return SR_ERR;
	}

	/* We're good to go now */
	sdi->status = SR_ST_ACTIVE;
	return SR_OK;
}

static int dev_close(struct sr_dev_inst *sdi)
{
	struct dev_context *devc = sdi->priv;

	if (sdi->status == SR_ST_ACTIVE) {
		/* Close the memory mapping and the file */
		beaglelogic_munmap(devc);
		beaglelogic_close(devc);
	}
	sdi->status = SR_ST_INACTIVE;
	return SR_OK;
}

static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
		const struct sr_channel_group *cg)
{
	struct dev_context *devc = sdi->priv;

	(void)cg;

	switch (key) {
	case SR_CONF_LIMIT_SAMPLES:
		*data = g_variant_new_uint64(devc->limit_samples);
		break;
	case SR_CONF_SAMPLERATE:
		*data = g_variant_new_uint64(devc->cur_samplerate);
		break;
	case SR_CONF_CAPTURE_RATIO:
		*data = g_variant_new_uint64(devc->capture_ratio);
		break;
	case SR_CONF_NUM_LOGIC_CHANNELS:
		*data = g_variant_new_uint32(g_slist_length(sdi->channels));
		break;
	default:
		return SR_ERR_NA;
	}

	return SR_OK;
}

static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
		const struct sr_channel_group *cg)
{
	struct dev_context *devc = sdi->priv;
	uint64_t tmp_u64;

	(void)cg;

	if (sdi->status != SR_ST_ACTIVE)
		return SR_ERR_DEV_CLOSED;

	switch (key) {
	case SR_CONF_SAMPLERATE:
		devc->cur_samplerate = g_variant_get_uint64(data);
		return beaglelogic_set_samplerate(devc);
	case SR_CONF_LIMIT_SAMPLES:
		tmp_u64 = g_variant_get_uint64(data);
		devc->limit_samples = tmp_u64;
		devc->triggerflags = BL_TRIGGERFLAGS_ONESHOT;

		/* Check if we have sufficient buffer size */
		tmp_u64 *= SAMPLEUNIT_TO_BYTES(devc->sampleunit);
		if (tmp_u64 > devc->buffersize) {
			sr_warn("Insufficient buffer space has been allocated.");
			sr_warn("Please use \'echo <size in bytes> > "\
				BEAGLELOGIC_SYSFS_ATTR(memalloc) \
				"\' as root to increase the buffer size, this"\
				" capture is now truncated to %d Msamples",
				devc->buffersize /
				(SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000));
		}
		return beaglelogic_set_triggerflags(devc);
	case SR_CONF_CAPTURE_RATIO:
		devc->capture_ratio = g_variant_get_uint64(data);
		if (devc->capture_ratio > 100)
			return SR_ERR;
		return SR_OK;
	default:
		return SR_ERR_NA;
	}

	return SR_OK;
}

static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
		const struct sr_channel_group *cg)
{
	int ret;
	GVariant *gvar;
	GVariantBuilder gvb;

	(void)sdi;
	(void)cg;

	ret = SR_OK;
	switch (key) {
	case SR_CONF_SCAN_OPTIONS:
		*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
				scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
		break;
	case SR_CONF_DEVICE_OPTIONS:
		*data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
				devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
		break;
	case SR_CONF_SAMPLERATE:
		g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
		gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
			samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
		g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar);
		*data = g_variant_builder_end(&gvb);
		break;
	case SR_CONF_TRIGGER_MATCH:
		*data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
				soft_trigger_matches, ARRAY_SIZE(soft_trigger_matches),
				sizeof(int32_t));
		break;
	default:
		return SR_ERR_NA;
	}

	return ret;
}

/* get a sane timeout for poll() */
#define BUFUNIT_TIMEOUT_MS(devc)	(100 + ((devc->bufunitsize * 1000) / \
				(uint32_t)(devc->cur_samplerate)))

static int dev_acquisition_start(const struct sr_dev_inst *sdi)
{
	struct dev_context *devc = sdi->priv;
	struct sr_trigger *trigger;

	if (sdi->status != SR_ST_ACTIVE)
		return SR_ERR_DEV_CLOSED;

	/* Clear capture state */
	devc->bytes_read = 0;
	devc->offset = 0;

	/* Configure channels */
	devc->sampleunit = g_slist_length(sdi->channels) > 8 ?
			BL_SAMPLEUNIT_16_BITS : BL_SAMPLEUNIT_8_BITS;
	beaglelogic_set_sampleunit(devc);

	/* Configure triggers & send header packet */
	if ((trigger = sr_session_trigger_get(sdi->session))) {
		int pre_trigger_samples = 0;
		if (devc->limit_samples > 0)
			pre_trigger_samples = devc->capture_ratio * devc->limit_samples/100;
		devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
		if (!devc->stl)
			return SR_ERR_MALLOC;
		devc->trigger_fired = FALSE;
	} else
		devc->trigger_fired = TRUE;
	std_session_send_df_header(sdi, LOG_PREFIX);

	/* Trigger and add poll on file */
	beaglelogic_start(devc);
	sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
			BUFUNIT_TIMEOUT_MS(devc), beaglelogic_receive_data,
			(void *)sdi);

	return SR_OK;
}

static int dev_acquisition_stop(struct sr_dev_inst *sdi)
{
	struct dev_context *devc = sdi->priv;

	if (sdi->status != SR_ST_ACTIVE)
		return SR_ERR_DEV_CLOSED;

	/* Execute a stop on BeagleLogic */
	beaglelogic_stop(devc);

	/* lseek to offset 0, flushes the cache */
	lseek(devc->fd, 0, SEEK_SET);

	/* Remove session source and send EOT packet */
	sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
	std_session_send_df_end(sdi, LOG_PREFIX);

	return SR_OK;
}

static struct sr_dev_driver beaglelogic_driver_info = {
	.name = "beaglelogic",
	.longname = "BeagleLogic",
	.api_version = 1,
	.init = std_init,
	.cleanup = std_cleanup,
	.scan = scan,
	.dev_list = std_dev_list,
	.config_get = config_get,
	.config_set = config_set,
	.config_list = config_list,
	.dev_open = dev_open,
	.dev_close = dev_close,
	.dev_acquisition_start = dev_acquisition_start,
	.dev_acquisition_stop = dev_acquisition_stop,
	.context = NULL,
};
SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info);