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 static void ipdbg_la_split_addr_port(const char *conn, char **addr,
48 char **strs = g_strsplit(conn, "/", 3);
50 *addr = g_strdup(strs[1]);
51 *port = g_strdup(strs[2]);
56 static GSList *scan(struct sr_dev_driver *di, GSList *options)
58 struct drv_context *drvc;
63 drvc->instances = NULL;
65 struct sr_config *src;
69 for (l = options; l; l = l->next) {
73 conn = g_variant_get_string(src->data, NULL);
81 struct ipdbg_la_tcp *tcp = ipdbg_la_tcp_new();
83 ipdbg_la_split_addr_port(conn, &tcp->address, &tcp->port);
88 if (ipdbg_la_tcp_open(tcp) != SR_OK)
91 ipdbg_la_send_reset(tcp);
92 ipdbg_la_send_reset(tcp);
94 if (ipdbg_la_request_id(tcp) != SR_OK)
97 struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst));
98 sdi->status = SR_ST_INACTIVE;
99 sdi->vendor = g_strdup("ipdbg.org");
100 sdi->model = g_strdup("IPDBG LA");
101 sdi->version = g_strdup("v1.0");
104 struct dev_context *devc = ipdbg_la_dev_new();
107 ipdbg_la_get_addrwidth_and_datawidth(tcp, devc);
109 sr_dbg("addr_width = %d, data_width = %d\n", devc->addr_width,
111 sr_dbg("limit samples = %" PRIu64 "\n", devc->limit_samples_max);
113 for (uint32_t i = 0; i < devc->data_width; i++) {
114 char *name = g_strdup_printf("CH%d", i);
115 sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, name);
119 sdi->inst_type = SR_INST_USER;
122 ipdbg_la_tcp_close(tcp);
124 devices = g_slist_append(devices, sdi);
126 return std_scan_complete(di, devices);
129 static int dev_clear(const struct sr_dev_driver *di)
131 struct drv_context *drvc = di->context;
132 struct sr_dev_inst *sdi;
136 for (l = drvc->instances; l; l = l->next) {
138 struct ipdbg_la_tcp *tcp = sdi->conn;
140 ipdbg_la_tcp_close(tcp);
141 ipdbg_la_tcp_free(tcp);
148 return std_dev_clear(di);
151 static int dev_open(struct sr_dev_inst *sdi)
153 struct ipdbg_la_tcp *tcp = sdi->conn;
158 if (ipdbg_la_tcp_open(tcp) != SR_OK)
164 static int dev_close(struct sr_dev_inst *sdi)
166 /* Should be called before a new call to scan(). */
167 struct ipdbg_la_tcp *tcp = sdi->conn;
170 ipdbg_la_tcp_close(tcp);
177 static int config_get(uint32_t key, GVariant **data,
178 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
180 struct dev_context *devc = sdi->priv;
185 case SR_CONF_CAPTURE_RATIO:
186 *data = g_variant_new_uint64(devc->capture_ratio);
188 case SR_CONF_LIMIT_SAMPLES:
189 *data = g_variant_new_uint64(devc->limit_samples);
198 static int config_set(uint32_t key, GVariant *data,
199 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
201 struct dev_context *devc = sdi->priv;
206 case SR_CONF_CAPTURE_RATIO:
207 devc->capture_ratio = g_variant_get_uint64(data);
209 case SR_CONF_LIMIT_SAMPLES:
211 uint64_t limit_samples = g_variant_get_uint64(data);
212 if (limit_samples <= devc->limit_samples_max)
213 devc->limit_samples = limit_samples;
223 static int config_list(uint32_t key, GVariant **data,
224 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
227 case SR_CONF_SCAN_OPTIONS:
228 case SR_CONF_DEVICE_OPTIONS:
229 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
230 case SR_CONF_TRIGGER_MATCH:
231 *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches));
240 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
242 struct ipdbg_la_tcp *tcp = sdi->conn;
243 struct dev_context *devc = sdi->priv;
245 ipdbg_la_convert_trigger(sdi);
246 ipdbg_la_send_trigger(devc, tcp);
247 ipdbg_la_send_delay(devc, tcp);
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.
252 sr_session_source_add(sdi->session, tcp->socket, G_IO_IN, 100,
253 ipdbg_la_receive_data, (struct sr_dev_inst *)sdi);
255 ipdbg_la_send_start(tcp);
260 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
262 struct ipdbg_la_tcp *tcp = sdi->conn;
263 struct dev_context *devc = sdi->priv;
265 const size_t bufsize = 1024;
266 uint8_t buffer[bufsize];
268 if (devc->num_transfers > 0) {
269 while (devc->num_transfers <
270 (devc->limit_samples_max * devc->data_width_bytes)) {
271 int recd = ipdbg_la_tcp_receive(tcp, buffer, bufsize);
273 devc->num_transfers += recd;
277 ipdbg_la_send_reset(tcp);
278 ipdbg_la_abort_acquisition(sdi);
283 static struct sr_dev_driver ipdbg_la_driver_info = {
285 .longname = "IPDBG LA",
288 .cleanup = std_cleanup,
290 .dev_list = std_dev_list,
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,
301 SR_REGISTER_DEV_DRIVER(ipdbg_la_driver_info);