]> sigrok.org Git - libsigrok.git/blame - input/chronovu_la8.c
All drivers: Drop unneeded comments.
[libsigrok.git] / input / chronovu_la8.c
CommitLineData
83e9d586
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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 2 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdlib.h>
22#include <fcntl.h>
23#include <unistd.h>
36423d04 24#include <sys/stat.h>
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
83e9d586
UH
27
28#define NUM_PACKETS 2048
29#define PACKET_SIZE 4096
30#define DEFAULT_NUM_PROBES 8
31
32/**
33 * Convert the LA8 'divcount' value to the respective samplerate (in Hz).
34 *
35 * LA8 hardware: sample period = (divcount + 1) * 10ns.
36 * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
37 * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
38 *
39 * @param divcount The divcount value as needed by the hardware.
44dae539 40 *
83e9d586
UH
41 * @return The samplerate in Hz, or 0xffffffffffffffff upon errors.
42 */
43static uint64_t divcount_to_samplerate(uint8_t divcount)
44{
45 if (divcount == 0xff)
46 return 0xffffffffffffffffULL;
47
48 return SR_MHZ(100) / (divcount + 1);
49}
50
51static int format_match(const char *filename)
52{
3217b032
UH
53 struct stat stat_buf;
54 int ret;
55
83e9d586 56 if (!filename) {
7b48d6e1 57 sr_err("la8 in: %s: filename was NULL", __func__);
83e9d586
UH
58 // return SR_ERR; /* FIXME */
59 return FALSE;
60 }
61
62 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
7b48d6e1 63 sr_err("la8 in: %s: input file '%s' does not exist",
133a37bf 64 __func__, filename);
83e9d586
UH
65 // return SR_ERR; /* FIXME */
66 return FALSE;
67 }
68
69 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
7b48d6e1 70 sr_err("la8 in: %s: input file '%s' not a regular file",
133a37bf 71 __func__, filename);
83e9d586
UH
72 // return SR_ERR; /* FIXME */
73 return FALSE;
74 }
75
3217b032
UH
76 /* Only accept files of length 8MB + 5 bytes. */
77 ret = stat(filename, &stat_buf);
78 if (ret != 0) {
79 sr_err("la8 in: %s: Error getting file size of '%s'",
80 __func__, filename);
81 return FALSE;
82 }
83 if (stat_buf.st_size != (8 * 1024 * 1024 + 5)) {
84 sr_dbg("la8 in: %s: File size must be exactly 8388613 bytes ("
85 "it actually is %d bytes in size), so this is not a "
86 "ChronoVu LA8 file.", __func__, stat_buf.st_size);
87 return FALSE;
88 }
83e9d586
UH
89
90 /* TODO: Check for divcount != 0xff. */
91
92 return TRUE;
93}
94
95static int init(struct sr_input *in)
96{
5c3c1241 97 struct sr_probe *probe;
464d12c7 98 int num_probes, i;
c37d2b1b 99 char name[SR_MAX_PROBENAME_LEN + 1];
c506a6a6
100 char *param;
101
102 num_probes = DEFAULT_NUM_PROBES;
103
104 if (in->param) {
105 param = g_hash_table_lookup(in->param, "numprobes");
106 if (param) {
107 num_probes = strtoul(param, NULL, 10);
108 if (num_probes < 1) {
109 sr_err("la8 in: %s: strtoul failed", __func__);
110 return SR_ERR;
111 }
83e9d586 112 }
83e9d586
UH
113 }
114
115 /* Create a virtual device. */
5c3c1241 116 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
464d12c7
KS
117
118 for (i = 0; i < num_probes; i++) {
119 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
c37d2b1b 120 /* TODO: Check return value. */
5c3c1241
BV
121 if (!(probe = sr_probe_new(i, SR_PROBE_LOGIC, TRUE, name)))
122 return SR_ERR;
123 in->sdi->probes = g_slist_append(in->sdi->probes, probe);
464d12c7 124 }
83e9d586
UH
125
126 return SR_OK;
127}
128
129static int loadfile(struct sr_input *in, const char *filename)
130{
131 struct sr_datafeed_header header;
132 struct sr_datafeed_packet packet;
f366e86c 133 struct sr_datafeed_meta_logic meta;
9c939c51 134 struct sr_datafeed_logic logic;
83e9d586
UH
135 uint8_t buf[PACKET_SIZE], divcount;
136 int i, fd, size, num_probes;
137 uint64_t samplerate;
138
139 /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */
140 if ((fd = open(filename, O_RDONLY)) == -1) {
7b48d6e1 141 sr_err("la8 in: %s: file open failed", __func__);
83e9d586
UH
142 return SR_ERR;
143 }
144
5c3c1241 145 num_probes = g_slist_length(in->sdi->probes);
83e9d586
UH
146
147 /* Seek to the end of the file, and read the divcount byte. */
148 divcount = 0x00; /* TODO: Don't hardcode! */
149
150 /* Convert the divcount value to a samplerate. */
151 samplerate = divcount_to_samplerate(divcount);
152 if (samplerate == 0xffffffffffffffffULL) {
153 close(fd); /* FIXME */
154 return SR_ERR;
155 }
7b48d6e1 156 sr_dbg("la8 in: %s: samplerate is %" PRIu64, __func__, samplerate);
83e9d586
UH
157
158 /* Send header packet to the session bus. */
7b48d6e1 159 sr_dbg("la8 in: %s: sending SR_DF_HEADER packet", __func__);
83e9d586 160 packet.type = SR_DF_HEADER;
83e9d586
UH
161 packet.payload = &header;
162 header.feed_version = 1;
163 gettimeofday(&header.starttime, NULL);
5c3c1241 164 sr_session_send(in->sdi, &packet);
f366e86c
BV
165
166 /* Send metadata about the SR_DF_LOGIC packets to come. */
167 packet.type = SR_DF_META_LOGIC;
168 packet.payload = &meta;
169 meta.samplerate = samplerate;
170 meta.num_probes = num_probes;
5c3c1241 171 sr_session_send(in->sdi, &packet);
83e9d586
UH
172
173 /* TODO: Handle trigger point. */
174
175 /* Send data packets to the session bus. */
7b48d6e1 176 sr_dbg("la8 in: %s: sending SR_DF_LOGIC data packets", __func__);
83e9d586 177 packet.type = SR_DF_LOGIC;
9c939c51
BV
178 packet.payload = &logic;
179 logic.unitsize = (num_probes + 7) / 8;
180 logic.data = buf;
83e9d586
UH
181
182 /* Send 8MB of total data to the session bus in small chunks. */
183 for (i = 0; i < NUM_PACKETS; i++) {
184 /* TODO: Handle errors, handle incomplete reads. */
185 size = read(fd, buf, PACKET_SIZE);
2285cf9b 186 logic.length = size;
5c3c1241 187 sr_session_send(in->sdi, &packet);
83e9d586
UH
188 }
189 close(fd); /* FIXME */
190
191 /* Send end packet to the session bus. */
7b48d6e1 192 sr_dbg("la8 in: %s: sending SR_DF_END", __func__);
83e9d586 193 packet.type = SR_DF_END;
83e9d586 194 packet.payload = NULL;
5c3c1241 195 sr_session_send(in->sdi, &packet);
83e9d586
UH
196
197 return SR_OK;
198}
199
7c1d391c 200SR_PRIV struct sr_input_format input_chronovu_la8 = {
83e9d586
UH
201 .id = "chronovu-la8",
202 .description = "ChronoVu LA8",
203 .format_match = format_match,
204 .init = init,
205 .loadfile = loadfile,
206};