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 int devcnt, final_devcnt, num_ports, fd, ret, i;
194 char buf[8], **device_names, **serial_params;
197 ports = g_slist_append(NULL, strdup(deviceinfo));
199 /* no specific device given, so scan all serial ports */
200 ports = list_serial_ports();
202 num_ports = g_slist_length(ports);
203 fds = calloc(1, num_ports * sizeof(GPollFD));
204 device_names = malloc(num_ports * (sizeof(char *)));
205 serial_params = malloc(num_ports * (sizeof(char *)));
207 for(l = ports; l; l = l->next) {
208 /* The discovery procedure is like this: first send the Reset command (0x00) 5 times,
209 * since the device could be anywhere in a 5-byte command. Then send the ID command
210 * (0x02). If the device responds with 4 bytes ("OLS1" or "SLA1"), we have a match.
211 * Since it may take the device a while to respond at 115Kb/s, we do all the sending
212 * first, then wait for all of them to respond with g_poll().
214 g_message("probing %s...", (char *) l->data);
215 fd = serial_open(l->data, O_RDWR | O_NONBLOCK);
217 serial_params[devcnt] = serial_backup_params(fd);
218 serial_set_params(fd, 115200, 8, 0, 1, 2);
220 for(i = 0; i < 5; i++) {
221 if( (ret = send_shortcommand(fd, CMD_RESET)) != SIGROK_OK) {
222 /* serial port is not writable */
226 if(ret != SIGROK_OK) {
227 serial_restore_params(fd, serial_params[devcnt]);
231 send_shortcommand(fd, CMD_ID);
233 fds[devcnt].events = G_IO_IN;
234 device_names[devcnt] = strdup(l->data);
240 /* 2ms should do it, that's enough time for 28 bytes to go over the bus */
244 g_poll(fds, devcnt, 1);
245 for(i = 0; i < devcnt; i++) {
246 if(fds[i].revents == G_IO_IN) {
247 if(read(fds[i].fd, buf, 4) == 4) {
248 if(!strncmp(buf, "1SLO", 4) || !strncmp(buf, "1ALS", 4)) {
249 if(!strncmp(buf, "1SLO", 4))
250 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
251 "Openbench", "Logic Sniffer", "v1.0");
253 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
254 "Sump", "Logic Analyzer", "v1.0");
255 sdi->serial = serial_device_instance_new(device_names[i], -1);
256 device_instances = g_slist_append(device_instances, sdi);
258 serial_close(fds[i].fd);
262 free(device_names[i]);
266 serial_restore_params(fds[i].fd, serial_params[i]);
267 serial_close(fds[i].fd);
269 free(serial_params[i]);
277 cur_samplerate = samplerates.low;
283 static int hw_opendev(int device_index)
285 struct sigrok_device_instance *sdi;
287 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
290 sdi->serial->fd = serial_open(sdi->serial->port, O_RDWR);
291 if(sdi->serial->fd == -1)
294 sdi->status = ST_ACTIVE;
300 static void hw_closedev(int device_index)
302 struct sigrok_device_instance *sdi;
304 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
307 if(sdi->serial->fd != -1) {
308 serial_close(sdi->serial->fd);
309 sdi->serial->fd = -1;
310 sdi->status = ST_INACTIVE;
316 static void hw_cleanup(void)
319 struct sigrok_device_instance *sdi;
321 /* properly close all devices */
322 for(l = device_instances; l; l = l->next) {
324 if(sdi->serial->fd != -1)
325 serial_close(sdi->serial->fd);
326 sigrok_device_instance_free(sdi);
328 g_slist_free(device_instances);
329 device_instances = NULL;
334 static void *hw_get_device_info(int device_index, int device_info_id)
336 struct sigrok_device_instance *sdi;
339 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
343 switch(device_info_id)
349 info = GINT_TO_POINTER(NUM_PROBES);
354 case DI_TRIGGER_TYPES:
355 info = (char *) TRIGGER_TYPES;
357 case DI_CUR_SAMPLERATE:
358 info = &cur_samplerate;
366 static int hw_get_status(int device_index)
368 struct sigrok_device_instance *sdi;
370 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
377 static int *hw_get_capabilities(void)
384 static int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
388 if(samplerate < samplerates.low || samplerate > samplerates.high)
389 return SIGROK_ERR_SAMPLERATE;
391 if(samplerate > CLOCK_RATE) {
392 flag_reg |= FLAG_DEMUX;
393 divider = (CLOCK_RATE * 2 / samplerate) - 1;
396 flag_reg &= ~FLAG_DEMUX;
397 divider = (CLOCK_RATE / samplerate) - 1;
399 divider = htonl(divider);
401 g_message("setting samplerate to %"PRIu64" Hz (divider %u, demux %s)", samplerate, divider,
402 flag_reg & FLAG_DEMUX ? "on" : "off");
403 if(send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, divider) != SIGROK_OK)
405 cur_samplerate = samplerate;
411 static int hw_set_configuration(int device_index, int capability, void *value)
413 struct sigrok_device_instance *sdi;
417 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
420 if(sdi->status != ST_ACTIVE)
423 if(capability == HWCAP_SAMPLERATE) {
425 ret = set_configuration_samplerate(sdi, *tmp_u64);
427 else if(capability == HWCAP_PROBECONFIG)
428 ret = configure_probes( (GSList *) value);
429 else if(capability == HWCAP_LIMIT_SAMPLES) {
430 limit_samples = strtoull(value, NULL, 10);
433 else if(capability == HWCAP_CAPTURE_RATIO) {
434 capture_ratio = strtol(value, NULL, 10);
435 if(capture_ratio < 0 || capture_ratio > 100) {
449 static int receive_data(int fd, int revents, void *user_data)
451 static int num_transfers = 0;
452 static int num_bytes = 0;
453 static char last_sample[4] = {0xff};
454 static unsigned char sample[4] = {0}, tmp_sample[4];
455 int count, buflen, num_channels, i, j;
456 struct datafeed_packet packet;
457 unsigned char byte, *buffer;
459 if(num_transfers++ == 0) {
460 /* first time round, means the device started sending data, and will not
461 * stop until done. if it stops sending for longer than it takes to send
462 * a byte, that means it's finished. we'll double that to 30ms to be sure...
465 source_add(fd, G_IO_IN, 100, receive_data, user_data);
469 for(i = 0x20; i > 0x02; i /= 2)
470 if((flag_reg & i) == 0)
473 if(revents == G_IO_IN && num_transfers / num_channels <= limit_samples) {
474 if(read(fd, &byte, 1) != 1)
477 sample[num_bytes++] = byte;
478 if(num_bytes == num_channels) {
479 /* got a full sample */
480 if(flag_reg & FLAG_RLE) {
481 /* in RLE mode -1 should never come in as a sample, because
482 * bit 31 is the "count" flag */
483 /* TODO: endianness may be wrong here, could be sample[3] */
484 if(sample[0] & 0x80 && !(last_sample[0] & 0x80)) {
485 count = (int) (*sample) & 0x7fffffff;
486 buffer = g_malloc(count);
488 for(i = 0; i < count; i++)
490 memcpy(buffer + buflen , last_sample, 4);
495 /* just a single sample, next sample will probably be a count
496 * referring to this -- but this one is still a part of the stream
508 if(num_channels < 4) {
509 /* some channel groups may have been turned off, to speed up transfer
510 * between the hardware and the PC. expand that here before submitting
511 * it over the session bus -- whatever is listening on the bus will be
512 * expecting a full 32-bit sample, based on the number of probes.
515 memset(tmp_sample, 0, 4);
516 for(i = 0; i < 4; i++) {
517 if((flag_reg & (8 >> i)) == 0) {
518 /* this channel group was enabled, copy from received sample */
519 tmp_sample[i] = sample[j++];
522 memcpy(sample, tmp_sample, 4);
525 /* send it all to the session bus */
526 packet.type = DF_LOGIC32;
527 packet.length = buflen;
528 packet.payload = buffer;
529 session_bus(user_data, &packet);
531 memcpy(last_sample, buffer, num_channels);
535 memset(sample, 0, 4);
540 /* this is the main loop telling us a timeout was reached, or we've
541 * acquired all the samples we asked for -- we're done */
542 tcflush(fd, TCIOFLUSH);
544 packet.type = DF_END;
546 session_bus(user_data, &packet);
553 static int hw_start_acquisition(int device_index, gpointer session_device_id)
555 struct datafeed_packet *packet;
556 struct datafeed_header *header;
557 struct sigrok_device_instance *sdi;
560 uint16_t readcount, delaycount;
561 uint8_t changrp_mask;
563 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
566 if(sdi->status != ST_ACTIVE)
569 if(trigger_mask[0]) {
571 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
573 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK)
575 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK)
577 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK)
580 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
582 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK)
584 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK)
586 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK)
589 /* trigger configuration */
590 /* TODO: the start flag should only be on the last used stage I think... */
591 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
593 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK)
595 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK)
597 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK)
599 delaycount = limit_samples / 4 * (capture_ratio / 100);
601 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
603 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
605 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
607 delaycount = limit_samples / 4;
610 set_configuration_samplerate(sdi, cur_samplerate);
612 /* send sample limit and pre/post-trigger capture ratio */
613 readcount = limit_samples / 4;
614 if(flag_reg & FLAG_DEMUX) {
615 data = (delaycount - 8) & 0xfff8 << 13;
616 data |= (readcount - 4) & 0xffff;
618 flag_reg |= FLAG_FILTER;
619 data = (readcount - 1) << 16;
620 data |= (delaycount - 1);
624 if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK)
628 /* enable/disable channel groups in the flag register according to the
629 * probe mask. The register stores them backwards, hence shift right from 1000.
632 for(i = 0; i < 4; i++)
634 if(probe_mask & (0xff << (i * 8)))
635 changrp_mask |= (8 >> i);
637 /* but the flag register wants them here, with 1 meaning "disable channel" */
638 flag_reg |= ~(changrp_mask << 2) & 0x3c;
640 data = flag_reg << 24;
641 if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
644 /* start acquisition on the device */
645 if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
648 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id);
650 /* send header packet to the session bus */
651 packet = g_malloc(sizeof(struct datafeed_packet));
652 header = g_malloc(sizeof(struct datafeed_header));
653 if(!packet || !header)
655 packet->type = DF_HEADER;
656 packet->length = sizeof(struct datafeed_header);
657 packet->payload = (unsigned char *) header;
658 header->feed_version = 1;
659 gettimeofday(&header->starttime, NULL);
660 header->samplerate = cur_samplerate;
661 header->protocol_id = PROTO_RAW;
662 header->num_probes = NUM_PROBES;
663 session_bus(session_device_id, packet);
671 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
673 struct datafeed_packet packet;
675 packet.type = DF_END;
677 session_bus(session_device_id, &packet);
683 struct device_plugin ols_plugin_info = {
694 hw_set_configuration,
695 hw_start_acquisition,