]> sigrok.org Git - libsigrok.git/blob - src/hardware/beaglelogic/api.c
beaglelogic: Fix a compiler warning.
[libsigrok.git] / src / hardware / beaglelogic / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014-17 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
24 static const uint32_t scanopts[] = {
25         SR_CONF_CONN,
26         SR_CONF_NUM_LOGIC_CHANNELS,
27 };
28
29 static const uint32_t drvopts[] = {
30         SR_CONF_LOGIC_ANALYZER,
31 };
32
33 static 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
42 static 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
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",
53 };
54
55 /* Possible sample rates : 10 Hz to 100 MHz = (100 / x) MHz */
56 static const uint64_t samplerates[] = {
57         SR_HZ(10),
58         SR_MHZ(100),
59         SR_HZ(1),
60 };
61
62 static 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 = NULL;
69         gchar **params;
70         int i, maxch;
71
72         maxch = NUM_CHANNELS;
73         for (l = options; l; l = l->next) {
74                 src = l->data;
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);
79         }
80
81         /* Probe for /dev/beaglelogic if not connecting via TCP */
82         if (!conn) {
83                 if (!g_file_test(BEAGLELOGIC_DEV_NODE, G_FILE_TEST_EXISTS))
84                         return NULL;
85         } else {
86                 params = g_strsplit(conn, "/", 0);
87                 if (!params || !params[1] || !params[2]) {
88                         sr_err("Invalid Parameters.");
89                         g_strfreev(params);
90                         return NULL;
91                 }
92                 if (g_ascii_strncasecmp(params[0], "tcp", 3)) {
93                         sr_err("Only TCP (tcp-raw) protocol is currently supported.");
94                         g_strfreev(params);
95                         return NULL;
96                 }
97         }
98
99         maxch = (maxch > 8) ? NUM_CHANNELS : 8;
100
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");
105
106         devc = g_malloc0(sizeof(struct dev_context));
107
108         /* Default non-zero values (if any) */
109         devc->fd = -1;
110         devc->limit_samples = (uint64_t)10000000;
111         devc->tcp_buffer = 0;
112
113         if (!conn) {
114                 devc->beaglelogic = &beaglelogic_native_ops;
115                 sr_info("BeagleLogic device found at "BEAGLELOGIC_DEV_NODE);
116         } else {
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]);
121                 g_strfreev(params);
122
123                 if (devc->beaglelogic->open(devc) != SR_OK)
124                         goto err_free;
125                 if (beaglelogic_tcp_detect(devc) != SR_OK)
126                         goto err_free;
127                 if (devc->beaglelogic->close(devc) != SR_OK)
128                         goto err_free;
129                 sr_info("BeagleLogic device found at %s : %s",
130                         devc->address, devc->port);
131         }
132
133         /* Fill the channels */
134         for (i = 0; i < maxch; i++)
135                 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE,
136                                 channel_names[i]);
137
138         sdi->priv = devc;
139
140         return std_scan_complete(di, g_slist_append(NULL, sdi));
141
142 err_free:
143         g_free(sdi->model);
144         g_free(sdi->version);
145         g_free(devc->address);
146         g_free(devc->port);
147         g_free(devc);
148         g_free(sdi);
149
150         return NULL;
151 }
152
153 static int dev_open(struct sr_dev_inst *sdi)
154 {
155         struct dev_context *devc = sdi->priv;
156
157         /* Open BeagleLogic */
158         if (devc->beaglelogic->open(devc))
159                 return SR_ERR;
160
161         /* Set fd and local attributes */
162         if (devc->beaglelogic == &beaglelogic_tcp_ops)
163                 devc->pollfd.fd = devc->socket;
164         else
165                 devc->pollfd.fd = devc->fd;
166         devc->pollfd.events = G_IO_IN;
167         devc->pollfd.revents = 0;
168
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);
174
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);
179
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);
185                         return SR_ERR;
186                 }
187         } else {
188                 devc->tcp_buffer = g_malloc(TCP_BUFFER_SIZE);
189         }
190
191         return SR_OK;
192 }
193
194 static int dev_close(struct sr_dev_inst *sdi)
195 {
196         struct dev_context *devc = sdi->priv;
197
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);
202
203         return SR_OK;
204 }
205
206 static void clear_helper(struct dev_context *devc)
207 {
208         g_free(devc->tcp_buffer);
209         g_free(devc->address);
210         g_free(devc->port);
211 }
212
213 static int dev_clear(const struct sr_dev_driver *di)
214 {
215         return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
216 }
217
218 static int config_get(uint32_t key, GVariant **data,
219         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
220 {
221         struct dev_context *devc = sdi->priv;
222
223         (void)cg;
224
225         switch (key) {
226         case SR_CONF_LIMIT_SAMPLES:
227                 *data = g_variant_new_uint64(devc->limit_samples);
228                 break;
229         case SR_CONF_SAMPLERATE:
230                 *data = g_variant_new_uint64(devc->cur_samplerate);
231                 break;
232         case SR_CONF_CAPTURE_RATIO:
233                 *data = g_variant_new_uint64(devc->capture_ratio);
234                 break;
235         case SR_CONF_NUM_LOGIC_CHANNELS:
236                 *data = g_variant_new_uint32(g_slist_length(sdi->channels));
237                 break;
238         default:
239                 return SR_ERR_NA;
240         }
241
242         return SR_OK;
243 }
244
245 static int config_set(uint32_t key, GVariant *data,
246         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
247 {
248         struct dev_context *devc = sdi->priv;
249         uint64_t tmp_u64;
250
251         (void)cg;
252
253         switch (key) {
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;
261
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",
270                                 devc->buffersize /
271                                 (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000));
272                 }
273                 return devc->beaglelogic->set_triggerflags(devc);
274         case SR_CONF_CAPTURE_RATIO:
275                 devc->capture_ratio = g_variant_get_uint64(data);
276                 break;
277         default:
278                 return SR_ERR_NA;
279         }
280
281         return SR_OK;
282 }
283
284 static int config_list(uint32_t key, GVariant **data,
285         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
286 {
287         switch (key) {
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));
293                 break;
294         case SR_CONF_TRIGGER_MATCH:
295                 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
296                 break;
297         default:
298                 return SR_ERR_NA;
299         }
300
301         return SR_OK;
302 }
303
304 /* get a sane timeout for poll() */
305 #define BUFUNIT_TIMEOUT_MS(devc)        (100 + ((devc->bufunitsize * 1000) / \
306                                 (uint32_t)(devc->cur_samplerate)))
307
308 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
309 {
310         struct dev_context *devc = sdi->priv;
311         GSList *l;
312         struct sr_trigger *trigger;
313         struct sr_channel *channel;
314
315         /* Clear capture state */
316         devc->bytes_read = 0;
317         devc->offset = 0;
318
319         /* Configure channels */
320         devc->sampleunit = BL_SAMPLEUNIT_8_BITS;
321
322         for (l = sdi->channels; l; l = l->next) {
323                 channel = l->data;
324                 if (channel->index >= 8 && channel->enabled)
325                         devc->sampleunit = BL_SAMPLEUNIT_16_BITS;
326         }
327         devc->beaglelogic->set_sampleunit(devc);
328
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;
332
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);
339                 if (!devc->stl)
340                         return SR_ERR_MALLOC;
341                 devc->trigger_fired = FALSE;
342         } else
343                 devc->trigger_fired = TRUE;
344         std_session_send_df_header(sdi);
345
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,
351                         (void *)sdi);
352         else
353                 sr_session_source_add_pollfd(sdi->session, &devc->pollfd,
354                         BUFUNIT_TIMEOUT_MS(devc), beaglelogic_tcp_receive_data,
355                         (void *)sdi);
356
357         return SR_OK;
358 }
359
360 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
361 {
362         struct dev_context *devc = sdi->priv;
363
364         /* Execute a stop on BeagleLogic */
365         devc->beaglelogic->stop(devc);
366
367         /* Flush the cache */
368         if (devc->beaglelogic == &beaglelogic_native_ops)
369                 lseek(devc->fd, 0, SEEK_SET);
370         else
371                 beaglelogic_tcp_drain(devc);
372
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);
376
377         return SR_OK;
378 }
379
380 static struct sr_dev_driver beaglelogic_driver_info = {
381         .name = "beaglelogic",
382         .longname = "BeagleLogic",
383         .api_version = 1,
384         .init = std_init,
385         .cleanup = std_cleanup,
386         .scan = scan,
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,
396         .context = NULL,
397 };
398 SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info);