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 sr_dbg("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 sr_dbg("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 sr_dbg("ols: got metadata key 0x%.2x value '%s'",
218 devicename = g_string_append(devicename, tmp_str->str);
221 /* FPGA firmware version */
223 g_string_append(version, ", ");
224 g_string_append(version, "FPGA version ");
225 g_string_append(version, tmp_str->str);
228 /* Ancillary version */
230 g_string_append(version, ", ");
231 g_string_append(version, "Ancillary version ");
232 g_string_append(version, tmp_str->str);
235 sr_info("ols: unknown token 0x%.2x: '%s'",
236 token, tmp_str->str);
239 g_string_free(tmp_str, TRUE);
242 /* 32-bit unsigned integer */
243 if (serial_read(fd, &tmp_int, 4) != 4)
245 tmp_int = reverse32(tmp_int);
246 sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x",
250 /* Number of usable probes */
251 ols->num_probes = tmp_int;
254 /* Amount of sample memory available (bytes) */
255 ols->max_samples = tmp_int;
258 /* Amount of dynamic memory available (bytes) */
259 /* what is this for? */
262 /* Maximum sample rate (hz) */
263 ols->max_samplerate = tmp_int;
266 /* protocol version */
267 ols->protocol_version = tmp_int;
270 sr_info("ols: unknown token 0x%.2x: 0x%.8x",
276 /* 8-bit unsigned integer */
277 if (serial_read(fd, &tmp_c, 1) != 1)
279 sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x",
283 /* Number of usable probes */
284 ols->num_probes = tmp_c;
287 /* protocol version */
288 ols->protocol_version = tmp_c;
291 sr_info("ols: unknown token 0x%.2x: 0x%.2x",
302 sdi->model = devicename->str;
303 sdi->version = version->str;
304 g_string_free(devicename, FALSE);
305 g_string_free(version, FALSE);
311 static int hw_init(const char *deviceinfo)
313 struct sr_device_instance *sdi;
314 struct ols_device *ols;
316 GPollFD *fds, probefd;
317 int devcnt, final_devcnt, num_ports, fd, ret, i;
318 char buf[8], **device_names, **serial_params;
321 ports = g_slist_append(NULL, strdup(deviceinfo));
323 /* No specific device given, so scan all serial ports. */
324 ports = list_serial_ports();
326 num_ports = g_slist_length(ports);
327 fds = calloc(1, num_ports * sizeof(GPollFD));
328 device_names = malloc(num_ports * sizeof(char *));
329 serial_params = malloc(num_ports * sizeof(char *));
331 for (l = ports; l; l = l->next) {
332 /* The discovery procedure is like this: first send the Reset
333 * command (0x00) 5 times, since the device could be anywhere
334 * in a 5-byte command. Then send the ID command (0x02).
335 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
338 * Since it may take the device a while to respond at 115Kb/s,
339 * we do all the sending first, then wait for all of them to
340 * respond with g_poll().
342 sr_info("ols: probing %s...", (char *)l->data);
343 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
345 serial_params[devcnt] = serial_backup_params(fd);
346 serial_set_params(fd, 115200, 8, 0, 1, 2);
348 for (i = 0; i < 5; i++) {
349 if ((ret = send_shortcommand(fd,
350 CMD_RESET)) != SR_OK) {
351 /* Serial port is not writable. */
356 serial_restore_params(fd,
357 serial_params[devcnt]);
361 send_shortcommand(fd, CMD_ID);
363 fds[devcnt].events = G_IO_IN;
364 device_names[devcnt] = strdup(l->data);
370 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
374 g_poll(fds, devcnt, 1);
376 for (i = 0; i < devcnt; i++) {
377 if (fds[i].revents != G_IO_IN)
379 if (serial_read(fds[i].fd, buf, 4) != 4)
381 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
384 /* definitely using the OLS protocol, check if it supports
385 * the metadata command
387 send_shortcommand(fds[i].fd, CMD_METADATA);
388 probefd.fd = fds[i].fd;
389 probefd.events = G_IO_IN;
390 if (g_poll(&probefd, 1, 10) > 0) {
392 sdi = get_metadata(fds[i].fd);
393 sdi->index = final_devcnt;
395 /* not an OLS -- some other board that uses the sump protocol */
396 sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE,
397 "Sump", "Logic Analyzer", "v1.0");
398 ols = ols_device_new();
399 ols->num_probes = 32;
402 sdi->serial = sr_serial_device_instance_new(device_names[i], -1);
403 device_instances = g_slist_append(device_instances, sdi);
405 serial_close(fds[i].fd);
410 /* clean up after all the probing */
411 for (i = 0; i < devcnt; i++) {
412 if (fds[i].fd != 0) {
413 serial_restore_params(fds[i].fd, serial_params[i]);
414 serial_close(fds[i].fd);
416 free(serial_params[i]);
417 free(device_names[i]);
428 static int hw_opendev(int device_index)
430 struct sr_device_instance *sdi;
432 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
435 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
436 if (sdi->serial->fd == -1)
439 sdi->status = SR_ST_ACTIVE;
444 static void hw_closedev(int device_index)
446 struct sr_device_instance *sdi;
448 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
451 if (sdi->serial->fd != -1) {
452 serial_close(sdi->serial->fd);
453 sdi->serial->fd = -1;
454 sdi->status = SR_ST_INACTIVE;
458 static void hw_cleanup(void)
461 struct sr_device_instance *sdi;
463 /* Properly close all devices. */
464 for (l = device_instances; l; l = l->next) {
466 if (sdi->serial->fd != -1)
467 serial_close(sdi->serial->fd);
468 sr_device_instance_free(sdi);
470 g_slist_free(device_instances);
471 device_instances = NULL;
474 static void *hw_get_device_info(int device_index, int device_info_id)
476 struct sr_device_instance *sdi;
477 struct ols_device *ols;
480 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
485 switch (device_info_id) {
489 case SR_DI_NUM_PROBES:
490 info = GINT_TO_POINTER(NUM_PROBES);
492 case SR_DI_SAMPLERATES:
495 case SR_DI_TRIGGER_TYPES:
496 info = (char *)TRIGGER_TYPES;
498 case SR_DI_CUR_SAMPLERATE:
499 info = &ols->cur_samplerate;
506 static int hw_get_status(int device_index)
508 struct sr_device_instance *sdi;
510 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
511 return SR_ST_NOT_FOUND;
516 static int *hw_get_capabilities(void)
521 static int set_configuration_samplerate(struct sr_device_instance *sdi,
524 struct ols_device *ols;
527 if (ols->max_samplerate) {
528 if (samplerate > ols->max_samplerate)
529 return SR_ERR_SAMPLERATE;
530 } else if (samplerate < samplerates.low || samplerate > samplerates.high)
531 return SR_ERR_SAMPLERATE;
533 ols->cur_samplerate = samplerate;
534 if (samplerate > CLOCK_RATE) {
535 ols->flag_reg |= FLAG_DEMUX;
536 ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
538 ols->flag_reg &= ~FLAG_DEMUX;
539 ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
545 static int hw_set_configuration(int device_index, int capability, void *value)
547 struct sr_device_instance *sdi;
548 struct ols_device *ols;
552 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
556 if (sdi->status != SR_ST_ACTIVE)
559 switch (capability) {
560 case SR_HWCAP_SAMPLERATE:
562 ret = set_configuration_samplerate(sdi, *tmp_u64);
564 case SR_HWCAP_PROBECONFIG:
565 ret = configure_probes(ols, (GSList *) value);
567 case SR_HWCAP_LIMIT_SAMPLES:
569 if (*tmp_u64 < MIN_NUM_SAMPLES)
571 ols->limit_samples = *tmp_u64;
572 sr_info("ols: sample limit %" PRIu64, ols->limit_samples);
575 case SR_HWCAP_CAPTURE_RATIO:
577 ols->capture_ratio = *tmp_u64;
578 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
579 ols->capture_ratio = 0;
591 static int receive_data(int fd, int revents, void *user_data)
593 struct sr_datafeed_packet packet;
594 struct sr_device_instance *sdi;
595 struct ols_device *ols;
597 int count, buflen, num_channels, offset, i, j;
598 unsigned char byte, *buffer;
600 /* find this device's ols_device struct by its fd */
602 for (l = device_instances; l; l = l->next) {
604 if (sdi->serial->fd == fd) {
610 /* shouldn't happen */
613 if (ols->num_transfers++ == 0) {
615 * First time round, means the device started sending data,
616 * and will not stop until done. If it stops sending for
617 * longer than it takes to send a byte, that means it's
618 * finished. We'll double that to 30ms to be sure...
620 sr_source_remove(fd);
621 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
622 ols->raw_sample_buf = malloc(ols->limit_samples * 4);
623 /* fill with 1010... for debugging */
624 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
628 for (i = 0x20; i > 0x02; i /= 2) {
629 if ((ols->flag_reg & i) == 0)
633 if (revents == G_IO_IN
634 && ols->num_transfers / num_channels <= ols->limit_samples) {
635 if (serial_read(fd, &byte, 1) != 1)
638 ols->sample[ols->num_bytes++] = byte;
639 sr_dbg("ols: received byte 0x%.2x", byte);
640 if (ols->num_bytes == num_channels) {
641 /* Got a full sample. */
642 sr_dbg("ols: received sample 0x%.*x",
643 ols->num_bytes * 2, (int) *ols->sample);
644 if (ols->flag_reg & FLAG_RLE) {
646 * In RLE mode -1 should never come in as a
647 * sample, because bit 31 is the "count" flag.
648 * TODO: Endianness may be wrong here, could be
651 if (ols->sample[0] & 0x80
652 && !(ols->last_sample[0] & 0x80)) {
653 count = (int)(*ols->sample) & 0x7fffffff;
654 buffer = g_malloc(count);
656 for (i = 0; i < count; i++) {
657 memcpy(buffer + buflen, ols->last_sample, 4);
662 * Just a single sample, next sample
663 * will probably be a count referring
664 * to this -- but this one is still a
665 * part of the stream.
667 buffer = ols->sample;
671 /* No compression. */
672 buffer = ols->sample;
676 if (num_channels < 4) {
678 * Some channel groups may have been turned
679 * off, to speed up transfer between the
680 * hardware and the PC. Expand that here before
681 * submitting it over the session bus --
682 * whatever is listening on the bus will be
683 * expecting a full 32-bit sample, based on
684 * the number of probes.
687 memset(ols->tmp_sample, 0, 4);
688 for (i = 0; i < 4; i++) {
689 if (((ols->flag_reg >> 2) & (1 << i)) == 0) {
691 * This channel group was
692 * enabled, copy from received
695 ols->tmp_sample[i] = ols->sample[j++];
698 memcpy(ols->sample, ols->tmp_sample, 4);
699 sr_dbg("ols: full sample 0x%.8x", (int) *ols->sample);
702 /* the OLS sends its sample buffer backwards.
703 * store it in reverse order here, so we can dump
704 * this on the session bus later.
706 offset = (ols->limit_samples - ols->num_transfers / num_channels) * 4;
707 memcpy(ols->raw_sample_buf + offset, ols->sample, 4);
709 if (buffer == ols->sample)
710 memcpy(ols->last_sample, buffer, num_channels);
714 memset(ols->sample, 0, 4);
719 * This is the main loop telling us a timeout was reached, or
720 * we've acquired all the samples we asked for -- we're done.
721 * Send the (properly-ordered) buffer to the frontend.
723 if (ols->trigger_at != -1) {
724 /* a trigger was set up, so we need to tell the frontend
727 if (ols->trigger_at > 0) {
728 /* there are pre-trigger samples, send those first */
729 packet.type = SR_DF_LOGIC;
730 packet.length = ols->trigger_at * 4;
732 packet.payload = ols->raw_sample_buf;
733 sr_session_bus(user_data, &packet);
736 packet.type = SR_DF_TRIGGER;
738 sr_session_bus(user_data, &packet);
740 packet.type = SR_DF_LOGIC;
741 packet.length = (ols->limit_samples * 4) - (ols->trigger_at * 4);
743 packet.payload = ols->raw_sample_buf + ols->trigger_at * 4;
744 sr_session_bus(user_data, &packet);
746 packet.type = SR_DF_LOGIC;
747 packet.length = ols->limit_samples * 4;
749 packet.payload = ols->raw_sample_buf;
750 sr_session_bus(user_data, &packet);
752 free(ols->raw_sample_buf);
756 packet.type = SR_DF_END;
758 sr_session_bus(user_data, &packet);
764 static int hw_start_acquisition(int device_index, gpointer session_device_id)
766 struct sr_datafeed_packet *packet;
767 struct sr_datafeed_header *header;
768 struct sr_device_instance *sdi;
769 struct ols_device *ols;
770 uint32_t trigger_config[4];
772 uint16_t readcount, delaycount;
773 uint8_t changrp_mask;
776 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
780 if (sdi->status != SR_ST_ACTIVE)
783 readcount = ols->limit_samples / 4;
785 memset(trigger_config, 0, 16);
786 trigger_config[ols->num_stages - 1] |= 0x08;
787 if (ols->trigger_mask[0]) {
788 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
789 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
791 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
792 reverse32(ols->trigger_mask[0])) != SR_OK)
794 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
795 reverse32(ols->trigger_value[0])) != SR_OK)
797 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
798 trigger_config[0]) != SR_OK)
801 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
802 reverse32(ols->trigger_mask[1])) != SR_OK)
804 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
805 reverse32(ols->trigger_value[1])) != SR_OK)
807 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
808 trigger_config[1]) != SR_OK)
811 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
812 reverse32(ols->trigger_mask[2])) != SR_OK)
814 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
815 reverse32(ols->trigger_value[2])) != SR_OK)
817 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
818 trigger_config[2]) != SR_OK)
821 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
822 reverse32(ols->trigger_mask[3])) != SR_OK)
824 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
825 reverse32(ols->trigger_value[3])) != SR_OK)
827 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
828 trigger_config[3]) != SR_OK)
831 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
832 ols->trigger_mask[0]) != SR_OK)
834 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
835 ols->trigger_value[0]) != SR_OK)
837 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
838 0x00000008) != SR_OK)
840 delaycount = readcount;
843 sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
844 "demux %s)", ols->cur_samplerate, ols->cur_samplerate_divider,
845 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
846 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
847 reverse32(ols->cur_samplerate_divider)) != SR_OK)
850 /* Send sample limit and pre/post-trigger capture ratio. */
851 data = ((readcount - 1) & 0xffff) << 16;
852 data |= (delaycount - 1) & 0xffff;
853 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
857 * Enable/disable channel groups in the flag register according to the
861 for (i = 0; i < 4; i++) {
862 if (ols->probe_mask & (0xff << (i * 8)))
863 changrp_mask |= (1 << i);
866 /* The flag register wants them here, and 1 means "disable channel". */
867 ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
868 ols->flag_reg |= FLAG_FILTER;
869 data = ols->flag_reg << 24;
870 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
873 /* Start acquisition on the device. */
874 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
877 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
880 /* Send header packet to the session bus. */
881 packet = g_malloc(sizeof(struct sr_datafeed_packet));
882 header = g_malloc(sizeof(struct sr_datafeed_header));
883 if (!packet || !header)
885 packet->type = SR_DF_HEADER;
886 packet->length = sizeof(struct sr_datafeed_header);
887 packet->payload = (unsigned char *)header;
888 header->feed_version = 1;
889 gettimeofday(&header->starttime, NULL);
890 header->samplerate = ols->cur_samplerate;
891 header->protocol_id = SR_PROTO_RAW;
892 header->num_logic_probes = NUM_PROBES;
893 header->num_analog_probes = 0;
894 sr_session_bus(session_device_id, packet);
901 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
903 struct sr_datafeed_packet packet;
905 /* Avoid compiler warnings. */
906 device_index = device_index;
908 packet.type = SR_DF_END;
910 sr_session_bus(session_device_id, &packet);
913 struct sr_device_plugin ols_plugin_info = {
915 "Openbench Logic Sniffer",
924 hw_set_configuration,
925 hw_start_acquisition,