2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 danselmi <da@da>
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.
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.
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/>.
23 static const uint32_t ipdbg_org_la_drvopts[] = {
24 SR_CONF_LOGIC_ANALYZER,
27 static const uint32_t ipdbg_org_la_scanopts[] = {
32 static const uint32_t ipdbg_org_la_devopts[] = {
33 SR_CONF_TRIGGER_MATCH | SR_CONF_LIST | SR_CONF_SET,
34 SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET
38 static const int32_t ipdbg_org_la_trigger_matches[] = {
46 SR_PRIV struct sr_dev_driver ipdbg_la_driver_info;
48 static void ipdbg_org_la_split_addr_port(const char *conn, char **addr,
51 char **strs = g_strsplit(conn, "/", 3);
53 *addr = g_strdup(strs[1]);
54 *port = g_strdup(strs[2]);
59 static GSList *scan(struct sr_dev_driver *di, GSList *options)
61 struct drv_context *drvc;
66 drvc->instances = NULL;
68 struct sr_config *src;
72 for (l = options; l; l = l->next) {
76 conn = g_variant_get_string(src->data, NULL);
84 struct ipdbg_org_la_tcp *tcp = ipdbg_org_la_tcp_new();
86 ipdbg_org_la_split_addr_port(conn, &tcp->address, &tcp->port);
91 if (ipdbg_org_la_tcp_open(tcp) != SR_OK)
94 ipdbg_org_la_send_reset(tcp);
95 ipdbg_org_la_send_reset(tcp);
96 ipdbg_org_la_request_id(tcp);
98 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
100 sr_err("no possible to allocate sr_dev_inst");
104 sdi->status = SR_ST_INACTIVE;
105 sdi->vendor = g_strdup("ipdbg.org");
106 sdi->model = g_strdup("Logic Analyzer");
107 sdi->version = g_strdup("v1.0");
110 struct ipdbg_org_la_dev_context *devc = ipdbg_org_la_dev_new();
113 ipdbg_org_la_get_addrwidth_and_datawidth(tcp, devc);
115 sr_dbg("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH,
117 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
119 for (uint32_t i = 0; i < devc->DATA_WIDTH; i++) {
120 const uint8_t buf_size = 16;
122 snprintf(buf, buf_size, "ch%d", i);
123 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buf);
126 sdi->inst_type = SR_INST_USER;
129 ipdbg_org_la_tcp_close(tcp);
131 devices = g_slist_append(devices, sdi);
133 return std_scan_complete(di, devices);
136 static int dev_clear(const struct sr_dev_driver *di)
138 struct drv_context *drvc = di->context;
139 struct sr_dev_inst *sdi;
143 for (l = drvc->instances; l; l = l->next) {
145 struct ipdbg_org_la_tcp *tcp = sdi->conn;
147 ipdbg_org_la_tcp_close(tcp);
148 ipdbg_org_la_tcp_free(tcp);
155 return std_dev_clear(di);
158 static int dev_open(struct sr_dev_inst *sdi)
160 sdi->status = SR_ST_INACTIVE;
162 struct ipdbg_org_la_tcp *tcp = sdi->conn;
167 if (ipdbg_org_la_tcp_open(tcp) != SR_OK)
170 sdi->status = SR_ST_ACTIVE;
175 static int dev_close(struct sr_dev_inst *sdi)
177 // Should be called before a new call to scan()
178 struct ipdbg_org_la_tcp *tcp = sdi->conn;
181 ipdbg_org_la_tcp_close(tcp);
184 sdi->status = SR_ST_INACTIVE;
189 static int config_get(uint32_t key, GVariant **data,
190 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
196 struct ipdbg_org_la_dev_context *devc = sdi->priv;
199 case SR_CONF_CAPTURE_RATIO:
200 *data = g_variant_new_uint64(devc->capture_ratio);
202 case SR_CONF_LIMIT_SAMPLES:
203 *data = g_variant_new_uint64(devc->limit_samples);
212 static int config_set(uint32_t key, GVariant *data,
213 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
220 if (sdi->status != SR_ST_ACTIVE)
221 return SR_ERR_DEV_CLOSED;
223 struct ipdbg_org_la_dev_context *devc = sdi->priv;
226 case SR_CONF_CAPTURE_RATIO:
227 value = g_variant_get_uint64(data);
229 devc->capture_ratio = value;
233 case SR_CONF_LIMIT_SAMPLES:
234 value = g_variant_get_uint64(data);
235 if (value <= devc->limit_samples_max)
236 devc->limit_samples = value;
247 static int config_list(uint32_t key, GVariant **data,
248 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
253 case SR_CONF_SCAN_OPTIONS:
254 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
255 ipdbg_org_la_scanopts,
257 (ipdbg_org_la_scanopts),
260 case SR_CONF_DEVICE_OPTIONS:
262 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
263 ipdbg_org_la_drvopts,
265 (ipdbg_org_la_drvopts),
268 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
269 ipdbg_org_la_devopts,
271 (ipdbg_org_la_devopts),
274 case SR_CONF_TRIGGER_MATCH:
275 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
276 ipdbg_org_la_trigger_matches,
278 (ipdbg_org_la_trigger_matches),
288 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
290 return std_init(di, sr_ctx);
293 static GSList *dev_list(const struct sr_dev_driver *di)
295 return ((struct drv_context *)(di->context))->instances;
298 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
300 if (sdi->status != SR_ST_ACTIVE)
301 return SR_ERR_DEV_CLOSED;
303 struct ipdbg_org_la_tcp *tcp = sdi->conn;
304 struct ipdbg_org_la_dev_context *devc = sdi->priv;
306 ipdbg_org_la_convert_trigger(sdi);
307 ipdbg_org_la_send_trigger(devc, tcp);
308 ipdbg_org_la_send_delay(devc, tcp);
310 /* If the device stops sending for longer than it takes to send a byte,
311 * that means it's finished. But wait at least 100 ms to be safe.
313 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
314 ipdbg_org_la_receive_data, (struct sr_dev_inst *)sdi);
316 ipdbg_org_la_send_start(tcp);
321 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
323 struct ipdbg_org_la_tcp *tcp = sdi->conn;
324 struct ipdbg_org_la_dev_context *devc = sdi->priv;
328 if (devc->num_transfers > 0) {
329 while (devc->num_transfers <
330 (devc->limit_samples_max * devc->DATA_WIDTH_BYTES)) {
331 ipdbg_org_la_tcp_receive(tcp, &byte);
332 devc->num_transfers++;
336 ipdbg_org_la_send_reset(tcp);
337 ipdbg_org_la_abort_acquisition(sdi);
342 SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = {
343 .name = "ipdbg-org-la",
344 .longname = "ipdbg.org logic analyzer",
347 .cleanup = std_cleanup,
349 .dev_list = dev_list,
350 .dev_clear = dev_clear,
351 .config_get = config_get,
352 .config_set = config_set,
353 .config_list = config_list,
354 .dev_open = dev_open,
355 .dev_close = dev_close,
356 .dev_acquisition_start = dev_acquisition_start,
357 .dev_acquisition_stop = dev_acquisition_stop,
361 SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);