]>
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 | ||
4f840ce9 | 62 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
bb993797 | 63 | { |
43376f33 | 64 | GSList *l; |
ad9dbc1c KA |
65 | struct sr_config *src; |
66 | struct sr_dev_inst *sdi; | |
67 | struct dev_context *devc; | |
9b2b3ef9 KA |
68 | const char *conn = NULL; |
69 | gchar **params; | |
ad9dbc1c | 70 | int i, maxch; |
bb993797 | 71 | |
6eab3021 | 72 | maxch = NUM_CHANNELS; |
ad9dbc1c KA |
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); | |
9b2b3ef9 KA |
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 */ | |
f82525e6 | 82 | if (!conn) { |
9b2b3ef9 KA |
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 | } | |
ad9dbc1c KA |
97 | } |
98 | ||
f82525e6 | 99 | maxch = (maxch > 8) ? NUM_CHANNELS : 8; |
ad9dbc1c | 100 | |
6eab3021 KA |
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"); | |
ad9dbc1c | 105 | |
cb17f580 UH |
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; | |
ad9dbc1c | 112 | |
f82525e6 | 113 | if (!conn) { |
9b2b3ef9 KA |
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; | |
a486fca9 KA |
127 | if (devc->beaglelogic->close(devc) != SR_OK) |
128 | goto err_free; | |
9b2b3ef9 KA |
129 | sr_info("BeagleLogic device found at %s : %s", |
130 | devc->address, devc->port); | |
131 | } | |
f82525e6 | 132 | |
0bca2e75 | 133 | /* Fill the channels */ |
5e23fcab ML |
134 | for (i = 0; i < maxch; i++) |
135 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, | |
0f34cb47 | 136 | channel_names[i]); |
ad9dbc1c KA |
137 | |
138 | sdi->priv = devc; | |
bb993797 | 139 | |
43376f33 | 140 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
f82525e6 | 141 | |
9b2b3ef9 KA |
142 | err_free: |
143 | g_free(sdi->model); | |
144 | g_free(sdi->version); | |
d6f20d03 KA |
145 | g_free(devc->address); |
146 | g_free(devc->port); | |
9b2b3ef9 KA |
147 | g_free(devc); |
148 | g_free(sdi); | |
149 | ||
150 | return NULL; | |
bb993797 KA |
151 | } |
152 | ||
bb993797 KA |
153 | static int dev_open(struct sr_dev_inst *sdi) |
154 | { | |
ad9dbc1c KA |
155 | struct dev_context *devc = sdi->priv; |
156 | ||
157 | /* Open BeagleLogic */ | |
a486fca9 KA |
158 | if (devc->beaglelogic->open(devc)) |
159 | return SR_ERR; | |
ad9dbc1c KA |
160 | |
161 | /* Set fd and local attributes */ | |
9b2b3ef9 KA |
162 | if (devc->beaglelogic == &beaglelogic_tcp_ops) |
163 | devc->pollfd.fd = devc->socket; | |
164 | else | |
165 | devc->pollfd.fd = devc->fd; | |
ad9dbc1c | 166 | devc->pollfd.events = G_IO_IN; |
89efe064 | 167 | devc->pollfd.revents = 0; |
ad9dbc1c KA |
168 | |
169 | /* Get the default attributes */ | |
0bca2e75 KA |
170 | devc->beaglelogic->get_samplerate(devc); |
171 | devc->beaglelogic->get_sampleunit(devc); | |
172 | devc->beaglelogic->get_buffersize(devc); | |
173 | devc->beaglelogic->get_bufunitsize(devc); | |
ad9dbc1c | 174 | |
713f3f84 KA |
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; | |
0bca2e75 | 178 | devc->beaglelogic->set_triggerflags(devc); |
713f3f84 | 179 | |
ad9dbc1c | 180 | /* Map the kernel capture FIFO for reads, saves 1 level of memcpy */ |
9b2b3ef9 KA |
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); | |
ad9dbc1c | 189 | } |
bb993797 | 190 | |
bb993797 KA |
191 | return SR_OK; |
192 | } | |
193 | ||
194 | static int dev_close(struct sr_dev_inst *sdi) | |
195 | { | |
ad9dbc1c | 196 | struct dev_context *devc = sdi->priv; |
bb993797 | 197 | |
093e1cba | 198 | /* Close the memory mapping and the file */ |
9b2b3ef9 KA |
199 | if (devc->beaglelogic == &beaglelogic_native_ops) |
200 | devc->beaglelogic->munmap(devc); | |
0bca2e75 | 201 | devc->beaglelogic->close(devc); |
093e1cba | 202 | |
d6f20d03 KA |
203 | return SR_OK; |
204 | } | |
205 | ||
206 | static void clear_helper(struct dev_context *devc) | |
207 | { | |
00f2e9bc UH |
208 | g_free(devc->tcp_buffer); |
209 | g_free(devc->address); | |
210 | g_free(devc->port); | |
d6f20d03 KA |
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); | |
bb993797 KA |
216 | } |
217 | ||
dd7a72ea UH |
218 | static int config_get(uint32_t key, GVariant **data, |
219 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
bb993797 | 220 | { |
ad9dbc1c | 221 | struct dev_context *devc = sdi->priv; |
dcd438ee | 222 | |
bb993797 KA |
223 | (void)cg; |
224 | ||
bb993797 | 225 | switch (key) { |
ad9dbc1c KA |
226 | case SR_CONF_LIMIT_SAMPLES: |
227 | *data = g_variant_new_uint64(devc->limit_samples); | |
228 | break; | |
ad9dbc1c KA |
229 | case SR_CONF_SAMPLERATE: |
230 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
231 | break; | |
e743a47d AJ |
232 | case SR_CONF_CAPTURE_RATIO: |
233 | *data = g_variant_new_uint64(devc->capture_ratio); | |
234 | break; | |
ad9dbc1c KA |
235 | case SR_CONF_NUM_LOGIC_CHANNELS: |
236 | *data = g_variant_new_uint32(g_slist_length(sdi->channels)); | |
237 | break; | |
bb993797 KA |
238 | default: |
239 | return SR_ERR_NA; | |
240 | } | |
241 | ||
ad9dbc1c | 242 | return SR_OK; |
bb993797 KA |
243 | } |
244 | ||
dd7a72ea UH |
245 | static int config_set(uint32_t key, GVariant *data, |
246 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
bb993797 | 247 | { |
ad9dbc1c KA |
248 | struct dev_context *devc = sdi->priv; |
249 | uint64_t tmp_u64; | |
dcd438ee | 250 | |
bb993797 KA |
251 | (void)cg; |
252 | ||
bb993797 | 253 | switch (key) { |
ad9dbc1c KA |
254 | case SR_CONF_SAMPLERATE: |
255 | devc->cur_samplerate = g_variant_get_uint64(data); | |
0bca2e75 | 256 | return devc->beaglelogic->set_samplerate(devc); |
ad9dbc1c KA |
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) \ | |
55f26c42 | 268 | "\' to increase the buffer size, this"\ |
ad9dbc1c KA |
269 | " capture is now truncated to %d Msamples", |
270 | devc->buffersize / | |
271 | (SAMPLEUNIT_TO_BYTES(devc->sampleunit) * 1000000)); | |
272 | } | |
0bca2e75 | 273 | return devc->beaglelogic->set_triggerflags(devc); |
e743a47d AJ |
274 | case SR_CONF_CAPTURE_RATIO: |
275 | devc->capture_ratio = g_variant_get_uint64(data); | |
efad7ccc | 276 | break; |
bb993797 | 277 | default: |
ad9dbc1c | 278 | return SR_ERR_NA; |
bb993797 KA |
279 | } |
280 | ||
ad9dbc1c | 281 | return SR_OK; |
bb993797 KA |
282 | } |
283 | ||
dd7a72ea UH |
284 | static int config_list(uint32_t key, GVariant **data, |
285 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
bb993797 | 286 | { |
bb993797 | 287 | switch (key) { |
138589b0 | 288 | case SR_CONF_SCAN_OPTIONS: |
ad9dbc1c | 289 | case SR_CONF_DEVICE_OPTIONS: |
05199c0a | 290 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
ad9dbc1c | 291 | case SR_CONF_SAMPLERATE: |
53012da6 | 292 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); |
ad9dbc1c KA |
293 | break; |
294 | case SR_CONF_TRIGGER_MATCH: | |
76d10d13 | 295 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); |
ad9dbc1c | 296 | break; |
bb993797 KA |
297 | default: |
298 | return SR_ERR_NA; | |
299 | } | |
300 | ||
a9010323 | 301 | return SR_OK; |
bb993797 KA |
302 | } |
303 | ||
ad9dbc1c KA |
304 | /* get a sane timeout for poll() */ |
305 | #define BUFUNIT_TIMEOUT_MS(devc) (100 + ((devc->bufunitsize * 1000) / \ | |
306 | (uint32_t)(devc->cur_samplerate))) | |
307 | ||
695dc859 | 308 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
bb993797 | 309 | { |
ad9dbc1c | 310 | struct dev_context *devc = sdi->priv; |
e71062d9 | 311 | GSList *l; |
ad9dbc1c | 312 | struct sr_trigger *trigger; |
e71062d9 | 313 | struct sr_channel *channel; |
bb993797 | 314 | |
ad9dbc1c KA |
315 | /* Clear capture state */ |
316 | devc->bytes_read = 0; | |
317 | devc->offset = 0; | |
318 | ||
319 | /* Configure channels */ | |
e71062d9 KA |
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 | } | |
0bca2e75 | 327 | devc->beaglelogic->set_sampleunit(devc); |
ad9dbc1c | 328 | |
ca0d1a21 | 329 | /* If continuous sampling, set the limit_samples to max possible value */ |
f82525e6 | 330 | if (devc->triggerflags == BL_TRIGGERFLAGS_CONTINUOUS) |
ca0d1a21 | 331 | devc->limit_samples = (uint64_t)-1; |
ca0d1a21 | 332 | |
ad9dbc1c KA |
333 | /* Configure triggers & send header packet */ |
334 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
e743a47d AJ |
335 | int pre_trigger_samples = 0; |
336 | if (devc->limit_samples > 0) | |
efad7ccc | 337 | pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; |
e743a47d | 338 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); |
98fec29e | 339 | if (!devc->stl) |
e743a47d | 340 | return SR_ERR_MALLOC; |
ad9dbc1c KA |
341 | devc->trigger_fired = FALSE; |
342 | } else | |
343 | devc->trigger_fired = TRUE; | |
bee2b016 | 344 | std_session_send_df_header(sdi); |
ad9dbc1c KA |
345 | |
346 | /* Trigger and add poll on file */ | |
0bca2e75 | 347 | devc->beaglelogic->start(devc); |
9b2b3ef9 KA |
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, | |
ad9dbc1c | 355 | (void *)sdi); |
bb993797 KA |
356 | |
357 | return SR_OK; | |
358 | } | |
359 | ||
695dc859 | 360 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
bb993797 | 361 | { |
ad9dbc1c | 362 | struct dev_context *devc = sdi->priv; |
ad9dbc1c | 363 | |
ad9dbc1c | 364 | /* Execute a stop on BeagleLogic */ |
0bca2e75 | 365 | devc->beaglelogic->stop(devc); |
ad9dbc1c | 366 | |
f7d7ee82 | 367 | /* Flush the cache */ |
9b2b3ef9 KA |
368 | if (devc->beaglelogic == &beaglelogic_native_ops) |
369 | lseek(devc->fd, 0, SEEK_SET); | |
f7d7ee82 KA |
370 | else |
371 | beaglelogic_tcp_drain(devc); | |
ad9dbc1c KA |
372 | |
373 | /* Remove session source and send EOT packet */ | |
374 | sr_session_source_remove_pollfd(sdi->session, &devc->pollfd); | |
bee2b016 | 375 | std_session_send_df_end(sdi); |
bb993797 KA |
376 | |
377 | return SR_OK; | |
378 | } | |
379 | ||
dd5c48a6 | 380 | static struct sr_dev_driver beaglelogic_driver_info = { |
bb993797 KA |
381 | .name = "beaglelogic", |
382 | .longname = "BeagleLogic", | |
383 | .api_version = 1, | |
c2fdcc25 | 384 | .init = std_init, |
700d6b64 | 385 | .cleanup = std_cleanup, |
bb993797 | 386 | .scan = scan, |
c01bf34c | 387 | .dev_list = std_dev_list, |
d6f20d03 | 388 | .dev_clear = dev_clear, |
bb993797 KA |
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, | |
41812aca | 396 | .context = NULL, |
bb993797 | 397 | }; |
dd5c48a6 | 398 | SR_REGISTER_DEV_DRIVER(beaglelogic_driver_info); |