]> sigrok.org Git - libsigrok.git/blame - hardware/openbench-logic-sniffer/ols.c
use strdup() instead of g_strdup()
[libsigrok.git] / hardware / openbench-logic-sniffer / ols.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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 <stdio.h>
21#include <stdint.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <unistd.h>
27#include <termios.h>
28#include <string.h>
29#include <sys/time.h>
30#include <inttypes.h>
31#include <glib.h>
32#include "sigrok.h"
33
34#define NUM_PROBES 32
35#define NUM_TRIGGER_STAGES 4
36#define TRIGGER_TYPES "01"
37#define SERIAL_SPEED B115200
38/* TODO: SERIAL_ bits, parity, stop bit */
39#define CLOCK_RATE 100000000
40
41
42/* command opcodes */
43#define CMD_RESET 0x00
44#define CMD_ID 0x02
45#define CMD_SET_FLAGS 0x82
46#define CMD_SET_DIVIDER 0x80
47#define CMD_RUN 0x01
48#define CMD_CAPTURE_SIZE 0x81
49#define CMD_SET_TRIGGER_MASK_0 0xc0
50#define CMD_SET_TRIGGER_MASK_1 0xc4
51#define CMD_SET_TRIGGER_MASK_2 0xc8
52#define CMD_SET_TRIGGER_MASK_3 0xcc
53#define CMD_SET_TRIGGER_VALUE_0 0xc1
54#define CMD_SET_TRIGGER_VALUE_1 0xc5
55#define CMD_SET_TRIGGER_VALUE_2 0xc9
56#define CMD_SET_TRIGGER_VALUE_3 0xcd
57#define CMD_SET_TRIGGER_CONFIG_0 0xc2
58#define CMD_SET_TRIGGER_CONFIG_1 0xc6
59#define CMD_SET_TRIGGER_CONFIG_2 0xca
60#define CMD_SET_TRIGGER_CONFIG_3 0xce
61
62/* bitmasks for CMD_FLAGS */
63#define FLAG_DEMUX 0x01
64#define FLAG_FILTER 0x02
65#define FLAG_CHANNELGROUP_1 0x04
66#define FLAG_CHANNELGROUP_2 0x08
67#define FLAG_CHANNELGROUP_3 0x10
68#define FLAG_CHANNELGROUP_4 0x20
69#define FLAG_CLOCK_EXTERNAL 0x40
70#define FLAG_CLOCK_INVERTED 0x80
71#define FLAG_RLE 0x0100
72
73
74static int capabilities[] = {
75 HWCAP_LOGIC_ANALYZER,
76 HWCAP_SAMPLERATE,
77 HWCAP_CAPTURE_RATIO,
78 HWCAP_LIMIT_SAMPLES,
79 0
80};
81
82static struct samplerates samplerates = {
83 1,
84 MHZ(200),
85 1,
86 0
87};
88
89/* list of struct serial_device_instance */
90static GSList *device_instances = NULL;
91
92/* current state of the flag register */
93uint32_t flag_reg = 0;
94
95static uint64_t cur_samplerate = 0;
96static uint64_t limit_samples = 0;
97/* pre/post trigger capture ratio, in percentage. 0 means no pre-trigger data. */
98int capture_ratio = 0;
99static uint32_t probe_mask = 0, trigger_mask[4] = {0}, trigger_value[4] = {0};
100
101
102
103int send_shortcommand(int fd, uint8_t command)
104{
105 char buf[1];
106
107 g_message("ols: sending cmd 0x%.2x", command);
108 buf[0] = command;
109 if(write(fd, buf, 1) != 1)
e31b636d 110 return SIGROK_ERR;
a1bb33af
UH
111
112 return SIGROK_OK;
113}
114
115
116int send_longcommand(int fd, uint8_t command, uint32_t data)
117{
118 char buf[5];
119
120 g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
121 buf[0] = command;
122 buf[1] = data & 0xff;
123 buf[2] = data & 0xff00 >> 8;
124 buf[3] = data & 0xff0000 >> 16;
125 buf[4] = data & 0xff000000 >> 24;
126 if(write(fd, buf, 5) != 5)
e31b636d 127 return SIGROK_ERR;
a1bb33af
UH
128
129 return SIGROK_OK;
130}
131
132static int configure_probes(GSList *probes)
133{
134 struct probe *probe;
135 GSList *l;
136 int probe_bit, changrp_mask, stage, i;
137 char *tc;
138
139 probe_mask = 0;
140 for(i = 0; i < NUM_TRIGGER_STAGES; i++)
141 {
142 trigger_mask[i] = 0;
143 trigger_value[i] = 0;
144 }
145
146 for(l = probes; l; l = l->next)
147 {
148 probe = (struct probe *) l->data;
149 probe_bit = 1 << (probe->index - 1);
150 probe_mask |= probe_bit;
151 if(probe->trigger)
152 {
153 stage = 0;
154 for(tc = probe->trigger; *tc; tc++)
155 {
156 trigger_mask[stage] |= probe_bit;
157 if(*tc == '1')
158 trigger_value[stage] |= probe_bit;
159 stage++;
160 if(stage > 3)
161 {
162 /* only supporting parallel mode, with up to 4 stages */
e31b636d 163 return SIGROK_ERR;
a1bb33af
UH
164 }
165 }
166 }
167 }
168
169 /* enable/disable channel groups in the flag register according
170 * to the probe mask we just made. The register stores them backwards,
171 * hence shift right from 1000.
172 */
173 changrp_mask = 0;
174 for(i = 0; i < 4; i++)
175 {
176 if(probe_mask & (0xff << i))
177 changrp_mask |= 8 >> i;
178 }
179 /* but the flag register wants them here */
180 flag_reg |= changrp_mask << 2;
181
182 return SIGROK_OK;
183}
184
185
186static int hw_init(char *deviceinfo)
187{
188 struct sigrok_device_instance *sdi;
189 GSList *ports, *l;
190 GPollFD *fds;
191 struct termios term, *prev_termios;
192 int devcnt, final_devcnt, num_ports, fd, i;
193 char buf[8], **device_names;
194
195 if(deviceinfo)
196 ports = g_slist_append(NULL, g_strdup(deviceinfo));
197 else
198 /* no specific device given, so scan all serial ports */
199 ports = list_serial_ports();
200
201 num_ports = g_slist_length(ports);
202 fds = g_malloc0(num_ports * sizeof(GPollFD));
203 device_names = g_malloc(num_ports * (sizeof(char *)));
204 prev_termios = g_malloc(num_ports * sizeof(struct termios));
205 devcnt = 0;
206 for(l = ports; l; l = l->next) {
207 /* The discovery procedure is like this: first send the Reset command (0x00) 5 times,
208 * since the device could be anywhere in a 5-byte command. Then send the ID command
209 * (0x02). If the device responds with 4 bytes ("OLS1" or "SLA1"), we have a match.
210 * Since it may take the device a while to respond at 115Kb/s, we do all the sending
211 * first, then wait for all of them to respond with g_poll().
212 */
213 fd = open(l->data, O_RDWR | O_NONBLOCK);
214 if(fd != -1) {
215 tcgetattr(fd, &prev_termios[devcnt]);
216 tcgetattr(fd, &term);
217 cfsetispeed(&term, SERIAL_SPEED);
218 tcsetattr(fd, TCSADRAIN, &term);
219 memset(buf, CMD_RESET, 5);
220 buf[5] = CMD_ID;
221 if(write(fd, buf, 6) == 6) {
222 fds[devcnt].fd = fd;
223 fds[devcnt].events = G_IO_IN;
224 device_names[devcnt] = l->data;
225 devcnt++;
226 g_message("probed device %s", (char *) l->data);
227 }
228 else {
229 /* restore port settings. of course, we've crapped all over the port. */
230 tcsetattr(fd, TCSADRAIN, &prev_termios[devcnt]);
231 g_free(l->data);
232 }
233 }
234 }
235
236 /* 2ms should do it, that's enough time for 28 bytes to go over the bus */
237 usleep(2000);
238
239 final_devcnt = 0;
240 g_poll(fds, devcnt, 1);
241 for(i = 0; i < devcnt; i++) {
242 if(fds[i].revents == G_IO_IN) {
243 if(read(fds[i].fd, buf, 4) == 4) {
244 if(!strncmp(buf, "1SLO", 4) || !strncmp(buf, "1ALS", 4)) {
245 if(!strncmp(buf, "1SLO", 4))
246 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
247 "Openbench", "Logic Sniffer", "v1.0");
248 else
249 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
250 "Sump", "Logic Analyzer", "v1.0");
251 sdi->serial = serial_device_instance_new(device_names[i], -1);
252 device_instances = g_slist_append(device_instances, sdi);
253 final_devcnt++;
254 fds[i].fd = 0;
255 }
256 }
257 }
258
259 if(fds[i].fd != 0)
260 tcsetattr(fds[i].fd, TCSADRAIN, &prev_termios[i]);
261 close(fds[i].fd);
262 }
263
264 g_free(fds);
265 g_free(device_names);
266 g_free(prev_termios);
267 g_slist_free(ports);
268
269 return final_devcnt;
270}
271
272
273static int hw_opendev(int device_index)
274{
275 struct sigrok_device_instance *sdi;
276
277 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 278 return SIGROK_ERR;
a1bb33af
UH
279
280 sdi->serial->fd = open(sdi->serial->port, O_RDWR);
281 if(sdi->serial->fd == -1)
e31b636d 282 return SIGROK_ERR;
a1bb33af
UH
283
284 sdi->status = ST_ACTIVE;
285
286 return SIGROK_OK;
287}
288
289
290static void hw_closedev(int device_index)
291{
292 struct sigrok_device_instance *sdi;
293
294 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
295 return;
296
297 if(sdi->serial->fd != -1) {
298 close(sdi->serial->fd);
299 sdi->serial->fd = -1;
300 sdi->status = ST_INACTIVE;
301 }
302
303}
304
305
306static void hw_cleanup(void)
307{
308 GSList *l;
309 struct sigrok_device_instance *sdi;
310
311 /* properly close all devices */
312 for(l = device_instances; l; l = l->next) {
313 sdi = l->data;
314 if(sdi->serial->fd != -1)
315 close(sdi->serial->fd);
316 sigrok_device_instance_free(sdi);
317 }
318 g_slist_free(device_instances);
319 device_instances = NULL;
320
321}
322
323
324static void *hw_get_device_info(int device_index, int device_info_id)
325{
326 struct sigrok_device_instance *sdi;
327 void *info;
328
329 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
330 return NULL;
331
332 info = NULL;
333 switch(device_info_id)
334 {
335 case DI_INSTANCE:
336 info = sdi;
337 break;
338 case DI_NUM_PROBES:
339 info = GINT_TO_POINTER(NUM_PROBES);
340 break;
341 case DI_SAMPLERATES:
342 info = &samplerates;
343 break;
344 case DI_TRIGGER_TYPES:
345 info = (char *) TRIGGER_TYPES;
346 break;
4c100f32 347 case DI_CUR_SAMPLERATE:
a1bb33af
UH
348 info = &cur_samplerate;
349 break;
350 }
351
352 return info;
353}
354
355
356static int hw_get_status(int device_index)
357{
358 struct sigrok_device_instance *sdi;
359
360 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
361 return ST_NOT_FOUND;
362
363 return sdi->status;
364}
365
366
367static int *hw_get_capabilities(void)
368{
369
370 return capabilities;
371}
372
373
374static int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
375{
376 uint32_t divider;
377
378 if(samplerate < samplerates.low || samplerate > samplerates.high)
e31b636d 379 return SIGROK_ERR_SAMPLERATE;
a1bb33af
UH
380
381 if(samplerate > CLOCK_RATE) {
382 flag_reg |= FLAG_DEMUX;
383 divider = (uint8_t) (CLOCK_RATE * 2 / samplerate) - 1;
384 }
385 else {
386 flag_reg &= FLAG_DEMUX;
387 divider = (uint8_t) (CLOCK_RATE / samplerate) - 1;
388 }
389 divider <<= 8;
390
391 g_message("setting samplerate to %"PRIu64" Hz (divider %u, demux %s)", samplerate, divider,
392 flag_reg & FLAG_DEMUX ? "on" : "off");
393 if(send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, divider) != SIGROK_OK)
e31b636d 394 return SIGROK_ERR;
a1bb33af
UH
395 cur_samplerate = samplerate;
396
397 return SIGROK_OK;
398}
399
400
401static int hw_set_configuration(int device_index, int capability, void *value)
402{
403 struct sigrok_device_instance *sdi;
404 int ret;
405 uint64_t *tmp_u64;
406
407 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 408 return SIGROK_ERR;
a1bb33af
UH
409
410 if(sdi->status != ST_ACTIVE)
e31b636d 411 return SIGROK_ERR;
a1bb33af
UH
412
413 if(capability == HWCAP_SAMPLERATE) {
414 tmp_u64 = value;
415 ret = set_configuration_samplerate(sdi, *tmp_u64);
416 }
417 else if(capability == HWCAP_PROBECONFIG)
418 ret = configure_probes( (GSList *) value);
419 else if(capability == HWCAP_LIMIT_SAMPLES) {
420 limit_samples = strtoull(value, NULL, 10);
421 ret = SIGROK_OK;
422 }
423 else if(capability == HWCAP_CAPTURE_RATIO) {
424 capture_ratio = strtol(value, NULL, 10);
425 if(capture_ratio < 0 || capture_ratio > 100) {
426 capture_ratio = 0;
e31b636d 427 ret = SIGROK_ERR;
a1bb33af
UH
428 }
429 else
430 ret = SIGROK_OK;
431 }
432 else
e31b636d 433 ret = SIGROK_ERR;
a1bb33af
UH
434
435 return ret;
436}
437
438
439static int receive_data(int fd, int revents, void *user_data)
440{
441 static int num_transfers = 0;
442 static int num_bytes = 0;
443 static char last_sample[4] = {0xff};
444 static unsigned char sample[4];
445 int count, buflen, i;
446 struct datafeed_packet packet;
447 unsigned char byte, *buffer;
448
449 if(num_transfers++ == 0) {
450 /* first time round, means the device started sending data, and will not
451 * stop until done. if it stops sending for longer than it takes to send
452 * a byte, that means it's finished. we'll double that to 30ms to be sure...
453 */
454 source_remove(fd);
455 source_add(fd, G_IO_IN, 30, receive_data, user_data);
456 }
457
458 /* TODO: / 4 depends on # of channels used */
459 if(revents == G_IO_IN && num_transfers / 4 <= limit_samples){
460 if(read(fd, &byte, 1) != 1)
461 return FALSE;
462
463 sample[num_bytes++] = byte;
464 if(num_bytes == 4) {
465 g_message("got sample 0x%.2x%.2x%.2x%.2x", sample[3], sample[2], sample[1], sample[0]);
466 /* got a full sample */
467 if(flag_reg & FLAG_RLE) {
468 /* in RLE mode -1 should never come in as a sample, because
469 * bit 31 is the "count" flag */
470 /* TODO: endianness may be wrong here, could be sample[3] */
471 if(sample[0] & 0x80 && !(last_sample[0] & 0x80)) {
472 count = (int) (*sample) & 0x7fffffff;
473 buffer = g_malloc(count);
474 buflen = 0;
475 for(i = 0; i < count; i++)
476 {
477 memcpy(buffer + buflen , last_sample, 4);
478 buflen += 4;
479 }
480 }
481 else {
482 /* just a single sample, next sample will probably be a count
483 * referring to this -- but this one is still a part of the stream
484 */
485 buffer = sample;
486 buflen = 4;
487 }
488 }
489 else {
490 /* no compression */
491 buffer = sample;
492 buflen = 4;
493 }
494
495 /* send it all to the session bus */
496 packet.type = DF_LOGIC32;
497 packet.length = buflen;
498 packet.payload = buffer;
499 session_bus(user_data, &packet);
500 if(buffer == sample)
501 memcpy(last_sample, buffer, 4);
502 else
503 g_free(buffer);
504
505 num_bytes = 0;
506 }
507 }
508 else {
509 /* this is the main loop telling us a timeout was reached -- we're done */
510 tcflush(fd, TCIOFLUSH);
511 close(fd);
512 packet.type = DF_END;
513 packet.length = 0;
514 session_bus(user_data, &packet);
515 }
516
517 return TRUE;
518}
519
520
521static int hw_start_acquisition(int device_index, gpointer session_device_id)
522{
523 struct datafeed_packet *packet;
524 struct datafeed_header *header;
525 struct sigrok_device_instance *sdi;
526 uint32_t data;
527
528 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
e31b636d 529 return SIGROK_ERR;
a1bb33af
UH
530
531 if(sdi->status != ST_ACTIVE)
e31b636d 532 return SIGROK_ERR;
a1bb33af
UH
533
534 /* reset again */
535 if(send_longcommand(sdi->serial->fd, CMD_RESET, 0) != SIGROK_OK)
e31b636d 536 return SIGROK_ERR;
a1bb33af
UH
537
538 /* send flag register */
539 data = flag_reg << 24;
540 if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
e31b636d 541 return SIGROK_ERR;
a1bb33af
UH
542
543 /* send sample limit and pre/post-trigger capture ratio */
544 data = limit_samples / 4 << 16;
545 if(capture_ratio)
546 data |= (limit_samples - (limit_samples / 100 * capture_ratio)) / 4;
547 data = 0x00190019;
548 if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK)
e31b636d 549 return SIGROK_ERR;
a1bb33af
UH
550
551 /* trigger masks */
552 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
e31b636d 553 return SIGROK_ERR;
a1bb33af 554// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK)
e31b636d 555// return SIGROK_ERR;
a1bb33af 556// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK)
e31b636d 557// return SIGROK_ERR;
a1bb33af 558// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK)
e31b636d 559// return SIGROK_ERR;
a1bb33af
UH
560
561 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
e31b636d 562 return SIGROK_ERR;
a1bb33af 563// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK)
e31b636d 564// return SIGROK_ERR;
a1bb33af 565// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK)
e31b636d 566// return SIGROK_ERR;
a1bb33af 567// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK)
e31b636d 568// return SIGROK_ERR;
a1bb33af
UH
569
570 /* trigger configuration */
571 /* TODO: the start flag should only be on the last used stage I think... */
572 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
e31b636d 573 return SIGROK_ERR;
a1bb33af 574// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK)
e31b636d 575// return SIGROK_ERR;
a1bb33af 576// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK)
e31b636d 577// return SIGROK_ERR;
a1bb33af 578// if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK)
e31b636d 579// return SIGROK_ERR;
a1bb33af
UH
580
581 set_configuration_samplerate(sdi, cur_samplerate);
582
583 /* start acquisition on the device */
584 if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
e31b636d 585 return SIGROK_ERR;
a1bb33af
UH
586
587 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id);
588
589 /* send header packet to the session bus */
590 packet = g_malloc(sizeof(struct datafeed_packet));
591 header = g_malloc(sizeof(struct datafeed_header));
592 if(!packet || !header)
e31b636d 593 return SIGROK_ERR;
a1bb33af
UH
594 packet->type = DF_HEADER;
595 packet->length = sizeof(struct datafeed_header);
596 packet->payload = (unsigned char *) header;
597 header->feed_version = 1;
598 gettimeofday(&header->starttime, NULL);
4c100f32 599 header->samplerate = cur_samplerate;
a1bb33af
UH
600 header->protocol_id = PROTO_RAW;
601 header->num_probes = NUM_PROBES;
602 session_bus(session_device_id, packet);
603 g_free(header);
604 g_free(packet);
605
606 return SIGROK_OK;
607}
608
609
610static void hw_stop_acquisition(int device_index, gpointer session_device_id)
611{
612 struct datafeed_packet packet;
613
614 packet.type = DF_END;
615 packet.length = 0;
616 session_bus(session_device_id, &packet);
617
618}
619
620
621
622struct device_plugin ols_plugin_info = {
623 "sump",
624 1,
625 hw_init,
626 hw_cleanup,
627
628 hw_opendev,
629 hw_closedev,
630 hw_get_device_info,
631 hw_get_status,
632 hw_get_capabilities,
633 hw_set_configuration,
634 hw_start_acquisition,
635 hw_stop_acquisition
636};
637