]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | static const uint32_t drvopts[] = { | |
24 | SR_CONF_LOGIC_ANALYZER, | |
25 | }; | |
26 | ||
27 | static const uint32_t scanopts[] = { | |
28 | SR_CONF_CONN, | |
29 | }; | |
30 | ||
31 | static const uint32_t devopts[] = { | |
32 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
33 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
35 | }; | |
36 | ||
37 | static const int32_t trigger_matches[] = { | |
38 | SR_TRIGGER_ZERO, | |
39 | SR_TRIGGER_ONE, | |
40 | SR_TRIGGER_RISING, | |
41 | SR_TRIGGER_FALLING, | |
42 | SR_TRIGGER_EDGE, | |
43 | }; | |
44 | ||
45 | static void ipdbg_la_split_addr_port(const char *conn, char **addr, | |
46 | char **port) | |
47 | { | |
48 | char **strs = g_strsplit(conn, "/", 3); | |
49 | ||
50 | *addr = g_strdup(strs[1]); | |
51 | *port = g_strdup(strs[2]); | |
52 | ||
53 | g_strfreev(strs); | |
54 | } | |
55 | ||
56 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
57 | { | |
58 | struct drv_context *drvc; | |
59 | GSList *devices; | |
60 | ||
61 | devices = NULL; | |
62 | drvc = di->context; | |
63 | drvc->instances = NULL; | |
64 | const char *conn; | |
65 | struct sr_config *src; | |
66 | GSList *l; | |
67 | ||
68 | conn = NULL; | |
69 | for (l = options; l; l = l->next) { | |
70 | src = l->data; | |
71 | switch (src->key) { | |
72 | case SR_CONF_CONN: | |
73 | conn = g_variant_get_string(src->data, NULL); | |
74 | break; | |
75 | } | |
76 | } | |
77 | ||
78 | if (!conn) | |
79 | return NULL; | |
80 | ||
81 | struct ipdbg_la_tcp *tcp = ipdbg_la_tcp_new(); | |
82 | ||
83 | ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port); | |
84 | ||
85 | if (!tcp->address) | |
86 | return NULL; | |
87 | ||
88 | if (ipdbg_la_tcp_open(tcp) != SR_OK) | |
89 | return NULL; | |
90 | ||
91 | ipdbg_la_send_reset(tcp); | |
92 | ipdbg_la_send_reset(tcp); | |
93 | ||
94 | if (ipdbg_la_request_id(tcp) != SR_OK) | |
95 | return NULL; | |
96 | ||
97 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
98 | sdi->status = SR_ST_INACTIVE; | |
99 | sdi->vendor = g_strdup("ipdbg.org"); | |
100 | sdi->model = g_strdup("IPDBG LA"); | |
101 | sdi->version = g_strdup("v1.0"); | |
102 | sdi->driver = di; | |
103 | ||
104 | struct dev_context *devc = ipdbg_la_dev_new(); | |
105 | sdi->priv = devc; | |
106 | ||
107 | ipdbg_la_get_addrwidth_and_datawidth(tcp, devc); | |
108 | ||
109 | sr_dbg("addr_width = %d, data_width = %d\n", devc->addr_width, | |
110 | devc->data_width); | |
111 | sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max); | |
112 | ||
113 | for (uint32_t i = 0; i < devc->data_width; i++) { | |
114 | char *name = g_strdup_printf("CH%d", i); | |
115 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name); | |
116 | g_free(name); | |
117 | } | |
118 | ||
119 | sdi->inst_type = SR_INST_USER; | |
120 | sdi->conn = tcp; | |
121 | ||
122 | ipdbg_la_tcp_close(tcp); | |
123 | ||
124 | devices = g_slist_append(devices, sdi); | |
125 | ||
126 | return std_scan_complete(di, devices); | |
127 | } | |
128 | ||
129 | static int dev_clear(const struct sr_dev_driver *di) | |
130 | { | |
131 | struct drv_context *drvc = di->context; | |
132 | struct sr_dev_inst *sdi; | |
133 | GSList *l; | |
134 | ||
135 | if (drvc) { | |
136 | for (l = drvc->instances; l; l = l->next) { | |
137 | sdi = l->data; | |
138 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
139 | if (tcp) { | |
140 | ipdbg_la_tcp_close(tcp); | |
141 | ipdbg_la_tcp_free(tcp); | |
142 | g_free(tcp); | |
143 | } | |
144 | sdi->conn = NULL; | |
145 | } | |
146 | } | |
147 | ||
148 | return std_dev_clear(di); | |
149 | } | |
150 | ||
151 | static int dev_open(struct sr_dev_inst *sdi) | |
152 | { | |
153 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
154 | ||
155 | if (!tcp) | |
156 | return SR_ERR; | |
157 | ||
158 | if (ipdbg_la_tcp_open(tcp) != SR_OK) | |
159 | return SR_ERR; | |
160 | ||
161 | return SR_OK; | |
162 | } | |
163 | ||
164 | static int dev_close(struct sr_dev_inst *sdi) | |
165 | { | |
166 | /* Should be called before a new call to scan(). */ | |
167 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
168 | ||
169 | if (tcp) | |
170 | ipdbg_la_tcp_close(tcp); | |
171 | ||
172 | sdi->conn = NULL; | |
173 | ||
174 | return SR_OK; | |
175 | } | |
176 | ||
177 | static int config_get(uint32_t key, GVariant **data, | |
178 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
179 | { | |
180 | struct dev_context *devc = sdi->priv; | |
181 | ||
182 | (void)cg; | |
183 | ||
184 | switch (key) { | |
185 | case SR_CONF_CAPTURE_RATIO: | |
186 | *data = g_variant_new_uint64(devc->capture_ratio); | |
187 | break; | |
188 | case SR_CONF_LIMIT_SAMPLES: | |
189 | *data = g_variant_new_uint64(devc->limit_samples); | |
190 | break; | |
191 | default: | |
192 | return SR_ERR_NA; | |
193 | } | |
194 | ||
195 | return SR_OK; | |
196 | } | |
197 | ||
198 | static int config_set(uint32_t key, GVariant *data, | |
199 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
200 | { | |
201 | struct dev_context *devc = sdi->priv; | |
202 | ||
203 | (void)cg; | |
204 | ||
205 | switch (key) { | |
206 | case SR_CONF_CAPTURE_RATIO: | |
207 | devc->capture_ratio = g_variant_get_uint64(data); | |
208 | break; | |
209 | case SR_CONF_LIMIT_SAMPLES: | |
210 | { | |
211 | uint64_t limit_samples = g_variant_get_uint64(data); | |
212 | if (limit_samples <= devc->limit_samples_max) | |
213 | devc->limit_samples = limit_samples; | |
214 | } | |
215 | break; | |
216 | default: | |
217 | return SR_ERR_NA; | |
218 | } | |
219 | ||
220 | return SR_OK; | |
221 | } | |
222 | ||
223 | static int config_list(uint32_t key, GVariant **data, | |
224 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
225 | { | |
226 | switch (key) { | |
227 | case SR_CONF_SCAN_OPTIONS: | |
228 | case SR_CONF_DEVICE_OPTIONS: | |
229 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
230 | case SR_CONF_TRIGGER_MATCH: | |
231 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
232 | break; | |
233 | default: | |
234 | return SR_ERR_NA; | |
235 | } | |
236 | ||
237 | return SR_OK; | |
238 | } | |
239 | ||
240 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
241 | { | |
242 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
243 | struct dev_context *devc = sdi->priv; | |
244 | ||
245 | ipdbg_la_convert_trigger(sdi); | |
246 | ipdbg_la_send_trigger(devc, tcp); | |
247 | ipdbg_la_send_delay(devc, tcp); | |
248 | ||
249 | /* If the device stops sending for longer than it takes to send a byte, | |
250 | * that means it's finished. But wait at least 100 ms to be safe. | |
251 | */ | |
252 | sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100, | |
253 | ipdbg_la_receive_data, (struct sr_dev_inst *)sdi); | |
254 | ||
255 | ipdbg_la_send_start(tcp); | |
256 | ||
257 | return SR_OK; | |
258 | } | |
259 | ||
260 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
261 | { | |
262 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
263 | struct dev_context *devc = sdi->priv; | |
264 | ||
265 | const size_t bufsize = 1024; | |
266 | uint8_t buffer[bufsize]; | |
267 | ||
268 | if (devc->num_transfers > 0) { | |
269 | while (devc->num_transfers < | |
270 | (devc->limit_samples_max * devc->data_width_bytes)) { | |
271 | int recd = ipdbg_la_tcp_receive(tcp, buffer, bufsize); | |
272 | if (recd > 0) | |
273 | devc->num_transfers += recd; | |
274 | } | |
275 | } | |
276 | ||
277 | ipdbg_la_send_reset(tcp); | |
278 | ipdbg_la_abort_acquisition(sdi); | |
279 | ||
280 | return SR_OK; | |
281 | } | |
282 | ||
283 | static struct sr_dev_driver ipdbg_la_driver_info = { | |
284 | .name = "ipdbg-la", | |
285 | .longname = "IPDBG LA", | |
286 | .api_version = 1, | |
287 | .init = std_init, | |
288 | .cleanup = std_cleanup, | |
289 | .scan = scan, | |
290 | .dev_list = std_dev_list, | |
291 | .dev_clear = dev_clear, | |
292 | .config_get = config_get, | |
293 | .config_set = config_set, | |
294 | .config_list = config_list, | |
295 | .dev_open = dev_open, | |
296 | .dev_close = dev_close, | |
297 | .dev_acquisition_start = dev_acquisition_start, | |
298 | .dev_acquisition_stop = dev_acquisition_stop, | |
299 | .context = NULL, | |
300 | }; | |
301 | SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info); |