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