]>
Commit | Line | Data |
---|---|---|
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> | |
cd315a80 UH |
24 | #include "sigrok.h" |
25 | #include "sigrok-internal.h" | |
83e9d586 UH |
26 | |
27 | #define NUM_PACKETS 2048 | |
28 | #define PACKET_SIZE 4096 | |
29 | #define DEFAULT_NUM_PROBES 8 | |
30 | ||
31 | /** | |
32 | * Convert the LA8 'divcount' value to the respective samplerate (in Hz). | |
33 | * | |
34 | * LA8 hardware: sample period = (divcount + 1) * 10ns. | |
35 | * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate). | |
36 | * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate). | |
37 | * | |
38 | * @param divcount The divcount value as needed by the hardware. | |
44dae539 | 39 | * |
83e9d586 UH |
40 | * @return The samplerate in Hz, or 0xffffffffffffffff upon errors. |
41 | */ | |
42 | static uint64_t divcount_to_samplerate(uint8_t divcount) | |
43 | { | |
44 | if (divcount == 0xff) | |
45 | return 0xffffffffffffffffULL; | |
46 | ||
47 | return SR_MHZ(100) / (divcount + 1); | |
48 | } | |
49 | ||
50 | static int format_match(const char *filename) | |
51 | { | |
3217b032 UH |
52 | struct stat stat_buf; |
53 | int ret; | |
54 | ||
83e9d586 | 55 | if (!filename) { |
7b48d6e1 | 56 | sr_err("la8 in: %s: filename was NULL", __func__); |
83e9d586 UH |
57 | // return SR_ERR; /* FIXME */ |
58 | return FALSE; | |
59 | } | |
60 | ||
61 | if (!g_file_test(filename, G_FILE_TEST_EXISTS)) { | |
7b48d6e1 | 62 | sr_err("la8 in: %s: input file '%s' does not exist", |
133a37bf | 63 | __func__, filename); |
83e9d586 UH |
64 | // return SR_ERR; /* FIXME */ |
65 | return FALSE; | |
66 | } | |
67 | ||
68 | if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) { | |
7b48d6e1 | 69 | sr_err("la8 in: %s: input file '%s' not a regular file", |
133a37bf | 70 | __func__, filename); |
83e9d586 UH |
71 | // return SR_ERR; /* FIXME */ |
72 | return FALSE; | |
73 | } | |
74 | ||
3217b032 UH |
75 | /* Only accept files of length 8MB + 5 bytes. */ |
76 | ret = stat(filename, &stat_buf); | |
77 | if (ret != 0) { | |
78 | sr_err("la8 in: %s: Error getting file size of '%s'", | |
79 | __func__, filename); | |
80 | return FALSE; | |
81 | } | |
82 | if (stat_buf.st_size != (8 * 1024 * 1024 + 5)) { | |
83 | sr_dbg("la8 in: %s: File size must be exactly 8388613 bytes (" | |
84 | "it actually is %d bytes in size), so this is not a " | |
85 | "ChronoVu LA8 file.", __func__, stat_buf.st_size); | |
86 | return FALSE; | |
87 | } | |
83e9d586 UH |
88 | |
89 | /* TODO: Check for divcount != 0xff. */ | |
90 | ||
91 | return TRUE; | |
92 | } | |
93 | ||
94 | static int init(struct sr_input *in) | |
95 | { | |
464d12c7 | 96 | int num_probes, i; |
c37d2b1b | 97 | char name[SR_MAX_PROBENAME_LEN + 1]; |
83e9d586 UH |
98 | |
99 | if (in->param && in->param[0]) { | |
100 | num_probes = strtoul(in->param, NULL, 10); | |
101 | if (num_probes < 1) { | |
7b48d6e1 | 102 | sr_err("la8 in: %s: strtoul failed", __func__); |
83e9d586 UH |
103 | return SR_ERR; |
104 | } | |
105 | } else { | |
106 | num_probes = DEFAULT_NUM_PROBES; | |
107 | } | |
108 | ||
109 | /* Create a virtual device. */ | |
bb7ef793 | 110 | in->vdev = sr_dev_new(NULL, 0); |
464d12c7 KS |
111 | |
112 | for (i = 0; i < num_probes; i++) { | |
113 | snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i); | |
c37d2b1b | 114 | /* TODO: Check return value. */ |
bb7ef793 | 115 | sr_dev_probe_add(in->vdev, name); |
464d12c7 | 116 | } |
83e9d586 UH |
117 | |
118 | return SR_OK; | |
119 | } | |
120 | ||
121 | static int loadfile(struct sr_input *in, const char *filename) | |
122 | { | |
123 | struct sr_datafeed_header header; | |
124 | struct sr_datafeed_packet packet; | |
9c939c51 | 125 | struct sr_datafeed_logic logic; |
83e9d586 UH |
126 | uint8_t buf[PACKET_SIZE], divcount; |
127 | int i, fd, size, num_probes; | |
128 | uint64_t samplerate; | |
129 | ||
130 | /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */ | |
131 | if ((fd = open(filename, O_RDONLY)) == -1) { | |
7b48d6e1 | 132 | sr_err("la8 in: %s: file open failed", __func__); |
83e9d586 UH |
133 | return SR_ERR; |
134 | } | |
135 | ||
bb7ef793 | 136 | num_probes = g_slist_length(in->vdev->probes); |
83e9d586 UH |
137 | |
138 | /* Seek to the end of the file, and read the divcount byte. */ | |
139 | divcount = 0x00; /* TODO: Don't hardcode! */ | |
140 | ||
141 | /* Convert the divcount value to a samplerate. */ | |
142 | samplerate = divcount_to_samplerate(divcount); | |
143 | if (samplerate == 0xffffffffffffffffULL) { | |
144 | close(fd); /* FIXME */ | |
145 | return SR_ERR; | |
146 | } | |
7b48d6e1 | 147 | sr_dbg("la8 in: %s: samplerate is %" PRIu64, __func__, samplerate); |
83e9d586 UH |
148 | |
149 | /* Send header packet to the session bus. */ | |
7b48d6e1 | 150 | sr_dbg("la8 in: %s: sending SR_DF_HEADER packet", __func__); |
83e9d586 | 151 | packet.type = SR_DF_HEADER; |
83e9d586 UH |
152 | packet.payload = &header; |
153 | header.feed_version = 1; | |
154 | gettimeofday(&header.starttime, NULL); | |
155 | header.num_logic_probes = num_probes; | |
83e9d586 | 156 | header.samplerate = samplerate; |
31ccebc4 | 157 | sr_session_send(in->vdev, &packet); |
83e9d586 UH |
158 | |
159 | /* TODO: Handle trigger point. */ | |
160 | ||
161 | /* Send data packets to the session bus. */ | |
7b48d6e1 | 162 | sr_dbg("la8 in: %s: sending SR_DF_LOGIC data packets", __func__); |
83e9d586 | 163 | packet.type = SR_DF_LOGIC; |
9c939c51 BV |
164 | packet.payload = &logic; |
165 | logic.unitsize = (num_probes + 7) / 8; | |
166 | logic.data = buf; | |
83e9d586 UH |
167 | |
168 | /* Send 8MB of total data to the session bus in small chunks. */ | |
169 | for (i = 0; i < NUM_PACKETS; i++) { | |
170 | /* TODO: Handle errors, handle incomplete reads. */ | |
171 | size = read(fd, buf, PACKET_SIZE); | |
2285cf9b | 172 | logic.length = size; |
31ccebc4 | 173 | sr_session_send(in->vdev, &packet); |
83e9d586 UH |
174 | } |
175 | close(fd); /* FIXME */ | |
176 | ||
177 | /* Send end packet to the session bus. */ | |
7b48d6e1 | 178 | sr_dbg("la8 in: %s: sending SR_DF_END", __func__); |
83e9d586 | 179 | packet.type = SR_DF_END; |
83e9d586 | 180 | packet.payload = NULL; |
31ccebc4 | 181 | sr_session_send(in->vdev, &packet); |
83e9d586 UH |
182 | |
183 | return SR_OK; | |
184 | } | |
185 | ||
7c1d391c | 186 | SR_PRIV struct sr_input_format input_chronovu_la8 = { |
83e9d586 UH |
187 | .id = "chronovu-la8", |
188 | .description = "ChronoVu LA8", | |
189 | .format_match = format_match, | |
190 | .init = init, | |
191 | .loadfile = loadfile, | |
192 | }; |