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) {
451 limit_samples = strtoull(value, NULL, 10);
453 } else if (capability == HWCAP_CAPTURE_RATIO) {
454 capture_ratio = strtol(value, NULL, 10);
455 if (capture_ratio < 0 || capture_ratio > 100) {
467 static int receive_data(int fd, int revents, void *user_data)
469 static unsigned int num_transfers = 0;
470 static int num_bytes = 0;
471 static char last_sample[4] = { 0xff, 0xff, 0xff, 0xff };
472 static unsigned char sample[4] = { 0, 0, 0, 0 };
473 static unsigned char tmp_sample[4];
474 int count, buflen, num_channels, i, j;
475 struct datafeed_packet packet;
476 unsigned char byte, *buffer;
478 if (num_transfers++ == 0) {
480 * First time round, means the device started sending data,
481 * and will not stop until done. If it stops sending for
482 * longer than it takes to send a byte, that means it's
483 * finished. We'll double that to 30ms to be sure...
486 source_add(fd, G_IO_IN, 100, receive_data, user_data);
490 for (i = 0x20; i > 0x02; i /= 2) {
491 if ((flag_reg & i) == 0)
495 if (revents == G_IO_IN
496 && num_transfers / num_channels <= limit_samples) {
497 if (read(fd, &byte, 1) != 1)
500 sample[num_bytes++] = byte;
501 if (num_bytes == num_channels) {
502 /* Got a full sample. */
503 if (flag_reg & FLAG_RLE) {
505 * In RLE mode -1 should never come in as a
506 * sample, because bit 31 is the "count" flag.
507 * TODO: Endianness may be wrong here, could be
511 && !(last_sample[0] & 0x80)) {
512 count = (int)(*sample) & 0x7fffffff;
513 buffer = g_malloc(count);
515 for (i = 0; i < count; i++) {
516 memcpy(buffer + buflen,
522 * Just a single sample, next sample
523 * will probably be a count referring
524 * to this -- but this one is still a
525 * part of the stream.
531 /* No compression. */
536 if (num_channels < 4) {
538 * Some channel groups may have been turned
539 * off, to speed up transfer between the
540 * hardware and the PC. Expand that here before
541 * submitting it over the session bus --
542 * whatever is listening on the bus will be
543 * expecting a full 32-bit sample, based on
544 * the number of probes.
547 memset(tmp_sample, 0, 4);
548 for (i = 0; i < 4; i++) {
549 if ((flag_reg & (8 >> i)) == 0) {
551 * This channel group was
552 * enabled, copy from received
555 tmp_sample[i] = sample[j++];
558 memcpy(sample, tmp_sample, 4);
561 /* Send it all to the session bus. */
562 packet.type = DF_LOGIC32;
563 packet.length = buflen;
564 packet.payload = buffer;
565 session_bus(user_data, &packet);
566 if (buffer == sample)
567 memcpy(last_sample, buffer, num_channels);
571 memset(sample, 0, 4);
576 * This is the main loop telling us a timeout was reached, or
577 * we've acquired all the samples we asked for -- we're done.
580 /* TODO: Move to serial.c? */
581 tcflush(fd, TCIOFLUSH);
584 packet.type = DF_END;
586 session_bus(user_data, &packet);
592 static int hw_start_acquisition(int device_index, gpointer session_device_id)
595 struct datafeed_packet *packet;
596 struct datafeed_header *header;
597 struct sigrok_device_instance *sdi;
599 uint16_t readcount, delaycount;
600 uint8_t changrp_mask;
602 if (!(sdi = get_sigrok_device_instance(device_instances, device_index)))
605 if (sdi->status != ST_ACTIVE)
608 if (trigger_mask[0]) {
610 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
611 trigger_mask[0]) != SIGROK_OK)
613 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1,
614 trigger_mask[1]) != SIGROK_OK)
616 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2,
617 trigger_mask[2]) != SIGROK_OK)
619 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3,
620 trigger_mask[3]) != SIGROK_OK)
623 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
624 trigger_value[0]) != SIGROK_OK)
626 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1,
627 trigger_value[1]) != SIGROK_OK)
629 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2,
630 trigger_value[2]) != SIGROK_OK)
632 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3,
633 trigger_value[3]) != SIGROK_OK)
636 /* Trigger configuration */
638 * TODO: The start flag should only be on the last used
641 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
642 0x00000008) != SIGROK_OK)
644 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1,
645 0x00000000) != SIGROK_OK)
647 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2,
648 0x00000000) != SIGROK_OK)
650 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3,
651 0x00000000) != SIGROK_OK)
653 delaycount = limit_samples / 4 * (capture_ratio / 100);
655 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0,
656 trigger_mask[0]) != SIGROK_OK)
658 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0,
659 trigger_value[0]) != SIGROK_OK)
661 if (send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0,
662 0x00000008) != SIGROK_OK)
664 delaycount = limit_samples / 4;
667 set_configuration_samplerate(sdi, cur_samplerate);
669 /* Send sample limit and pre/post-trigger capture ratio. */
670 readcount = limit_samples / 4;
671 if (flag_reg & FLAG_DEMUX) {
672 data = (delaycount - 8) & 0xfff8 << 13;
673 data |= (readcount - 4) & 0xffff;
675 flag_reg |= FLAG_FILTER;
676 data = (readcount - 1) << 16;
677 data |= (delaycount - 1);
681 if (send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE,
686 * Enable/disable channel groups in the flag register according to the
687 * probe mask. The register stores them backwards, hence shift right
691 for (i = 0; i < 4; i++) {
692 if (probe_mask & (0xff << (i * 8)))
693 changrp_mask |= (8 >> i);
696 /* The flag register wants them here, and 1 means "disable channel". */
697 flag_reg |= ~(changrp_mask << 2) & 0x3c;
699 data = flag_reg << 24;
700 if (send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
703 /* Start acquisition on the device. */
704 if (send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
707 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data,
710 /* Send header packet to the session bus. */
711 packet = g_malloc(sizeof(struct datafeed_packet));
712 header = g_malloc(sizeof(struct datafeed_header));
713 if (!packet || !header)
715 packet->type = DF_HEADER;
716 packet->length = sizeof(struct datafeed_header);
717 packet->payload = (unsigned char *)header;
718 header->feed_version = 1;
719 gettimeofday(&header->starttime, NULL);
720 header->samplerate = cur_samplerate;
721 header->protocol_id = PROTO_RAW;
722 header->num_probes = NUM_PROBES;
723 session_bus(session_device_id, packet);
730 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
732 struct datafeed_packet packet;
735 device_index = device_index;
737 packet.type = DF_END;
739 session_bus(session_device_id, &packet);
742 struct device_plugin ols_plugin_info = {
752 hw_set_configuration,
753 hw_start_acquisition,