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 struct dev_context *beaglelogic_devc_alloc(void)
64 struct dev_context *devc;
66 devc = g_malloc0(sizeof(struct dev_context));
68 /* Default non-zero values (if any) */
70 devc->limit_samples = (uint64_t)10000000;
76 static GSList *scan(struct sr_dev_driver *di, GSList *options)
79 struct sr_config *src;
80 struct sr_dev_inst *sdi;
81 struct dev_context *devc;
82 const char *conn = NULL;
87 for (l = options; l; l = l->next) {
89 if (src->key == SR_CONF_NUM_LOGIC_CHANNELS)
90 maxch = g_variant_get_int32(src->data);
91 if (src->key == SR_CONF_CONN)
92 conn = g_variant_get_string(src->data, NULL);
95 /* Probe for /dev/beaglelogic if not connecting via TCP */
97 if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
100 params = g_strsplit(conn, "/", 0);
101 if (!params || !params[1] || !params[2]) {
102 sr_err("Invalid Parameters.");
106 if (g_ascii_strncasecmp(params[0], "tcp", 3)) {
107 sr_err("Only TCP (tcp-raw) protocol is currently supported.");
113 maxch = (maxch > 8) ? NUM_CHANNELS : 8;
115 sdi = g_new0(struct sr_dev_inst, 1);
116 sdi->status = SR_ST_INACTIVE;
117 sdi->model = g_strdup("BeagleLogic");
118 sdi->version = g_strdup("1.0");
120 devc = beaglelogic_devc_alloc();
123 devc->beaglelogic = &beaglelogic_native_ops;
124 sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);
126 devc->read_timeout = 1000 * 1000;
127 devc->beaglelogic = &beaglelogic_tcp_ops;
128 devc->address = g_strdup(params[1]);
129 devc->port = g_strdup(params[2]);
132 if (devc->beaglelogic->open(devc) != SR_OK)
134 if (beaglelogic_tcp_detect(devc) != SR_OK)
136 if (devc->beaglelogic->close(devc) != SR_OK)
138 sr_info("BeagleLogic device found at %s : %s",
139 devc->address, devc->port);
142 /* Fill the channels */
143 for (i = 0; i < maxch; i++)
144 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
149 return std_scan_complete(di, g_slist_append(NULL, sdi));
153 g_free(sdi->version);
154 g_free(devc->address);
162 static int dev_open(struct sr_dev_inst *sdi)
164 struct dev_context *devc = sdi->priv;
166 /* Open BeagleLogic */
167 if (devc->beaglelogic->open(devc))
170 /* Set fd and local attributes */
171 if (devc->beaglelogic == &beaglelogic_tcp_ops)
172 devc->pollfd.fd = devc->socket;
174 devc->pollfd.fd = devc->fd;
175 devc->pollfd.events = G_IO_IN;
176 devc->pollfd.revents = 0;
178 /* Get the default attributes */
179 devc->beaglelogic->get_samplerate(devc);
180 devc->beaglelogic->get_sampleunit(devc);
181 devc->beaglelogic->get_buffersize(devc);
182 devc->beaglelogic->get_bufunitsize(devc);
184 /* Set the triggerflags to default for continuous capture unless we
185 * explicitly limit samples using SR_CONF_LIMIT_SAMPLES */
186 devc->triggerflags = BL_TRIGGERFLAGS_CONTINUOUS;
187 devc->beaglelogic->set_triggerflags(devc);
189 /* Map the kernel capture FIFO for reads, saves 1 level of memcpy */
190 if (devc->beaglelogic == &beaglelogic_native_ops) {
191 if (devc->beaglelogic->mmap(devc) != SR_OK) {
192 sr_err("Unable to map capture buffer");
193 devc->beaglelogic->close(devc);
197 devc->tcp_buffer = g_malloc(TCP_BUFFER_SIZE);
203 static int dev_close(struct sr_dev_inst *sdi)
205 struct dev_context *devc = sdi->priv;
207 /* Close the memory mapping and the file */
208 if (devc->beaglelogic == &beaglelogic_native_ops)
209 devc->beaglelogic->munmap(devc);
210 devc->beaglelogic->close(devc);
215 static void clear_helper(struct dev_context *devc)
217 g_free(devc->tcp_buffer);
218 g_free(devc->address);
222 static int dev_clear(const struct sr_dev_driver *di)
224 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
227 static int config_get(uint32_t key, GVariant **data,
228 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
230 struct dev_context *devc = sdi->priv;
235 case SR_CONF_LIMIT_SAMPLES:
236 *data = g_variant_new_uint64(devc->limit_samples);
238 case SR_CONF_SAMPLERATE:
239 *data = g_variant_new_uint64(devc->cur_samplerate);
241 case SR_CONF_CAPTURE_RATIO:
242 *data = g_variant_new_uint64(devc->capture_ratio);
244 case SR_CONF_NUM_LOGIC_CHANNELS:
245 *data = g_variant_new_uint32(g_slist_length(sdi->channels));
254 static int config_set(uint32_t key, GVariant *data,
255 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
257 struct dev_context *devc = sdi->priv;
263 case SR_CONF_SAMPLERATE:
264 devc->cur_samplerate = g_variant_get_uint64(data);
265 return devc->beaglelogic->set_samplerate(devc);
266 case SR_CONF_LIMIT_SAMPLES:
267 tmp_u64 = g_variant_get_uint64(data);
268 devc->limit_samples = tmp_u64;
269 devc->triggerflags = BL_TRIGGERFLAGS_ONESHOT;
271 /* Check if we have sufficient buffer size */
272 tmp_u64 *= SAMPLEUNIT_TO_BYTES(devc->sampleunit);
273 if (tmp_u64 > devc->buffersize) {
274 sr_warn("Insufficient buffer space has been allocated.");
275 sr_warn("Please use \'echo <size in bytes> > "\
276 BEAGLELOGIC_SYSFS_ATTR(memalloc) \
277 "\' to increase the buffer size, this"\
278 " capture is now truncated to %d Msamples",
280 (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000));
282 return devc->beaglelogic->set_triggerflags(devc);
283 case SR_CONF_CAPTURE_RATIO:
284 devc->capture_ratio = g_variant_get_uint64(data);
293 static int config_list(uint32_t key, GVariant **data,
294 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
297 case SR_CONF_SCAN_OPTIONS:
298 case SR_CONF_DEVICE_OPTIONS:
299 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
300 case SR_CONF_SAMPLERATE:
301 *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates));
303 case SR_CONF_TRIGGER_MATCH:
304 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
313 /* get a sane timeout for poll() */
314 #define BUFUNIT_TIMEOUT_MS(devc) (100 + ((devc->bufunitsize * 1000) / \
315 (uint32_t)(devc->cur_samplerate)))
317 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
319 struct dev_context *devc = sdi->priv;
321 struct sr_trigger *trigger;
322 struct sr_channel *channel;
324 /* Clear capture state */
325 devc->bytes_read = 0;
328 /* Configure channels */
329 devc->sampleunit = BL_SAMPLEUNIT_8_BITS;
331 for (l = sdi->channels; l; l = l->next) {
333 if (channel->index >= 8 && channel->enabled)
334 devc->sampleunit = BL_SAMPLEUNIT_16_BITS;
336 devc->beaglelogic->set_sampleunit(devc);
338 /* If continuous sampling, set the limit_samples to max possible value */
339 if (devc->triggerflags == BL_TRIGGERFLAGS_CONTINUOUS)
340 devc->limit_samples = (uint64_t)-1;
342 /* Configure triggers & send header packet */
343 if ((trigger = sr_session_trigger_get(sdi->session))) {
344 int pre_trigger_samples = 0;
345 if (devc->limit_samples > 0)
346 pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100;
347 devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples);
349 return SR_ERR_MALLOC;
350 devc->trigger_fired = FALSE;
352 devc->trigger_fired = TRUE;
353 std_session_send_df_header(sdi);
355 /* Trigger and add poll on file */
356 devc->beaglelogic->start(devc);
357 if (devc->beaglelogic == &beaglelogic_native_ops)
358 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
359 BUFUNIT_TIMEOUT_MS(devc), beaglelogic_native_receive_data,
362 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
363 BUFUNIT_TIMEOUT_MS(devc), beaglelogic_tcp_receive_data,
369 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
371 struct dev_context *devc = sdi->priv;
373 /* Execute a stop on BeagleLogic */
374 devc->beaglelogic->stop(devc);
376 /* Flush the cache */
377 if (devc->beaglelogic == &beaglelogic_native_ops)
378 lseek(devc->fd, 0, SEEK_SET);
380 beaglelogic_tcp_drain(devc);
382 /* Remove session source and send EOT packet */
383 sr_session_source_remove_pollfd(sdi->session, &devc->pollfd);
384 std_session_send_df_end(sdi);
389 static struct sr_dev_driver beaglelogic_driver_info = {
390 .name = "beaglelogic",
391 .longname = "BeagleLogic",
394 .cleanup = std_cleanup,
396 .dev_list = std_dev_list,
397 .dev_clear = dev_clear,
398 .config_get = config_get,
399 .config_set = config_set,
400 .config_list = config_list,
401 .dev_open = dev_open,
402 .dev_close = dev_close,
403 .dev_acquisition_start = dev_acquisition_start,
404 .dev_acquisition_stop = dev_acquisition_stop,
407 SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info);