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>
42 #define NUM_TRIGGER_STAGES 4
43 #define TRIGGER_TYPES "01"
44 #define SERIAL_SPEED B115200
45 #define CLOCK_RATE 100000000
48 #define CMD_RESET 0x00
50 #define CMD_SET_FLAGS 0x82
51 #define CMD_SET_DIVIDER 0x80
53 #define CMD_CAPTURE_SIZE 0x81
54 #define CMD_SET_TRIGGER_MASK_0 0xc0
55 #define CMD_SET_TRIGGER_MASK_1 0xc4
56 #define CMD_SET_TRIGGER_MASK_2 0xc8
57 #define CMD_SET_TRIGGER_MASK_3 0xcc
58 #define CMD_SET_TRIGGER_VALUE_0 0xc1
59 #define CMD_SET_TRIGGER_VALUE_1 0xc5
60 #define CMD_SET_TRIGGER_VALUE_2 0xc9
61 #define CMD_SET_TRIGGER_VALUE_3 0xcd
62 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
63 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
64 #define CMD_SET_TRIGGER_CONFIG_2 0xca
65 #define CMD_SET_TRIGGER_CONFIG_3 0xce
67 /* Bitmasks for CMD_FLAGS */
68 #define FLAG_DEMUX 0x01
69 #define FLAG_FILTER 0x02
70 #define FLAG_CHANNELGROUP_1 0x04
71 #define FLAG_CHANNELGROUP_2 0x08
72 #define FLAG_CHANNELGROUP_3 0x10
73 #define FLAG_CHANNELGROUP_4 0x20
74 #define FLAG_CLOCK_EXTERNAL 0x40
75 #define FLAG_CLOCK_INVERTED 0x80
76 #define FLAG_RLE 0x0100
78 static int capabilities[] = {
86 static struct samplerates samplerates = {
93 /* List of struct serial_device_instance */
94 static GSList *device_instances = NULL;
96 /* Current state of the flag register */
97 static uint32_t flag_reg = 0;
99 static uint64_t cur_samplerate = 0;
100 static uint64_t limit_samples = 0;
102 * Pre/post trigger capture ratio, in percentage.
103 * 0 means no pre-trigger data.
105 static int capture_ratio = 0;
106 static int trigger_at = -1;
107 static uint32_t probe_mask = 0xffffffff;
108 static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
109 static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
110 static int num_stages = 0;
112 static int send_shortcommand(int fd, uint8_t command)
116 g_debug("ols: sending cmd 0x%.2x", command);
118 if (write(fd, buf, 1) != 1)
124 static int send_longcommand(int fd, uint8_t command, uint32_t data)
128 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
130 buf[1] = (data & 0xff000000) >> 24;
131 buf[2] = (data & 0xff0000) >> 16;
132 buf[3] = (data & 0xff00) >> 8;
133 buf[4] = data & 0xff;
134 if (write(fd, buf, 5) != 5)
140 static int configure_probes(GSList *probes)
144 int probe_bit, stage, i;
148 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
150 trigger_value[i] = 0;
154 for (l = probes; l; l = l->next) {
155 probe = (struct probe *)l->data;
160 * Set up the probe mask for later configuration into the
163 probe_bit = 1 << (probe->index - 1);
164 probe_mask |= probe_bit;
169 /* Configure trigger mask and value. */
171 for (tc = probe->trigger; tc && *tc; tc++) {
172 trigger_mask[stage] |= probe_bit;
174 trigger_value[stage] |= probe_bit;
178 * TODO: Only supporting parallel mode, with
183 if (stage > num_stages)
190 static uint32_t reverse16(uint32_t in)
194 out = (in & 0xff) << 8;
195 out |= (in & 0xff00) >> 8;
196 out |= (in & 0xff0000) << 8;
197 out |= (in & 0xff000000) >> 8;
202 static uint32_t reverse32(uint32_t in)
206 out = (in & 0xff) << 24;
207 out |= (in & 0xff00) << 8;
208 out |= (in & 0xff0000) >> 8;
209 out |= (in & 0xff000000) >> 24;
214 static int hw_init(char *deviceinfo)
216 struct sigrok_device_instance *sdi;
219 int devcnt, final_devcnt, num_ports, fd, ret, i;
220 char buf[8], **device_names, **serial_params;
223 ports = g_slist_append(NULL, strdup(deviceinfo));
225 /* No specific device given, so scan all serial ports. */
226 ports = list_serial_ports();
228 num_ports = g_slist_length(ports);
229 fds = calloc(1, num_ports * sizeof(GPollFD));
230 device_names = malloc(num_ports * sizeof(char *));
231 serial_params = malloc(num_ports * sizeof(char *));
233 for (l = ports; l; l = l->next) {
234 /* The discovery procedure is like this: first send the Reset
235 * command (0x00) 5 times, since the device could be anywhere
236 * in a 5-byte command. Then send the ID command (0x02).
237 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
240 * Since it may take the device a while to respond at 115Kb/s,
241 * we do all the sending first, then wait for all of them to
242 * respond with g_poll().
244 g_message("ols: probing %s...", (char *)l->data);
245 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
247 serial_params[devcnt] = serial_backup_params(fd);
248 serial_set_params(fd, 115200, 8, 0, 1, 2);
250 for (i = 0; i < 5; i++) {
251 if ((ret = send_shortcommand(fd,
252 CMD_RESET)) != SIGROK_OK) {
253 /* Serial port is not writable. */
257 if (ret != SIGROK_OK) {
258 serial_restore_params(fd,
259 serial_params[devcnt]);
263 send_shortcommand(fd, CMD_ID);
265 fds[devcnt].events = G_IO_IN;
266 device_names[devcnt] = strdup(l->data);
272 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
276 g_poll(fds, devcnt, 1);
277 for (i = 0; i < devcnt; i++) {
278 if (fds[i].revents == G_IO_IN) {
279 if (read(fds[i].fd, buf, 4) == 4) {
280 if (!strncmp(buf, "1SLO", 4)
281 || !strncmp(buf, "1ALS", 4)) {
282 if (!strncmp(buf, "1SLO", 4))
283 sdi = sigrok_device_instance_new
284 (final_devcnt, ST_INACTIVE,
286 "Logic Sniffer", "v1.0");
288 sdi = sigrok_device_instance_new
289 (final_devcnt, ST_INACTIVE,
290 "Openbench", "Logic Sniffer",
292 sdi->serial = serial_device_instance_new
293 (device_names[i], -1);
295 g_slist_append(device_instances, sdi);
297 serial_close(fds[i].fd);
301 free(device_names[i]);
304 if (fds[i].fd != 0) {
305 serial_restore_params(fds[i].fd, serial_params[i]);
306 serial_close(fds[i].fd);
308 free(serial_params[i]);
316 cur_samplerate = samplerates.low;
321 static int hw_opendev(int device_index)
323 struct sigrok_device_instance *sdi;
325 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
328 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
329 if (sdi->serial->fd == -1)
332 sdi->status = ST_ACTIVE;
337 static void hw_closedev(int device_index)
339 struct sigrok_device_instance *sdi;
341 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
344 if (sdi->serial->fd != -1) {
345 serial_close(sdi->serial->fd);
346 sdi->serial->fd = -1;
347 sdi->status = ST_INACTIVE;
351 static void hw_cleanup(void)
354 struct sigrok_device_instance *sdi;
356 /* Properly close all devices. */
357 for (l = device_instances; l; l = l->next) {
359 if (sdi->serial->fd != -1)
360 serial_close(sdi->serial->fd);
361 sigrok_device_instance_free(sdi);
363 g_slist_free(device_instances);
364 device_instances = NULL;
367 static void *hw_get_device_info(int device_index, int device_info_id)
369 struct sigrok_device_instance *sdi;
372 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
376 switch (device_info_id) {
381 info = GINT_TO_POINTER(NUM_PROBES);
386 case DI_TRIGGER_TYPES:
387 info = (char *)TRIGGER_TYPES;
389 case DI_CUR_SAMPLERATE:
390 info = &cur_samplerate;
397 static int hw_get_status(int device_index)
399 struct sigrok_device_instance *sdi;
401 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
407 static int *hw_get_capabilities(void)
412 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
417 if (samplerate < samplerates.low || samplerate > samplerates.high)
418 return SIGROK_ERR_SAMPLERATE;
420 if (samplerate > CLOCK_RATE) {
421 flag_reg |= FLAG_DEMUX;
422 divider = (CLOCK_RATE * 2 / samplerate) - 1;
424 flag_reg &= ~FLAG_DEMUX;
425 divider = (CLOCK_RATE / samplerate) - 1;
428 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
429 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
431 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, reverse32(divider)) != SIGROK_OK)
433 cur_samplerate = samplerate;
438 static int hw_set_configuration(int device_index, int capability, void *value)
440 struct sigrok_device_instance *sdi;
444 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
447 if (sdi->status != ST_ACTIVE)
450 switch (capability) {
451 case HWCAP_SAMPLERATE:
453 ret = set_configuration_samplerate(sdi, *tmp_u64);
455 case HWCAP_PROBECONFIG:
456 ret = configure_probes((GSList *) value);
458 case HWCAP_LIMIT_SAMPLES:
460 limit_samples = *tmp_u64;
461 g_message("ols: sample limit %" PRIu64, limit_samples);
464 case HWCAP_CAPTURE_RATIO:
466 capture_ratio = *tmp_u64;
467 if (capture_ratio < 0 || capture_ratio > 100) {
480 static int receive_data(int fd, int revents, void *user_data)
482 static unsigned int num_transfers = 0;
483 static int num_bytes = 0;
484 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
485 static unsigned char sample[4] = { 0, 0, 0, 0 };
486 static unsigned char tmp_sample[4];
487 static unsigned char *raw_sample_buf = NULL;
488 int count, buflen, num_channels, offset, i, j;
489 struct datafeed_packet packet;
490 unsigned char byte, *buffer;
492 if (num_transfers++ == 0) {
494 * First time round, means the device started sending data,
495 * and will not stop until done. If it stops sending for
496 * longer than it takes to send a byte, that means it's
497 * finished. We'll double that to 30ms to be sure...
500 source_add(fd, G_IO_IN, 30, receive_data, user_data);
501 raw_sample_buf = malloc(limit_samples * 4);
502 /* fill with 1010... for debugging */
503 memset(raw_sample_buf, 0x82, limit_samples * 4);
507 for (i = 0x20; i > 0x02; i /= 2) {
508 if ((flag_reg & i) == 0)
512 if (revents == G_IO_IN
513 && num_transfers / num_channels <= limit_samples) {
514 if (read(fd, &byte, 1) != 1)
517 sample[num_bytes++] = byte;
518 g_debug("ols: received byte 0x%.2x", byte);
519 if (num_bytes == num_channels) {
520 g_debug("ols: received sample 0x%.*x", num_bytes * 2, (int) *sample);
521 /* Got a full sample. */
522 if (flag_reg & FLAG_RLE) {
524 * In RLE mode -1 should never come in as a
525 * sample, because bit 31 is the "count" flag.
526 * TODO: Endianness may be wrong here, could be
530 && !(last_sample[0] & 0x80)) {
531 count = (int)(*sample) & 0x7fffffff;
532 buffer = g_malloc(count);
534 for (i = 0; i < count; i++) {
535 memcpy(buffer + buflen, last_sample, 4);
540 * Just a single sample, next sample
541 * will probably be a count referring
542 * to this -- but this one is still a
543 * part of the stream.
549 /* No compression. */
554 if (num_channels < 4) {
556 * Some channel groups may have been turned
557 * off, to speed up transfer between the
558 * hardware and the PC. Expand that here before
559 * submitting it over the session bus --
560 * whatever is listening on the bus will be
561 * expecting a full 32-bit sample, based on
562 * the number of probes.
565 memset(tmp_sample, 0, 4);
566 for (i = 0; i < 4; i++) {
567 if (((flag_reg >> 2) & (1 << i)) == 0) {
569 * This channel group was
570 * enabled, copy from received
573 tmp_sample[i] = sample[j++];
576 memcpy(sample, tmp_sample, 4);
577 g_debug("ols: full sample 0x%.8x", (int) *sample);
580 /* the OLS sends its sample buffer backwards.
581 * store it in reverse order here, so we can dump
582 * this on the session bus later.
584 offset = (limit_samples - num_transfers / num_channels) * 4;
585 memcpy(raw_sample_buf + offset, sample, 4);
587 if (buffer == sample)
588 memcpy(last_sample, buffer, num_channels);
592 memset(sample, 0, 4);
597 * This is the main loop telling us a timeout was reached, or
598 * we've acquired all the samples we asked for -- we're done.
599 * Send the (properly-ordered) buffer to the frontend.
601 if (trigger_at != -1) {
602 /* a trigger was set up, so we need to tell the frontend
605 if (trigger_at > 0) {
606 /* there are pre-trigger samples, send those first */
607 packet.type = DF_LOGIC;
608 packet.length = trigger_at * 4;
610 packet.payload = raw_sample_buf;
611 session_bus(user_data, &packet);
614 packet.type = DF_TRIGGER;
616 session_bus(user_data, &packet);
618 packet.type = DF_LOGIC;
619 packet.length = (limit_samples * 4) - (trigger_at * 4);
621 packet.payload = raw_sample_buf + trigger_at * 4;
622 session_bus(user_data, &packet);
624 packet.type = DF_LOGIC;
625 packet.length = limit_samples * 4;
627 packet.payload = raw_sample_buf;
628 session_bus(user_data, &packet);
630 free(raw_sample_buf);
634 packet.type = DF_END;
636 session_bus(user_data, &packet);
642 static int hw_start_acquisition(int device_index, gpointer session_device_id)
645 struct datafeed_packet *packet;
646 struct datafeed_header *header;
647 struct sigrok_device_instance *sdi;
648 uint32_t trigger_config[4];
650 uint16_t readcount, delaycount;
651 uint8_t changrp_mask;
653 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
656 if (sdi->status != ST_ACTIVE)
659 readcount = limit_samples / 4;
661 memset(trigger_config, 0, 16);
662 trigger_config[num_stages-1] |= 0x08;
663 if (trigger_mask[0]) {
664 delaycount = readcount * (1 - capture_ratio / 100.0);
665 trigger_at = (readcount - delaycount) * 4 - num_stages;
667 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
668 reverse32(trigger_mask[0])) != SIGROK_OK)
670 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
671 reverse32(trigger_value[0])) != SIGROK_OK)
673 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
674 trigger_config[0]) != SIGROK_OK)
677 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
678 reverse32(trigger_mask[1])) != SIGROK_OK)
680 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
681 reverse32(trigger_value[1])) != SIGROK_OK)
683 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
684 trigger_config[1]) != SIGROK_OK)
687 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
688 reverse32(trigger_mask[2])) != SIGROK_OK)
690 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
691 reverse32(trigger_value[2])) != SIGROK_OK)
693 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
694 trigger_config[2]) != SIGROK_OK)
697 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
698 reverse32(trigger_mask[3])) != SIGROK_OK)
700 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
701 reverse32(trigger_value[3])) != SIGROK_OK)
703 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
704 trigger_config[3]) != SIGROK_OK)
707 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
708 trigger_mask[0]) != SIGROK_OK)
710 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
711 trigger_value[0]) != SIGROK_OK)
713 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
714 0x00000008) != SIGROK_OK)
716 delaycount = readcount;
719 set_configuration_samplerate(sdi, cur_samplerate);
721 /* Send sample limit and pre/post-trigger capture ratio. */
722 data = ((readcount - 1) & 0xffff) << 16;
723 data |= (delaycount - 1) & 0xffff;
724 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SIGROK_OK)
728 * Enable/disable channel groups in the flag register according to the
732 for (i = 0; i < 4; i++) {
733 if (probe_mask & (0xff << (i * 8)))
734 changrp_mask |= (1 << i);
737 /* The flag register wants them here, and 1 means "disable channel". */
738 flag_reg |= ~(changrp_mask << 2) & 0x3c;
739 flag_reg |= FLAG_FILTER;
740 data = flag_reg << 24;
741 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
744 /* Start acquisition on the device. */
745 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
748 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
751 /* Send header packet to the session bus. */
752 packet = g_malloc(sizeof(struct datafeed_packet));
753 header = g_malloc(sizeof(struct datafeed_header));
754 if (!packet || !header)
756 packet->type = DF_HEADER;
757 packet->length = sizeof(struct datafeed_header);
758 packet->payload = (unsigned char *)header;
759 header->feed_version = 1;
760 gettimeofday(&header->starttime, NULL);
761 header->samplerate = cur_samplerate;
762 header->protocol_id = PROTO_RAW;
763 header->num_logic_probes = NUM_PROBES;
764 header->num_analog_probes = 0;
765 session_bus(session_device_id, packet);
772 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
774 struct datafeed_packet packet;
777 device_index = device_index;
779 packet.type = DF_END;
781 session_bus(session_device_id, &packet);
784 struct device_plugin ols_plugin_info = {
794 hw_set_configuration,
795 hw_start_acquisition,