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 if (!(ols = g_try_malloc0(sizeof(struct ols_device)))) {
178 sr_err("ols: %s: ols malloc failed", __func__);
182 ols->trigger_at = -1;
183 ols->probe_mask = 0xffffffff;
184 ols->cur_samplerate = SR_KHZ(200);
189 static struct sr_device_instance *get_metadata(int fd)
191 struct sr_device_instance *sdi;
192 struct ols_device *ols;
194 uint8_t key, type, token;
195 GString *tmp_str, *devicename, *version;
198 sdi = sr_device_instance_new(0, SR_ST_INACTIVE, NULL, NULL, NULL);
199 ols = ols_device_new();
202 devicename = g_string_new("");
203 version = g_string_new("");
207 if (serial_read(fd, &key, 1) != 1 || key == 0x00)
213 /* NULL-terminated string */
214 tmp_str = g_string_new("");
215 while (serial_read(fd, &tmp_c, 1) == 1 && tmp_c != '\0')
216 g_string_append_c(tmp_str, tmp_c);
217 sr_dbg("ols: got metadata key 0x%.2x value '%s'",
222 devicename = g_string_append(devicename, tmp_str->str);
225 /* FPGA firmware version */
227 g_string_append(version, ", ");
228 g_string_append(version, "FPGA version ");
229 g_string_append(version, tmp_str->str);
232 /* Ancillary version */
234 g_string_append(version, ", ");
235 g_string_append(version, "Ancillary version ");
236 g_string_append(version, tmp_str->str);
239 sr_info("ols: unknown token 0x%.2x: '%s'",
240 token, tmp_str->str);
243 g_string_free(tmp_str, TRUE);
246 /* 32-bit unsigned integer */
247 if (serial_read(fd, &tmp_int, 4) != 4)
249 tmp_int = reverse32(tmp_int);
250 sr_dbg("ols: got metadata key 0x%.2x value 0x%.8x",
254 /* Number of usable probes */
255 ols->num_probes = tmp_int;
258 /* Amount of sample memory available (bytes) */
259 ols->max_samples = tmp_int;
262 /* Amount of dynamic memory available (bytes) */
263 /* what is this for? */
266 /* Maximum sample rate (hz) */
267 ols->max_samplerate = tmp_int;
270 /* protocol version */
271 ols->protocol_version = tmp_int;
274 sr_info("ols: unknown token 0x%.2x: 0x%.8x",
280 /* 8-bit unsigned integer */
281 if (serial_read(fd, &tmp_c, 1) != 1)
283 sr_dbg("ols: got metadata key 0x%.2x value 0x%.2x",
287 /* Number of usable probes */
288 ols->num_probes = tmp_c;
291 /* protocol version */
292 ols->protocol_version = tmp_c;
295 sr_info("ols: unknown token 0x%.2x: 0x%.2x",
306 sdi->model = devicename->str;
307 sdi->version = version->str;
308 g_string_free(devicename, FALSE);
309 g_string_free(version, FALSE);
315 static int hw_init(const char *deviceinfo)
317 struct sr_device_instance *sdi;
318 struct ols_device *ols;
320 GPollFD *fds, probefd;
321 int devcnt, final_devcnt, num_ports, fd, ret, i;
322 char buf[8], **device_names, **serial_params;
325 ports = g_slist_append(NULL, strdup(deviceinfo));
327 /* No specific device given, so scan all serial ports. */
328 ports = list_serial_ports();
330 num_ports = g_slist_length(ports);
331 fds = calloc(1, num_ports * sizeof(GPollFD));
332 device_names = malloc(num_ports * sizeof(char *));
333 serial_params = malloc(num_ports * sizeof(char *));
335 for (l = ports; l; l = l->next) {
336 /* The discovery procedure is like this: first send the Reset
337 * command (0x00) 5 times, since the device could be anywhere
338 * in a 5-byte command. Then send the ID command (0x02).
339 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
342 * Since it may take the device a while to respond at 115Kb/s,
343 * we do all the sending first, then wait for all of them to
344 * respond with g_poll().
346 sr_info("ols: probing %s...", (char *)l->data);
347 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
349 serial_params[devcnt] = serial_backup_params(fd);
350 serial_set_params(fd, 115200, 8, 0, 1, 2);
352 for (i = 0; i < 5; i++) {
353 if ((ret = send_shortcommand(fd,
354 CMD_RESET)) != SR_OK) {
355 /* Serial port is not writable. */
360 serial_restore_params(fd,
361 serial_params[devcnt]);
365 send_shortcommand(fd, CMD_ID);
367 fds[devcnt].events = G_IO_IN;
368 device_names[devcnt] = strdup(l->data);
374 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
378 g_poll(fds, devcnt, 1);
380 for (i = 0; i < devcnt; i++) {
381 if (fds[i].revents != G_IO_IN)
383 if (serial_read(fds[i].fd, buf, 4) != 4)
385 if (strncmp(buf, "1SLO", 4) && strncmp(buf, "1ALS", 4))
388 /* definitely using the OLS protocol, check if it supports
389 * the metadata command
391 send_shortcommand(fds[i].fd, CMD_METADATA);
392 probefd.fd = fds[i].fd;
393 probefd.events = G_IO_IN;
394 if (g_poll(&probefd, 1, 10) > 0) {
396 sdi = get_metadata(fds[i].fd);
397 sdi->index = final_devcnt;
399 /* not an OLS -- some other board that uses the sump protocol */
400 sdi = sr_device_instance_new(final_devcnt, SR_ST_INACTIVE,
401 "Sump", "Logic Analyzer", "v1.0");
402 ols = ols_device_new();
403 ols->num_probes = 32;
406 sdi->serial = sr_serial_device_instance_new(device_names[i], -1);
407 device_instances = g_slist_append(device_instances, sdi);
409 serial_close(fds[i].fd);
414 /* clean up after all the probing */
415 for (i = 0; i < devcnt; i++) {
416 if (fds[i].fd != 0) {
417 serial_restore_params(fds[i].fd, serial_params[i]);
418 serial_close(fds[i].fd);
420 free(serial_params[i]);
421 free(device_names[i]);
432 static int hw_opendev(int device_index)
434 struct sr_device_instance *sdi;
436 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
439 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
440 if (sdi->serial->fd == -1)
443 sdi->status = SR_ST_ACTIVE;
448 static void hw_closedev(int device_index)
450 struct sr_device_instance *sdi;
452 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
455 if (sdi->serial->fd != -1) {
456 serial_close(sdi->serial->fd);
457 sdi->serial->fd = -1;
458 sdi->status = SR_ST_INACTIVE;
462 static void hw_cleanup(void)
465 struct sr_device_instance *sdi;
467 /* Properly close all devices. */
468 for (l = device_instances; l; l = l->next) {
470 if (sdi->serial->fd != -1)
471 serial_close(sdi->serial->fd);
472 sr_device_instance_free(sdi);
474 g_slist_free(device_instances);
475 device_instances = NULL;
478 static void *hw_get_device_info(int device_index, int device_info_id)
480 struct sr_device_instance *sdi;
481 struct ols_device *ols;
484 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
489 switch (device_info_id) {
493 case SR_DI_NUM_PROBES:
494 info = GINT_TO_POINTER(NUM_PROBES);
496 case SR_DI_SAMPLERATES:
499 case SR_DI_TRIGGER_TYPES:
500 info = (char *)TRIGGER_TYPES;
502 case SR_DI_CUR_SAMPLERATE:
503 info = &ols->cur_samplerate;
510 static int hw_get_status(int device_index)
512 struct sr_device_instance *sdi;
514 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
515 return SR_ST_NOT_FOUND;
520 static int *hw_get_capabilities(void)
525 static int set_configuration_samplerate(struct sr_device_instance *sdi,
528 struct ols_device *ols;
531 if (ols->max_samplerate) {
532 if (samplerate > ols->max_samplerate)
533 return SR_ERR_SAMPLERATE;
534 } else if (samplerate < samplerates.low || samplerate > samplerates.high)
535 return SR_ERR_SAMPLERATE;
537 ols->cur_samplerate = samplerate;
538 if (samplerate > CLOCK_RATE) {
539 ols->flag_reg |= FLAG_DEMUX;
540 ols->cur_samplerate_divider = (CLOCK_RATE * 2 / samplerate) - 1;
542 ols->flag_reg &= ~FLAG_DEMUX;
543 ols->cur_samplerate_divider = (CLOCK_RATE / samplerate) - 1;
549 static int hw_set_configuration(int device_index, int capability, void *value)
551 struct sr_device_instance *sdi;
552 struct ols_device *ols;
556 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
560 if (sdi->status != SR_ST_ACTIVE)
563 switch (capability) {
564 case SR_HWCAP_SAMPLERATE:
566 ret = set_configuration_samplerate(sdi, *tmp_u64);
568 case SR_HWCAP_PROBECONFIG:
569 ret = configure_probes(ols, (GSList *) value);
571 case SR_HWCAP_LIMIT_SAMPLES:
573 if (*tmp_u64 < MIN_NUM_SAMPLES)
575 ols->limit_samples = *tmp_u64;
576 sr_info("ols: sample limit %" PRIu64, ols->limit_samples);
579 case SR_HWCAP_CAPTURE_RATIO:
581 ols->capture_ratio = *tmp_u64;
582 if (ols->capture_ratio < 0 || ols->capture_ratio > 100) {
583 ols->capture_ratio = 0;
595 static int receive_data(int fd, int revents, void *user_data)
597 struct sr_datafeed_packet packet;
598 struct sr_device_instance *sdi;
599 struct ols_device *ols;
601 int count, buflen, num_channels, offset, i, j;
602 unsigned char byte, *buffer;
604 /* find this device's ols_device struct by its fd */
606 for (l = device_instances; l; l = l->next) {
608 if (sdi->serial->fd == fd) {
614 /* shouldn't happen */
617 if (ols->num_transfers++ == 0) {
619 * First time round, means the device started sending data,
620 * and will not stop until done. If it stops sending for
621 * longer than it takes to send a byte, that means it's
622 * finished. We'll double that to 30ms to be sure...
624 sr_source_remove(fd);
625 sr_source_add(fd, G_IO_IN, 30, receive_data, user_data);
626 ols->raw_sample_buf = malloc(ols->limit_samples * 4);
627 /* fill with 1010... for debugging */
628 memset(ols->raw_sample_buf, 0x82, ols->limit_samples * 4);
632 for (i = 0x20; i > 0x02; i /= 2) {
633 if ((ols->flag_reg & i) == 0)
637 if (revents == G_IO_IN
638 && ols->num_transfers / num_channels <= ols->limit_samples) {
639 if (serial_read(fd, &byte, 1) != 1)
642 ols->sample[ols->num_bytes++] = byte;
643 sr_dbg("ols: received byte 0x%.2x", byte);
644 if (ols->num_bytes == num_channels) {
645 /* Got a full sample. */
646 sr_dbg("ols: received sample 0x%.*x",
647 ols->num_bytes * 2, (int) *ols->sample);
648 if (ols->flag_reg & FLAG_RLE) {
650 * In RLE mode -1 should never come in as a
651 * sample, because bit 31 is the "count" flag.
652 * TODO: Endianness may be wrong here, could be
655 if (ols->sample[0] & 0x80
656 && !(ols->last_sample[0] & 0x80)) {
657 count = (int)(*ols->sample) & 0x7fffffff;
658 if (!(buffer = g_try_malloc(count))) {
659 sr_err("ols: %s: buffer malloc "
665 for (i = 0; i < count; i++) {
666 memcpy(buffer + buflen, ols->last_sample, 4);
671 * Just a single sample, next sample
672 * will probably be a count referring
673 * to this -- but this one is still a
674 * part of the stream.
676 buffer = ols->sample;
680 /* No compression. */
681 buffer = ols->sample;
685 if (num_channels < 4) {
687 * Some channel groups may have been turned
688 * off, to speed up transfer between the
689 * hardware and the PC. Expand that here before
690 * submitting it over the session bus --
691 * whatever is listening on the bus will be
692 * expecting a full 32-bit sample, based on
693 * the number of probes.
696 memset(ols->tmp_sample, 0, 4);
697 for (i = 0; i < 4; i++) {
698 if (((ols->flag_reg >> 2) & (1 << i)) == 0) {
700 * This channel group was
701 * enabled, copy from received
704 ols->tmp_sample[i] = ols->sample[j++];
707 memcpy(ols->sample, ols->tmp_sample, 4);
708 sr_dbg("ols: full sample 0x%.8x", (int) *ols->sample);
711 /* the OLS sends its sample buffer backwards.
712 * store it in reverse order here, so we can dump
713 * this on the session bus later.
715 offset = (ols->limit_samples - ols->num_transfers / num_channels) * 4;
716 memcpy(ols->raw_sample_buf + offset, ols->sample, 4);
718 if (buffer == ols->sample)
719 memcpy(ols->last_sample, buffer, num_channels);
723 memset(ols->sample, 0, 4);
728 * This is the main loop telling us a timeout was reached, or
729 * we've acquired all the samples we asked for -- we're done.
730 * Send the (properly-ordered) buffer to the frontend.
732 if (ols->trigger_at != -1) {
733 /* a trigger was set up, so we need to tell the frontend
736 if (ols->trigger_at > 0) {
737 /* there are pre-trigger samples, send those first */
738 packet.type = SR_DF_LOGIC;
739 packet.length = ols->trigger_at * 4;
741 packet.payload = ols->raw_sample_buf;
742 sr_session_bus(user_data, &packet);
745 packet.type = SR_DF_TRIGGER;
747 sr_session_bus(user_data, &packet);
749 packet.type = SR_DF_LOGIC;
750 packet.length = (ols->limit_samples * 4) - (ols->trigger_at * 4);
752 packet.payload = ols->raw_sample_buf + ols->trigger_at * 4;
753 sr_session_bus(user_data, &packet);
755 packet.type = SR_DF_LOGIC;
756 packet.length = ols->limit_samples * 4;
758 packet.payload = ols->raw_sample_buf;
759 sr_session_bus(user_data, &packet);
761 free(ols->raw_sample_buf);
765 packet.type = SR_DF_END;
767 sr_session_bus(user_data, &packet);
773 static int hw_start_acquisition(int device_index, gpointer session_device_id)
775 struct sr_datafeed_packet *packet;
776 struct sr_datafeed_header *header;
777 struct sr_device_instance *sdi;
778 struct ols_device *ols;
779 uint32_t trigger_config[4];
781 uint16_t readcount, delaycount;
782 uint8_t changrp_mask;
785 if (!(sdi = sr_get_device_instance(device_instances, device_index)))
789 if (sdi->status != SR_ST_ACTIVE)
792 readcount = ols->limit_samples / 4;
794 memset(trigger_config, 0, 16);
795 trigger_config[ols->num_stages - 1] |= 0x08;
796 if (ols->trigger_mask[0]) {
797 delaycount = readcount * (1 - ols->capture_ratio / 100.0);
798 ols->trigger_at = (readcount - delaycount) * 4 - ols->num_stages;
800 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
801 reverse32(ols->trigger_mask[0])) != SR_OK)
803 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
804 reverse32(ols->trigger_value[0])) != SR_OK)
806 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
807 trigger_config[0]) != SR_OK)
810 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
811 reverse32(ols->trigger_mask[1])) != SR_OK)
813 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
814 reverse32(ols->trigger_value[1])) != SR_OK)
816 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
817 trigger_config[1]) != SR_OK)
820 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
821 reverse32(ols->trigger_mask[2])) != SR_OK)
823 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
824 reverse32(ols->trigger_value[2])) != SR_OK)
826 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
827 trigger_config[2]) != SR_OK)
830 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
831 reverse32(ols->trigger_mask[3])) != SR_OK)
833 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
834 reverse32(ols->trigger_value[3])) != SR_OK)
836 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
837 trigger_config[3]) != SR_OK)
840 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
841 ols->trigger_mask[0]) != SR_OK)
843 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
844 ols->trigger_value[0]) != SR_OK)
846 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
847 0x00000008) != SR_OK)
849 delaycount = readcount;
852 sr_info("ols: setting samplerate to %" PRIu64 " Hz (divider %u, "
853 "demux %s)", ols->cur_samplerate, ols->cur_samplerate_divider,
854 ols->flag_reg & FLAG_DEMUX ? "on" : "off");
855 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
856 reverse32(ols->cur_samplerate_divider)) != SR_OK)
859 /* Send sample limit and pre/post-trigger capture ratio. */
860 data = ((readcount - 1) & 0xffff) << 16;
861 data |= (delaycount - 1) & 0xffff;
862 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SR_OK)
866 * Enable/disable channel groups in the flag register according to the
870 for (i = 0; i < 4; i++) {
871 if (ols->probe_mask & (0xff << (i * 8)))
872 changrp_mask |= (1 << i);
875 /* The flag register wants them here, and 1 means "disable channel". */
876 ols->flag_reg |= ~(changrp_mask << 2) & 0x3c;
877 ols->flag_reg |= FLAG_FILTER;
878 data = ols->flag_reg << 24;
879 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SR_OK)
882 /* Start acquisition on the device. */
883 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SR_OK)
886 sr_source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
889 if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) {
890 sr_err("ols: %s: packet malloc failed", __func__);
891 return SR_ERR_MALLOC;
894 if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) {
895 sr_err("ols: %s: header malloc failed", __func__);
896 return SR_ERR_MALLOC;
899 /* Send header packet to the session bus. */
900 packet->type = SR_DF_HEADER;
901 packet->length = sizeof(struct sr_datafeed_header);
902 packet->payload = (unsigned char *)header;
903 header->feed_version = 1;
904 gettimeofday(&header->starttime, NULL);
905 header->samplerate = ols->cur_samplerate;
906 header->protocol_id = SR_PROTO_RAW;
907 header->num_logic_probes = NUM_PROBES;
908 header->num_analog_probes = 0;
909 sr_session_bus(session_device_id, packet);
916 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
918 struct sr_datafeed_packet packet;
920 /* Avoid compiler warnings. */
921 device_index = device_index;
923 packet.type = SR_DF_END;
925 sr_session_bus(session_device_id, &packet);
928 struct sr_device_plugin ols_plugin_info = {
930 "Openbench Logic Sniffer",
939 hw_set_configuration,
940 hw_start_acquisition,