2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
23 #include <sys/types.h>
31 #include <arpa/inet.h>
36 #define NUM_TRIGGER_STAGES 4
37 #define TRIGGER_TYPES "01"
38 #define SERIAL_SPEED B115200
39 /* TODO: SERIAL_ bits, parity, stop bit */
40 #define CLOCK_RATE 100000000
43 #define CMD_RESET 0x00
45 #define CMD_SET_FLAGS 0x82
46 #define CMD_SET_DIVIDER 0x80
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
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
73 static int capabilities[] = {
81 static struct samplerates samplerates = {
88 /* List of struct serial_device_instance */
89 static GSList *device_instances = NULL;
91 /* Current state of the flag register */
92 static uint32_t flag_reg = 0;
94 static uint64_t cur_samplerate = 0;
95 static uint64_t limit_samples = 0;
97 * Pre/post trigger capture ratio, in percentage.
98 * 0 means no pre-trigger data.
100 static int capture_ratio = 0;
101 static uint32_t probe_mask = 0xffffffff;
102 static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
103 static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
105 static int send_shortcommand(int fd, uint8_t command)
109 g_message("ols: sending cmd 0x%.2x", command);
111 if (write(fd, buf, 1) != 1)
117 static int send_longcommand(int fd, uint8_t command, uint32_t data)
121 g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
123 buf[1] = (data & 0xff000000) >> 24;
124 buf[2] = (data & 0xff0000) >> 16;
125 buf[3] = (data & 0xff00) >> 8;
126 buf[4] = data & 0xff;
127 if (write(fd, buf, 5) != 5)
133 static int configure_probes(GSList *probes)
137 int probe_bit, stage, i;
141 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
143 trigger_value[i] = 0;
146 for (l = probes; l; l = l->next) {
147 probe = (struct probe *)l->data;
152 * Set up the probe mask for later configuration into the
155 probe_bit = 1 << (probe->index - 1);
156 probe_mask |= probe_bit;
161 /* Configure trigger mask and value. */
163 for (tc = probe->trigger; tc && *tc; tc++) {
164 trigger_mask[stage] |= probe_bit;
166 trigger_value[stage] |= probe_bit;
170 * TODO: Only supporting parallel mode, with
180 static void byteswap(uint32_t * in)
184 out = (*in & 0xff) << 8;
185 out |= (*in & 0xff00) >> 8;
186 out |= (*in & 0xff0000) << 8;
187 out |= (*in & 0xff000000) >> 8;
191 static int hw_init(char *deviceinfo)
193 struct sigrok_device_instance *sdi;
196 int devcnt, final_devcnt, num_ports, fd, ret, i;
197 char buf[8], **device_names, **serial_params;
200 ports = g_slist_append(NULL, strdup(deviceinfo));
202 /* No specific device given, so scan all serial ports. */
203 ports = list_serial_ports();
205 num_ports = g_slist_length(ports);
206 fds = calloc(1, num_ports * sizeof(GPollFD));
207 device_names = malloc(num_ports * sizeof(char *));
208 serial_params = malloc(num_ports * sizeof(char *));
210 for (l = ports; l; l = l->next) {
211 /* The discovery procedure is like this: first send the Reset
212 * command (0x00) 5 times, since the device could be anywhere
213 * in a 5-byte command. Then send the ID command (0x02).
214 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
217 * Since it may take the device a while to respond at 115Kb/s,
218 * we do all the sending first, then wait for all of them to
219 * respond with g_poll().
221 g_message("probing %s...", (char *)l->data);
222 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
224 serial_params[devcnt] = serial_backup_params(fd);
225 serial_set_params(fd, 115200, 8, 0, 1, 2);
227 for (i = 0; i < 5; i++) {
228 if ((ret = send_shortcommand(fd,
229 CMD_RESET)) != SIGROK_OK) {
230 /* Serial port is not writable. */
234 if (ret != SIGROK_OK) {
235 serial_restore_params(fd,
236 serial_params[devcnt]);
240 send_shortcommand(fd, CMD_ID);
242 fds[devcnt].events = G_IO_IN;
243 device_names[devcnt] = strdup(l->data);
249 /* 2ms should do (enough time for 28 bytes to go over the bus). */
253 g_poll(fds, devcnt, 1);
254 for (i = 0; i < devcnt; i++) {
255 if (fds[i].revents == G_IO_IN) {
256 if (read(fds[i].fd, buf, 4) == 4) {
257 if (!strncmp(buf, "1SLO", 4)
258 || !strncmp(buf, "1ALS", 4)) {
259 if (!strncmp(buf, "1SLO", 4))
260 sdi = sigrok_device_instance_new
261 (final_devcnt, ST_INACTIVE,
263 "Logic Sniffer", "v1.0");
265 sdi = sigrok_device_instance_new
266 (final_devcnt, ST_INACTIVE,
267 "Sump", "Logic Analyzer",
269 sdi->serial = serial_device_instance_new
270 (device_names[i], -1);
272 g_slist_append(device_instances, sdi);
274 serial_close(fds[i].fd);
278 free(device_names[i]);
281 if (fds[i].fd != 0) {
282 serial_restore_params(fds[i].fd, serial_params[i]);
283 serial_close(fds[i].fd);
285 free(serial_params[i]);
293 cur_samplerate = samplerates.low;
298 static int hw_opendev(int device_index)
300 struct sigrok_device_instance *sdi;
302 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
305 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
306 if (sdi->serial->fd == -1)
309 sdi->status = ST_ACTIVE;
314 static void hw_closedev(int device_index)
316 struct sigrok_device_instance *sdi;
318 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
321 if (sdi->serial->fd != -1) {
322 serial_close(sdi->serial->fd);
323 sdi->serial->fd = -1;
324 sdi->status = ST_INACTIVE;
328 static void hw_cleanup(void)
331 struct sigrok_device_instance *sdi;
333 /* Properly close all devices. */
334 for (l = device_instances; l; l = l->next) {
336 if (sdi->serial->fd != -1)
337 serial_close(sdi->serial->fd);
338 sigrok_device_instance_free(sdi);
340 g_slist_free(device_instances);
341 device_instances = NULL;
344 static void *hw_get_device_info(int device_index, int device_info_id)
346 struct sigrok_device_instance *sdi;
349 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
353 switch (device_info_id) {
358 info = GINT_TO_POINTER(NUM_PROBES);
363 case DI_TRIGGER_TYPES:
364 info = (char *)TRIGGER_TYPES;
366 case DI_CUR_SAMPLERATE:
367 info = &cur_samplerate;
374 static int hw_get_status(int device_index)
376 struct sigrok_device_instance *sdi;
378 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
384 static int *hw_get_capabilities(void)
389 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
394 if (samplerate < samplerates.low || samplerate > samplerates.high)
395 return SIGROK_ERR_SAMPLERATE;
397 if (samplerate > CLOCK_RATE) {
398 flag_reg |= FLAG_DEMUX;
399 divider = (CLOCK_RATE * 2 / samplerate) - 1;
401 flag_reg &= ~FLAG_DEMUX;
402 divider = (CLOCK_RATE / samplerate) - 1;
404 divider = htonl(divider);
406 g_message("setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
407 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
409 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
410 divider) != SIGROK_OK)
412 cur_samplerate = samplerate;
417 static int hw_set_configuration(int device_index, int capability, void *value)
419 struct sigrok_device_instance *sdi;
423 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
426 if (sdi->status != ST_ACTIVE)
429 if (capability == HWCAP_SAMPLERATE) {
431 ret = set_configuration_samplerate(sdi, *tmp_u64);
432 } else if (capability == HWCAP_PROBECONFIG) {
433 ret = configure_probes((GSList *) value);
434 } else if (capability == HWCAP_LIMIT_SAMPLES) {
435 limit_samples = strtoull(value, NULL, 10);
437 } else if (capability == HWCAP_CAPTURE_RATIO) {
438 capture_ratio = strtol(value, NULL, 10);
439 if (capture_ratio < 0 || capture_ratio > 100) {
451 static int receive_data(int fd, int revents, void *user_data)
453 static unsigned int num_transfers = 0;
454 static int num_bytes = 0;
455 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
456 static unsigned char sample[4] = { 0, 0, 0, 0 };
457 static unsigned char tmp_sample[4];
458 int count, buflen, num_channels, i, j;
459 struct datafeed_packet packet;
460 unsigned char byte, *buffer;
462 if (num_transfers++ == 0) {
464 * First time round, means the device started sending data,
465 * and will not stop until done. If it stops sending for
466 * longer than it takes to send a byte, that means it's
467 * finished. We'll double that to 30ms to be sure...
470 source_add(fd, G_IO_IN, 100, receive_data, user_data);
474 for (i = 0x20; i > 0x02; i /= 2) {
475 if ((flag_reg & i) == 0)
479 if (revents == G_IO_IN
480 && num_transfers / num_channels <= limit_samples) {
481 if (read(fd, &byte, 1) != 1)
484 sample[num_bytes++] = byte;
485 if (num_bytes == num_channels) {
486 /* Got a full sample. */
487 if (flag_reg & FLAG_RLE) {
489 * In RLE mode -1 should never come in as a
490 * sample, because bit 31 is the "count" flag.
491 * TODO: Endianness may be wrong here, could be
495 && !(last_sample[0] & 0x80)) {
496 count = (int)(*sample) & 0x7fffffff;
497 buffer = g_malloc(count);
499 for (i = 0; i < count; i++) {
500 memcpy(buffer + buflen,
506 * Just a single sample, next sample
507 * will probably be a count referring
508 * to this -- but this one is still a
509 * part of the stream.
515 /* No compression. */
520 if (num_channels < 4) {
522 * Some channel groups may have been turned
523 * off, to speed up transfer between the
524 * hardware and the PC. Expand that here before
525 * submitting it over the session bus --
526 * whatever is listening on the bus will be
527 * expecting a full 32-bit sample, based on
528 * the number of probes.
531 memset(tmp_sample, 0, 4);
532 for (i = 0; i < 4; i++) {
533 if ((flag_reg & (8 >> i)) == 0) {
535 * This channel group was
536 * enabled, copy from received
539 tmp_sample[i] = sample[j++];
542 memcpy(sample, tmp_sample, 4);
545 /* Send it all to the session bus. */
546 packet.type = DF_LOGIC32;
547 packet.length = buflen;
548 packet.payload = buffer;
549 session_bus(user_data, &packet);
550 if (buffer == sample)
551 memcpy(last_sample, buffer, num_channels);
555 memset(sample, 0, 4);
560 * This is the main loop telling us a timeout was reached, or
561 * we've acquired all the samples we asked for -- we're done.
563 tcflush(fd, TCIOFLUSH);
565 packet.type = DF_END;
567 session_bus(user_data, &packet);
573 static int hw_start_acquisition(int device_index, gpointer session_device_id)
576 struct datafeed_packet *packet;
577 struct datafeed_header *header;
578 struct sigrok_device_instance *sdi;
580 uint16_t readcount, delaycount;
581 uint8_t changrp_mask;
583 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
586 if (sdi->status != ST_ACTIVE)
589 if (trigger_mask[0]) {
591 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
592 trigger_mask[0]) != SIGROK_OK)
594 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
595 trigger_mask[1]) != SIGROK_OK)
597 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
598 trigger_mask[2]) != SIGROK_OK)
600 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
601 trigger_mask[3]) != SIGROK_OK)
604 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
605 trigger_value[0]) != SIGROK_OK)
607 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
608 trigger_value[1]) != SIGROK_OK)
610 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
611 trigger_value[2]) != SIGROK_OK)
613 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
614 trigger_value[3]) != SIGROK_OK)
617 /* Trigger configuration */
619 * TODO: The start flag should only be on the last used
622 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
623 0x00000008) != SIGROK_OK)
625 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
626 0x00000000) != SIGROK_OK)
628 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
629 0x00000000) != SIGROK_OK)
631 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
632 0x00000000) != SIGROK_OK)
634 delaycount = limit_samples / 4 * (capture_ratio / 100);
636 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
637 trigger_mask[0]) != SIGROK_OK)
639 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
640 trigger_value[0]) != SIGROK_OK)
642 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
643 0x00000008) != SIGROK_OK)
645 delaycount = limit_samples / 4;
648 set_configuration_samplerate(sdi, cur_samplerate);
650 /* Send sample limit and pre/post-trigger capture ratio. */
651 readcount = limit_samples / 4;
652 if (flag_reg & FLAG_DEMUX) {
653 data = (delaycount - 8) & 0xfff8 << 13;
654 data |= (readcount - 4) & 0xffff;
656 flag_reg |= FLAG_FILTER;
657 data = (readcount - 1) << 16;
658 data |= (delaycount - 1);
662 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE,
667 * Enable/disable channel groups in the flag register according to the
668 * probe mask. The register stores them backwards, hence shift right
672 for (i = 0; i < 4; i++) {
673 if (probe_mask & (0xff << (i * 8)))
674 changrp_mask |= (8 >> i);
677 /* The flag register wants them here, and 1 means "disable channel". */
678 flag_reg |= ~(changrp_mask << 2) & 0x3c;
680 data = flag_reg << 24;
681 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
684 /* Start acquisition on the device. */
685 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
688 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
691 /* Send header packet to the session bus. */
692 packet = g_malloc(sizeof(struct datafeed_packet));
693 header = g_malloc(sizeof(struct datafeed_header));
694 if (!packet || !header)
696 packet->type = DF_HEADER;
697 packet->length = sizeof(struct datafeed_header);
698 packet->payload = (unsigned char *)header;
699 header->feed_version = 1;
700 gettimeofday(&header->starttime, NULL);
701 header->samplerate = cur_samplerate;
702 header->protocol_id = PROTO_RAW;
703 header->num_probes = NUM_PROBES;
704 session_bus(session_device_id, packet);
711 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
713 struct datafeed_packet packet;
716 device_index = device_index;
718 packet.type = DF_END;
720 session_bus(session_device_id, &packet);
723 struct device_plugin ols_plugin_info = {
733 hw_set_configuration,
734 hw_start_acquisition,