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 /* TODO: SERIAL_ bits, parity, stop bit */
46 #define CLOCK_RATE 100000000
49 #define CMD_RESET 0x00
51 #define CMD_SET_FLAGS 0x82
52 #define CMD_SET_DIVIDER 0x80
54 #define CMD_CAPTURE_SIZE 0x81
55 #define CMD_SET_TRIGGER_MASK_0 0xc0
56 #define CMD_SET_TRIGGER_MASK_1 0xc4
57 #define CMD_SET_TRIGGER_MASK_2 0xc8
58 #define CMD_SET_TRIGGER_MASK_3 0xcc
59 #define CMD_SET_TRIGGER_VALUE_0 0xc1
60 #define CMD_SET_TRIGGER_VALUE_1 0xc5
61 #define CMD_SET_TRIGGER_VALUE_2 0xc9
62 #define CMD_SET_TRIGGER_VALUE_3 0xcd
63 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
64 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
65 #define CMD_SET_TRIGGER_CONFIG_2 0xca
66 #define CMD_SET_TRIGGER_CONFIG_3 0xce
68 /* Bitmasks for CMD_FLAGS */
69 #define FLAG_DEMUX 0x01
70 #define FLAG_FILTER 0x02
71 #define FLAG_CHANNELGROUP_1 0x04
72 #define FLAG_CHANNELGROUP_2 0x08
73 #define FLAG_CHANNELGROUP_3 0x10
74 #define FLAG_CHANNELGROUP_4 0x20
75 #define FLAG_CLOCK_EXTERNAL 0x40
76 #define FLAG_CLOCK_INVERTED 0x80
77 #define FLAG_RLE 0x0100
79 static int capabilities[] = {
87 static struct samplerates samplerates = {
94 /* List of struct serial_device_instance */
95 static GSList *device_instances = NULL;
97 /* Current state of the flag register */
98 static uint32_t flag_reg = 0;
100 static uint64_t cur_samplerate = 0;
101 static uint64_t limit_samples = 0;
103 * Pre/post trigger capture ratio, in percentage.
104 * 0 means no pre-trigger data.
106 static int capture_ratio = 0;
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 };
111 static int send_shortcommand(int fd, uint8_t command)
115 g_message("ols: sending cmd 0x%.2x", command);
117 if (write(fd, buf, 1) != 1)
123 static int send_longcommand(int fd, uint8_t command, uint32_t data)
127 g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
129 buf[1] = (data & 0xff000000) >> 24;
130 buf[2] = (data & 0xff0000) >> 16;
131 buf[3] = (data & 0xff00) >> 8;
132 buf[4] = data & 0xff;
133 if (write(fd, buf, 5) != 5)
139 static int configure_probes(GSList *probes)
143 int probe_bit, stage, i;
147 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
149 trigger_value[i] = 0;
152 for (l = probes; l; l = l->next) {
153 probe = (struct probe *)l->data;
158 * Set up the probe mask for later configuration into the
161 probe_bit = 1 << (probe->index - 1);
162 probe_mask |= probe_bit;
167 /* Configure trigger mask and value. */
169 for (tc = probe->trigger; tc && *tc; tc++) {
170 trigger_mask[stage] |= probe_bit;
172 trigger_value[stage] |= probe_bit;
176 * TODO: Only supporting parallel mode, with
186 static void byteswap(uint32_t * in)
190 out = (*in & 0xff) << 8;
191 out |= (*in & 0xff00) >> 8;
192 out |= (*in & 0xff0000) << 8;
193 out |= (*in & 0xff000000) >> 8;
197 static int hw_init(char *deviceinfo)
199 struct sigrok_device_instance *sdi;
202 int devcnt, final_devcnt, num_ports, fd, ret, i;
203 char buf[8], **device_names, **serial_params;
206 ports = g_slist_append(NULL, strdup(deviceinfo));
208 /* No specific device given, so scan all serial ports. */
209 ports = list_serial_ports();
211 num_ports = g_slist_length(ports);
212 fds = calloc(1, num_ports * sizeof(GPollFD));
213 device_names = malloc(num_ports * sizeof(char *));
214 serial_params = malloc(num_ports * sizeof(char *));
216 for (l = ports; l; l = l->next) {
217 /* The discovery procedure is like this: first send the Reset
218 * command (0x00) 5 times, since the device could be anywhere
219 * in a 5-byte command. Then send the ID command (0x02).
220 * If the device responds with 4 bytes ("OLS1" or "SLA1"), we
223 * Since it may take the device a while to respond at 115Kb/s,
224 * we do all the sending first, then wait for all of them to
225 * respond with g_poll().
227 g_message("probing %s...", (char *)l->data);
230 // hdl = serial_open(l->data, 0);
232 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
235 serial_params[devcnt] = serial_backup_params(fd);
236 serial_set_params(fd, 115200, 8, 0, 1, 2);
238 for (i = 0; i < 5; i++) {
239 if ((ret = send_shortcommand(fd,
240 CMD_RESET)) != SIGROK_OK) {
241 /* Serial port is not writable. */
245 if (ret != SIGROK_OK) {
246 serial_restore_params(fd,
247 serial_params[devcnt]);
251 send_shortcommand(fd, CMD_ID);
253 fds[devcnt].events = G_IO_IN;
254 device_names[devcnt] = strdup(l->data);
260 /* 2ms isn't enough for reliable transfer with pl2303, let's try 10 */
264 g_poll(fds, devcnt, 1);
265 for (i = 0; i < devcnt; i++) {
266 if (fds[i].revents == G_IO_IN) {
267 if (read(fds[i].fd, buf, 4) == 4) {
268 if (!strncmp(buf, "1SLO", 4)
269 || !strncmp(buf, "1ALS", 4)) {
270 if (!strncmp(buf, "1SLO", 4))
271 sdi = sigrok_device_instance_new
272 (final_devcnt, ST_INACTIVE,
274 "Logic Sniffer", "v1.0");
276 sdi = sigrok_device_instance_new
277 (final_devcnt, ST_INACTIVE,
278 "Sump", "Logic Analyzer",
280 sdi->serial = serial_device_instance_new
281 (device_names[i], -1);
283 g_slist_append(device_instances, sdi);
285 serial_close(fds[i].fd);
289 free(device_names[i]);
292 if (fds[i].fd != 0) {
293 serial_restore_params(fds[i].fd, serial_params[i]);
294 serial_close(fds[i].fd);
296 free(serial_params[i]);
304 cur_samplerate = samplerates.low;
309 static int hw_opendev(int device_index)
311 struct sigrok_device_instance *sdi;
313 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
316 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
317 if (sdi->serial->fd == -1)
320 sdi->status = ST_ACTIVE;
325 static void hw_closedev(int device_index)
327 struct sigrok_device_instance *sdi;
329 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
332 if (sdi->serial->fd != -1) {
333 serial_close(sdi->serial->fd);
334 sdi->serial->fd = -1;
335 sdi->status = ST_INACTIVE;
339 static void hw_cleanup(void)
342 struct sigrok_device_instance *sdi;
344 /* Properly close all devices. */
345 for (l = device_instances; l; l = l->next) {
347 if (sdi->serial->fd != -1)
348 serial_close(sdi->serial->fd);
349 sigrok_device_instance_free(sdi);
351 g_slist_free(device_instances);
352 device_instances = NULL;
355 static void *hw_get_device_info(int device_index, int device_info_id)
357 struct sigrok_device_instance *sdi;
360 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
364 switch (device_info_id) {
369 info = GINT_TO_POINTER(NUM_PROBES);
374 case DI_TRIGGER_TYPES:
375 info = (char *)TRIGGER_TYPES;
377 case DI_CUR_SAMPLERATE:
378 info = &cur_samplerate;
385 static int hw_get_status(int device_index)
387 struct sigrok_device_instance *sdi;
389 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
395 static int *hw_get_capabilities(void)
400 static int set_configuration_samplerate(struct sigrok_device_instance *sdi,
405 if (samplerate < samplerates.low || samplerate > samplerates.high)
406 return SIGROK_ERR_SAMPLERATE;
408 if (samplerate > CLOCK_RATE) {
409 flag_reg |= FLAG_DEMUX;
410 divider = (CLOCK_RATE * 2 / samplerate) - 1;
412 flag_reg &= ~FLAG_DEMUX;
413 divider = (CLOCK_RATE / samplerate) - 1;
417 // divider = htonl(divider);
419 divider = htonl(divider);
422 g_message("setting samplerate to %" PRIu64 " Hz (divider %u, demux %s)",
423 samplerate, divider, flag_reg & FLAG_DEMUX ? "on" : "off");
425 if (send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER,
426 divider) != SIGROK_OK)
428 cur_samplerate = samplerate;
433 static int hw_set_configuration(int device_index, int capability, void *value)
435 struct sigrok_device_instance *sdi;
439 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
442 if (sdi->status != ST_ACTIVE)
445 if (capability == HWCAP_SAMPLERATE) {
447 ret = set_configuration_samplerate(sdi, *tmp_u64);
448 } else if (capability == HWCAP_PROBECONFIG) {
449 ret = configure_probes((GSList *) value);
450 } else if (capability == HWCAP_LIMIT_SAMPLES) {
452 limit_samples = *tmp_u64;
454 } else if (capability == HWCAP_CAPTURE_RATIO) {
455 capture_ratio = strtol(value, NULL, 10);
456 if (capture_ratio < 0 || capture_ratio > 100) {
468 static int receive_data(int fd, int revents, void *user_data)
470 static unsigned int num_transfers = 0;
471 static int num_bytes = 0;
472 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
473 static unsigned char sample[4] = { 0, 0, 0, 0 };
474 static unsigned char tmp_sample[4];
475 int count, buflen, num_channels, i, j;
476 struct datafeed_packet packet;
477 unsigned char byte, *buffer;
479 if (num_transfers++ == 0) {
481 * First time round, means the device started sending data,
482 * and will not stop until done. If it stops sending for
483 * longer than it takes to send a byte, that means it's
484 * finished. We'll double that to 30ms to be sure...
487 source_add(fd, G_IO_IN, 100, receive_data, user_data);
491 for (i = 0x20; i > 0x02; i /= 2) {
492 if ((flag_reg & i) == 0)
496 if (revents == G_IO_IN
497 && num_transfers / num_channels <= limit_samples) {
498 if (read(fd, &byte, 1) != 1)
501 sample[num_bytes++] = byte;
502 if (num_bytes == num_channels) {
503 /* Got a full sample. */
504 if (flag_reg & FLAG_RLE) {
506 * In RLE mode -1 should never come in as a
507 * sample, because bit 31 is the "count" flag.
508 * TODO: Endianness may be wrong here, could be
512 && !(last_sample[0] & 0x80)) {
513 count = (int)(*sample) & 0x7fffffff;
514 buffer = g_malloc(count);
516 for (i = 0; i < count; i++) {
517 memcpy(buffer + buflen,
523 * Just a single sample, next sample
524 * will probably be a count referring
525 * to this -- but this one is still a
526 * part of the stream.
532 /* No compression. */
537 if (num_channels < 4) {
539 * Some channel groups may have been turned
540 * off, to speed up transfer between the
541 * hardware and the PC. Expand that here before
542 * submitting it over the session bus --
543 * whatever is listening on the bus will be
544 * expecting a full 32-bit sample, based on
545 * the number of probes.
548 memset(tmp_sample, 0, 4);
549 for (i = 0; i < 4; i++) {
550 if ((flag_reg & (8 >> i)) == 0) {
552 * This channel group was
553 * enabled, copy from received
556 tmp_sample[i] = sample[j++];
559 memcpy(sample, tmp_sample, 4);
562 /* Send it all to the session bus. */
563 packet.type = DF_LOGIC32;
564 packet.length = buflen;
565 packet.payload = buffer;
566 session_bus(user_data, &packet);
567 if (buffer == sample)
568 memcpy(last_sample, buffer, num_channels);
572 memset(sample, 0, 4);
577 * This is the main loop telling us a timeout was reached, or
578 * we've acquired all the samples we asked for -- we're done.
581 /* TODO: Move to serial.c? */
582 tcflush(fd, TCIOFLUSH);
585 packet.type = DF_END;
587 session_bus(user_data, &packet);
593 static int hw_start_acquisition(int device_index, gpointer session_device_id)
596 struct datafeed_packet *packet;
597 struct datafeed_header *header;
598 struct sigrok_device_instance *sdi;
600 uint16_t readcount, delaycount;
601 uint8_t changrp_mask;
603 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
606 if (sdi->status != ST_ACTIVE)
609 if (trigger_mask[0]) {
611 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
612 trigger_mask[0]) != SIGROK_OK)
614 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
615 trigger_mask[1]) != SIGROK_OK)
617 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
618 trigger_mask[2]) != SIGROK_OK)
620 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
621 trigger_mask[3]) != SIGROK_OK)
624 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
625 trigger_value[0]) != SIGROK_OK)
627 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
628 trigger_value[1]) != SIGROK_OK)
630 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
631 trigger_value[2]) != SIGROK_OK)
633 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
634 trigger_value[3]) != SIGROK_OK)
637 /* Trigger configuration */
639 * TODO: The start flag should only be on the last used
642 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
643 0x00000008) != SIGROK_OK)
645 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
646 0x00000000) != SIGROK_OK)
648 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
649 0x00000000) != SIGROK_OK)
651 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
652 0x00000000) != SIGROK_OK)
654 delaycount = limit_samples / 4 * (capture_ratio / 100);
656 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
657 trigger_mask[0]) != SIGROK_OK)
659 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
660 trigger_value[0]) != SIGROK_OK)
662 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
663 0x00000008) != SIGROK_OK)
665 delaycount = limit_samples / 4;
668 set_configuration_samplerate(sdi, cur_samplerate);
670 /* Send sample limit and pre/post-trigger capture ratio. */
671 readcount = limit_samples / 4;
672 if (flag_reg & FLAG_DEMUX) {
673 data = (delaycount - 8) & 0xfff8 << 13;
674 data |= (readcount - 4) & 0xffff;
676 flag_reg |= FLAG_FILTER;
677 data = (readcount - 1) << 16;
678 data |= (delaycount - 1);
682 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE,
687 * Enable/disable channel groups in the flag register according to the
688 * probe mask. The register stores them backwards, hence shift right
692 for (i = 0; i < 4; i++) {
693 if (probe_mask & (0xff << (i * 8)))
694 changrp_mask |= (8 >> i);
697 /* The flag register wants them here, and 1 means "disable channel". */
698 flag_reg |= ~(changrp_mask << 2) & 0x3c;
700 data = flag_reg << 24;
701 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
704 /* Start acquisition on the device. */
705 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
708 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
711 /* Send header packet to the session bus. */
712 packet = g_malloc(sizeof(struct datafeed_packet));
713 header = g_malloc(sizeof(struct datafeed_header));
714 if (!packet || !header)
716 packet->type = DF_HEADER;
717 packet->length = sizeof(struct datafeed_header);
718 packet->payload = (unsigned char *)header;
719 header->feed_version = 1;
720 gettimeofday(&header->starttime, NULL);
721 header->samplerate = cur_samplerate;
722 header->protocol_id = PROTO_RAW;
723 header->num_probes = NUM_PROBES;
724 session_bus(session_device_id, packet);
731 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
733 struct datafeed_packet packet;
736 device_index = device_index;
738 packet.type = DF_END;
740 session_bus(session_device_id, &packet);
743 struct device_plugin ols_plugin_info = {
753 hw_set_configuration,
754 hw_start_acquisition,