2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014-17 Kumar Abhishek <abhishek@theembeddedkitchen.net>
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.
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, see <http://www.gnu.org/licenses/>.
22 #include "beaglelogic.h"
24 static const uint32_t scanopts[] = {
26 SR_CONF_NUM_LOGIC_CHANNELS,
29 static const uint32_t drvopts[] = {
30 SR_CONF_LOGIC_ANALYZER,
33 static const uint32_t devopts[] = {
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,
42 static const int32_t trigger_matches[] = {
50 SR_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",
55 /* Possible sample rates : 10 Hz to 100 MHz = (100 / x) MHz */
56 static const uint64_t samplerates[] = {
62 static GSList *scan(struct sr_dev_driver *di, GSList *options)
65 struct sr_config *src;
66 struct sr_dev_inst *sdi;
67 struct dev_context *devc;
68 const char *conn = NULL;
73 for (l = options; l; l = l->next) {
75 if (src->key == SR_CONF_NUM_LOGIC_CHANNELS)
76 maxch = g_variant_get_int32(src->data);
77 if (src->key == SR_CONF_CONN)
78 conn = g_variant_get_string(src->data, NULL);
81 /* Probe for /dev/beaglelogic if not connecting via TCP */
83 if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
86 params = g_strsplit(conn, "/", 0);
87 if (!params || !params[1] || !params[2]) {
88 sr_err("Invalid Parameters.");
92 if (g_ascii_strncasecmp(params[0], "tcp", 3)) {
93 sr_err("Only TCP (tcp-raw) protocol is currently supported.");
99 maxch = (maxch > 8) ? NUM_CHANNELS : 8;
101 sdi = g_new0(struct sr_dev_inst, 1);
102 sdi->status = SR_ST_INACTIVE;
103 sdi->model = g_strdup("BeagleLogic");
104 sdi->version = g_strdup("1.0");
106 devc = g_malloc0(sizeof(struct dev_context));
108 /* Default non-zero values (if any) */
110 devc->limit_samples = (uint64_t)10000000;
111 devc->tcp_buffer = 0;
114 devc->beaglelogic = &beaglelogic_native_ops;
115 sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);
117 devc->read_timeout = 1000 * 1000;
118 devc->beaglelogic = &beaglelogic_tcp_ops;
119 devc->address = g_strdup(params[1]);
120 devc->port = g_strdup(params[2]);
123 if (devc->beaglelogic->open(devc) != SR_OK)
125 if (beaglelogic_tcp_detect(devc) != SR_OK)
127 if (devc->beaglelogic->close(devc) != SR_OK)
129 sr_info("BeagleLogic device found at %s : %s",
130 devc->address, devc->port);
133 /* Fill the channels */
134 for (i = 0; i < maxch; i++)
135 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
140 return std_scan_complete(di, g_slist_append(NULL, sdi));
144 g_free(sdi->version);
145 g_free(devc->address);
153 static int dev_open(struct sr_dev_inst *sdi)
155 struct dev_context *devc = sdi->priv;
157 /* Open BeagleLogic */
158 if (devc->beaglelogic->open(devc))
161 /* Set fd and local attributes */
162 if (devc->beaglelogic == &beaglelogic_tcp_ops)
163 devc->pollfd.fd = devc->socket;
165 devc->pollfd.fd = devc->fd;
166 devc->pollfd.events = G_IO_IN;
167 devc->pollfd.revents = 0;
169 /* Get the default attributes */
170 devc->beaglelogic->get_samplerate(devc);
171 devc->beaglelogic->get_sampleunit(devc);
172 devc->beaglelogic->get_buffersize(devc);
173 devc->beaglelogic->get_bufunitsize(devc);
175 /* Set the triggerflags to default for continuous capture unless we
176 * explicitly limit samples using SR_CONF_LIMIT_SAMPLES */
177 devc->triggerflags = BL_TRIGGERFLAGS_CONTINUOUS;
178 devc->beaglelogic->set_triggerflags(devc);
180 /* Map the kernel capture FIFO for reads, saves 1 level of memcpy */
181 if (devc->beaglelogic == &beaglelogic_native_ops) {
182 if (devc->beaglelogic->mmap(devc) != SR_OK) {
183 sr_err("Unable to map capture buffer");
184 devc->beaglelogic->close(devc);
188 devc->tcp_buffer = g_malloc(TCP_BUFFER_SIZE);
194 static int dev_close(struct sr_dev_inst *sdi)
196 struct dev_context *devc = sdi->priv;
198 /* Close the memory mapping and the file */
199 if (devc->beaglelogic == &beaglelogic_native_ops)
200 devc->beaglelogic->munmap(devc);
201 devc->beaglelogic->close(devc);
206 static void clear_helper(struct dev_context *devc)
208 g_free(devc->tcp_buffer);
209 g_free(devc->address);
213 static int dev_clear(const struct sr_dev_driver *di)
215 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
218 static int config_get(uint32_t key, GVariant **data,
219 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
221 struct dev_context *devc = sdi->priv;
226 case SR_CONF_LIMIT_SAMPLES:
227 *data = g_variant_new_uint64(devc->limit_samples);
229 case SR_CONF_SAMPLERATE:
230 *data = g_variant_new_uint64(devc->cur_samplerate);
232 case SR_CONF_CAPTURE_RATIO:
233 *data = g_variant_new_uint64(devc->capture_ratio);
235 case SR_CONF_NUM_LOGIC_CHANNELS:
236 *data = g_variant_new_uint32(g_slist_length(sdi->channels));
245 static int config_set(uint32_t key, GVariant *data,
246 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
248 struct dev_context *devc = sdi->priv;
254 case SR_CONF_SAMPLERATE:
255 devc->cur_samplerate = g_variant_get_uint64(data);
256 return devc->beaglelogic->set_samplerate(devc);
257 case SR_CONF_LIMIT_SAMPLES:
258 tmp_u64 = g_variant_get_uint64(data);
259 devc->limit_samples = tmp_u64;
260 devc->triggerflags = BL_TRIGGERFLAGS_ONESHOT;
262 /* Check if we have sufficient buffer size */
263 tmp_u64 *= SAMPLEUNIT_TO_BYTES(devc->sampleunit);
264 if (tmp_u64 > devc->buffersize) {
265 sr_warn("Insufficient buffer space has been allocated.");
266 sr_warn("Please use \'echo <size in bytes> > "\
267 BEAGLELOGIC_SYSFS_ATTR(memalloc) \
268 "\' to increase the buffer size, this"\
269 " capture is now truncated to %d Msamples",
271 (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000));
273 return devc->beaglelogic->set_triggerflags(devc);
274 case SR_CONF_CAPTURE_RATIO:
275 devc->capture_ratio = g_variant_get_uint64(data);
284 static int config_list(uint32_t key, GVariant **data,
285 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
288 case SR_CONF_SCAN_OPTIONS:
289 case SR_CONF_DEVICE_OPTIONS:
290 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
291 case SR_CONF_SAMPLERATE:
292 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
294 case SR_CONF_TRIGGER_MATCH:
295 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
304 /* get a sane timeout for poll() */
305 #define BUFUNIT_TIMEOUT_MS(devc) (100 + ((devc->bufunitsize * 1000) / \
306 (uint32_t)(devc->cur_samplerate)))
308 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
310 struct dev_context *devc = sdi->priv;
312 struct sr_trigger *trigger;
313 struct sr_channel *channel;
315 /* Clear capture state */
316 devc->bytes_read = 0;
319 /* Configure channels */
320 devc->sampleunit = BL_SAMPLEUNIT_8_BITS;
322 for (l = sdi->channels; l; l = l->next) {
324 if (channel->index >= 8 && channel->enabled)
325 devc->sampleunit = BL_SAMPLEUNIT_16_BITS;
327 devc->beaglelogic->set_sampleunit(devc);
329 /* If continuous sampling, set the limit_samples to max possible value */
330 if (devc->triggerflags == BL_TRIGGERFLAGS_CONTINUOUS)
331 devc->limit_samples = (uint64_t)-1;
333 /* Configure triggers & send header packet */
334 if ((trigger = sr_session_trigger_get(sdi->session))) {
335 int pre_trigger_samples = 0;
336 if (devc->limit_samples > 0)
337 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
338 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
340 return SR_ERR_MALLOC;
341 devc->trigger_fired = FALSE;
343 devc->trigger_fired = TRUE;
344 std_session_send_df_header(sdi);
346 /* Trigger and add poll on file */
347 devc->beaglelogic->start(devc);
348 if (devc->beaglelogic == &beaglelogic_native_ops)
349 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
350 BUFUNIT_TIMEOUT_MS(devc), beaglelogic_native_receive_data,
353 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
354 BUFUNIT_TIMEOUT_MS(devc), beaglelogic_tcp_receive_data,
360 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
362 struct dev_context *devc = sdi->priv;
364 /* Execute a stop on BeagleLogic */
365 devc->beaglelogic->stop(devc);
367 /* Flush the cache */
368 if (devc->beaglelogic == &beaglelogic_native_ops)
369 lseek(devc->fd, 0, SEEK_SET);
371 beaglelogic_tcp_drain(devc);
373 /* Remove session source and send EOT packet */
374 sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
375 std_session_send_df_end(sdi);
380 static struct sr_dev_driver beaglelogic_driver_info = {
381 .name = "beaglelogic",
382 .longname = "BeagleLogic",
385 .cleanup = std_cleanup,
387 .dev_list = std_dev_list,
388 .dev_clear = dev_clear,
389 .config_get = config_get,
390 .config_set = config_set,
391 .config_list = config_list,
392 .dev_open = dev_open,
393 .dev_close = dev_close,
394 .dev_acquisition_start = dev_acquisition_start,
395 .dev_acquisition_stop = dev_acquisition_stop,
398 SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info);