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;
50 static void ipdbg_org_la_split_addr_port(const char *conn, char **addr, char **port)
52 char **strs = g_strsplit(conn, "/", 3);
54 *addr = g_strdup(strs[1]);
55 *port = g_strdup(strs[2]);
60 static GSList *scan(struct sr_dev_driver *di, GSList *options)
62 struct drv_context *drvc;
69 drvc->instances = NULL;
71 struct sr_config *src;
75 for (l = options; l; l = l->next) {
79 conn = g_variant_get_string(src->data, NULL);
87 struct ipdbg_org_la_tcp *tcp = ipdbg_org_la_new_tcp();
89 ipdbg_org_la_split_addr_port(conn, &tcp->address, &tcp->port);
95 if(ipdbg_org_la_tcp_open(tcp) != SR_OK)
98 ipdbg_org_la_sendReset(tcp);
99 ipdbg_org_la_sendReset(tcp);
101 ipdbg_org_la_requestID(tcp);
103 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
105 sr_err("no possible to allocate sr_dev_inst");
108 sdi->status = SR_ST_INACTIVE;
109 sdi->vendor = g_strdup("ipdbg.org");
110 sdi->model = g_strdup("Logic Analyzer");
111 sdi->version = g_strdup("v1.0");
113 const size_t bufSize = 16;
117 struct ipdbg_org_la_dev_context *devc = ipdbg_org_la_dev_new();
120 ipdbg_org_la_get_addrwidth_and_datawidth(tcp, devc);
122 sr_err("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH, devc->DATA_WIDTH);
123 sr_err("limit samples = %d\n", devc->limit_samples_max);
125 for (unsigned int i = 0; i < devc->DATA_WIDTH; i++)
127 snprintf(buff, bufSize, "ch%d", i);
128 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buff);
131 sdi->inst_type = SR_INST_USER;
134 ipdbg_org_la_tcp_close(tcp);
136 //drvc->instances = g_slist_append(drvc->instances, sdi);
137 devices = g_slist_append(devices, sdi);
139 return std_scan_complete(di, devices);
142 static int dev_clear(const struct sr_dev_driver *di)
144 struct drv_context *drvc = di->context;
145 struct sr_dev_inst *sdi;
149 for (l = drvc->instances; l; l = l->next) {
151 struct ipdbg_org_la_tcp *tcp = sdi->conn;
154 ipdbg_org_la_tcp_close(tcp);
155 ipdbg_org_la_tcp_free(tcp);
162 return std_dev_clear(di);
165 static int dev_open(struct sr_dev_inst *sdi)
169 sdi->status = SR_ST_INACTIVE;
171 struct ipdbg_org_la_tcp *tcp = sdi->conn;
178 if(ipdbg_org_la_tcp_open(tcp) != SR_OK)
181 sdi->status = SR_ST_ACTIVE;
186 static int dev_close(struct sr_dev_inst *sdi)
190 // should be called before a new call to scan()
191 struct ipdbg_org_la_tcp *tcp = sdi->conn;
193 ipdbg_org_la_tcp_close(tcp);
197 sdi->status = SR_ST_INACTIVE;
202 static int config_get(uint32_t key, GVariant **data,
203 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
211 struct ipdbg_org_la_dev_context *devc = sdi->priv;
215 case SR_CONF_CAPTURE_RATIO:
216 *data = g_variant_new_uint64(devc->capture_ratio);
218 case SR_CONF_LIMIT_SAMPLES:
219 *data = g_variant_new_uint64(devc->limit_samples);
228 static int config_set(uint32_t key, GVariant *data,
229 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
236 if (sdi->status != SR_ST_ACTIVE)
237 return SR_ERR_DEV_CLOSED;
239 struct ipdbg_org_la_dev_context *devc = sdi->priv;
243 case SR_CONF_CAPTURE_RATIO:
244 devc->capture_ratio = g_variant_get_uint64(data);
245 if (devc->capture_ratio < 0 || devc->capture_ratio > 100)
247 devc->capture_ratio = 50;
253 case SR_CONF_LIMIT_SAMPLES:
254 devc->limit_samples = g_variant_get_uint64(data);
255 if(devc->limit_samples > devc->limit_samples_max)
257 devc->limit_samples = devc->limit_samples_max;
270 static int config_list(uint32_t key, GVariant **data,
271 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
276 case SR_CONF_SCAN_OPTIONS:
277 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, ipdbg_org_la_scanopts, ARRAY_SIZE(ipdbg_org_la_scanopts), sizeof(uint32_t));
279 case SR_CONF_DEVICE_OPTIONS:
281 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, ipdbg_org_la_drvopts, ARRAY_SIZE(ipdbg_org_la_drvopts), sizeof(uint32_t));
283 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, ipdbg_org_la_devopts, ARRAY_SIZE(ipdbg_org_la_devopts), sizeof(uint32_t));
285 case SR_CONF_TRIGGER_MATCH:
286 *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));
296 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
298 return std_init(di, sr_ctx);
301 static GSList *dev_list(const struct sr_dev_driver *di)
303 return ((struct drv_context *) (di->context))->instances;
306 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
308 if (sdi->status != SR_ST_ACTIVE)
309 return SR_ERR_DEV_CLOSED;
310 struct ipdbg_org_la_tcp *tcp = sdi->conn;
312 struct ipdbg_org_la_dev_context *devc = sdi->priv;
314 ipdbg_org_la_convert_trigger(sdi);
316 /* Send Triggerkonviguration */
317 ipdbg_org_la_sendTrigger(devc, tcp);
320 ipdbg_org_la_sendDelay(devc, tcp);;
322 /* If the device stops sending for longer than it takes to send a byte,
323 * that means it's finished. But wait at least 100 ms to be safe.
325 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100, ipdbg_org_la_receive_data, (struct sr_dev_inst *)sdi);
327 ipdbg_org_la_sendStart(tcp);
332 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
334 struct ipdbg_org_la_dev_context *devc = sdi->priv;
335 struct ipdbg_org_la_tcp *tcp = sdi->conn;
339 if(devc->num_transfers > 0)
341 while (devc->num_transfers < devc->limit_samples_max*devc->DATA_WIDTH_BYTES)
343 ipdbg_org_la_tcp_receive(tcp, &byte);
344 devc->num_transfers++;
348 ipdbg_org_la_sendReset(tcp);
349 ipdbg_org_la_abort_acquisition(sdi);
356 SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = {
357 .name = "ipdbg-org-la",
358 .longname = "ipdbg.org logic analyzer",
361 .cleanup = std_cleanup,
363 .dev_list = dev_list,
364 .dev_clear = dev_clear,
365 .config_get = config_get,
366 .config_set = config_set,
367 .config_list = config_list,
368 .dev_open = dev_open,
369 .dev_close = dev_close,
370 .dev_acquisition_start = dev_acquisition_start,
371 .dev_acquisition_stop = dev_acquisition_stop,
375 SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);