]>
Commit | Line | Data |
---|---|---|
bb993797 KA |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
3124d3bc | 4 | * Copyright (C) 2014-17 Kumar Abhishek <abhishek@theembeddedkitchen.net> |
bb993797 KA |
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 | ||
6ec6c43b | 20 | #include <config.h> |
bb993797 | 21 | #include "protocol.h" |
ad9dbc1c | 22 | #include "beaglelogic.h" |
bb993797 | 23 | |
138589b0 | 24 | static const uint32_t scanopts[] = { |
9b2b3ef9 | 25 | SR_CONF_CONN, |
138589b0 BV |
26 | SR_CONF_NUM_LOGIC_CHANNELS, |
27 | }; | |
28 | ||
05199c0a UH |
29 | static const uint32_t drvopts[] = { |
30 | SR_CONF_LOGIC_ANALYZER, | |
31 | }; | |
32 | ||
f254bc4b | 33 | static const uint32_t devopts[] = { |
ad9dbc1c | 34 | SR_CONF_CONTINUOUS, |
5827f61b | 35 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
f955ffe8 | 36 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
5827f61b | 37 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, |
e743a47d | 38 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, |
5827f61b | 39 | SR_CONF_NUM_LOGIC_CHANNELS | SR_CONF_GET, |
ad9dbc1c KA |
40 | }; |
41 | ||
76d10d13 | 42 | static const int32_t trigger_matches[] = { |
ad9dbc1c KA |
43 | SR_TRIGGER_ZERO, |
44 | SR_TRIGGER_ONE, | |
45 | SR_TRIGGER_RISING, | |
46 | SR_TRIGGER_FALLING, | |
47 | SR_TRIGGER_EDGE, | |
48 | }; | |
49 | ||
0f34cb47 UH |
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", | |
ad9dbc1c KA |
53 | }; |
54 | ||
55 | /* Possible sample rates : 10 Hz to 100 MHz = (100 / x) MHz */ | |
56 | static const uint64_t samplerates[] = { | |
138589b0 BV |
57 | SR_HZ(10), |
58 | SR_MHZ(100), | |
59 | SR_HZ(1), | |
ad9dbc1c KA |
60 | }; |
61 | ||
f57d8ffe | 62 | static struct dev_context *beaglelogic_devc_alloc(void) |
ad9dbc1c KA |
63 | { |
64 | struct dev_context *devc; | |
65 | ||
f57d8ffe | 66 | devc = g_malloc0(sizeof(struct dev_context)); |
ad9dbc1c KA |
67 | |
68 | /* Default non-zero values (if any) */ | |
69 | devc->fd = -1; | |
f955ffe8 | 70 | devc->limit_samples = (uint64_t)10000000; |
9b2b3ef9 | 71 | devc->tcp_buffer = 0; |
ad9dbc1c KA |
72 | |
73 | return devc; | |
74 | } | |
75 | ||
4f840ce9 | 76 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
bb993797 | 77 | { |
43376f33 | 78 | GSList *l; |
ad9dbc1c KA |
79 | struct sr_config *src; |
80 | struct sr_dev_inst *sdi; | |
81 | struct dev_context *devc; | |
9b2b3ef9 KA |
82 | const char *conn = NULL; |
83 | gchar **params; | |
ad9dbc1c | 84 | int i, maxch; |
bb993797 | 85 | |
6eab3021 | 86 | maxch = NUM_CHANNELS; |
ad9dbc1c KA |
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); | |
9b2b3ef9 KA |
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 | } | |
ad9dbc1c KA |
111 | } |
112 | ||
6eab3021 | 113 | if (maxch > 8) |
ad9dbc1c | 114 | maxch = NUM_CHANNELS; |
6eab3021 | 115 | else |
ad9dbc1c | 116 | maxch = 8; |
ad9dbc1c | 117 | |
6eab3021 KA |
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"); | |
ad9dbc1c | 122 | |
6eab3021 | 123 | devc = beaglelogic_devc_alloc(); |
ad9dbc1c | 124 | |
9b2b3ef9 KA |
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; | |
a486fca9 KA |
139 | if (devc->beaglelogic->close(devc) != SR_OK) |
140 | goto err_free; | |
9b2b3ef9 KA |
141 | sr_info("BeagleLogic device found at %s : %s", |
142 | devc->address, devc->port); | |
143 | } | |
0bca2e75 | 144 | /* Fill the channels */ |
5e23fcab ML |
145 | for (i = 0; i < maxch; i++) |
146 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, | |
0f34cb47 | 147 | channel_names[i]); |
ad9dbc1c KA |
148 | |
149 | sdi->priv = devc; | |
bb993797 | 150 | |
43376f33 | 151 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
9b2b3ef9 KA |
152 | err_free: |
153 | g_free(sdi->model); | |
154 | g_free(sdi->version); | |
d6f20d03 KA |
155 | g_free(devc->address); |
156 | g_free(devc->port); | |
9b2b3ef9 KA |
157 | g_free(devc); |
158 | g_free(sdi); | |
159 | ||
160 | return NULL; | |
bb993797 KA |
161 | } |
162 | ||
bb993797 KA |
163 | static int dev_open(struct sr_dev_inst *sdi) |
164 | { | |
ad9dbc1c KA |
165 | struct dev_context *devc = sdi->priv; |
166 | ||
167 | /* Open BeagleLogic */ | |
a486fca9 KA |
168 | if (devc->beaglelogic->open(devc)) |
169 | return SR_ERR; | |
ad9dbc1c KA |
170 | |
171 | /* Set fd and local attributes */ | |
9b2b3ef9 KA |
172 | if (devc->beaglelogic == &beaglelogic_tcp_ops) |
173 | devc->pollfd.fd = devc->socket; | |
174 | else | |
175 | devc->pollfd.fd = devc->fd; | |
ad9dbc1c | 176 | devc->pollfd.events = G_IO_IN; |
89efe064 | 177 | devc->pollfd.revents = 0; |
ad9dbc1c KA |
178 | |
179 | /* Get the default attributes */ | |
0bca2e75 KA |
180 | devc->beaglelogic->get_samplerate(devc); |
181 | devc->beaglelogic->get_sampleunit(devc); | |
182 | devc->beaglelogic->get_buffersize(devc); | |
183 | devc->beaglelogic->get_bufunitsize(devc); | |
ad9dbc1c | 184 | |
713f3f84 KA |
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; | |
0bca2e75 | 188 | devc->beaglelogic->set_triggerflags(devc); |
713f3f84 | 189 | |
ad9dbc1c | 190 | /* Map the kernel capture FIFO for reads, saves 1 level of memcpy */ |
9b2b3ef9 KA |
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); | |
ad9dbc1c | 199 | } |
bb993797 | 200 | |
bb993797 KA |
201 | return SR_OK; |
202 | } | |
203 | ||
204 | static int dev_close(struct sr_dev_inst *sdi) | |
205 | { | |
ad9dbc1c | 206 | struct dev_context *devc = sdi->priv; |
bb993797 | 207 | |
093e1cba | 208 | /* Close the memory mapping and the file */ |
9b2b3ef9 KA |
209 | if (devc->beaglelogic == &beaglelogic_native_ops) |
210 | devc->beaglelogic->munmap(devc); | |
0bca2e75 | 211 | devc->beaglelogic->close(devc); |
093e1cba | 212 | |
d6f20d03 KA |
213 | return SR_OK; |
214 | } | |
215 | ||
216 | static void clear_helper(struct dev_context *devc) | |
217 | { | |
9b2b3ef9 KA |
218 | if (devc->tcp_buffer) |
219 | g_free(devc->tcp_buffer); | |
220 | ||
d6f20d03 KA |
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); | |
bb993797 KA |
231 | } |
232 | ||
dd7a72ea UH |
233 | static int config_get(uint32_t key, GVariant **data, |
234 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
bb993797 | 235 | { |
ad9dbc1c | 236 | struct dev_context *devc = sdi->priv; |
dcd438ee | 237 | |
bb993797 KA |
238 | (void)cg; |
239 | ||
bb993797 | 240 | switch (key) { |
ad9dbc1c KA |
241 | case SR_CONF_LIMIT_SAMPLES: |
242 | *data = g_variant_new_uint64(devc->limit_samples); | |
243 | break; | |
ad9dbc1c KA |
244 | case SR_CONF_SAMPLERATE: |
245 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
246 | break; | |
e743a47d AJ |
247 | case SR_CONF_CAPTURE_RATIO: |
248 | *data = g_variant_new_uint64(devc->capture_ratio); | |
249 | break; | |
ad9dbc1c KA |
250 | case SR_CONF_NUM_LOGIC_CHANNELS: |
251 | *data = g_variant_new_uint32(g_slist_length(sdi->channels)); | |
252 | break; | |
bb993797 KA |
253 | default: |
254 | return SR_ERR_NA; | |
255 | } | |
256 | ||
ad9dbc1c | 257 | return SR_OK; |
bb993797 KA |
258 | } |
259 | ||
dd7a72ea UH |
260 | static int config_set(uint32_t key, GVariant *data, |
261 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
bb993797 | 262 | { |
ad9dbc1c KA |
263 | struct dev_context *devc = sdi->priv; |
264 | uint64_t tmp_u64; | |
dcd438ee | 265 | |
bb993797 KA |
266 | (void)cg; |
267 | ||
bb993797 | 268 | switch (key) { |
ad9dbc1c KA |
269 | case SR_CONF_SAMPLERATE: |
270 | devc->cur_samplerate = g_variant_get_uint64(data); | |
0bca2e75 | 271 | return devc->beaglelogic->set_samplerate(devc); |
ad9dbc1c KA |
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) \ | |
55f26c42 | 283 | "\' to increase the buffer size, this"\ |
ad9dbc1c KA |
284 | " capture is now truncated to %d Msamples", |
285 | devc->buffersize / | |
286 | (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000)); | |
287 | } | |
0bca2e75 | 288 | return devc->beaglelogic->set_triggerflags(devc); |
e743a47d AJ |
289 | case SR_CONF_CAPTURE_RATIO: |
290 | devc->capture_ratio = g_variant_get_uint64(data); | |
efad7ccc | 291 | break; |
bb993797 | 292 | default: |
ad9dbc1c | 293 | return SR_ERR_NA; |
bb993797 KA |
294 | } |
295 | ||
ad9dbc1c | 296 | return SR_OK; |
bb993797 KA |
297 | } |
298 | ||
dd7a72ea UH |
299 | static int config_list(uint32_t key, GVariant **data, |
300 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
bb993797 | 301 | { |
bb993797 | 302 | switch (key) { |
138589b0 | 303 | case SR_CONF_SCAN_OPTIONS: |
ad9dbc1c | 304 | case SR_CONF_DEVICE_OPTIONS: |
05199c0a | 305 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
ad9dbc1c | 306 | case SR_CONF_SAMPLERATE: |
53012da6 | 307 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); |
ad9dbc1c KA |
308 | break; |
309 | case SR_CONF_TRIGGER_MATCH: | |
76d10d13 | 310 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); |
ad9dbc1c | 311 | break; |
bb993797 KA |
312 | default: |
313 | return SR_ERR_NA; | |
314 | } | |
315 | ||
a9010323 | 316 | return SR_OK; |
bb993797 KA |
317 | } |
318 | ||
ad9dbc1c KA |
319 | /* get a sane timeout for poll() */ |
320 | #define BUFUNIT_TIMEOUT_MS(devc) (100 + ((devc->bufunitsize * 1000) / \ | |
321 | (uint32_t)(devc->cur_samplerate))) | |
322 | ||
695dc859 | 323 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
bb993797 | 324 | { |
ad9dbc1c | 325 | struct dev_context *devc = sdi->priv; |
e71062d9 | 326 | GSList *l; |
ad9dbc1c | 327 | struct sr_trigger *trigger; |
e71062d9 | 328 | struct sr_channel *channel; |
bb993797 | 329 | |
ad9dbc1c KA |
330 | /* Clear capture state */ |
331 | devc->bytes_read = 0; | |
332 | devc->offset = 0; | |
333 | ||
334 | /* Configure channels */ | |
e71062d9 KA |
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 | } | |
0bca2e75 | 342 | devc->beaglelogic->set_sampleunit(devc); |
ad9dbc1c | 343 | |
ca0d1a21 KA |
344 | /* If continuous sampling, set the limit_samples to max possible value */ |
345 | if (devc->triggerflags == BL_TRIGGERFLAGS_CONTINUOUS) { | |
346 | devc->limit_samples = (uint64_t)-1; | |
347 | } | |
348 | ||
ad9dbc1c KA |
349 | /* Configure triggers & send header packet */ |
350 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
e743a47d AJ |
351 | int pre_trigger_samples = 0; |
352 | if (devc->limit_samples > 0) | |
efad7ccc | 353 | pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; |
e743a47d | 354 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); |
98fec29e | 355 | if (!devc->stl) |
e743a47d | 356 | return SR_ERR_MALLOC; |
ad9dbc1c KA |
357 | devc->trigger_fired = FALSE; |
358 | } else | |
359 | devc->trigger_fired = TRUE; | |
bee2b016 | 360 | std_session_send_df_header(sdi); |
ad9dbc1c KA |
361 | |
362 | /* Trigger and add poll on file */ | |
0bca2e75 | 363 | devc->beaglelogic->start(devc); |
9b2b3ef9 KA |
364 | if (devc->beaglelogic == &beaglelogic_native_ops) |
365 | sr_session_source_add_pollfd(sdi->session, &devc->pollfd, | |
366 | BUFUNIT_TIMEOUT_MS(devc), beaglelogic_native_receive_data, | |
367 | (void *)sdi); | |
368 | else | |
369 | sr_session_source_add_pollfd(sdi->session, &devc->pollfd, | |
370 | BUFUNIT_TIMEOUT_MS(devc), beaglelogic_tcp_receive_data, | |
ad9dbc1c | 371 | (void *)sdi); |
bb993797 KA |
372 | |
373 | return SR_OK; | |
374 | } | |
375 | ||
695dc859 | 376 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
bb993797 | 377 | { |
ad9dbc1c | 378 | struct dev_context *devc = sdi->priv; |
ad9dbc1c | 379 | |
ad9dbc1c | 380 | /* Execute a stop on BeagleLogic */ |
0bca2e75 | 381 | devc->beaglelogic->stop(devc); |
ad9dbc1c | 382 | |
f7d7ee82 | 383 | /* Flush the cache */ |
9b2b3ef9 KA |
384 | if (devc->beaglelogic == &beaglelogic_native_ops) |
385 | lseek(devc->fd, 0, SEEK_SET); | |
f7d7ee82 KA |
386 | else |
387 | beaglelogic_tcp_drain(devc); | |
ad9dbc1c KA |
388 | |
389 | /* Remove session source and send EOT packet */ | |
390 | sr_session_source_remove_pollfd(sdi->session, &devc->pollfd); | |
bee2b016 | 391 | std_session_send_df_end(sdi); |
bb993797 KA |
392 | |
393 | return SR_OK; | |
394 | } | |
395 | ||
dd5c48a6 | 396 | static struct sr_dev_driver beaglelogic_driver_info = { |
bb993797 KA |
397 | .name = "beaglelogic", |
398 | .longname = "BeagleLogic", | |
399 | .api_version = 1, | |
c2fdcc25 | 400 | .init = std_init, |
700d6b64 | 401 | .cleanup = std_cleanup, |
bb993797 | 402 | .scan = scan, |
c01bf34c | 403 | .dev_list = std_dev_list, |
d6f20d03 | 404 | .dev_clear = dev_clear, |
bb993797 KA |
405 | .config_get = config_get, |
406 | .config_set = config_set, | |
407 | .config_list = config_list, | |
408 | .dev_open = dev_open, | |
409 | .dev_close = dev_close, | |
410 | .dev_acquisition_start = dev_acquisition_start, | |
411 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 412 | .context = NULL, |
bb993797 | 413 | }; |
dd5c48a6 | 414 | SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info); |