]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/beaglelogic/api.c
Backport recent changes from mainline.
[libsigrok.git] / src / hardware / beaglelogic / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014-2017 Kumar Abhishek <abhishek@theembeddedkitchen.net>
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 3 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#include "beaglelogic.h"
23
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
26 SR_CONF_NUM_LOGIC_CHANNELS,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_LOGIC_ANALYZER,
31};
32
33static const uint32_t devopts[] = {
34 SR_CONF_CONTINUOUS,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST,
38 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
39 SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_GET,
40};
41
42static const int32_t trigger_matches[] = {
43 SR_TRIGGER_ZERO,
44 SR_TRIGGER_ONE,
45 SR_TRIGGER_RISING,
46 SR_TRIGGER_FALLING,
47 SR_TRIGGER_EDGE,
48};
49
50SR_PRIV const char *channel_names[] = {
51 "P8_45", "P8_46", "P8_43", "P8_44", "P8_41", "P8_42", "P8_39",
52 "P8_40", "P8_27", "P8_29", "P8_28", "P8_30", "P8_21", "P8_20",
53};
54
55/* Possible sample rates : 10 Hz to 100 MHz = (100 / x) MHz */
56static const uint64_t samplerates[] = {
57 SR_HZ(10),
58 SR_MHZ(100),
59 SR_HZ(1),
60};
61
62static GSList *scan(struct sr_dev_driver *di, GSList *options)
63{
64 GSList *l;
65 struct sr_config *src;
66 struct sr_dev_inst *sdi;
67 struct dev_context *devc;
68 const char *conn;
69 gchar **params;
70 int i, maxch;
71
72 maxch = NUM_CHANNELS;
73 conn = NULL;
74 for (l = options; l; l = l->next) {
75 src = l->data;
76 if (src->key == SR_CONF_NUM_LOGIC_CHANNELS)
77 maxch = g_variant_get_int32(src->data);
78 if (src->key == SR_CONF_CONN)
79 conn = g_variant_get_string(src->data, NULL);
80 }
81
82 /* Probe for /dev/beaglelogic if not connecting via TCP */
83 if (!conn) {
84 params = NULL;
85 if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
86 return NULL;
87 } else {
88 params = g_strsplit(conn, "/", 0);
89 if (!params || !params[1] || !params[2]) {
90 sr_err("Invalid Parameters.");
91 g_strfreev(params);
92 return NULL;
93 }
94 if (g_ascii_strncasecmp(params[0], "tcp", 3)) {
95 sr_err("Only TCP (tcp-raw) protocol is currently supported.");
96 g_strfreev(params);
97 return NULL;
98 }
99 }
100
101 maxch = (maxch > 8) ? NUM_CHANNELS : 8;
102
103 sdi = g_new0(struct sr_dev_inst, 1);
104 sdi->status = SR_ST_INACTIVE;
105 sdi->model = g_strdup("BeagleLogic");
106 sdi->version = g_strdup("1.0");
107
108 devc = g_malloc0(sizeof(struct dev_context));
109
110 /* Default non-zero values (if any) */
111 devc->fd = -1;
112 devc->limit_samples = 10000000;
113 devc->tcp_buffer = 0;
114
115 if (!conn) {
116 devc->beaglelogic = &beaglelogic_native_ops;
117 sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);
118 } else {
119 devc->read_timeout = 1000 * 1000;
120 devc->beaglelogic = &beaglelogic_tcp_ops;
121 devc->address = g_strdup(params[1]);
122 devc->port = g_strdup(params[2]);
123 g_strfreev(params);
124
125 if (devc->beaglelogic->open(devc) != SR_OK)
126 goto err_free;
127 if (beaglelogic_tcp_detect(devc) != SR_OK)
128 goto err_free;
129 if (devc->beaglelogic->close(devc) != SR_OK)
130 goto err_free;
131 sr_info("BeagleLogic device found at %s : %s",
132 devc->address, devc->port);
133 }
134
135 /* Fill the channels */
136 for (i = 0; i < maxch; i++)
137 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
138 channel_names[i]);
139
140 sdi->priv = devc;
141
142 return std_scan_complete(di, g_slist_append(NULL, sdi));
143
144err_free:
145 g_free(sdi->model);
146 g_free(sdi->version);
147 g_free(devc->address);
148 g_free(devc->port);
149 g_free(devc);
150 g_free(sdi);
151
152 return NULL;
153}
154
155static int dev_open(struct sr_dev_inst *sdi)
156{
157 struct dev_context *devc = sdi->priv;
158
159 /* Open BeagleLogic */
160 if (devc->beaglelogic->open(devc))
161 return SR_ERR;
162
163 /* Set fd and local attributes */
164 if (devc->beaglelogic == &beaglelogic_tcp_ops)
165 devc->pollfd.fd = devc->socket;
166 else
167 devc->pollfd.fd = devc->fd;
168 devc->pollfd.events = G_IO_IN;
169 devc->pollfd.revents = 0;
170
171 /* Get the default attributes */
172 devc->beaglelogic->get_samplerate(devc);
173 devc->beaglelogic->get_sampleunit(devc);
174 devc->beaglelogic->get_buffersize(devc);
175 devc->beaglelogic->get_bufunitsize(devc);
176
177 /* Set the triggerflags to default for continuous capture unless we
178 * explicitly limit samples using SR_CONF_LIMIT_SAMPLES */
179 devc->triggerflags = BL_TRIGGERFLAGS_CONTINUOUS;
180 devc->beaglelogic->set_triggerflags(devc);
181
182 /* Map the kernel capture FIFO for reads, saves 1 level of memcpy */
183 if (devc->beaglelogic == &beaglelogic_native_ops) {
184 if (devc->beaglelogic->mmap(devc) != SR_OK) {
185 sr_err("Unable to map capture buffer");
186 devc->beaglelogic->close(devc);
187 return SR_ERR;
188 }
189 } else {
190 devc->tcp_buffer = g_malloc(TCP_BUFFER_SIZE);
191 }
192
193 return SR_OK;
194}
195
196static int dev_close(struct sr_dev_inst *sdi)
197{
198 struct dev_context *devc = sdi->priv;
199
200 /* Close the memory mapping and the file */
201 if (devc->beaglelogic == &beaglelogic_native_ops)
202 devc->beaglelogic->munmap(devc);
203 devc->beaglelogic->close(devc);
204
205 return SR_OK;
206}
207
208static void clear_helper(struct dev_context *devc)
209{
210 g_free(devc->tcp_buffer);
211 g_free(devc->address);
212 g_free(devc->port);
213}
214
215static int dev_clear(const struct sr_dev_driver *di)
216{
217 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
218}
219
220static int config_get(uint32_t key, GVariant **data,
221 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
222{
223 struct dev_context *devc = sdi->priv;
224
225 (void)cg;
226
227 switch (key) {
228 case SR_CONF_LIMIT_SAMPLES:
229 *data = g_variant_new_uint64(devc->limit_samples);
230 break;
231 case SR_CONF_SAMPLERATE:
232 *data = g_variant_new_uint64(devc->cur_samplerate);
233 break;
234 case SR_CONF_CAPTURE_RATIO:
235 *data = g_variant_new_uint64(devc->capture_ratio);
236 break;
237 case SR_CONF_NUM_LOGIC_CHANNELS:
238 *data = g_variant_new_uint32(g_slist_length(sdi->channels));
239 break;
240 default:
241 return SR_ERR_NA;
242 }
243
244 return SR_OK;
245}
246
247static int config_set(uint32_t key, GVariant *data,
248 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
249{
250 struct dev_context *devc = sdi->priv;
251 uint64_t tmp_u64;
252
253 (void)cg;
254
255 switch (key) {
256 case SR_CONF_SAMPLERATE:
257 devc->cur_samplerate = g_variant_get_uint64(data);
258 return devc->beaglelogic->set_samplerate(devc);
259 case SR_CONF_LIMIT_SAMPLES:
260 tmp_u64 = g_variant_get_uint64(data);
261 devc->limit_samples = tmp_u64;
262 devc->triggerflags = BL_TRIGGERFLAGS_ONESHOT;
263
264 /* Check if we have sufficient buffer size */
265 tmp_u64 *= SAMPLEUNIT_TO_BYTES(devc->sampleunit);
266 if (tmp_u64 > devc->buffersize) {
267 sr_warn("Insufficient buffer space has been allocated.");
268 sr_warn("Please use \'echo <size in bytes> > "\
269 BEAGLELOGIC_SYSFS_ATTR(memalloc) \
270 "\' to increase the buffer size, this"\
271 " capture is now truncated to %d Msamples",
272 devc->buffersize /
273 (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000));
274 }
275 return devc->beaglelogic->set_triggerflags(devc);
276 case SR_CONF_CAPTURE_RATIO:
277 devc->capture_ratio = g_variant_get_uint64(data);
278 break;
279 default:
280 return SR_ERR_NA;
281 }
282
283 return SR_OK;
284}
285
286static int config_list(uint32_t key, GVariant **data,
287 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
288{
289 switch (key) {
290 case SR_CONF_SCAN_OPTIONS:
291 case SR_CONF_DEVICE_OPTIONS:
292 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
293 case SR_CONF_SAMPLERATE:
294 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
295 break;
296 case SR_CONF_TRIGGER_MATCH:
297 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
298 break;
299 default:
300 return SR_ERR_NA;
301 }
302
303 return SR_OK;
304}
305
306/* get a sane timeout for poll() */
307#define BUFUNIT_TIMEOUT_MS(devc) (100 + ((devc->bufunitsize * 1000) / \
308 (uint32_t)(devc->cur_samplerate)))
309
310static int dev_acquisition_start(const struct sr_dev_inst *sdi)
311{
312 struct dev_context *devc = sdi->priv;
313 GSList *l;
314 struct sr_trigger *trigger;
315 struct sr_channel *channel;
316
317 /* Clear capture state */
318 devc->bytes_read = 0;
319 devc->offset = 0;
320
321 /* Configure channels */
322 devc->sampleunit = BL_SAMPLEUNIT_8_BITS;
323
324 for (l = sdi->channels; l; l = l->next) {
325 channel = l->data;
326 if (channel->index >= 8 && channel->enabled)
327 devc->sampleunit = BL_SAMPLEUNIT_16_BITS;
328 }
329 devc->beaglelogic->set_sampleunit(devc);
330
331 /* If continuous sampling, set the limit_samples to max possible value */
332 if (devc->triggerflags == BL_TRIGGERFLAGS_CONTINUOUS)
333 devc->limit_samples = UINT64_MAX;
334
335 /* Configure triggers & send header packet */
336 if ((trigger = sr_session_trigger_get(sdi->session))) {
337 int pre_trigger_samples = 0;
338 if (devc->limit_samples > 0)
339 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
340 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
341 if (!devc->stl)
342 return SR_ERR_MALLOC;
343 devc->trigger_fired = FALSE;
344 } else
345 devc->trigger_fired = TRUE;
346 std_session_send_df_header(sdi);
347
348 /* Trigger and add poll on file */
349 devc->beaglelogic->start(devc);
350 if (devc->beaglelogic == &beaglelogic_native_ops)
351 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
352 BUFUNIT_TIMEOUT_MS(devc), beaglelogic_native_receive_data,
353 (void *)sdi);
354 else
355 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
356 BUFUNIT_TIMEOUT_MS(devc), beaglelogic_tcp_receive_data,
357 (void *)sdi);
358
359 return SR_OK;
360}
361
362static int dev_acquisition_stop(struct sr_dev_inst *sdi)
363{
364 struct dev_context *devc = sdi->priv;
365
366 /* Execute a stop on BeagleLogic */
367 devc->beaglelogic->stop(devc);
368
369 /* Flush the cache */
370 if (devc->beaglelogic == &beaglelogic_native_ops)
371 lseek(devc->fd, 0, SEEK_SET);
372 else
373 beaglelogic_tcp_drain(devc);
374
375 /* Remove session source and send EOT packet */
376 sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
377 std_session_send_df_end(sdi);
378
379 return SR_OK;
380}
381
382static struct sr_dev_driver beaglelogic_driver_info = {
383 .name = "beaglelogic",
384 .longname = "BeagleLogic",
385 .api_version = 1,
386 .init = std_init,
387 .cleanup = std_cleanup,
388 .scan = scan,
389 .dev_list = std_dev_list,
390 .dev_clear = dev_clear,
391 .config_get = config_get,
392 .config_set = config_set,
393 .config_list = config_list,
394 .dev_open = dev_open,
395 .dev_close = dev_close,
396 .dev_acquisition_start = dev_acquisition_start,
397 .dev_acquisition_stop = dev_acquisition_stop,
398 .context = NULL,
399};
400SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info);