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 O_NONBLOCK FIONBIO
46 #define NUM_TRIGGER_STAGES 4
47 #define TRIGGER_TYPES "01"
48 #define SERIAL_SPEED B115200
49 #define CLOCK_RATE 100000000
52 #define CMD_RESET 0x00
54 #define CMD_SET_FLAGS 0x82
55 #define CMD_SET_DIVIDER 0x80
57 #define CMD_CAPTURE_SIZE 0x81
58 #define CMD_SET_TRIGGER_MASK_0 0xc0
59 #define CMD_SET_TRIGGER_MASK_1 0xc4
60 #define CMD_SET_TRIGGER_MASK_2 0xc8
61 #define CMD_SET_TRIGGER_MASK_3 0xcc
62 #define CMD_SET_TRIGGER_VALUE_0 0xc1
63 #define CMD_SET_TRIGGER_VALUE_1 0xc5
64 #define CMD_SET_TRIGGER_VALUE_2 0xc9
65 #define CMD_SET_TRIGGER_VALUE_3 0xcd
66 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
67 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
68 #define CMD_SET_TRIGGER_CONFIG_2 0xca
69 #define CMD_SET_TRIGGER_CONFIG_3 0xce
71 /* Bitmasks for CMD_FLAGS */
72 #define FLAG_DEMUX 0x01
73 #define FLAG_FILTER 0x02
74 #define FLAG_CHANNELGROUP_1 0x04
75 #define FLAG_CHANNELGROUP_2 0x08
76 #define FLAG_CHANNELGROUP_3 0x10
77 #define FLAG_CHANNELGROUP_4 0x20
78 #define FLAG_CLOCK_EXTERNAL 0x40
79 #define FLAG_CLOCK_INVERTED 0x80
80 #define FLAG_RLE 0x0100
82 static int capabilities[] = {
90 static struct samplerates samplerates = {
97 /* List of struct serial_device_instance */
98 static GSList *device_instances = NULL;
100 /* Current state of the flag register */
101 static uint32_t flag_reg = 0;
103 static uint64_t cur_samplerate = 0;
104 static uint64_t limit_samples = 0;
106 * Pre/post trigger capture ratio, in percentage.
107 * 0 means no pre-trigger data.
109 static int capture_ratio = 0;
110 static int trigger_at = -1;
111 static uint32_t probe_mask = 0xffffffff;
112 static uint32_t trigger_mask[4] = { 0, 0, 0, 0 };
113 static uint32_t trigger_value[4] = { 0, 0, 0, 0 };
114 static int num_stages = 0;
116 static int send_shortcommand(int fd, uint8_t command)
120 g_debug("ols: sending cmd 0x%.2x", command);
122 if (serial_write(fd, buf, 1) != 1)
128 static int send_longcommand(int fd, uint8_t command, uint32_t data)
132 g_debug("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
134 buf[1] = (data & 0xff000000) >> 24;
135 buf[2] = (data & 0xff0000) >> 16;
136 buf[3] = (data & 0xff00) >> 8;
137 buf[4] = data & 0xff;
138 if (serial_write(fd, buf, 5) != 5)
144 static int configure_probes(GSList *probes)
148 int probe_bit, stage, i;
152 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
154 trigger_value[i] = 0;
158 for (l = probes; l; l = l->next) {
159 probe = (struct probe *)l->data;
164 * Set up the probe mask for later configuration into the
167 probe_bit = 1 << (probe->index - 1);
168 probe_mask |= probe_bit;
173 /* Configure trigger mask and value. */
175 for (tc = probe->trigger; tc && *tc; tc++) {
176 trigger_mask[stage] |= probe_bit;
178 trigger_value[stage] |= probe_bit;
182 * TODO: Only supporting parallel mode, with
187 if (stage > num_stages)
194 static uint32_t reverse16(uint32_t in)
198 out = (in & 0xff) << 8;
199 out |= (in & 0xff00) >> 8;
200 out |= (in & 0xff0000) << 8;
201 out |= (in & 0xff000000) >> 8;
206 static uint32_t reverse32(uint32_t in)
210 out = (in & 0xff) << 24;
211 out |= (in & 0xff00) << 8;
212 out |= (in & 0xff0000) >> 8;
213 out |= (in & 0xff000000) >> 24;
218 static int hw_init(char *deviceinfo)
220 struct sigrok_device_instance *sdi;
223 int devcnt, final_devcnt, num_ports, fd, ret, i;
224 char buf[8], **device_names, **serial_params;
227 ports = g_slist_append(NULL, strdup(deviceinfo));
229 /* No specific device given, so scan all serial ports. */
230 ports = list_serial_ports();
232 num_ports = g_slist_length(ports);
233 fds = calloc(1, num_ports * sizeof(GPollFD));
234 device_names = malloc(num_ports * sizeof(char *));
235 serial_params = malloc(num_ports * sizeof(char *));
237 for (l = ports; l; l = l->next) {
238 /* The discovery procedure is like this: first send the Reset
239 * command (0x00) 5 times, since the device could be anywhere
240 * in a 5-byte command. Then send the ID command (0x02).
241 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
244 * Since it may take the device a while to respond at 115Kb/s,
245 * we do all the sending first, then wait for all of them to
246 * respond with g_poll().
248 g_message("ols: probing %s...", (char *)l->data);
249 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
251 serial_params[devcnt] = serial_backup_params(fd);
252 serial_set_params(fd, 115200, 8, 0, 1, 2);
254 for (i = 0; i < 5; i++) {
255 if ((ret = send_shortcommand(fd,
256 CMD_RESET)) != SIGROK_OK) {
257 /* Serial port is not writable. */
261 if (ret != SIGROK_OK) {
262 serial_restore_params(fd,
263 serial_params[devcnt]);
267 send_shortcommand(fd, CMD_ID);
269 fds[devcnt].events = G_IO_IN;
270 device_names[devcnt] = strdup(l->data);
276 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
280 g_poll(fds, devcnt, 1);
281 for (i = 0; i < devcnt; i++) {
282 if (fds[i].revents == G_IO_IN) {
283 if (serial_read(fds[i].fd, buf, 4) == 4) {
284 if (!strncmp(buf, "1SLO", 4)
285 || !strncmp(buf, "1ALS", 4)) {
286 if (!strncmp(buf, "1SLO", 4))
287 sdi = sigrok_device_instance_new
288 (final_devcnt, ST_INACTIVE,
290 "Logic Sniffer", "v1.0");
292 sdi = sigrok_device_instance_new
293 (final_devcnt, ST_INACTIVE,
294 "Openbench", "Logic Sniffer",
296 sdi->serial = serial_device_instance_new
297 (device_names[i], -1);
299 g_slist_append(device_instances, sdi);
301 serial_close(fds[i].fd);
305 free(device_names[i]);
308 if (fds[i].fd != 0) {
309 serial_restore_params(fds[i].fd, serial_params[i]);
310 serial_close(fds[i].fd);
312 free(serial_params[i]);
320 cur_samplerate = samplerates.low;
325 static int hw_opendev(int device_index)
327 struct sigrok_device_instance *sdi;
329 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
332 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
333 if (sdi->serial->fd == -1)
336 sdi->status = ST_ACTIVE;
341 static void hw_closedev(int device_index)
343 struct sigrok_device_instance *sdi;
345 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
348 if (sdi->serial->fd != -1) {
349 serial_close(sdi->serial->fd);
350 sdi->serial->fd = -1;
351 sdi->status = ST_INACTIVE;
355 static void hw_cleanup(void)
358 struct sigrok_device_instance *sdi;
360 /* Properly close all devices. */
361 for (l = device_instances; l; l = l->next) {
363 if (sdi->serial->fd != -1)
364 serial_close(sdi->serial->fd);
365 sigrok_device_instance_free(sdi);
367 g_slist_free(device_instances);
368 device_instances = NULL;
371 static void *hw_get_device_info(int device_index, int device_info_id)
373 struct sigrok_device_instance *sdi;
376 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
380 switch (device_info_id) {
385 info = GINT_TO_POINTER(NUM_PROBES);
390 case DI_TRIGGER_TYPES:
391 info = (char *)TRIGGER_TYPES;
393 case DI_CUR_SAMPLERATE:
394 info = &cur_samplerate;
401 static int hw_get_status(int device_index)
403 struct sigrok_device_instance *sdi;
405 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
411 static int *hw_get_capabilities(void)
416 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
421 if (samplerate < samplerates.low || samplerate > samplerates.high)
422 return SIGROK_ERR_SAMPLERATE;
424 if (samplerate > CLOCK_RATE) {
425 flag_reg |= FLAG_DEMUX;
426 divider = (CLOCK_RATE * 2 / samplerate) - 1;
428 flag_reg &= ~FLAG_DEMUX;
429 divider = (CLOCK_RATE / samplerate) - 1;
432 g_message("ols: setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
433 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
435 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, reverse32(divider)) != SIGROK_OK)
437 cur_samplerate = samplerate;
442 static int hw_set_configuration(int device_index, int capability, void *value)
444 struct sigrok_device_instance *sdi;
448 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
451 if (sdi->status != ST_ACTIVE)
454 switch (capability) {
455 case HWCAP_SAMPLERATE:
457 ret = set_configuration_samplerate(sdi, *tmp_u64);
459 case HWCAP_PROBECONFIG:
460 ret = configure_probes((GSList *) value);
462 case HWCAP_LIMIT_SAMPLES:
464 limit_samples = *tmp_u64;
465 g_message("ols: sample limit %" PRIu64, limit_samples);
468 case HWCAP_CAPTURE_RATIO:
470 capture_ratio = *tmp_u64;
471 if (capture_ratio < 0 || capture_ratio > 100) {
484 static int receive_data(int fd, int revents, void *user_data)
486 static unsigned int num_transfers = 0;
487 static int num_bytes = 0;
488 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
489 static unsigned char sample[4] = { 0, 0, 0, 0 };
490 static unsigned char tmp_sample[4];
491 static unsigned char *raw_sample_buf = NULL;
492 int count, buflen, num_channels, offset, i, j;
493 struct datafeed_packet packet;
494 unsigned char byte, *buffer;
496 if (num_transfers++ == 0) {
498 * First time round, means the device started sending data,
499 * and will not stop until done. If it stops sending for
500 * longer than it takes to send a byte, that means it's
501 * finished. We'll double that to 30ms to be sure...
504 source_add(fd, G_IO_IN, 30, receive_data, user_data);
505 raw_sample_buf = malloc(limit_samples * 4);
506 /* fill with 1010... for debugging */
507 memset(raw_sample_buf, 0x82, limit_samples * 4);
511 for (i = 0x20; i > 0x02; i /= 2) {
512 if ((flag_reg & i) == 0)
516 if (revents == G_IO_IN
517 && num_transfers / num_channels <= limit_samples) {
518 if (serial_read(fd, &byte, 1) != 1)
521 sample[num_bytes++] = byte;
522 g_debug("ols: received byte 0x%.2x", byte);
523 if (num_bytes == num_channels) {
524 g_debug("ols: received sample 0x%.*x", num_bytes * 2, (int) *sample);
525 /* Got a full sample. */
526 if (flag_reg & FLAG_RLE) {
528 * In RLE mode -1 should never come in as a
529 * sample, because bit 31 is the "count" flag.
530 * TODO: Endianness may be wrong here, could be
534 && !(last_sample[0] & 0x80)) {
535 count = (int)(*sample) & 0x7fffffff;
536 buffer = g_malloc(count);
538 for (i = 0; i < count; i++) {
539 memcpy(buffer + buflen, last_sample, 4);
544 * Just a single sample, next sample
545 * will probably be a count referring
546 * to this -- but this one is still a
547 * part of the stream.
553 /* No compression. */
558 if (num_channels < 4) {
560 * Some channel groups may have been turned
561 * off, to speed up transfer between the
562 * hardware and the PC. Expand that here before
563 * submitting it over the session bus --
564 * whatever is listening on the bus will be
565 * expecting a full 32-bit sample, based on
566 * the number of probes.
569 memset(tmp_sample, 0, 4);
570 for (i = 0; i < 4; i++) {
571 if (((flag_reg >> 2) & (1 << i)) == 0) {
573 * This channel group was
574 * enabled, copy from received
577 tmp_sample[i] = sample[j++];
580 memcpy(sample, tmp_sample, 4);
581 g_debug("ols: full sample 0x%.8x", (int) *sample);
584 /* the OLS sends its sample buffer backwards.
585 * store it in reverse order here, so we can dump
586 * this on the session bus later.
588 offset = (limit_samples - num_transfers / num_channels) * 4;
589 memcpy(raw_sample_buf + offset, sample, 4);
591 if (buffer == sample)
592 memcpy(last_sample, buffer, num_channels);
596 memset(sample, 0, 4);
601 * This is the main loop telling us a timeout was reached, or
602 * we've acquired all the samples we asked for -- we're done.
603 * Send the (properly-ordered) buffer to the frontend.
605 if (trigger_at != -1) {
606 /* a trigger was set up, so we need to tell the frontend
609 if (trigger_at > 0) {
610 /* there are pre-trigger samples, send those first */
611 packet.type = DF_LOGIC;
612 packet.length = trigger_at * 4;
614 packet.payload = raw_sample_buf;
615 session_bus(user_data, &packet);
618 packet.type = DF_TRIGGER;
620 session_bus(user_data, &packet);
622 packet.type = DF_LOGIC;
623 packet.length = (limit_samples * 4) - (trigger_at * 4);
625 packet.payload = raw_sample_buf + trigger_at * 4;
626 session_bus(user_data, &packet);
628 packet.type = DF_LOGIC;
629 packet.length = limit_samples * 4;
631 packet.payload = raw_sample_buf;
632 session_bus(user_data, &packet);
634 free(raw_sample_buf);
638 packet.type = DF_END;
640 session_bus(user_data, &packet);
646 static int hw_start_acquisition(int device_index, gpointer session_device_id)
649 struct datafeed_packet *packet;
650 struct datafeed_header *header;
651 struct sigrok_device_instance *sdi;
652 uint32_t trigger_config[4];
654 uint16_t readcount, delaycount;
655 uint8_t changrp_mask;
657 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
660 if (sdi->status != ST_ACTIVE)
663 readcount = limit_samples / 4;
665 memset(trigger_config, 0, 16);
666 trigger_config[num_stages-1] |= 0x08;
667 if (trigger_mask[0]) {
668 delaycount = readcount * (1 - capture_ratio / 100.0);
669 trigger_at = (readcount - delaycount) * 4 - num_stages;
671 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
672 reverse32(trigger_mask[0])) != SIGROK_OK)
674 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
675 reverse32(trigger_value[0])) != SIGROK_OK)
677 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
678 trigger_config[0]) != SIGROK_OK)
681 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
682 reverse32(trigger_mask[1])) != SIGROK_OK)
684 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
685 reverse32(trigger_value[1])) != SIGROK_OK)
687 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
688 trigger_config[1]) != SIGROK_OK)
691 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
692 reverse32(trigger_mask[2])) != SIGROK_OK)
694 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
695 reverse32(trigger_value[2])) != SIGROK_OK)
697 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
698 trigger_config[2]) != SIGROK_OK)
701 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
702 reverse32(trigger_mask[3])) != SIGROK_OK)
704 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
705 reverse32(trigger_value[3])) != SIGROK_OK)
707 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
708 trigger_config[3]) != SIGROK_OK)
711 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
712 trigger_mask[0]) != SIGROK_OK)
714 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
715 trigger_value[0]) != SIGROK_OK)
717 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
718 0x00000008) != SIGROK_OK)
720 delaycount = readcount;
723 set_configuration_samplerate(sdi, cur_samplerate);
725 /* Send sample limit and pre/post-trigger capture ratio. */
726 data = ((readcount - 1) & 0xffff) << 16;
727 data |= (delaycount - 1) & 0xffff;
728 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, reverse16(data)) != SIGROK_OK)
732 * Enable/disable channel groups in the flag register according to the
736 for (i = 0; i < 4; i++) {
737 if (probe_mask & (0xff << (i * 8)))
738 changrp_mask |= (1 << i);
741 /* The flag register wants them here, and 1 means "disable channel". */
742 flag_reg |= ~(changrp_mask << 2) & 0x3c;
743 flag_reg |= FLAG_FILTER;
744 data = flag_reg << 24;
745 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
748 /* Start acquisition on the device. */
749 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
752 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
755 /* Send header packet to the session bus. */
756 packet = g_malloc(sizeof(struct datafeed_packet));
757 header = g_malloc(sizeof(struct datafeed_header));
758 if (!packet || !header)
760 packet->type = DF_HEADER;
761 packet->length = sizeof(struct datafeed_header);
762 packet->payload = (unsigned char *)header;
763 header->feed_version = 1;
764 gettimeofday(&header->starttime, NULL);
765 header->samplerate = cur_samplerate;
766 header->protocol_id = PROTO_RAW;
767 header->num_logic_probes = NUM_PROBES;
768 header->num_analog_probes = 0;
769 session_bus(session_device_id, packet);
776 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
778 struct datafeed_packet packet;
781 device_index = device_index;
783 packet.type = DF_END;
785 session_bus(session_device_id, &packet);
788 struct device_plugin ols_plugin_info = {
798 hw_set_configuration,
799 hw_start_acquisition,