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>
46 #define O_NONBLOCK FIONBIO
50 static int capabilities[] = {
51 SR_HWCAP_LOGIC_ANALYZER,
53 SR_HWCAP_CAPTURE_RATIO,
54 SR_HWCAP_LIMIT_SAMPLES,
58 /* default supported samplerates, can be overridden by device metadata */
59 static struct sr_samplerates samplerates = {
66 /* List of struct sr_serial_device_instance */
67 static GSList *device_instances = NULL;
71 static int send_shortcommand(int fd, uint8_t command)
75 g_debug("ols: sending cmd 0x%.2x", command);
77 if (serial_write(fd, buf, 1) != 1)
83 static int send_longcommand(int fd, uint8_t command, uint32_t data)
87 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
89 buf[1] = (data & 0xff000000) >> 24;
90 buf[2] = (data & 0xff0000) >> 16;
91 buf[3] = (data & 0xff00) >> 8;
93 if (serial_write(fd, buf, 5) != 5)
99 static int configure_probes(struct ols_device *ols, GSList *probes)
101 struct sr_probe *probe;
103 int probe_bit, stage, i;
107 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
108 ols->trigger_mask[i] = 0;
109 ols->trigger_value[i] = 0;
113 for (l = probes; l; l = l->next) {
114 probe = (struct sr_probe *)l->data;
119 * Set up the probe mask for later configuration into the
122 probe_bit = 1 << (probe->index - 1);
123 ols->probe_mask |= probe_bit;
128 /* Configure trigger mask and value. */
130 for (tc = probe->trigger; tc && *tc; tc++) {
131 ols->trigger_mask[stage] |= probe_bit;
133 ols->trigger_value[stage] |= probe_bit;
137 * TODO: Only supporting parallel mode, with
142 if (stage > ols->num_stages)
143 ols->num_stages = stage;
149 static uint32_t reverse16(uint32_t in)
153 out = (in & 0xff) << 8;
154 out |= (in & 0xff00) >> 8;
155 out |= (in & 0xff0000) << 8;
156 out |= (in & 0xff000000) >> 8;
161 static uint32_t reverse32(uint32_t in)
165 out = (in & 0xff) << 24;
166 out |= (in & 0xff00) << 8;
167 out |= (in & 0xff0000) >> 8;
168 out |= (in & 0xff000000) >> 24;
173 static struct ols_device *ols_device_new(void)
175 struct ols_device *ols;
177 ols = g_malloc0(sizeof(struct ols_device));
178 ols->trigger_at = -1;
179 ols->probe_mask = 0xffffffff;
180 ols->cur_samplerate = SR_KHZ(200);
185 static struct sr_device_instance *get_metadata(int fd)
187 struct sr_device_instance *sdi;
188 struct ols_device *ols;
190 uint8_t key, type, token;
191 GString *tmp_str, *devicename, *version;
194 sdi = sr_device_instance_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
195 ols = ols_device_new();
198 devicename = g_string_new("");
199 version = g_string_new("");
203 if (serial_read(fd, &key, 1) != 1 || key == 0x00)
209 /* NULL-terminated string */
210 tmp_str = g_string_new("");
211 while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0')
212 g_string_append_c(tmp_str, tmp_c);
213 g_debug("ols: got metadata key 0x%.2x value '%s'", key, tmp_str->str);
217 devicename = g_string_append(devicename, tmp_str->str);
220 /* FPGA firmware version */
222 g_string_append(version, ", ");
223 g_string_append(version, "FPGA version ");
224 g_string_append(version, tmp_str->str);
227 /* Ancillary version */
229 g_string_append(version, ", ");
230 g_string_append(version, "Ancillary version ");
231 g_string_append(version, tmp_str->str);
234 g_message("ols: unknown token 0x%.2x: '%s'", token, tmp_str->str);
237 g_string_free(tmp_str, TRUE);
240 /* 32-bit unsigned integer */
241 if (serial_read(fd, &tmp_int, 4) != 4)
243 tmp_int = reverse32(tmp_int);
244 g_debug("ols: got metadata key 0x%.2x value 0x%.8x", key, tmp_int);
247 /* Number of usable probes */
248 ols->num_probes = tmp_int;
251 /* Amount of sample memory available (bytes) */
252 ols->max_samples = tmp_int;
255 /* Amount of dynamic memory available (bytes) */
256 /* what is this for? */
259 /* Maximum sample rate (hz) */
260 ols->max_samplerate = tmp_int;
263 /* protocol version */
264 ols->protocol_version = tmp_int;
267 g_message("ols: unknown token 0x%.2x: 0x%.8x", token, tmp_int);
272 /* 8-bit unsigned integer */
273 if (serial_read(fd, &tmp_c, 1) != 1)
275 g_debug("ols: got metadata key 0x%.2x value 0x%.2x", key, tmp_c);
278 /* Number of usable probes */
279 ols->num_probes = tmp_c;
282 /* protocol version */
283 ols->protocol_version = tmp_c;
286 g_message("ols: unknown token 0x%.2x: 0x%.2x", token, tmp_c);
296 sdi->model = devicename->str;
297 sdi->version = version->str;
298 g_string_free(devicename, FALSE);
299 g_string_free(version, FALSE);
305 static int hw_init(const char *deviceinfo)
307 struct sr_device_instance *sdi;
308 struct ols_device *ols;
310 GPollFD *fds, probefd;
311 int devcnt, final_devcnt, num_ports, fd, ret, i;
312 char buf[8], **device_names, **serial_params;
315 ports = g_slist_append(NULL, strdup(deviceinfo));
317 /* No specific device given, so scan all serial ports. */
318 ports = list_serial_ports();
320 num_ports = g_slist_length(ports);
321 fds = calloc(1, num_ports * sizeof(GPollFD));
322 device_names = malloc(num_ports * sizeof(char *));
323 serial_params = malloc(num_ports * sizeof(char *));
325 for (l = ports; l; l = l->next) {
326 /* The discovery procedure is like this: first send the Reset
327 * command (0x00) 5 times, since the device could be anywhere
328 * in a 5-byte command. Then send the ID command (0x02).
329 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
332 * Since it may take the device a while to respond at 115Kb/s,
333 * we do all the sending first, then wait for all of them to
334 * respond with g_poll().
336 g_message("ols: probing %s...", (char *)l->data);
337 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
339 serial_params[devcnt] = serial_backup_params(fd);
340 serial_set_params(fd, 115200, 8, 0, 1, 2);
342 for (i = 0; i < 5; i++) {
343 if ((ret = send_shortcommand(fd,
344 CMD_RESET)) != SR_OK) {
345 /* Serial port is not writable. */
350 serial_restore_params(fd,
351 serial_params[devcnt]);
355 send_shortcommand(fd, CMD_ID);
357 fds[devcnt].events = G_IO_IN;
358 device_names[devcnt] = strdup(l->data);
364 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
368 g_poll(fds, devcnt, 1);
370 for (i = 0; i < devcnt; i++) {
371 if (fds[i].revents != G_IO_IN)
373 if (serial_read(fds[i].fd, buf, 4) != 4)
375 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
378 /* definitely using the OLS protocol, check if it supports
379 * the metadata command
381 send_shortcommand(fds[i].fd, CMD_METADATA);
382 probefd.fd = fds[i].fd;
383 probefd.events = G_IO_IN;
384 if (g_poll(&probefd, 1, 10) > 0) {
386 sdi = get_metadata(fds[i].fd);
387 sdi->index = final_devcnt;
389 /* not an OLS -- some other board that uses the sump protocol */
390 sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE,
391 "Sump", "Logic Analyzer", "v1.0");
392 ols = ols_device_new();
393 ols->num_probes = 32;
396 sdi->serial = sr_serial_device_instance_new(device_names[i], -1);
397 device_instances = g_slist_append(device_instances, sdi);
399 serial_close(fds[i].fd);
404 /* clean up after all the probing */
405 for (i = 0; i < devcnt; i++) {
406 if (fds[i].fd != 0) {
407 serial_restore_params(fds[i].fd, serial_params[i]);
408 serial_close(fds[i].fd);
410 free(serial_params[i]);
411 free(device_names[i]);
422 static int hw_opendev(int device_index)
424 struct sr_device_instance *sdi;
426 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
429 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
430 if (sdi->serial->fd == -1)
433 sdi->status = SR_ST_ACTIVE;
438 static void hw_closedev(int device_index)
440 struct sr_device_instance *sdi;
442 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
445 if (sdi->serial->fd != -1) {
446 serial_close(sdi->serial->fd);
447 sdi->serial->fd = -1;
448 sdi->status = SR_ST_INACTIVE;
452 static void hw_cleanup(void)
455 struct sr_device_instance *sdi;
457 /* Properly close all devices. */
458 for (l = device_instances; l; l = l->next) {
460 if (sdi->serial->fd != -1)
461 serial_close(sdi->serial->fd);
462 sr_device_instance_free(sdi);
464 g_slist_free(device_instances);
465 device_instances = NULL;
468 static void *hw_get_device_info(int device_index, int device_info_id)
470 struct sr_device_instance *sdi;
471 struct ols_device *ols;
474 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
479 switch (device_info_id) {
483 case SR_DI_NUM_PROBES:
484 info = GINT_TO_POINTER(NUM_PROBES);
486 case SR_DI_SAMPLERATES:
489 case SR_DI_TRIGGER_TYPES:
490 info = (char *)TRIGGER_TYPES;
492 case SR_DI_CUR_SAMPLERATE:
493 info = &ols->cur_samplerate;
500 static int hw_get_status(int device_index)
502 struct sr_device_instance *sdi;
504 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
505 return SR_ST_NOT_FOUND;
510 static int *hw_get_capabilities(void)
515 static int set_configuration_samplerate(struct sr_device_instance *sdi,
518 struct ols_device *ols;
521 if (ols->max_samplerate) {
522 if (samplerate > ols->max_samplerate)
523 return SR_ERR_SAMPLERATE;
524 } else if (samplerate < samplerates.low || samplerate > samplerates.high)
525 return SR_ERR_SAMPLERATE;
527 ols->cur_samplerate = samplerate;
528 if (samplerate > CLOCK_RATE) {
529 ols->flag_reg |= FLAG_DEMUX;
530 ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
532 ols->flag_reg &= ~FLAG_DEMUX;
533 ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
539 static int hw_set_configuration(int device_index, int capability, void *value)
541 struct sr_device_instance *sdi;
542 struct ols_device *ols;
546 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
550 if (sdi->status != SR_ST_ACTIVE)
553 switch (capability) {
554 case SR_HWCAP_SAMPLERATE:
556 ret = set_configuration_samplerate(sdi, *tmp_u64);
558 case SR_HWCAP_PROBECONFIG:
559 ret = configure_probes(ols, (GSList *) value);
561 case SR_HWCAP_LIMIT_SAMPLES:
563 if (*tmp_u64 < MIN_NUM_SAMPLES)
565 ols->limit_samples = *tmp_u64;
566 g_message("ols: sample limit %" PRIu64, ols->limit_samples);
569 case SR_HWCAP_CAPTURE_RATIO:
571 ols->capture_ratio = *tmp_u64;
572 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
573 ols->capture_ratio = 0;
585 static int receive_data(int fd, int revents, void *user_data)
587 struct sr_datafeed_packet packet;
588 struct sr_device_instance *sdi;
589 struct ols_device *ols;
591 int count, buflen, num_channels, offset, i, j;
592 unsigned char byte, *buffer;
594 /* find this device's ols_device struct by its fd */
596 for (l = device_instances; l; l = l->next) {
598 if (sdi->serial->fd == fd) {
604 /* shouldn't happen */
607 if (ols->num_transfers++ == 0) {
609 * First time round, means the device started sending data,
610 * and will not stop until done. If it stops sending for
611 * longer than it takes to send a byte, that means it's
612 * finished. We'll double that to 30ms to be sure...
614 sr_source_remove(fd);
615 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
616 ols->raw_sample_buf = malloc(ols->limit_samples * 4);
617 /* fill with 1010... for debugging */
618 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
622 for (i = 0x20; i > 0x02; i /= 2) {
623 if ((ols->flag_reg & i) == 0)
627 if (revents == G_IO_IN
628 && ols->num_transfers / num_channels <= ols->limit_samples) {
629 if (serial_read(fd, &byte, 1) != 1)
632 ols->sample[ols->num_bytes++] = byte;
633 g_debug("ols: received byte 0x%.2x", byte);
634 if (ols->num_bytes == num_channels) {
635 /* Got a full sample. */
636 g_debug("ols: received sample 0x%.*x", ols->num_bytes * 2, (int) *ols->sample);
637 if (ols->flag_reg & FLAG_RLE) {
639 * In RLE mode -1 should never come in as a
640 * sample, because bit 31 is the "count" flag.
641 * TODO: Endianness may be wrong here, could be
644 if (ols->sample[0] & 0x80
645 && !(ols->last_sample[0] & 0x80)) {
646 count = (int)(*ols->sample) & 0x7fffffff;
647 buffer = g_malloc(count);
649 for (i = 0; i < count; i++) {
650 memcpy(buffer + buflen, ols->last_sample, 4);
655 * Just a single sample, next sample
656 * will probably be a count referring
657 * to this -- but this one is still a
658 * part of the stream.
660 buffer = ols->sample;
664 /* No compression. */
665 buffer = ols->sample;
669 if (num_channels < 4) {
671 * Some channel groups may have been turned
672 * off, to speed up transfer between the
673 * hardware and the PC. Expand that here before
674 * submitting it over the session bus --
675 * whatever is listening on the bus will be
676 * expecting a full 32-bit sample, based on
677 * the number of probes.
680 memset(ols->tmp_sample, 0, 4);
681 for (i = 0; i < 4; i++) {
682 if (((ols->flag_reg >> 2) & (1 << i)) == 0) {
684 * This channel group was
685 * enabled, copy from received
688 ols->tmp_sample[i] = ols->sample[j++];
691 memcpy(ols->sample, ols->tmp_sample, 4);
692 g_debug("ols: full sample 0x%.8x", (int) *ols->sample);
695 /* the OLS sends its sample buffer backwards.
696 * store it in reverse order here, so we can dump
697 * this on the session bus later.
699 offset = (ols->limit_samples - ols->num_transfers / num_channels) * 4;
700 memcpy(ols->raw_sample_buf + offset, ols->sample, 4);
702 if (buffer == ols->sample)
703 memcpy(ols->last_sample, buffer, num_channels);
707 memset(ols->sample, 0, 4);
712 * This is the main loop telling us a timeout was reached, or
713 * we've acquired all the samples we asked for -- we're done.
714 * Send the (properly-ordered) buffer to the frontend.
716 if (ols->trigger_at != -1) {
717 /* a trigger was set up, so we need to tell the frontend
720 if (ols->trigger_at > 0) {
721 /* there are pre-trigger samples, send those first */
722 packet.type = SR_DF_LOGIC;
723 packet.length = ols->trigger_at * 4;
725 packet.payload = ols->raw_sample_buf;
726 sr_session_bus(user_data, &packet);
729 packet.type = SR_DF_TRIGGER;
731 sr_session_bus(user_data, &packet);
733 packet.type = SR_DF_LOGIC;
734 packet.length = (ols->limit_samples * 4) - (ols->trigger_at * 4);
736 packet.payload = ols->raw_sample_buf + ols->trigger_at * 4;
737 sr_session_bus(user_data, &packet);
739 packet.type = SR_DF_LOGIC;
740 packet.length = ols->limit_samples * 4;
742 packet.payload = ols->raw_sample_buf;
743 sr_session_bus(user_data, &packet);
745 free(ols->raw_sample_buf);
749 packet.type = SR_DF_END;
751 sr_session_bus(user_data, &packet);
757 static int hw_start_acquisition(int device_index, gpointer session_device_id)
759 struct sr_datafeed_packet *packet;
760 struct sr_datafeed_header *header;
761 struct sr_device_instance *sdi;
762 struct ols_device *ols;
763 uint32_t trigger_config[4];
765 uint16_t readcount, delaycount;
766 uint8_t changrp_mask;
769 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
773 if (sdi->status != SR_ST_ACTIVE)
776 readcount = ols->limit_samples / 4;
778 memset(trigger_config, 0, 16);
779 trigger_config[ols->num_stages - 1] |= 0x08;
780 if (ols->trigger_mask[0]) {
781 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
782 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
784 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
785 reverse32(ols->trigger_mask[0])) != SR_OK)
787 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
788 reverse32(ols->trigger_value[0])) != SR_OK)
790 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
791 trigger_config[0]) != SR_OK)
794 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
795 reverse32(ols->trigger_mask[1])) != SR_OK)
797 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
798 reverse32(ols->trigger_value[1])) != SR_OK)
800 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
801 trigger_config[1]) != SR_OK)
804 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
805 reverse32(ols->trigger_mask[2])) != SR_OK)
807 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
808 reverse32(ols->trigger_value[2])) != SR_OK)
810 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
811 trigger_config[2]) != SR_OK)
814 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
815 reverse32(ols->trigger_mask[3])) != SR_OK)
817 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
818 reverse32(ols->trigger_value[3])) != SR_OK)
820 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
821 trigger_config[3]) != SR_OK)
824 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
825 ols->trigger_mask[0]) != SR_OK)
827 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
828 ols->trigger_value[0]) != SR_OK)
830 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
831 0x00000008) != SR_OK)
833 delaycount = readcount;
836 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
837 ols->cur_samplerate, ols->cur_samplerate_divider,
838 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
839 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
840 reverse32(ols->cur_samplerate_divider)) != SR_OK)
843 /* Send sample limit and pre/post-trigger capture ratio. */
844 data = ((readcount - 1) & 0xffff) << 16;
845 data |= (delaycount - 1) & 0xffff;
846 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
850 * Enable/disable channel groups in the flag register according to the
854 for (i = 0; i < 4; i++) {
855 if (ols->probe_mask & (0xff << (i * 8)))
856 changrp_mask |= (1 << i);
859 /* The flag register wants them here, and 1 means "disable channel". */
860 ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
861 ols->flag_reg |= FLAG_FILTER;
862 data = ols->flag_reg << 24;
863 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
866 /* Start acquisition on the device. */
867 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
870 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
873 /* Send header packet to the session bus. */
874 packet = g_malloc(sizeof(struct sr_datafeed_packet));
875 header = g_malloc(sizeof(struct sr_datafeed_header));
876 if (!packet || !header)
878 packet->type = SR_DF_HEADER;
879 packet->length = sizeof(struct sr_datafeed_header);
880 packet->payload = (unsigned char *)header;
881 header->feed_version = 1;
882 gettimeofday(&header->starttime, NULL);
883 header->samplerate = ols->cur_samplerate;
884 header->protocol_id = SR_PROTO_RAW;
885 header->num_logic_probes = NUM_PROBES;
886 header->num_analog_probes = 0;
887 sr_session_bus(session_device_id, packet);
894 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
896 struct sr_datafeed_packet packet;
898 /* Avoid compiler warnings. */
899 device_index = device_index;
901 packet.type = SR_DF_END;
903 sr_session_bus(session_device_id, &packet);
906 struct sr_device_plugin ols_plugin_info = {
908 "Openbench Logic Sniffer",
917 hw_set_configuration,
918 hw_start_acquisition,