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>
31 #include <arpa/inet.h>
36 #define NUM_TRIGGER_STAGES 4
37 #define TRIGGER_TYPES "01"
38 #define SERIAL_SPEED B115200
39 /* TODO: SERIAL_ bits, parity, stop bit */
40 #define CLOCK_RATE 100000000
44 #define CMD_RESET 0x00
46 #define CMD_SET_FLAGS 0x82
47 #define CMD_SET_DIVIDER 0x80
49 #define CMD_CAPTURE_SIZE 0x81
50 #define CMD_SET_TRIGGER_MASK_0 0xc0
51 #define CMD_SET_TRIGGER_MASK_1 0xc4
52 #define CMD_SET_TRIGGER_MASK_2 0xc8
53 #define CMD_SET_TRIGGER_MASK_3 0xcc
54 #define CMD_SET_TRIGGER_VALUE_0 0xc1
55 #define CMD_SET_TRIGGER_VALUE_1 0xc5
56 #define CMD_SET_TRIGGER_VALUE_2 0xc9
57 #define CMD_SET_TRIGGER_VALUE_3 0xcd
58 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
59 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
60 #define CMD_SET_TRIGGER_CONFIG_2 0xca
61 #define CMD_SET_TRIGGER_CONFIG_3 0xce
63 /* bitmasks for CMD_FLAGS */
64 #define FLAG_DEMUX 0x01
65 #define FLAG_FILTER 0x02
66 #define FLAG_CHANNELGROUP_1 0x04
67 #define FLAG_CHANNELGROUP_2 0x08
68 #define FLAG_CHANNELGROUP_3 0x10
69 #define FLAG_CHANNELGROUP_4 0x20
70 #define FLAG_CLOCK_EXTERNAL 0x40
71 #define FLAG_CLOCK_INVERTED 0x80
72 #define FLAG_RLE 0x0100
75 static int capabilities[] = {
83 static struct samplerates samplerates = {
90 /* list of struct serial_device_instance */
91 static GSList *device_instances = NULL;
93 /* current state of the flag register */
94 static uint32_t flag_reg = 0;
96 static uint64_t cur_samplerate = 0;
97 static uint64_t limit_samples = 0;
98 /* pre/post trigger capture ratio, in percentage. 0 means no pre-trigger data. */
99 static int capture_ratio = 0;
100 static uint32_t probe_mask = 0xffffffff, trigger_mask[4] = {0}, trigger_value[4] = {0};
104 static int send_shortcommand(int fd, uint8_t command)
108 g_message("ols: sending cmd 0x%.2x", command);
110 if(write(fd, buf, 1) != 1)
117 static int send_longcommand(int fd, uint8_t command, uint32_t data)
121 g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
123 buf[1] = (data & 0xff000000) >> 24;
124 buf[2] = (data & 0xff0000) >> 16;
125 buf[3] = (data & 0xff00) >> 8;
126 buf[4] = data & 0xff;
127 if(write(fd, buf, 5) != 5)
133 static int configure_probes(GSList *probes)
137 int probe_bit, stage, i;
141 for(i = 0; i < NUM_TRIGGER_STAGES; i++) {
143 trigger_value[i] = 0;
146 for(l = probes; l; l = l->next) {
147 probe = (struct probe *) l->data;
151 /* set up the probe mask for later configuration into the flag register */
152 probe_bit = 1 << (probe->index - 1);
153 probe_mask |= probe_bit;
158 /* configure trigger mask and value */
160 for(tc = probe->trigger; tc && *tc; tc++) {
161 trigger_mask[stage] |= probe_bit;
163 trigger_value[stage] |= probe_bit;
166 /* TODO: only supporting parallel mode, with up to 4 stages */
175 static void byteswap(uint32_t *in)
179 out = (*in & 0xff) << 8;
180 out |= (*in & 0xff00) >> 8;
181 out |= (*in & 0xff0000) << 8;
182 out |= (*in & 0xff000000) >> 8;
188 static int hw_init(char *deviceinfo)
190 struct sigrok_device_instance *sdi;
193 struct termios term, *prev_termios;
194 int devcnt, final_devcnt, num_ports, fd, ret, i;
195 char buf[8], **device_names;
198 ports = g_slist_append(NULL, strdup(deviceinfo));
200 /* no specific device given, so scan all serial ports */
201 ports = list_serial_ports();
203 num_ports = g_slist_length(ports);
204 fds = calloc(1, num_ports * sizeof(GPollFD));
205 device_names = malloc(num_ports * (sizeof(char *)));
206 prev_termios = malloc(num_ports * sizeof(struct termios));
208 for(l = ports; l; l = l->next) {
209 /* The discovery procedure is like this: first send the Reset command (0x00) 5 times,
210 * since the device could be anywhere in a 5-byte command. Then send the ID command
211 * (0x02). If the device responds with 4 bytes ("OLS1" or "SLA1"), we have a match.
212 * Since it may take the device a while to respond at 115Kb/s, we do all the sending
213 * first, then wait for all of them to respond with g_poll().
215 g_message("probing %s...", (char *) l->data);
216 fd = open(l->data, O_RDWR | O_NONBLOCK);
218 tcgetattr(fd, &prev_termios[devcnt]);
219 tcgetattr(fd, &term);
220 cfsetispeed(&term, SERIAL_SPEED);
221 term.c_cflag &= ~CSIZE;
223 term.c_cflag &= ~CSTOPB;
224 term.c_cflag |= IXON | IXOFF;
225 term.c_iflag |= IGNPAR;
226 tcsetattr(fd, TCSADRAIN, &term);
228 for(i = 0; i < 5; i++) {
229 if( (ret = send_shortcommand(fd, CMD_RESET)) != SIGROK_OK) {
230 /* serial port is not writable... restore port settings */
231 tcsetattr(fd, TCSADRAIN, &prev_termios[devcnt]);
238 send_shortcommand(fd, CMD_ID);
240 fds[devcnt].events = G_IO_IN;
241 device_names[devcnt] = strdup(l->data);
247 /* 2ms should do it, that's enough time for 28 bytes to go over the bus */
251 g_poll(fds, devcnt, 1);
252 for(i = 0; i < devcnt; i++) {
253 if(fds[i].revents == G_IO_IN) {
254 if(read(fds[i].fd, buf, 4) == 4) {
255 if(!strncmp(buf, "1SLO", 4) || !strncmp(buf, "1ALS", 4)) {
256 if(!strncmp(buf, "1SLO", 4))
257 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
258 "Openbench", "Logic Sniffer", "v1.0");
260 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
261 "Sump", "Logic Analyzer", "v1.0");
262 sdi->serial = serial_device_instance_new(device_names[i], -1);
263 device_instances = g_slist_append(device_instances, sdi);
269 free(device_names[i]);
273 tcsetattr(fds[i].fd, TCSADRAIN, &prev_termios[i]);
283 cur_samplerate = samplerates.low;
289 static int hw_opendev(int device_index)
291 struct sigrok_device_instance *sdi;
293 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
296 sdi->serial->fd = open(sdi->serial->port, O_RDWR);
297 if(sdi->serial->fd == -1)
300 sdi->status = ST_ACTIVE;
306 static void hw_closedev(int device_index)
308 struct sigrok_device_instance *sdi;
310 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
313 if(sdi->serial->fd != -1) {
314 close(sdi->serial->fd);
315 sdi->serial->fd = -1;
316 sdi->status = ST_INACTIVE;
322 static void hw_cleanup(void)
325 struct sigrok_device_instance *sdi;
327 /* properly close all devices */
328 for(l = device_instances; l; l = l->next) {
330 if(sdi->serial->fd != -1)
331 close(sdi->serial->fd);
332 sigrok_device_instance_free(sdi);
334 g_slist_free(device_instances);
335 device_instances = NULL;
340 static void *hw_get_device_info(int device_index, int device_info_id)
342 struct sigrok_device_instance *sdi;
345 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
349 switch(device_info_id)
355 info = GINT_TO_POINTER(NUM_PROBES);
360 case DI_TRIGGER_TYPES:
361 info = (char *) TRIGGER_TYPES;
363 case DI_CUR_SAMPLERATE:
364 info = &cur_samplerate;
372 static int hw_get_status(int device_index)
374 struct sigrok_device_instance *sdi;
376 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
383 static int *hw_get_capabilities(void)
390 static int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
394 if(samplerate < samplerates.low || samplerate > samplerates.high)
395 return SIGROK_ERR_SAMPLERATE;
397 if(samplerate > CLOCK_RATE) {
398 flag_reg |= FLAG_DEMUX;
399 divider = (CLOCK_RATE * 2 / samplerate) - 1;
402 flag_reg &= ~FLAG_DEMUX;
403 divider = (CLOCK_RATE / samplerate) - 1;
405 divider = htonl(divider);
407 g_message("setting samplerate to %"PRIu64" Hz (divider %u, demux %s)", samplerate, divider,
408 flag_reg & FLAG_DEMUX ? "on" : "off");
409 if(send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, divider) != SIGROK_OK)
411 cur_samplerate = samplerate;
417 static int hw_set_configuration(int device_index, int capability, void *value)
419 struct sigrok_device_instance *sdi;
423 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
426 if(sdi->status != ST_ACTIVE)
429 if(capability == HWCAP_SAMPLERATE) {
431 ret = set_configuration_samplerate(sdi, *tmp_u64);
433 else if(capability == HWCAP_PROBECONFIG)
434 ret = configure_probes( (GSList *) value);
435 else if(capability == HWCAP_LIMIT_SAMPLES) {
436 limit_samples = strtoull(value, NULL, 10);
439 else if(capability == HWCAP_CAPTURE_RATIO) {
440 capture_ratio = strtol(value, NULL, 10);
441 if(capture_ratio < 0 || capture_ratio > 100) {
455 static int receive_data(int fd, int revents, void *user_data)
457 static int num_transfers = 0;
458 static int num_bytes = 0;
459 static char last_sample[4] = {0xff};
460 static unsigned char sample[4] = {0}, tmp_sample[4];
461 int count, buflen, num_channels, i, j;
462 struct datafeed_packet packet;
463 unsigned char byte, *buffer;
465 if(num_transfers++ == 0) {
466 /* first time round, means the device started sending data, and will not
467 * stop until done. if it stops sending for longer than it takes to send
468 * a byte, that means it's finished. we'll double that to 30ms to be sure...
471 source_add(fd, G_IO_IN, 100, receive_data, user_data);
475 for(i = 0x20; i > 0x02; i /= 2)
476 if((flag_reg & i) == 0)
479 if(revents == G_IO_IN && num_transfers / num_channels <= limit_samples) {
480 if(read(fd, &byte, 1) != 1)
483 sample[num_bytes++] = byte;
484 if(num_bytes == num_channels) {
485 /* got a full sample */
486 if(flag_reg & FLAG_RLE) {
487 /* in RLE mode -1 should never come in as a sample, because
488 * bit 31 is the "count" flag */
489 /* TODO: endianness may be wrong here, could be sample[3] */
490 if(sample[0] & 0x80 && !(last_sample[0] & 0x80)) {
491 count = (int) (*sample) & 0x7fffffff;
492 buffer = g_malloc(count);
494 for(i = 0; i < count; i++)
496 memcpy(buffer + buflen , last_sample, 4);
501 /* just a single sample, next sample will probably be a count
502 * referring to this -- but this one is still a part of the stream
514 if(num_channels < 4) {
515 /* some channel groups may have been turned off, to speed up transfer
516 * between the hardware and the PC. expand that here before submitting
517 * it over the session bus -- whatever is listening on the bus will be
518 * expecting a full 32-bit sample, based on the number of probes.
521 memset(tmp_sample, 0, 4);
522 for(i = 0; i < 4; i++) {
523 if((flag_reg & (8 >> i)) == 0) {
524 /* this channel group was enabled, copy from received sample */
525 tmp_sample[i] = sample[j++];
528 memcpy(sample, tmp_sample, 4);
531 /* send it all to the session bus */
532 packet.type = DF_LOGIC32;
533 packet.length = buflen;
534 packet.payload = buffer;
535 session_bus(user_data, &packet);
537 memcpy(last_sample, buffer, num_channels);
541 memset(sample, 0, 4);
546 /* this is the main loop telling us a timeout was reached, or we've
547 * acquired all the samples we asked for -- we're done */
548 tcflush(fd, TCIOFLUSH);
550 packet.type = DF_END;
552 session_bus(user_data, &packet);
559 static int hw_start_acquisition(int device_index, gpointer session_device_id)
561 struct datafeed_packet *packet;
562 struct datafeed_header *header;
563 struct sigrok_device_instance *sdi;
566 uint16_t readcount, delaycount;
567 uint8_t changrp_mask;
569 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
572 if(sdi->status != ST_ACTIVE)
575 if(trigger_mask[0]) {
577 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
579 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK)
581 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK)
583 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK)
586 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
588 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK)
590 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK)
592 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK)
595 /* trigger configuration */
596 /* TODO: the start flag should only be on the last used stage I think... */
597 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
599 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK)
601 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK)
603 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK)
605 delaycount = limit_samples / 4 * (capture_ratio / 100);
607 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
609 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
611 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
613 delaycount = limit_samples / 4;
616 set_configuration_samplerate(sdi, cur_samplerate);
618 /* send sample limit and pre/post-trigger capture ratio */
619 readcount = limit_samples / 4;
620 if(flag_reg & FLAG_DEMUX) {
621 data = (delaycount - 8) & 0xfff8 << 13;
622 data |= (readcount - 4) & 0xffff;
624 flag_reg |= FLAG_FILTER;
625 data = (readcount - 1) << 16;
626 data |= (delaycount - 1);
630 if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK)
634 /* enable/disable channel groups in the flag register according to the
635 * probe mask. The register stores them backwards, hence shift right from 1000.
638 for(i = 0; i < 4; i++)
640 if(probe_mask & (0xff << (i * 8)))
641 changrp_mask |= (8 >> i);
643 /* but the flag register wants them here, with 1 meaning "disable channel" */
644 flag_reg |= ~(changrp_mask << 2) & 0x3c;
646 data = flag_reg << 24;
647 if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
650 /* start acquisition on the device */
651 if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
654 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id);
656 /* send header packet to the session bus */
657 packet = g_malloc(sizeof(struct datafeed_packet));
658 header = g_malloc(sizeof(struct datafeed_header));
659 if(!packet || !header)
661 packet->type = DF_HEADER;
662 packet->length = sizeof(struct datafeed_header);
663 packet->payload = (unsigned char *) header;
664 header->feed_version = 1;
665 gettimeofday(&header->starttime, NULL);
666 header->samplerate = cur_samplerate;
667 header->protocol_id = PROTO_RAW;
668 header->num_probes = NUM_PROBES;
669 session_bus(session_device_id, packet);
677 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
679 struct datafeed_packet packet;
681 packet.type = DF_END;
683 session_bus(session_device_id, &packet);
689 struct device_plugin ols_plugin_info = {
700 hw_set_configuration,
701 hw_start_acquisition,