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>
35 #define NUM_TRIGGER_STAGES 4
36 #define TRIGGER_TYPES "01"
37 #define SERIAL_SPEED B115200
38 /* TODO: SERIAL_ bits, parity, stop bit */
39 #define CLOCK_RATE 100000000
43 #define CMD_RESET 0x00
45 #define CMD_SET_FLAGS 0x82
46 #define CMD_SET_DIVIDER 0x80
48 #define CMD_CAPTURE_SIZE 0x81
49 #define CMD_SET_TRIGGER_MASK_0 0xc0
50 #define CMD_SET_TRIGGER_MASK_1 0xc4
51 #define CMD_SET_TRIGGER_MASK_2 0xc8
52 #define CMD_SET_TRIGGER_MASK_3 0xcc
53 #define CMD_SET_TRIGGER_VALUE_0 0xc1
54 #define CMD_SET_TRIGGER_VALUE_1 0xc5
55 #define CMD_SET_TRIGGER_VALUE_2 0xc9
56 #define CMD_SET_TRIGGER_VALUE_3 0xcd
57 #define CMD_SET_TRIGGER_CONFIG_0 0xc2
58 #define CMD_SET_TRIGGER_CONFIG_1 0xc6
59 #define CMD_SET_TRIGGER_CONFIG_2 0xca
60 #define CMD_SET_TRIGGER_CONFIG_3 0xce
62 /* bitmasks for CMD_FLAGS */
63 #define FLAG_DEMUX 0x01
64 #define FLAG_FILTER 0x02
65 #define FLAG_CHANNELGROUP_1 0x04
66 #define FLAG_CHANNELGROUP_2 0x08
67 #define FLAG_CHANNELGROUP_3 0x10
68 #define FLAG_CHANNELGROUP_4 0x20
69 #define FLAG_CLOCK_EXTERNAL 0x40
70 #define FLAG_CLOCK_INVERTED 0x80
71 #define FLAG_RLE 0x0100
74 static int capabilities[] = {
82 static struct samplerates samplerates = {
89 /* list of struct serial_device_instance */
90 static GSList *device_instances = NULL;
92 /* current state of the flag register */
93 uint32_t flag_reg = 0;
95 static uint64_t cur_samplerate = 0;
96 static uint64_t limit_samples = 0;
97 /* pre/post trigger capture ratio, in percentage. 0 means no pre-trigger data. */
98 int capture_ratio = 0;
99 static uint32_t probe_mask = 0, trigger_mask[4] = {0}, trigger_value[4] = {0};
103 int send_shortcommand(int fd, uint8_t command)
107 g_message("ols: sending cmd 0x%.2x", command);
109 if(write(fd, buf, 1) != 1)
116 int send_longcommand(int fd, uint8_t command, uint32_t data)
120 g_message("ols: sending cmd 0x%.2x data 0x%.8x", command, data);
122 buf[1] = data & 0xff;
123 buf[2] = data & 0xff00 >> 8;
124 buf[3] = data & 0xff0000 >> 16;
125 buf[4] = data & 0xff000000 >> 24;
126 if(write(fd, buf, 5) != 5)
132 static int configure_probes(GSList *probes)
136 int probe_bit, changrp_mask, stage, i;
140 for(i = 0; i < NUM_TRIGGER_STAGES; i++)
143 trigger_value[i] = 0;
146 for(l = probes; l; l = l->next)
148 probe = (struct probe *) l->data;
149 probe_bit = 1 << (probe->index - 1);
150 probe_mask |= probe_bit;
154 for(tc = probe->trigger; *tc; tc++)
156 trigger_mask[stage] |= probe_bit;
158 trigger_value[stage] |= probe_bit;
162 /* only supporting parallel mode, with up to 4 stages */
169 /* enable/disable channel groups in the flag register according
170 * to the probe mask we just made. The register stores them backwards,
171 * hence shift right from 1000.
174 for(i = 0; i < 4; i++)
176 if(probe_mask & (0xff << i))
177 changrp_mask |= 8 >> i;
179 /* but the flag register wants them here */
180 flag_reg |= changrp_mask << 2;
186 static int hw_init(char *deviceinfo)
188 struct sigrok_device_instance *sdi;
191 struct termios term, *prev_termios;
192 int devcnt, final_devcnt, num_ports, fd, i;
193 char buf[8], **device_names;
196 ports = g_slist_append(NULL, g_strdup(deviceinfo));
198 /* no specific device given, so scan all serial ports */
199 ports = list_serial_ports();
201 num_ports = g_slist_length(ports);
202 fds = g_malloc0(num_ports * sizeof(GPollFD));
203 device_names = g_malloc(num_ports * (sizeof(char *)));
204 prev_termios = g_malloc(num_ports * sizeof(struct termios));
206 for(l = ports; l; l = l->next) {
207 /* The discovery procedure is like this: first send the Reset command (0x00) 5 times,
208 * since the device could be anywhere in a 5-byte command. Then send the ID command
209 * (0x02). If the device responds with 4 bytes ("OLS1" or "SLA1"), we have a match.
210 * Since it may take the device a while to respond at 115Kb/s, we do all the sending
211 * first, then wait for all of them to respond with g_poll().
213 fd = open(l->data, O_RDWR | O_NONBLOCK);
215 tcgetattr(fd, &prev_termios[devcnt]);
216 tcgetattr(fd, &term);
217 cfsetispeed(&term, SERIAL_SPEED);
218 tcsetattr(fd, TCSADRAIN, &term);
219 memset(buf, CMD_RESET, 5);
221 if(write(fd, buf, 6) == 6) {
223 fds[devcnt].events = G_IO_IN;
224 device_names[devcnt] = l->data;
226 g_message("probed device %s", (char *) l->data);
229 /* restore port settings. of course, we've crapped all over the port. */
230 tcsetattr(fd, TCSADRAIN, &prev_termios[devcnt]);
236 /* 2ms should do it, that's enough time for 28 bytes to go over the bus */
240 g_poll(fds, devcnt, 1);
241 for(i = 0; i < devcnt; i++) {
242 if(fds[i].revents == G_IO_IN) {
243 if(read(fds[i].fd, buf, 4) == 4) {
244 if(!strncmp(buf, "1SLO", 4) || !strncmp(buf, "1ALS", 4)) {
245 if(!strncmp(buf, "1SLO", 4))
246 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
247 "Openbench", "Logic Sniffer", "v1.0");
249 sdi = sigrok_device_instance_new(final_devcnt, ST_INACTIVE,
250 "Sump", "Logic Analyzer", "v1.0");
251 sdi->serial = serial_device_instance_new(device_names[i], -1);
252 device_instances = g_slist_append(device_instances, sdi);
260 tcsetattr(fds[i].fd, TCSADRAIN, &prev_termios[i]);
265 g_free(device_names);
266 g_free(prev_termios);
273 static int hw_opendev(int device_index)
275 struct sigrok_device_instance *sdi;
277 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
280 sdi->serial->fd = open(sdi->serial->port, O_RDWR);
281 if(sdi->serial->fd == -1)
284 sdi->status = ST_ACTIVE;
290 static void hw_closedev(int device_index)
292 struct sigrok_device_instance *sdi;
294 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
297 if(sdi->serial->fd != -1) {
298 close(sdi->serial->fd);
299 sdi->serial->fd = -1;
300 sdi->status = ST_INACTIVE;
306 static void hw_cleanup(void)
309 struct sigrok_device_instance *sdi;
311 /* properly close all devices */
312 for(l = device_instances; l; l = l->next) {
314 if(sdi->serial->fd != -1)
315 close(sdi->serial->fd);
316 sigrok_device_instance_free(sdi);
318 g_slist_free(device_instances);
319 device_instances = NULL;
324 static void *hw_get_device_info(int device_index, int device_info_id)
326 struct sigrok_device_instance *sdi;
329 if( !(sdi = get_sigrok_device_instance(device_instances, device_index)) )
333 switch(device_info_id)
339 info = GINT_TO_POINTER(NUM_PROBES);
344 case DI_TRIGGER_TYPES:
345 info = (char *) TRIGGER_TYPES;
347 case DI_CUR_SAMPLE_RATE:
348 info = &cur_samplerate;
356 static int hw_get_status(int device_index)
358 struct sigrok_device_instance *sdi;
360 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
367 static int *hw_get_capabilities(void)
374 static int set_configuration_samplerate(struct sigrok_device_instance *sdi, uint64_t samplerate)
378 if(samplerate < samplerates.low || samplerate > samplerates.high)
379 return SIGROK_ERR_BADVALUE;
381 if(samplerate > CLOCK_RATE) {
382 flag_reg |= FLAG_DEMUX;
383 divider = (uint8_t) (CLOCK_RATE * 2 / samplerate) - 1;
386 flag_reg &= FLAG_DEMUX;
387 divider = (uint8_t) (CLOCK_RATE / samplerate) - 1;
391 g_message("setting samplerate to %"PRIu64" Hz (divider %u, demux %s)", samplerate, divider,
392 flag_reg & FLAG_DEMUX ? "on" : "off");
393 if(send_longcommand(sdi->serial->fd, CMD_SET_DIVIDER, divider) != SIGROK_OK)
395 cur_samplerate = samplerate;
401 static int hw_set_configuration(int device_index, int capability, void *value)
403 struct sigrok_device_instance *sdi;
407 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
410 if(sdi->status != ST_ACTIVE)
413 if(capability == HWCAP_SAMPLERATE) {
415 ret = set_configuration_samplerate(sdi, *tmp_u64);
417 else if(capability == HWCAP_PROBECONFIG)
418 ret = configure_probes( (GSList *) value);
419 else if(capability == HWCAP_LIMIT_SAMPLES) {
420 limit_samples = strtoull(value, NULL, 10);
423 else if(capability == HWCAP_CAPTURE_RATIO) {
424 capture_ratio = strtol(value, NULL, 10);
425 if(capture_ratio < 0 || capture_ratio > 100) {
439 static int receive_data(int fd, int revents, void *user_data)
441 static int num_transfers = 0;
442 static int num_bytes = 0;
443 static char last_sample[4] = {0xff};
444 static unsigned char sample[4];
445 int count, buflen, i;
446 struct datafeed_packet packet;
447 unsigned char byte, *buffer;
449 if(num_transfers++ == 0) {
450 /* first time round, means the device started sending data, and will not
451 * stop until done. if it stops sending for longer than it takes to send
452 * a byte, that means it's finished. we'll double that to 30ms to be sure...
455 source_add(fd, G_IO_IN, 30, receive_data, user_data);
458 /* TODO: / 4 depends on # of channels used */
459 if(revents == G_IO_IN && num_transfers / 4 <= limit_samples){
460 if(read(fd, &byte, 1) != 1)
463 sample[num_bytes++] = byte;
465 g_message("got sample 0x%.2x%.2x%.2x%.2x", sample[3], sample[2], sample[1], sample[0]);
466 /* got a full sample */
467 if(flag_reg & FLAG_RLE) {
468 /* in RLE mode -1 should never come in as a sample, because
469 * bit 31 is the "count" flag */
470 /* TODO: endianness may be wrong here, could be sample[3] */
471 if(sample[0] & 0x80 && !(last_sample[0] & 0x80)) {
472 count = (int) (*sample) & 0x7fffffff;
473 buffer = g_malloc(count);
475 for(i = 0; i < count; i++)
477 memcpy(buffer + buflen , last_sample, 4);
482 /* just a single sample, next sample will probably be a count
483 * referring to this -- but this one is still a part of the stream
495 /* send it all to the session bus */
496 packet.type = DF_LOGIC32;
497 packet.length = buflen;
498 packet.payload = buffer;
499 session_bus(user_data, &packet);
501 memcpy(last_sample, buffer, 4);
509 /* this is the main loop telling us a timeout was reached -- we're done */
510 tcflush(fd, TCIOFLUSH);
512 packet.type = DF_END;
514 session_bus(user_data, &packet);
521 static int hw_start_acquisition(int device_index, gpointer session_device_id)
523 struct datafeed_packet *packet;
524 struct datafeed_header *header;
525 struct sigrok_device_instance *sdi;
528 if(!(sdi = get_sigrok_device_instance(device_instances, device_index)))
531 if(sdi->status != ST_ACTIVE)
535 if(send_longcommand(sdi->serial->fd, CMD_RESET, 0) != SIGROK_OK)
538 /* send flag register */
539 data = flag_reg << 24;
540 if(send_longcommand(sdi->serial->fd, CMD_SET_FLAGS, data) != SIGROK_OK)
543 /* send sample limit and pre/post-trigger capture ratio */
544 data = limit_samples / 4 << 16;
546 data |= (limit_samples - (limit_samples / 100 * capture_ratio)) / 4;
548 if(send_longcommand(sdi->serial->fd, CMD_CAPTURE_SIZE, data) != SIGROK_OK)
552 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_0, trigger_mask[0]) != SIGROK_OK)
554 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_1, trigger_mask[1]) != SIGROK_OK)
555 // return SIGROK_NOK;
556 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_2, trigger_mask[2]) != SIGROK_OK)
557 // return SIGROK_NOK;
558 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_MASK_3, trigger_mask[3]) != SIGROK_OK)
559 // return SIGROK_NOK;
561 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_0, trigger_value[0]) != SIGROK_OK)
563 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_1, trigger_value[1]) != SIGROK_OK)
564 // return SIGROK_NOK;
565 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_2, trigger_value[2]) != SIGROK_OK)
566 // return SIGROK_NOK;
567 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_VALUE_3, trigger_value[3]) != SIGROK_OK)
568 // return SIGROK_NOK;
570 /* trigger configuration */
571 /* TODO: the start flag should only be on the last used stage I think... */
572 if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_0, 0x00000008) != SIGROK_OK)
574 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_1, 0x00000000) != SIGROK_OK)
575 // return SIGROK_NOK;
576 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_2, 0x00000000) != SIGROK_OK)
577 // return SIGROK_NOK;
578 // if(send_longcommand(sdi->serial->fd, CMD_SET_TRIGGER_CONFIG_3, 0x00000000) != SIGROK_OK)
579 // return SIGROK_NOK;
581 set_configuration_samplerate(sdi, cur_samplerate);
583 /* start acquisition on the device */
584 if(send_shortcommand(sdi->serial->fd, CMD_RUN) != SIGROK_OK)
587 source_add(sdi->serial->fd, G_IO_IN, -1, receive_data, session_device_id);
589 /* send header packet to the session bus */
590 packet = g_malloc(sizeof(struct datafeed_packet));
591 header = g_malloc(sizeof(struct datafeed_header));
592 if(!packet || !header)
594 packet->type = DF_HEADER;
595 packet->length = sizeof(struct datafeed_header);
596 packet->payload = (unsigned char *) header;
597 header->feed_version = 1;
598 gettimeofday(&header->starttime, NULL);
599 header->rate = cur_samplerate;
600 header->protocol_id = PROTO_RAW;
601 header->num_probes = NUM_PROBES;
602 session_bus(session_device_id, packet);
610 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
612 struct datafeed_packet packet;
614 packet.type = DF_END;
616 session_bus(session_device_id, &packet);
622 struct device_plugin ols_plugin_info = {
633 hw_set_configuration,
634 hw_start_acquisition,