]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 danselmi <da@da> | |
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 ipdbg_org_la_drvopts[] = { | |
24 | SR_CONF_LOGIC_ANALYZER, | |
25 | }; | |
26 | ||
27 | static const uint32_t ipdbg_org_la_scanopts[] = { | |
28 | SR_CONF_CONN, | |
29 | }; | |
30 | ||
31 | static const uint32_t ipdbg_org_la_devopts[] = { | |
32 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST | SR_CONF_SET, | |
33 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET, | |
35 | }; | |
36 | ||
37 | static const int32_t ipdbg_org_la_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 | SR_PRIV struct sr_dev_driver ipdbg_la_driver_info; | |
46 | ||
47 | static void ipdbg_org_la_split_addr_port(const char *conn, char **addr, | |
48 | char **port) | |
49 | { | |
50 | char **strs = g_strsplit(conn, "/", 3); | |
51 | ||
52 | *addr = g_strdup(strs[1]); | |
53 | *port = g_strdup(strs[2]); | |
54 | ||
55 | g_strfreev(strs); | |
56 | } | |
57 | ||
58 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
59 | { | |
60 | struct drv_context *drvc; | |
61 | GSList *devices; | |
62 | ||
63 | devices = NULL; | |
64 | drvc = di->context; | |
65 | drvc->instances = NULL; | |
66 | const char *conn; | |
67 | struct sr_config *src; | |
68 | GSList *l; | |
69 | ||
70 | conn = NULL; | |
71 | for (l = options; l; l = l->next) { | |
72 | src = l->data; | |
73 | switch (src->key) { | |
74 | case SR_CONF_CONN: | |
75 | conn = g_variant_get_string(src->data, NULL); | |
76 | break; | |
77 | } | |
78 | } | |
79 | ||
80 | if (!conn) | |
81 | return NULL; | |
82 | ||
83 | struct ipdbg_org_la_tcp *tcp = ipdbg_org_la_tcp_new(); | |
84 | ||
85 | ipdbg_org_la_split_addr_port(conn, &tcp->address, &tcp->port); | |
86 | ||
87 | if (!tcp->address) | |
88 | return NULL; | |
89 | ||
90 | if (ipdbg_org_la_tcp_open(tcp) != SR_OK) | |
91 | return NULL; | |
92 | ||
93 | ipdbg_org_la_send_reset(tcp); | |
94 | ipdbg_org_la_send_reset(tcp); | |
95 | ||
96 | if (ipdbg_org_la_request_id(tcp) != SR_OK) | |
97 | return NULL; | |
98 | ||
99 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
100 | if (!sdi) { | |
101 | sr_err("no possible to allocate sr_dev_inst"); | |
102 | return NULL; | |
103 | } | |
104 | ||
105 | sdi->status = SR_ST_INACTIVE; | |
106 | sdi->vendor = g_strdup("ipdbg.org"); | |
107 | sdi->model = g_strdup("Logic Analyzer"); | |
108 | sdi->version = g_strdup("v1.0"); | |
109 | sdi->driver = di; | |
110 | ||
111 | struct ipdbg_org_la_dev_context *devc = ipdbg_org_la_dev_new(); | |
112 | sdi->priv = devc; | |
113 | ||
114 | ipdbg_org_la_get_addrwidth_and_datawidth(tcp, devc); | |
115 | ||
116 | sr_dbg("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH, | |
117 | devc->DATA_WIDTH); | |
118 | sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max); | |
119 | ||
120 | for (uint32_t i = 0; i < devc->DATA_WIDTH; i++) { | |
121 | const uint8_t buf_size = 16; | |
122 | char buf[buf_size]; | |
123 | snprintf(buf, buf_size, "ch%d", i); | |
124 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buf); | |
125 | } | |
126 | ||
127 | sdi->inst_type = SR_INST_USER; | |
128 | sdi->conn = tcp; | |
129 | ||
130 | ipdbg_org_la_tcp_close(tcp); | |
131 | ||
132 | devices = g_slist_append(devices, sdi); | |
133 | ||
134 | return std_scan_complete(di, devices); | |
135 | } | |
136 | ||
137 | static int dev_clear(const struct sr_dev_driver *di) | |
138 | { | |
139 | struct drv_context *drvc = di->context; | |
140 | struct sr_dev_inst *sdi; | |
141 | GSList *l; | |
142 | ||
143 | if (drvc) { | |
144 | for (l = drvc->instances; l; l = l->next) { | |
145 | sdi = l->data; | |
146 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
147 | if (tcp) { | |
148 | ipdbg_org_la_tcp_close(tcp); | |
149 | ipdbg_org_la_tcp_free(tcp); | |
150 | g_free(tcp); | |
151 | } | |
152 | sdi->conn = NULL; | |
153 | } | |
154 | } | |
155 | ||
156 | return std_dev_clear(di); | |
157 | } | |
158 | ||
159 | static int dev_open(struct sr_dev_inst *sdi) | |
160 | { | |
161 | sdi->status = SR_ST_INACTIVE; | |
162 | ||
163 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
164 | ||
165 | if (!tcp) | |
166 | return SR_ERR; | |
167 | ||
168 | if (ipdbg_org_la_tcp_open(tcp) != SR_OK) | |
169 | return SR_ERR; | |
170 | ||
171 | sdi->status = SR_ST_ACTIVE; | |
172 | ||
173 | return SR_OK; | |
174 | } | |
175 | ||
176 | static int dev_close(struct sr_dev_inst *sdi) | |
177 | { | |
178 | // Should be called before a new call to scan() | |
179 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
180 | ||
181 | if (tcp) | |
182 | ipdbg_org_la_tcp_close(tcp); | |
183 | ||
184 | sdi->conn = NULL; | |
185 | sdi->status = SR_ST_INACTIVE; | |
186 | ||
187 | return SR_OK; | |
188 | } | |
189 | ||
190 | static int config_get(uint32_t key, GVariant **data, | |
191 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
192 | { | |
193 | int ret = SR_OK; | |
194 | ||
195 | (void)cg; | |
196 | ||
197 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
198 | ||
199 | switch (key) { | |
200 | case SR_CONF_CAPTURE_RATIO: | |
201 | *data = g_variant_new_uint64(devc->capture_ratio); | |
202 | break; | |
203 | case SR_CONF_LIMIT_SAMPLES: | |
204 | *data = g_variant_new_uint64(devc->limit_samples); | |
205 | break; | |
206 | default: | |
207 | ret = SR_ERR_NA; | |
208 | } | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
213 | static int config_set(uint32_t key, GVariant *data, | |
214 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
215 | { | |
216 | int ret = SR_OK; | |
217 | uint64_t value; | |
218 | ||
219 | (void)cg; | |
220 | ||
221 | if (sdi->status != SR_ST_ACTIVE) | |
222 | return SR_ERR_DEV_CLOSED; | |
223 | ||
224 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
225 | ||
226 | switch (key) { | |
227 | case SR_CONF_CAPTURE_RATIO: | |
228 | value = g_variant_get_uint64(data); | |
229 | if (value <= 100) | |
230 | devc->capture_ratio = value; | |
231 | else | |
232 | ret = SR_ERR; | |
233 | break; | |
234 | case SR_CONF_LIMIT_SAMPLES: | |
235 | value = g_variant_get_uint64(data); | |
236 | if (value <= devc->limit_samples_max) | |
237 | devc->limit_samples = value; | |
238 | else | |
239 | ret = SR_ERR; | |
240 | break; | |
241 | default: | |
242 | ret = SR_ERR_NA; | |
243 | } | |
244 | ||
245 | return ret; | |
246 | } | |
247 | ||
248 | static int config_list(uint32_t key, GVariant **data, | |
249 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
250 | { | |
251 | (void)cg; | |
252 | ||
253 | switch (key) { | |
254 | case SR_CONF_SCAN_OPTIONS: | |
255 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
256 | ipdbg_org_la_scanopts, | |
257 | ARRAY_SIZE | |
258 | (ipdbg_org_la_scanopts), | |
259 | sizeof(uint32_t)); | |
260 | break; | |
261 | case SR_CONF_DEVICE_OPTIONS: | |
262 | if (!sdi) | |
263 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
264 | ipdbg_org_la_drvopts, | |
265 | ARRAY_SIZE | |
266 | (ipdbg_org_la_drvopts), | |
267 | sizeof(uint32_t)); | |
268 | else | |
269 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
270 | ipdbg_org_la_devopts, | |
271 | ARRAY_SIZE | |
272 | (ipdbg_org_la_devopts), | |
273 | sizeof(uint32_t)); | |
274 | break; | |
275 | case SR_CONF_TRIGGER_MATCH: | |
276 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
277 | ipdbg_org_la_trigger_matches, | |
278 | ARRAY_SIZE | |
279 | (ipdbg_org_la_trigger_matches), | |
280 | sizeof(int32_t)); | |
281 | break; | |
282 | default: | |
283 | return SR_ERR_NA; | |
284 | } | |
285 | ||
286 | return SR_OK; | |
287 | } | |
288 | ||
289 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
290 | { | |
291 | return std_init(di, sr_ctx); | |
292 | } | |
293 | ||
294 | static GSList *dev_list(const struct sr_dev_driver *di) | |
295 | { | |
296 | return ((struct drv_context *)(di->context))->instances; | |
297 | } | |
298 | ||
299 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
300 | { | |
301 | if (sdi->status != SR_ST_ACTIVE) | |
302 | return SR_ERR_DEV_CLOSED; | |
303 | ||
304 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
305 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
306 | ||
307 | ipdbg_org_la_convert_trigger(sdi); | |
308 | ipdbg_org_la_send_trigger(devc, tcp); | |
309 | ipdbg_org_la_send_delay(devc, tcp); | |
310 | ||
311 | /* If the device stops sending for longer than it takes to send a byte, | |
312 | * that means it's finished. But wait at least 100 ms to be safe. | |
313 | */ | |
314 | sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100, | |
315 | ipdbg_org_la_receive_data, (struct sr_dev_inst *)sdi); | |
316 | ||
317 | ipdbg_org_la_send_start(tcp); | |
318 | ||
319 | return SR_OK; | |
320 | } | |
321 | ||
322 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
323 | { | |
324 | struct ipdbg_org_la_tcp *tcp = sdi->conn; | |
325 | struct ipdbg_org_la_dev_context *devc = sdi->priv; | |
326 | ||
327 | uint8_t byte; | |
328 | ||
329 | if (devc->num_transfers > 0) { | |
330 | while (devc->num_transfers < | |
331 | (devc->limit_samples_max * devc->DATA_WIDTH_BYTES)) { | |
332 | ipdbg_org_la_tcp_receive(tcp, &byte); | |
333 | devc->num_transfers++; | |
334 | } | |
335 | } | |
336 | ||
337 | ipdbg_org_la_send_reset(tcp); | |
338 | ipdbg_org_la_abort_acquisition(sdi); | |
339 | ||
340 | return SR_OK; | |
341 | } | |
342 | ||
343 | SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = { | |
344 | .name = "ipdbg-org-la", | |
345 | .longname = "ipdbg.org logic analyzer", | |
346 | .api_version = 1, | |
347 | .init = init, | |
348 | .cleanup = std_cleanup, | |
349 | .scan = scan, | |
350 | .dev_list = dev_list, | |
351 | .dev_clear = dev_clear, | |
352 | .config_get = config_get, | |
353 | .config_set = config_set, | |
354 | .config_list = config_list, | |
355 | .dev_open = dev_open, | |
356 | .dev_close = dev_close, | |
357 | .dev_acquisition_start = dev_acquisition_start, | |
358 | .dev_acquisition_stop = dev_acquisition_stop, | |
359 | .context = NULL, | |
360 | }; | |
361 | ||
362 | SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info); |