]>
Commit | Line | Data |
---|---|---|
a4210e18 EK |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
932ef10f | 4 | * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch> |
a4210e18 EK |
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 | ||
1f15efc1 | 23 | static const uint32_t drvopts[] = { |
9d2e5483 | 24 | SR_CONF_LOGIC_ANALYZER, |
a4210e18 EK |
25 | }; |
26 | ||
1f15efc1 | 27 | static const uint32_t scanopts[] = { |
9d2e5483 | 28 | SR_CONF_CONN, |
a4210e18 EK |
29 | }; |
30 | ||
1f15efc1 | 31 | static const uint32_t devopts[] = { |
1f9652a8 | 32 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, |
9d2e5483 | 33 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, |
1f9652a8 | 34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
a4210e18 EK |
35 | }; |
36 | ||
1f15efc1 | 37 | static const int32_t trigger_matches[] = { |
9d2e5483 SA |
38 | SR_TRIGGER_ZERO, |
39 | SR_TRIGGER_ONE, | |
40 | SR_TRIGGER_RISING, | |
41 | SR_TRIGGER_FALLING, | |
42 | SR_TRIGGER_EDGE, | |
a4210e18 EK |
43 | }; |
44 | ||
8a9788e2 | 45 | static void ipdbg_la_split_addr_port(const char *conn, char **addr, |
9d2e5483 | 46 | char **port) |
a4210e18 | 47 | { |
9d2e5483 | 48 | char **strs = g_strsplit(conn, "/", 3); |
a4210e18 | 49 | |
9d2e5483 SA |
50 | *addr = g_strdup(strs[1]); |
51 | *port = g_strdup(strs[2]); | |
a4210e18 | 52 | |
9d2e5483 | 53 | g_strfreev(strs); |
a4210e18 EK |
54 | } |
55 | ||
56 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
57 | { | |
9d2e5483 SA |
58 | struct drv_context *drvc; |
59 | GSList *devices; | |
a4210e18 | 60 | |
9d2e5483 SA |
61 | devices = NULL; |
62 | drvc = di->context; | |
63 | drvc->instances = NULL; | |
64 | const char *conn; | |
65 | struct sr_config *src; | |
66 | GSList *l; | |
a4210e18 | 67 | |
9d2e5483 SA |
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 | } | |
a4210e18 | 77 | |
9d2e5483 SA |
78 | if (!conn) |
79 | return NULL; | |
a4210e18 | 80 | |
8a9788e2 | 81 | struct ipdbg_la_tcp *tcp = ipdbg_la_tcp_new(); |
a4210e18 | 82 | |
8a9788e2 | 83 | ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port); |
a4210e18 | 84 | |
9d2e5483 SA |
85 | if (!tcp->address) |
86 | return NULL; | |
a4210e18 | 87 | |
8a9788e2 | 88 | if (ipdbg_la_tcp_open(tcp) != SR_OK) |
9d2e5483 | 89 | return NULL; |
a4210e18 | 90 | |
8a9788e2 UH |
91 | ipdbg_la_send_reset(tcp); |
92 | ipdbg_la_send_reset(tcp); | |
ed186484 | 93 | |
8a9788e2 | 94 | if (ipdbg_la_request_id(tcp) != SR_OK) |
ed186484 | 95 | return NULL; |
a4210e18 | 96 | |
9d2e5483 | 97 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
9d2e5483 SA |
98 | sdi->status = SR_ST_INACTIVE; |
99 | sdi->vendor = g_strdup("ipdbg.org"); | |
8a9788e2 | 100 | sdi->model = g_strdup("IPDBG LA"); |
9d2e5483 SA |
101 | sdi->version = g_strdup("v1.0"); |
102 | sdi->driver = di; | |
a4210e18 | 103 | |
1f15efc1 | 104 | struct dev_context *devc = ipdbg_la_dev_new(); |
9d2e5483 | 105 | sdi->priv = devc; |
a4210e18 | 106 | |
8a9788e2 | 107 | ipdbg_la_get_addrwidth_and_datawidth(tcp, devc); |
a4210e18 | 108 | |
ac3625be UH |
109 | sr_dbg("addr_width = %d, data_width = %d\n", devc->addr_width, |
110 | devc->data_width); | |
9d2e5483 | 111 | sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max); |
7f4c9a04 | 112 | |
ac3625be | 113 | for (uint32_t i = 0; i < devc->data_width; i++) { |
8bc8e909 UH |
114 | char *name = g_strdup_printf("CH%d", i); |
115 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name); | |
116 | g_free(name); | |
9d2e5483 | 117 | } |
a4210e18 | 118 | |
9d2e5483 SA |
119 | sdi->inst_type = SR_INST_USER; |
120 | sdi->conn = tcp; | |
a4210e18 | 121 | |
8a9788e2 | 122 | ipdbg_la_tcp_close(tcp); |
a4210e18 | 123 | |
9d2e5483 | 124 | devices = g_slist_append(devices, sdi); |
750303aa | 125 | |
9d2e5483 | 126 | return std_scan_complete(di, devices); |
a4210e18 EK |
127 | } |
128 | ||
129 | static int dev_clear(const struct sr_dev_driver *di) | |
130 | { | |
9d2e5483 SA |
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; | |
8a9788e2 | 138 | struct ipdbg_la_tcp *tcp = sdi->conn; |
9d2e5483 | 139 | if (tcp) { |
8a9788e2 UH |
140 | ipdbg_la_tcp_close(tcp); |
141 | ipdbg_la_tcp_free(tcp); | |
9d2e5483 SA |
142 | g_free(tcp); |
143 | } | |
144 | sdi->conn = NULL; | |
145 | } | |
146 | } | |
147 | ||
148 | return std_dev_clear(di); | |
a4210e18 EK |
149 | } |
150 | ||
151 | static int dev_open(struct sr_dev_inst *sdi) | |
152 | { | |
8a9788e2 | 153 | struct ipdbg_la_tcp *tcp = sdi->conn; |
a4210e18 | 154 | |
9d2e5483 SA |
155 | if (!tcp) |
156 | return SR_ERR; | |
a4210e18 | 157 | |
8a9788e2 | 158 | if (ipdbg_la_tcp_open(tcp) != SR_OK) |
9d2e5483 | 159 | return SR_ERR; |
a4210e18 | 160 | |
9d2e5483 | 161 | return SR_OK; |
a4210e18 EK |
162 | } |
163 | ||
164 | static int dev_close(struct sr_dev_inst *sdi) | |
165 | { | |
13ac501a | 166 | /* Should be called before a new call to scan(). */ |
8a9788e2 | 167 | struct ipdbg_la_tcp *tcp = sdi->conn; |
38e7493d | 168 | |
9d2e5483 | 169 | if (tcp) |
8a9788e2 | 170 | ipdbg_la_tcp_close(tcp); |
a4210e18 | 171 | |
9d2e5483 | 172 | sdi->conn = NULL; |
a4210e18 | 173 | |
9d2e5483 | 174 | return SR_OK; |
a4210e18 EK |
175 | } |
176 | ||
177 | static int config_get(uint32_t key, GVariant **data, | |
9d2e5483 | 178 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
a4210e18 | 179 | { |
77b6b98d | 180 | struct dev_context *devc = sdi->priv; |
a4210e18 | 181 | |
9d2e5483 | 182 | (void)cg; |
a4210e18 | 183 | |
9d2e5483 SA |
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: | |
77b6b98d | 192 | return SR_ERR_NA; |
9d2e5483 | 193 | } |
a4210e18 | 194 | |
77b6b98d | 195 | return SR_OK; |
a4210e18 EK |
196 | } |
197 | ||
198 | static int config_set(uint32_t key, GVariant *data, | |
9d2e5483 | 199 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
a4210e18 | 200 | { |
77b6b98d | 201 | struct dev_context *devc = sdi->priv; |
9d2e5483 SA |
202 | |
203 | (void)cg; | |
204 | ||
9d2e5483 SA |
205 | switch (key) { |
206 | case SR_CONF_CAPTURE_RATIO: | |
77b6b98d | 207 | devc->capture_ratio = g_variant_get_uint64(data); |
9d2e5483 SA |
208 | break; |
209 | case SR_CONF_LIMIT_SAMPLES: | |
f3549a1c DA |
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 | } | |
9d2e5483 SA |
215 | break; |
216 | default: | |
77b6b98d | 217 | return SR_ERR_NA; |
9d2e5483 SA |
218 | } |
219 | ||
77b6b98d | 220 | return SR_OK; |
a4210e18 EK |
221 | } |
222 | ||
223 | static int config_list(uint32_t key, GVariant **data, | |
9d2e5483 | 224 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
a4210e18 | 225 | { |
9d2e5483 SA |
226 | switch (key) { |
227 | case SR_CONF_SCAN_OPTIONS: | |
9d2e5483 | 228 | case SR_CONF_DEVICE_OPTIONS: |
77b6b98d | 229 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
9d2e5483 | 230 | case SR_CONF_TRIGGER_MATCH: |
77b6b98d | 231 | *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); |
9d2e5483 SA |
232 | break; |
233 | default: | |
234 | return SR_ERR_NA; | |
235 | } | |
236 | ||
237 | return SR_OK; | |
a4210e18 EK |
238 | } |
239 | ||
a4210e18 EK |
240 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
241 | { | |
8a9788e2 | 242 | struct ipdbg_la_tcp *tcp = sdi->conn; |
1f15efc1 | 243 | struct dev_context *devc = sdi->priv; |
a4210e18 | 244 | |
8a9788e2 UH |
245 | ipdbg_la_convert_trigger(sdi); |
246 | ipdbg_la_send_trigger(devc, tcp); | |
247 | ipdbg_la_send_delay(devc, tcp); | |
a4210e18 | 248 | |
9d2e5483 SA |
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, | |
8a9788e2 | 253 | ipdbg_la_receive_data, (struct sr_dev_inst *)sdi); |
a4210e18 | 254 | |
8a9788e2 | 255 | ipdbg_la_send_start(tcp); |
a4210e18 | 256 | |
9d2e5483 | 257 | return SR_OK; |
a4210e18 EK |
258 | } |
259 | ||
260 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
261 | { | |
8a9788e2 | 262 | struct ipdbg_la_tcp *tcp = sdi->conn; |
1f15efc1 | 263 | struct dev_context *devc = sdi->priv; |
a4210e18 | 264 | |
8e249032 DA |
265 | const size_t bufsize = 1024; |
266 | uint8_t buffer[bufsize]; | |
b51288e3 | 267 | |
9d2e5483 SA |
268 | if (devc->num_transfers > 0) { |
269 | while (devc->num_transfers < | |
ac3625be | 270 | (devc->limit_samples_max * devc->data_width_bytes)) { |
8e249032 DA |
271 | int recd = ipdbg_la_tcp_receive(tcp, buffer, bufsize); |
272 | if (recd > 0) | |
273 | devc->num_transfers += recd; | |
9d2e5483 SA |
274 | } |
275 | } | |
a4210e18 | 276 | |
8a9788e2 UH |
277 | ipdbg_la_send_reset(tcp); |
278 | ipdbg_la_abort_acquisition(sdi); | |
a4210e18 | 279 | |
9d2e5483 | 280 | return SR_OK; |
a4210e18 EK |
281 | } |
282 | ||
3f34a402 | 283 | static struct sr_dev_driver ipdbg_la_driver_info = { |
8a9788e2 UH |
284 | .name = "ipdbg-la", |
285 | .longname = "IPDBG LA", | |
9d2e5483 | 286 | .api_version = 1, |
3831eaf9 | 287 | .init = std_init, |
9d2e5483 SA |
288 | .cleanup = std_cleanup, |
289 | .scan = scan, | |
3831eaf9 | 290 | .dev_list = std_dev_list, |
9d2e5483 SA |
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, | |
a4210e18 | 300 | }; |
a4210e18 | 301 | SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info); |