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>
36 #include <arpa/inet.h>
40 #include <sigrok-internal.h>
43 #define O_NONBLOCK FIONBIO
47 #define NUM_TRIGGER_STAGES 4
48 #define TRIGGER_TYPES "01"
49 #define SERIAL_SPEED B115200
50 #define CLOCK_RATE 100000000
53 #define CMD_RESET 0x00
55 #define CMD_SET_FLAGS 0x82
56 #define CMD_SET_DIVIDER 0x80
58 #define CMD_CAPTURE_SIZE 0x81
59 #define CMD_SET_TRIGGER_MASK_0 0xc0
60 #define CMD_SET_TRIGGER_MASK_1 0xc4
61 #define CMD_SET_TRIGGER_MASK_2 0xc8
62 #define CMD_SET_TRIGGER_MASK_3 0xcc
63 #define CMD_SET_TRIGGER_VALUE_0 0xc1
64 #define CMD_SET_TRIGGER_VALUE_1 0xc5
65 #define CMD_SET_TRIGGER_VALUE_2 0xc9
66 #define CMD_SET_TRIGGER_VALUE_3 0xcd
67 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
68 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
69 #define CMD_SET_TRIGGER_CONFIG_2 0xca
70 #define CMD_SET_TRIGGER_CONFIG_3 0xce
72 /* Bitmasks for CMD_FLAGS */
73 #define FLAG_DEMUX 0x01
74 #define FLAG_FILTER 0x02
75 #define FLAG_CHANNELGROUP_1 0x04
76 #define FLAG_CHANNELGROUP_2 0x08
77 #define FLAG_CHANNELGROUP_3 0x10
78 #define FLAG_CHANNELGROUP_4 0x20
79 #define FLAG_CLOCK_EXTERNAL 0x40
80 #define FLAG_CLOCK_INVERTED 0x80
81 #define FLAG_RLE 0x0100
83 static int capabilities[] = {
91 static struct samplerates samplerates = {
98 /* List of struct serial_device_instance */
99 static GSList *device_instances = NULL;
101 /* Current state of the flag register */
102 static uint32_t flag_reg = 0;
104 static uint64_t cur_samplerate = 0;
105 static uint64_t limit_samples = 0;
107 * Pre/post trigger capture ratio, in percentage.
108 * 0 means no pre-trigger data.
110 static int capture_ratio = 0;
111 static int trigger_at = -1;
112 static uint32_t probe_mask = 0xffffffff;
113 static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
114 static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
115 static int num_stages = 0;
117 static int send_shortcommand(int fd, uint8_t command)
121 g_debug("ols: sending cmd 0x%.2x", command);
123 if (serial_write(fd, buf, 1) != 1)
129 static int send_longcommand(int fd, uint8_t command, uint32_t data)
133 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
135 buf[1] = (data & 0xff000000) >> 24;
136 buf[2] = (data & 0xff0000) >> 16;
137 buf[3] = (data & 0xff00) >> 8;
138 buf[4] = data & 0xff;
139 if (serial_write(fd, buf, 5) != 5)
145 static int configure_probes(GSList *probes)
149 int probe_bit, stage, i;
153 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
155 trigger_value[i] = 0;
159 for (l = probes; l; l = l->next) {
160 probe = (struct probe *)l->data;
165 * Set up the probe mask for later configuration into the
168 probe_bit = 1 << (probe->index - 1);
169 probe_mask |= probe_bit;
174 /* Configure trigger mask and value. */
176 for (tc = probe->trigger; tc && *tc; tc++) {
177 trigger_mask[stage] |= probe_bit;
179 trigger_value[stage] |= probe_bit;
183 * TODO: Only supporting parallel mode, with
188 if (stage > num_stages)
195 static uint32_t reverse16(uint32_t in)
199 out = (in & 0xff) << 8;
200 out |= (in & 0xff00) >> 8;
201 out |= (in & 0xff0000) << 8;
202 out |= (in & 0xff000000) >> 8;
207 static uint32_t reverse32(uint32_t in)
211 out = (in & 0xff) << 24;
212 out |= (in & 0xff00) << 8;
213 out |= (in & 0xff0000) >> 8;
214 out |= (in & 0xff000000) >> 24;
219 static int hw_init(char *deviceinfo)
221 struct sigrok_device_instance *sdi;
224 int devcnt, final_devcnt, num_ports, fd, ret, i;
225 char buf[8], **device_names, **serial_params;
228 ports = g_slist_append(NULL, strdup(deviceinfo));
230 /* No specific device given, so scan all serial ports. */
231 ports = list_serial_ports();
233 num_ports = g_slist_length(ports);
234 fds = calloc(1, num_ports * sizeof(GPollFD));
235 device_names = malloc(num_ports * sizeof(char *));
236 serial_params = malloc(num_ports * sizeof(char *));
238 for (l = ports; l; l = l->next) {
239 /* The discovery procedure is like this: first send the Reset
240 * command (0x00) 5 times, since the device could be anywhere
241 * in a 5-byte command. Then send the ID command (0x02).
242 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
245 * Since it may take the device a while to respond at 115Kb/s,
246 * we do all the sending first, then wait for all of them to
247 * respond with g_poll().
249 g_message("ols: probing %s...", (char *)l->data);
250 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
252 serial_params[devcnt] = serial_backup_params(fd);
253 serial_set_params(fd, 115200, 8, 0, 1, 2);
255 for (i = 0; i < 5; i++) {
256 if ((ret = send_shortcommand(fd,
257 CMD_RESET)) != SIGROK_OK) {
258 /* Serial port is not writable. */
262 if (ret != SIGROK_OK) {
263 serial_restore_params(fd,
264 serial_params[devcnt]);
268 send_shortcommand(fd, CMD_ID);
270 fds[devcnt].events = G_IO_IN;
271 device_names[devcnt] = strdup(l->data);
277 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
281 g_poll(fds, devcnt, 1);
282 for (i = 0; i < devcnt; i++) {
283 if (fds[i].revents == G_IO_IN) {
284 if (serial_read(fds[i].fd, buf, 4) == 4) {
285 if (!strncmp(buf, "1SLO", 4)
286 || !strncmp(buf, "1ALS", 4)) {
287 if (!strncmp(buf, "1SLO", 4))
288 sdi = sigrok_device_instance_new
289 (final_devcnt, ST_INACTIVE,
291 "Logic Sniffer", "v1.0");
293 sdi = sigrok_device_instance_new
294 (final_devcnt, ST_INACTIVE,
295 "Openbench", "Logic Sniffer",
297 sdi->serial = serial_device_instance_new
298 (device_names[i], -1);
300 g_slist_append(device_instances, sdi);
302 serial_close(fds[i].fd);
306 free(device_names[i]);
309 if (fds[i].fd != 0) {
310 serial_restore_params(fds[i].fd, serial_params[i]);
311 serial_close(fds[i].fd);
313 free(serial_params[i]);
321 cur_samplerate = samplerates.low;
326 static int hw_opendev(int device_index)
328 struct sigrok_device_instance *sdi;
330 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
333 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
334 if (sdi->serial->fd == -1)
337 sdi->status = ST_ACTIVE;
342 static void hw_closedev(int device_index)
344 struct sigrok_device_instance *sdi;
346 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
349 if (sdi->serial->fd != -1) {
350 serial_close(sdi->serial->fd);
351 sdi->serial->fd = -1;
352 sdi->status = ST_INACTIVE;
356 static void hw_cleanup(void)
359 struct sigrok_device_instance *sdi;
361 /* Properly close all devices. */
362 for (l = device_instances; l; l = l->next) {
364 if (sdi->serial->fd != -1)
365 serial_close(sdi->serial->fd);
366 sigrok_device_instance_free(sdi);
368 g_slist_free(device_instances);
369 device_instances = NULL;
372 static void *hw_get_device_info(int device_index, int device_info_id)
374 struct sigrok_device_instance *sdi;
377 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
381 switch (device_info_id) {
386 info = GINT_TO_POINTER(NUM_PROBES);
391 case DI_TRIGGER_TYPES:
392 info = (char *)TRIGGER_TYPES;
394 case DI_CUR_SAMPLERATE:
395 info = &cur_samplerate;
402 static int hw_get_status(int device_index)
404 struct sigrok_device_instance *sdi;
406 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
412 static int *hw_get_capabilities(void)
417 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
422 if (samplerate < samplerates.low || samplerate > samplerates.high)
423 return SIGROK_ERR_SAMPLERATE;
425 if (samplerate > CLOCK_RATE) {
426 flag_reg |= FLAG_DEMUX;
427 divider = (CLOCK_RATE * 2 / samplerate) - 1;
429 flag_reg &= ~FLAG_DEMUX;
430 divider = (CLOCK_RATE / samplerate) - 1;
433 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
434 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
436 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, reverse32(divider)) != SIGROK_OK)
438 cur_samplerate = samplerate;
443 static int hw_set_configuration(int device_index, int capability, void *value)
445 struct sigrok_device_instance *sdi;
449 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
452 if (sdi->status != ST_ACTIVE)
455 switch (capability) {
456 case HWCAP_SAMPLERATE:
458 ret = set_configuration_samplerate(sdi, *tmp_u64);
460 case HWCAP_PROBECONFIG:
461 ret = configure_probes((GSList *) value);
463 case HWCAP_LIMIT_SAMPLES:
465 limit_samples = *tmp_u64;
466 g_message("ols: sample limit %" PRIu64, limit_samples);
469 case HWCAP_CAPTURE_RATIO:
471 capture_ratio = *tmp_u64;
472 if (capture_ratio < 0 || capture_ratio > 100) {
485 static int receive_data(int fd, int revents, void *user_data)
487 static unsigned int num_transfers = 0;
488 static int num_bytes = 0;
489 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
490 static unsigned char sample[4] = { 0, 0, 0, 0 };
491 static unsigned char tmp_sample[4];
492 static unsigned char *raw_sample_buf = NULL;
493 int count, buflen, num_channels, offset, i, j;
494 struct datafeed_packet packet;
495 unsigned char byte, *buffer;
497 if (num_transfers++ == 0) {
499 * First time round, means the device started sending data,
500 * and will not stop until done. If it stops sending for
501 * longer than it takes to send a byte, that means it's
502 * finished. We'll double that to 30ms to be sure...
505 source_add(fd, G_IO_IN, 30, receive_data, user_data);
506 raw_sample_buf = malloc(limit_samples * 4);
507 /* fill with 1010... for debugging */
508 memset(raw_sample_buf, 0x82, limit_samples * 4);
512 for (i = 0x20; i > 0x02; i /= 2) {
513 if ((flag_reg & i) == 0)
517 if (revents == G_IO_IN
518 && num_transfers / num_channels <= limit_samples) {
519 if (serial_read(fd, &byte, 1) != 1)
522 sample[num_bytes++] = byte;
523 g_debug("ols: received byte 0x%.2x", byte);
524 if (num_bytes == num_channels) {
525 g_debug("ols: received sample 0x%.*x", num_bytes * 2, (int) *sample);
526 /* Got a full sample. */
527 if (flag_reg & FLAG_RLE) {
529 * In RLE mode -1 should never come in as a
530 * sample, because bit 31 is the "count" flag.
531 * TODO: Endianness may be wrong here, could be
535 && !(last_sample[0] & 0x80)) {
536 count = (int)(*sample) & 0x7fffffff;
537 buffer = g_malloc(count);
539 for (i = 0; i < count; i++) {
540 memcpy(buffer + buflen, last_sample, 4);
545 * Just a single sample, next sample
546 * will probably be a count referring
547 * to this -- but this one is still a
548 * part of the stream.
554 /* No compression. */
559 if (num_channels < 4) {
561 * Some channel groups may have been turned
562 * off, to speed up transfer between the
563 * hardware and the PC. Expand that here before
564 * submitting it over the session bus --
565 * whatever is listening on the bus will be
566 * expecting a full 32-bit sample, based on
567 * the number of probes.
570 memset(tmp_sample, 0, 4);
571 for (i = 0; i < 4; i++) {
572 if (((flag_reg >> 2) & (1 << i)) == 0) {
574 * This channel group was
575 * enabled, copy from received
578 tmp_sample[i] = sample[j++];
581 memcpy(sample, tmp_sample, 4);
582 g_debug("ols: full sample 0x%.8x", (int) *sample);
585 /* the OLS sends its sample buffer backwards.
586 * store it in reverse order here, so we can dump
587 * this on the session bus later.
589 offset = (limit_samples - num_transfers / num_channels) * 4;
590 memcpy(raw_sample_buf + offset, sample, 4);
592 if (buffer == sample)
593 memcpy(last_sample, buffer, num_channels);
597 memset(sample, 0, 4);
602 * This is the main loop telling us a timeout was reached, or
603 * we've acquired all the samples we asked for -- we're done.
604 * Send the (properly-ordered) buffer to the frontend.
606 if (trigger_at != -1) {
607 /* a trigger was set up, so we need to tell the frontend
610 if (trigger_at > 0) {
611 /* there are pre-trigger samples, send those first */
612 packet.type = DF_LOGIC;
613 packet.length = trigger_at * 4;
615 packet.payload = raw_sample_buf;
616 session_bus(user_data, &packet);
619 packet.type = DF_TRIGGER;
621 session_bus(user_data, &packet);
623 packet.type = DF_LOGIC;
624 packet.length = (limit_samples * 4) - (trigger_at * 4);
626 packet.payload = raw_sample_buf + trigger_at * 4;
627 session_bus(user_data, &packet);
629 packet.type = DF_LOGIC;
630 packet.length = limit_samples * 4;
632 packet.payload = raw_sample_buf;
633 session_bus(user_data, &packet);
635 free(raw_sample_buf);
639 packet.type = DF_END;
641 session_bus(user_data, &packet);
647 static int hw_start_acquisition(int device_index, gpointer session_device_id)
650 struct datafeed_packet *packet;
651 struct datafeed_header *header;
652 struct sigrok_device_instance *sdi;
653 uint32_t trigger_config[4];
655 uint16_t readcount, delaycount;
656 uint8_t changrp_mask;
658 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
661 if (sdi->status != ST_ACTIVE)
664 readcount = limit_samples / 4;
666 memset(trigger_config, 0, 16);
667 trigger_config[num_stages-1] |= 0x08;
668 if (trigger_mask[0]) {
669 delaycount = readcount * (1 - capture_ratio / 100.0);
670 trigger_at = (readcount - delaycount) * 4 - num_stages;
672 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
673 reverse32(trigger_mask[0])) != SIGROK_OK)
675 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
676 reverse32(trigger_value[0])) != SIGROK_OK)
678 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
679 trigger_config[0]) != SIGROK_OK)
682 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
683 reverse32(trigger_mask[1])) != SIGROK_OK)
685 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
686 reverse32(trigger_value[1])) != SIGROK_OK)
688 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
689 trigger_config[1]) != SIGROK_OK)
692 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
693 reverse32(trigger_mask[2])) != SIGROK_OK)
695 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
696 reverse32(trigger_value[2])) != SIGROK_OK)
698 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
699 trigger_config[2]) != SIGROK_OK)
702 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
703 reverse32(trigger_mask[3])) != SIGROK_OK)
705 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
706 reverse32(trigger_value[3])) != SIGROK_OK)
708 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
709 trigger_config[3]) != SIGROK_OK)
712 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
713 trigger_mask[0]) != SIGROK_OK)
715 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
716 trigger_value[0]) != SIGROK_OK)
718 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
719 0x00000008) != SIGROK_OK)
721 delaycount = readcount;
724 set_configuration_samplerate(sdi, cur_samplerate);
726 /* Send sample limit and pre/post-trigger capture ratio. */
727 data = ((readcount - 1) & 0xffff) << 16;
728 data |= (delaycount - 1) & 0xffff;
729 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SIGROK_OK)
733 * Enable/disable channel groups in the flag register according to the
737 for (i = 0; i < 4; i++) {
738 if (probe_mask & (0xff << (i * 8)))
739 changrp_mask |= (1 << i);
742 /* The flag register wants them here, and 1 means "disable channel". */
743 flag_reg |= ~(changrp_mask << 2) & 0x3c;
744 flag_reg |= FLAG_FILTER;
745 data = flag_reg << 24;
746 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
749 /* Start acquisition on the device. */
750 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
753 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
756 /* Send header packet to the session bus. */
757 packet = g_malloc(sizeof(struct datafeed_packet));
758 header = g_malloc(sizeof(struct datafeed_header));
759 if (!packet || !header)
761 packet->type = DF_HEADER;
762 packet->length = sizeof(struct datafeed_header);
763 packet->payload = (unsigned char *)header;
764 header->feed_version = 1;
765 gettimeofday(&header->starttime, NULL);
766 header->samplerate = cur_samplerate;
767 header->protocol_id = PROTO_RAW;
768 header->num_logic_probes = NUM_PROBES;
769 header->num_analog_probes = 0;
770 session_bus(session_device_id, packet);
777 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
779 struct datafeed_packet packet;
781 /* Avoid compiler warnings. */
782 device_index = device_index;
784 packet.type = DF_END;
786 session_bus(session_device_id, &packet);
789 struct device_plugin ols_plugin_info = {
799 hw_set_configuration,
800 hw_start_acquisition,