2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch>
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 drvopts[] = {
24 SR_CONF_LOGIC_ANALYZER,
27 static const uint32_t scanopts[] = {
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,
37 static const int32_t trigger_matches[] = {
45 SR_PRIV struct sr_dev_driver ipdbg_la_driver_info;
47 static void ipdbg_la_split_addr_port(const char *conn, char **addr,
50 char **strs = g_strsplit(conn, "/", 3);
52 *addr = g_strdup(strs[1]);
53 *port = g_strdup(strs[2]);
58 static GSList *scan(struct sr_dev_driver *di, GSList *options)
60 struct drv_context *drvc;
65 drvc->instances = NULL;
67 struct sr_config *src;
71 for (l = options; l; l = l->next) {
75 conn = g_variant_get_string(src->data, NULL);
83 struct ipdbg_la_tcp *tcp = ipdbg_la_tcp_new();
85 ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port);
90 if (ipdbg_la_tcp_open(tcp) != SR_OK)
93 ipdbg_la_send_reset(tcp);
94 ipdbg_la_send_reset(tcp);
96 if (ipdbg_la_request_id(tcp) != SR_OK)
99 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
101 sr_err("no possible to allocate sr_dev_inst");
105 sdi->status = SR_ST_INACTIVE;
106 sdi->vendor = g_strdup("ipdbg.org");
107 sdi->model = g_strdup("IPDBG LA");
108 sdi->version = g_strdup("v1.0");
111 struct dev_context *devc = ipdbg_la_dev_new();
114 ipdbg_la_get_addrwidth_and_datawidth(tcp, devc);
116 sr_dbg("addr_width = %d, data_width = %d\n", devc->ADDR_WIDTH,
118 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
120 for (uint32_t i = 0; i < devc->DATA_WIDTH; i++) {
121 const uint8_t buf_size = 16;
123 snprintf(buf, buf_size, "ch%d", i);
124 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, buf);
127 sdi->inst_type = SR_INST_USER;
130 ipdbg_la_tcp_close(tcp);
132 devices = g_slist_append(devices, sdi);
134 return std_scan_complete(di, devices);
137 static int dev_clear(const struct sr_dev_driver *di)
139 struct drv_context *drvc = di->context;
140 struct sr_dev_inst *sdi;
144 for (l = drvc->instances; l; l = l->next) {
146 struct ipdbg_la_tcp *tcp = sdi->conn;
148 ipdbg_la_tcp_close(tcp);
149 ipdbg_la_tcp_free(tcp);
156 return std_dev_clear(di);
159 static int dev_open(struct sr_dev_inst *sdi)
161 sdi->status = SR_ST_INACTIVE;
163 struct ipdbg_la_tcp *tcp = sdi->conn;
168 if (ipdbg_la_tcp_open(tcp) != SR_OK)
171 sdi->status = SR_ST_ACTIVE;
176 static int dev_close(struct sr_dev_inst *sdi)
178 // Should be called before a new call to scan()
179 struct ipdbg_la_tcp *tcp = sdi->conn;
182 ipdbg_la_tcp_close(tcp);
185 sdi->status = SR_ST_INACTIVE;
190 static int config_get(uint32_t key, GVariant **data,
191 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
193 struct dev_context *devc = sdi->priv;
198 case SR_CONF_CAPTURE_RATIO:
199 *data = g_variant_new_uint64(devc->capture_ratio);
201 case SR_CONF_LIMIT_SAMPLES:
202 *data = g_variant_new_uint64(devc->limit_samples);
211 static int config_set(uint32_t key, GVariant *data,
212 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
214 struct dev_context *devc = sdi->priv;
218 if (sdi->status != SR_ST_ACTIVE)
219 return SR_ERR_DEV_CLOSED;
222 case SR_CONF_CAPTURE_RATIO:
223 devc->capture_ratio = g_variant_get_uint64(data);
225 case SR_CONF_LIMIT_SAMPLES:
226 devc->limit_samples = g_variant_get_uint64(data);
235 static int config_list(uint32_t key, GVariant **data,
236 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
239 case SR_CONF_SCAN_OPTIONS:
240 case SR_CONF_DEVICE_OPTIONS:
241 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
242 case SR_CONF_TRIGGER_MATCH:
243 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
252 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
254 return std_init(di, sr_ctx);
257 static GSList *dev_list(const struct sr_dev_driver *di)
259 return ((struct drv_context *)(di->context))->instances;
262 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
264 if (sdi->status != SR_ST_ACTIVE)
265 return SR_ERR_DEV_CLOSED;
267 struct ipdbg_la_tcp *tcp = sdi->conn;
268 struct dev_context *devc = sdi->priv;
270 ipdbg_la_convert_trigger(sdi);
271 ipdbg_la_send_trigger(devc, tcp);
272 ipdbg_la_send_delay(devc, tcp);
274 /* If the device stops sending for longer than it takes to send a byte,
275 * that means it's finished. But wait at least 100 ms to be safe.
277 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
278 ipdbg_la_receive_data, (struct sr_dev_inst *)sdi);
280 ipdbg_la_send_start(tcp);
285 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
287 struct ipdbg_la_tcp *tcp = sdi->conn;
288 struct dev_context *devc = sdi->priv;
292 if (devc->num_transfers > 0) {
293 while (devc->num_transfers <
294 (devc->limit_samples_max * devc->DATA_WIDTH_BYTES)) {
295 ipdbg_la_tcp_receive(tcp, &byte);
296 devc->num_transfers++;
300 ipdbg_la_send_reset(tcp);
301 ipdbg_la_abort_acquisition(sdi);
306 SR_PRIV struct sr_dev_driver ipdbg_la_driver_info = {
308 .longname = "IPDBG LA",
311 .cleanup = std_cleanup,
313 .dev_list = dev_list,
314 .dev_clear = dev_clear,
315 .config_get = config_get,
316 .config_set = config_set,
317 .config_list = config_list,
318 .dev_open = dev_open,
319 .dev_close = dev_close,
320 .dev_acquisition_start = dev_acquisition_start,
321 .dev_acquisition_stop = dev_acquisition_stop,
325 SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);