]>
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 | SR_PRIV struct sr_dev_driver ipdbg_la_driver_info; | |
46 | ||
47 | static void ipdbg_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_la_tcp *tcp = ipdbg_la_tcp_new(); | |
84 | ||
85 | ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port); | |
86 | ||
87 | if (!tcp->address) | |
88 | return NULL; | |
89 | ||
90 | if (ipdbg_la_tcp_open(tcp) != SR_OK) | |
91 | return NULL; | |
92 | ||
93 | ipdbg_la_send_reset(tcp); | |
94 | ipdbg_la_send_reset(tcp); | |
95 | ||
96 | if (ipdbg_la_request_id(tcp) != SR_OK) | |
97 | return NULL; | |
98 | ||
99 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
100 | sdi->status = SR_ST_INACTIVE; | |
101 | sdi->vendor = g_strdup("ipdbg.org"); | |
102 | sdi->model = g_strdup("IPDBG LA"); | |
103 | sdi->version = g_strdup("v1.0"); | |
104 | sdi->driver = di; | |
105 | ||
106 | struct dev_context *devc = ipdbg_la_dev_new(); | |
107 | sdi->priv = devc; | |
108 | ||
109 | ipdbg_la_get_addrwidth_and_datawidth(tcp, devc); | |
110 | ||
111 | sr_dbg("addr_width = %d, data_width = %d\n", devc->addr_width, | |
112 | devc->data_width); | |
113 | sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max); | |
114 | ||
115 | for (uint32_t i = 0; i < devc->data_width; i++) { | |
116 | char *name = g_strdup_printf("CH%d", i); | |
117 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name); | |
118 | g_free(name); | |
119 | } | |
120 | ||
121 | sdi->inst_type = SR_INST_USER; | |
122 | sdi->conn = tcp; | |
123 | ||
124 | ipdbg_la_tcp_close(tcp); | |
125 | ||
126 | devices = g_slist_append(devices, sdi); | |
127 | ||
128 | return std_scan_complete(di, devices); | |
129 | } | |
130 | ||
131 | static int dev_clear(const struct sr_dev_driver *di) | |
132 | { | |
133 | struct drv_context *drvc = di->context; | |
134 | struct sr_dev_inst *sdi; | |
135 | GSList *l; | |
136 | ||
137 | if (drvc) { | |
138 | for (l = drvc->instances; l; l = l->next) { | |
139 | sdi = l->data; | |
140 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
141 | if (tcp) { | |
142 | ipdbg_la_tcp_close(tcp); | |
143 | ipdbg_la_tcp_free(tcp); | |
144 | g_free(tcp); | |
145 | } | |
146 | sdi->conn = NULL; | |
147 | } | |
148 | } | |
149 | ||
150 | return std_dev_clear(di); | |
151 | } | |
152 | ||
153 | static int dev_open(struct sr_dev_inst *sdi) | |
154 | { | |
155 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
156 | ||
157 | if (!tcp) | |
158 | return SR_ERR; | |
159 | ||
160 | if (ipdbg_la_tcp_open(tcp) != SR_OK) | |
161 | return SR_ERR; | |
162 | ||
163 | return SR_OK; | |
164 | } | |
165 | ||
166 | static int dev_close(struct sr_dev_inst *sdi) | |
167 | { | |
168 | /* Should be called before a new call to scan(). */ | |
169 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
170 | ||
171 | if (tcp) | |
172 | ipdbg_la_tcp_close(tcp); | |
173 | ||
174 | sdi->conn = NULL; | |
175 | ||
176 | return SR_OK; | |
177 | } | |
178 | ||
179 | static int config_get(uint32_t key, GVariant **data, | |
180 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
181 | { | |
182 | struct dev_context *devc = sdi->priv; | |
183 | ||
184 | (void)cg; | |
185 | ||
186 | switch (key) { | |
187 | case SR_CONF_CAPTURE_RATIO: | |
188 | *data = g_variant_new_uint64(devc->capture_ratio); | |
189 | break; | |
190 | case SR_CONF_LIMIT_SAMPLES: | |
191 | *data = g_variant_new_uint64(devc->limit_samples); | |
192 | break; | |
193 | default: | |
194 | return SR_ERR_NA; | |
195 | } | |
196 | ||
197 | return SR_OK; | |
198 | } | |
199 | ||
200 | static int config_set(uint32_t key, GVariant *data, | |
201 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
202 | { | |
203 | struct dev_context *devc = sdi->priv; | |
204 | ||
205 | (void)cg; | |
206 | ||
207 | switch (key) { | |
208 | case SR_CONF_CAPTURE_RATIO: | |
209 | devc->capture_ratio = g_variant_get_uint64(data); | |
210 | break; | |
211 | case SR_CONF_LIMIT_SAMPLES: | |
212 | devc->limit_samples = g_variant_get_uint64(data); | |
213 | break; | |
214 | default: | |
215 | return SR_ERR_NA; | |
216 | } | |
217 | ||
218 | return SR_OK; | |
219 | } | |
220 | ||
221 | static int config_list(uint32_t key, GVariant **data, | |
222 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
223 | { | |
224 | switch (key) { | |
225 | case SR_CONF_SCAN_OPTIONS: | |
226 | case SR_CONF_DEVICE_OPTIONS: | |
227 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
228 | case SR_CONF_TRIGGER_MATCH: | |
229 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); | |
230 | break; | |
231 | default: | |
232 | return SR_ERR_NA; | |
233 | } | |
234 | ||
235 | return SR_OK; | |
236 | } | |
237 | ||
238 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
239 | { | |
240 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
241 | struct dev_context *devc = sdi->priv; | |
242 | ||
243 | ipdbg_la_convert_trigger(sdi); | |
244 | ipdbg_la_send_trigger(devc, tcp); | |
245 | ipdbg_la_send_delay(devc, tcp); | |
246 | ||
247 | /* If the device stops sending for longer than it takes to send a byte, | |
248 | * that means it's finished. But wait at least 100 ms to be safe. | |
249 | */ | |
250 | sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100, | |
251 | ipdbg_la_receive_data, (struct sr_dev_inst *)sdi); | |
252 | ||
253 | ipdbg_la_send_start(tcp); | |
254 | ||
255 | return SR_OK; | |
256 | } | |
257 | ||
258 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
259 | { | |
260 | struct ipdbg_la_tcp *tcp = sdi->conn; | |
261 | struct dev_context *devc = sdi->priv; | |
262 | ||
263 | uint8_t byte; | |
264 | ||
265 | if (devc->num_transfers > 0) { | |
266 | while (devc->num_transfers < | |
267 | (devc->limit_samples_max * devc->data_width_bytes)) { | |
268 | ipdbg_la_tcp_receive(tcp, &byte); | |
269 | devc->num_transfers++; | |
270 | } | |
271 | } | |
272 | ||
273 | ipdbg_la_send_reset(tcp); | |
274 | ipdbg_la_abort_acquisition(sdi); | |
275 | ||
276 | return SR_OK; | |
277 | } | |
278 | ||
279 | SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = { | |
280 | .name = "ipdbg-la", | |
281 | .longname = "IPDBG LA", | |
282 | .api_version = 1, | |
283 | .init = std_init, | |
284 | .cleanup = std_cleanup, | |
285 | .scan = scan, | |
286 | .dev_list = std_dev_list, | |
287 | .dev_clear = dev_clear, | |
288 | .config_get = config_get, | |
289 | .config_set = config_set, | |
290 | .config_list = config_list, | |
291 | .dev_open = dev_open, | |
292 | .dev_close = dev_close, | |
293 | .dev_acquisition_start = dev_acquisition_start, | |
294 | .dev_acquisition_stop = dev_acquisition_stop, | |
295 | .context = NULL, | |
296 | }; | |
297 | ||
298 | SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info); |