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>
38 #include <arpa/inet.h>
42 #include <sigrok-internal.h>
45 #define O_NONBLOCK FIONBIO
49 #define NUM_TRIGGER_STAGES 4
50 #define TRIGGER_TYPES "01"
51 #define SERIAL_SPEED B115200
52 #define CLOCK_RATE SR_MHZ(100)
53 #define MIN_NUM_SAMPLES 4
56 #define CMD_RESET 0x00
58 #define CMD_SET_FLAGS 0x82
59 #define CMD_SET_DIVIDER 0x80
61 #define CMD_CAPTURE_SIZE 0x81
62 #define CMD_SET_TRIGGER_MASK_0 0xc0
63 #define CMD_SET_TRIGGER_MASK_1 0xc4
64 #define CMD_SET_TRIGGER_MASK_2 0xc8
65 #define CMD_SET_TRIGGER_MASK_3 0xcc
66 #define CMD_SET_TRIGGER_VALUE_0 0xc1
67 #define CMD_SET_TRIGGER_VALUE_1 0xc5
68 #define CMD_SET_TRIGGER_VALUE_2 0xc9
69 #define CMD_SET_TRIGGER_VALUE_3 0xcd
70 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
71 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
72 #define CMD_SET_TRIGGER_CONFIG_2 0xca
73 #define CMD_SET_TRIGGER_CONFIG_3 0xce
75 /* Bitmasks for CMD_FLAGS */
76 #define FLAG_DEMUX 0x01
77 #define FLAG_FILTER 0x02
78 #define FLAG_CHANNELGROUP_1 0x04
79 #define FLAG_CHANNELGROUP_2 0x08
80 #define FLAG_CHANNELGROUP_3 0x10
81 #define FLAG_CHANNELGROUP_4 0x20
82 #define FLAG_CLOCK_EXTERNAL 0x40
83 #define FLAG_CLOCK_INVERTED 0x80
84 #define FLAG_RLE 0x0100
86 static int capabilities[] = {
87 SR_HWCAP_LOGIC_ANALYZER,
89 SR_HWCAP_CAPTURE_RATIO,
90 SR_HWCAP_LIMIT_SAMPLES,
94 static struct sr_samplerates samplerates = {
101 /* List of struct sr_serial_device_instance */
102 static GSList *device_instances = NULL;
104 /* Current state of the flag register */
105 static uint32_t flag_reg = 0;
107 static uint64_t cur_samplerate = 0;
108 static uint64_t limit_samples = 0;
110 * Pre/post trigger capture ratio, in percentage.
111 * 0 means no pre-trigger data.
113 static int capture_ratio = 0;
114 static int trigger_at = -1;
115 static uint32_t probe_mask = 0xffffffff;
116 static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
117 static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
118 static int num_stages = 0;
120 static int send_shortcommand(int fd, uint8_t command)
124 g_debug("ols: sending cmd 0x%.2x", command);
126 if (serial_write(fd, buf, 1) != 1)
132 static int send_longcommand(int fd, uint8_t command, uint32_t data)
136 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
138 buf[1] = (data & 0xff000000) >> 24;
139 buf[2] = (data & 0xff0000) >> 16;
140 buf[3] = (data & 0xff00) >> 8;
141 buf[4] = data & 0xff;
142 if (serial_write(fd, buf, 5) != 5)
148 static int configure_probes(GSList *probes)
150 struct sr_probe *probe;
152 int probe_bit, stage, i;
156 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
158 trigger_value[i] = 0;
162 for (l = probes; l; l = l->next) {
163 probe = (struct sr_probe *)l->data;
168 * Set up the probe mask for later configuration into the
171 probe_bit = 1 << (probe->index - 1);
172 probe_mask |= probe_bit;
177 /* Configure trigger mask and value. */
179 for (tc = probe->trigger; tc && *tc; tc++) {
180 trigger_mask[stage] |= probe_bit;
182 trigger_value[stage] |= probe_bit;
186 * TODO: Only supporting parallel mode, with
191 if (stage > num_stages)
198 static uint32_t reverse16(uint32_t in)
202 out = (in & 0xff) << 8;
203 out |= (in & 0xff00) >> 8;
204 out |= (in & 0xff0000) << 8;
205 out |= (in & 0xff000000) >> 8;
210 static uint32_t reverse32(uint32_t in)
214 out = (in & 0xff) << 24;
215 out |= (in & 0xff00) << 8;
216 out |= (in & 0xff0000) >> 8;
217 out |= (in & 0xff000000) >> 24;
222 static int hw_init(const char *deviceinfo)
224 struct sr_device_instance *sdi;
227 int devcnt, final_devcnt, num_ports, fd, ret, i;
228 char buf[8], **device_names, **serial_params;
231 ports = g_slist_append(NULL, strdup(deviceinfo));
233 /* No specific device given, so scan all serial ports. */
234 ports = list_serial_ports();
236 num_ports = g_slist_length(ports);
237 fds = calloc(1, num_ports * sizeof(GPollFD));
238 device_names = malloc(num_ports * sizeof(char *));
239 serial_params = malloc(num_ports * sizeof(char *));
241 for (l = ports; l; l = l->next) {
242 /* The discovery procedure is like this: first send the Reset
243 * command (0x00) 5 times, since the device could be anywhere
244 * in a 5-byte command. Then send the ID command (0x02).
245 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
248 * Since it may take the device a while to respond at 115Kb/s,
249 * we do all the sending first, then wait for all of them to
250 * respond with g_poll().
252 g_message("ols: probing %s...", (char *)l->data);
253 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
255 serial_params[devcnt] = serial_backup_params(fd);
256 serial_set_params(fd, 115200, 8, 0, 1, 2);
258 for (i = 0; i < 5; i++) {
259 if ((ret = send_shortcommand(fd,
260 CMD_RESET)) != SR_OK) {
261 /* Serial port is not writable. */
266 serial_restore_params(fd,
267 serial_params[devcnt]);
271 send_shortcommand(fd, CMD_ID);
273 fds[devcnt].events = G_IO_IN;
274 device_names[devcnt] = strdup(l->data);
280 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
284 g_poll(fds, devcnt, 1);
285 for (i = 0; i < devcnt; i++) {
286 if (fds[i].revents == G_IO_IN) {
287 if (serial_read(fds[i].fd, buf, 4) == 4) {
288 if (!strncmp(buf, "1SLO", 4)
289 || !strncmp(buf, "1ALS", 4)) {
290 if (!strncmp(buf, "1SLO", 4))
291 sdi = sr_device_instance_new
292 (final_devcnt, SR_ST_INACTIVE,
294 "Logic Sniffer", "v1.0");
296 sdi = sr_device_instance_new
297 (final_devcnt, SR_ST_INACTIVE,
298 "Openbench", "Logic Sniffer",
300 sdi->serial = sr_serial_device_instance_new
301 (device_names[i], -1);
303 g_slist_append(device_instances, sdi);
305 serial_close(fds[i].fd);
309 free(device_names[i]);
312 if (fds[i].fd != 0) {
313 serial_restore_params(fds[i].fd, serial_params[i]);
314 serial_close(fds[i].fd);
316 free(serial_params[i]);
324 cur_samplerate = SR_KHZ(200);
329 static int hw_opendev(int device_index)
331 struct sr_device_instance *sdi;
333 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
336 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
337 if (sdi->serial->fd == -1)
340 sdi->status = SR_ST_ACTIVE;
345 static void hw_closedev(int device_index)
347 struct sr_device_instance *sdi;
349 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
352 if (sdi->serial->fd != -1) {
353 serial_close(sdi->serial->fd);
354 sdi->serial->fd = -1;
355 sdi->status = SR_ST_INACTIVE;
359 static void hw_cleanup(void)
362 struct sr_device_instance *sdi;
364 /* Properly close all devices. */
365 for (l = device_instances; l; l = l->next) {
367 if (sdi->serial->fd != -1)
368 serial_close(sdi->serial->fd);
369 sr_device_instance_free(sdi);
371 g_slist_free(device_instances);
372 device_instances = NULL;
375 static void *hw_get_device_info(int device_index, int device_info_id)
377 struct sr_device_instance *sdi;
380 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
384 switch (device_info_id) {
388 case SR_DI_NUM_PROBES:
389 info = GINT_TO_POINTER(NUM_PROBES);
391 case SR_DI_SAMPLERATES:
394 case SR_DI_TRIGGER_TYPES:
395 info = (char *)TRIGGER_TYPES;
397 case SR_DI_CUR_SAMPLERATE:
398 info = &cur_samplerate;
405 static int hw_get_status(int device_index)
407 struct sr_device_instance *sdi;
409 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
410 return SR_ST_NOT_FOUND;
415 static int *hw_get_capabilities(void)
420 static int set_configuration_samplerate(struct sr_device_instance *sdi,
425 if (samplerate < samplerates.low || samplerate > samplerates.high)
426 return SR_ERR_SAMPLERATE;
428 if (samplerate > CLOCK_RATE) {
429 flag_reg |= FLAG_DEMUX;
430 divider = (CLOCK_RATE * 2 / samplerate) - 1;
432 flag_reg &= ~FLAG_DEMUX;
433 divider = (CLOCK_RATE / samplerate) - 1;
436 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
437 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
439 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, reverse32(divider)) != SR_OK)
441 cur_samplerate = samplerate;
446 static int hw_set_configuration(int device_index, int capability, void *value)
448 struct sr_device_instance *sdi;
452 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
455 if (sdi->status != SR_ST_ACTIVE)
458 switch (capability) {
459 case SR_HWCAP_SAMPLERATE:
461 ret = set_configuration_samplerate(sdi, *tmp_u64);
463 case SR_HWCAP_PROBECONFIG:
464 ret = configure_probes((GSList *) value);
466 case SR_HWCAP_LIMIT_SAMPLES:
468 if (*tmp_u64 < MIN_NUM_SAMPLES)
470 limit_samples = *tmp_u64;
471 g_message("ols: sample limit %" PRIu64, limit_samples);
474 case SR_HWCAP_CAPTURE_RATIO:
476 capture_ratio = *tmp_u64;
477 if (capture_ratio < 0 || capture_ratio > 100) {
490 static int receive_data(int fd, int revents, void *user_data)
492 static unsigned int num_transfers = 0;
493 static int num_bytes = 0;
494 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
495 static unsigned char sample[4] = { 0, 0, 0, 0 };
496 static unsigned char tmp_sample[4];
497 static unsigned char *raw_sample_buf = NULL;
498 int count, buflen, num_channels, offset, i, j;
499 struct sr_datafeed_packet packet;
500 unsigned char byte, *buffer;
502 if (num_transfers++ == 0) {
504 * First time round, means the device started sending data,
505 * and will not stop until done. If it stops sending for
506 * longer than it takes to send a byte, that means it's
507 * finished. We'll double that to 30ms to be sure...
509 sr_source_remove(fd);
510 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
511 raw_sample_buf = malloc(limit_samples * 4);
512 /* fill with 1010... for debugging */
513 memset(raw_sample_buf, 0x82, limit_samples * 4);
517 for (i = 0x20; i > 0x02; i /= 2) {
518 if ((flag_reg & i) == 0)
522 if (revents == G_IO_IN
523 && num_transfers / num_channels <= limit_samples) {
524 if (serial_read(fd, &byte, 1) != 1)
527 sample[num_bytes++] = byte;
528 g_debug("ols: received byte 0x%.2x", byte);
529 if (num_bytes == num_channels) {
530 g_debug("ols: received sample 0x%.*x", num_bytes * 2, (int) *sample);
531 /* Got a full sample. */
532 if (flag_reg & FLAG_RLE) {
534 * In RLE mode -1 should never come in as a
535 * sample, because bit 31 is the "count" flag.
536 * TODO: Endianness may be wrong here, could be
540 && !(last_sample[0] & 0x80)) {
541 count = (int)(*sample) & 0x7fffffff;
542 buffer = g_malloc(count);
544 for (i = 0; i < count; i++) {
545 memcpy(buffer + buflen, last_sample, 4);
550 * Just a single sample, next sample
551 * will probably be a count referring
552 * to this -- but this one is still a
553 * part of the stream.
559 /* No compression. */
564 if (num_channels < 4) {
566 * Some channel groups may have been turned
567 * off, to speed up transfer between the
568 * hardware and the PC. Expand that here before
569 * submitting it over the session bus --
570 * whatever is listening on the bus will be
571 * expecting a full 32-bit sample, based on
572 * the number of probes.
575 memset(tmp_sample, 0, 4);
576 for (i = 0; i < 4; i++) {
577 if (((flag_reg >> 2) & (1 << i)) == 0) {
579 * This channel group was
580 * enabled, copy from received
583 tmp_sample[i] = sample[j++];
586 memcpy(sample, tmp_sample, 4);
587 g_debug("ols: full sample 0x%.8x", (int) *sample);
590 /* the OLS sends its sample buffer backwards.
591 * store it in reverse order here, so we can dump
592 * this on the session bus later.
594 offset = (limit_samples - num_transfers / num_channels) * 4;
595 memcpy(raw_sample_buf + offset, sample, 4);
597 if (buffer == sample)
598 memcpy(last_sample, buffer, num_channels);
602 memset(sample, 0, 4);
607 * This is the main loop telling us a timeout was reached, or
608 * we've acquired all the samples we asked for -- we're done.
609 * Send the (properly-ordered) buffer to the frontend.
611 if (trigger_at != -1) {
612 /* a trigger was set up, so we need to tell the frontend
615 if (trigger_at > 0) {
616 /* there are pre-trigger samples, send those first */
617 packet.type = SR_DF_LOGIC;
618 packet.length = trigger_at * 4;
620 packet.payload = raw_sample_buf;
621 sr_session_bus(user_data, &packet);
624 packet.type = SR_DF_TRIGGER;
626 sr_session_bus(user_data, &packet);
628 packet.type = SR_DF_LOGIC;
629 packet.length = (limit_samples * 4) - (trigger_at * 4);
631 packet.payload = raw_sample_buf + trigger_at * 4;
632 sr_session_bus(user_data, &packet);
634 packet.type = SR_DF_LOGIC;
635 packet.length = limit_samples * 4;
637 packet.payload = raw_sample_buf;
638 sr_session_bus(user_data, &packet);
640 free(raw_sample_buf);
644 packet.type = SR_DF_END;
646 sr_session_bus(user_data, &packet);
652 static int hw_start_acquisition(int device_index, gpointer session_device_id)
655 struct sr_datafeed_packet *packet;
656 struct sr_datafeed_header *header;
657 struct sr_device_instance *sdi;
658 uint32_t trigger_config[4];
660 uint16_t readcount, delaycount;
661 uint8_t changrp_mask;
663 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
666 if (sdi->status != SR_ST_ACTIVE)
669 readcount = limit_samples / 4;
671 memset(trigger_config, 0, 16);
672 trigger_config[num_stages-1] |= 0x08;
673 if (trigger_mask[0]) {
674 delaycount = readcount * (1 - capture_ratio / 100.0);
675 trigger_at = (readcount - delaycount) * 4 - num_stages;
677 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
678 reverse32(trigger_mask[0])) != SR_OK)
680 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
681 reverse32(trigger_value[0])) != SR_OK)
683 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
684 trigger_config[0]) != SR_OK)
687 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
688 reverse32(trigger_mask[1])) != SR_OK)
690 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
691 reverse32(trigger_value[1])) != SR_OK)
693 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
694 trigger_config[1]) != SR_OK)
697 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
698 reverse32(trigger_mask[2])) != SR_OK)
700 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
701 reverse32(trigger_value[2])) != SR_OK)
703 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
704 trigger_config[2]) != SR_OK)
707 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
708 reverse32(trigger_mask[3])) != SR_OK)
710 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
711 reverse32(trigger_value[3])) != SR_OK)
713 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
714 trigger_config[3]) != SR_OK)
717 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
718 trigger_mask[0]) != SR_OK)
720 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
721 trigger_value[0]) != SR_OK)
723 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
724 0x00000008) != SR_OK)
726 delaycount = readcount;
729 set_configuration_samplerate(sdi, cur_samplerate);
731 /* Send sample limit and pre/post-trigger capture ratio. */
732 data = ((readcount - 1) & 0xffff) << 16;
733 data |= (delaycount - 1) & 0xffff;
734 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
738 * Enable/disable channel groups in the flag register according to the
742 for (i = 0; i < 4; i++) {
743 if (probe_mask & (0xff << (i * 8)))
744 changrp_mask |= (1 << i);
747 /* The flag register wants them here, and 1 means "disable channel". */
748 flag_reg |= ~(changrp_mask << 2) & 0x3c;
749 flag_reg |= FLAG_FILTER;
750 data = flag_reg << 24;
751 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
754 /* Start acquisition on the device. */
755 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
758 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
761 /* Send header packet to the session bus. */
762 packet = g_malloc(sizeof(struct sr_datafeed_packet));
763 header = g_malloc(sizeof(struct sr_datafeed_header));
764 if (!packet || !header)
766 packet->type = SR_DF_HEADER;
767 packet->length = sizeof(struct sr_datafeed_header);
768 packet->payload = (unsigned char *)header;
769 header->feed_version = 1;
770 gettimeofday(&header->starttime, NULL);
771 header->samplerate = cur_samplerate;
772 header->protocol_id = SR_PROTO_RAW;
773 header->num_logic_probes = NUM_PROBES;
774 header->num_analog_probes = 0;
775 sr_session_bus(session_device_id, packet);
782 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
784 struct sr_datafeed_packet packet;
786 /* Avoid compiler warnings. */
787 device_index = device_index;
789 packet.type = SR_DF_END;
791 sr_session_bus(session_device_id, &packet);
794 struct sr_device_plugin ols_plugin_info = {
796 "Openbench Logic Sniffer",
805 hw_set_configuration,
806 hw_start_acquisition,