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