]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/ipdbg-la/protocol.c
fx2lafw: silence error message in query for channel group's device options
[libsigrok.git] / src / hardware / ipdbg-la / protocol.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 Eva Kissling <eva.kissling@bluewin.ch>
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
22#ifdef _WIN32
23#define _WIN32_WINNT 0x0501
24#include <winsock2.h>
25#include <ws2tcpip.h>
26#endif
27
28#include <string.h>
29#include <unistd.h>
30
31#ifndef _WIN32
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <netdb.h>
36#endif
37
38#include <errno.h>
39#include "protocol.h"
40
41#include <sys/ioctl.h>
42
43#define BUFFER_SIZE 4
44
45/* Top-level command opcodes */
46#define CMD_SET_TRIGGER 0x00
47#define CMD_CFG_TRIGGER 0xF0
48#define CMD_CFG_LA 0x0F
49#define CMD_START 0xFE
50#define CMD_RESET 0xEE
51
52#define CMD_GET_BUS_WIDTHS 0xAA
53#define CMD_GET_LA_ID 0xBB
54#define CMD_ESCAPE 0x55
55
56/* Trigger subfunction command opcodes */
57#define CMD_TRIG_MASKS 0xF1
58#define CMD_TRIG_MASK 0xF3
59#define CMD_TRIG_VALUE 0xF7
60
61#define CMD_TRIG_MASKS_LAST 0xF9
62#define CMD_TRIG_MASK_LAST 0xFB
63#define CMD_TRIG_VALUE_LAST 0xFF
64
65#define CMD_TRIG_SELECT_EDGE_MASK 0xF5
66#define CMD_TRIG_SET_EDGE_MASK 0xF6
67
68/* LA subfunction command opcodes */
69#define CMD_LA_DELAY 0x1F
70
71static gboolean data_available(struct ipdbg_la_tcp *tcp)
72{
73#ifdef __WIN32__
74 ioctlsocket(tcp->socket, FIONREAD, &bytes_available);
75#else
76 int status;
77
78 if (ioctl(tcp->socket, FIONREAD, &status) < 0) { // TIOCMGET
79 sr_err("FIONREAD failed: %s\n", g_strerror(errno));
80 return FALSE;
81 }
82
83 return (status < 1) ? FALSE : TRUE;
84#endif // __WIN32__
85}
86
87SR_PRIV struct ipdbg_la_tcp *ipdbg_la_tcp_new(void)
88{
89 struct ipdbg_la_tcp *tcp;
90
91 tcp = g_malloc0(sizeof(struct ipdbg_la_tcp));
92 tcp->address = NULL;
93 tcp->port = NULL;
94 tcp->socket = -1;
95
96 return tcp;
97}
98
99SR_PRIV void ipdbg_la_tcp_free(struct ipdbg_la_tcp *tcp)
100{
101 g_free(tcp->address);
102 g_free(tcp->port);
103}
104
105SR_PRIV int ipdbg_la_tcp_open(struct ipdbg_la_tcp *tcp)
106{
107 struct addrinfo hints;
108 struct addrinfo *results, *res;
109 int err;
110
111 memset(&hints, 0, sizeof(hints));
112 hints.ai_family = AF_UNSPEC;
113 hints.ai_socktype = SOCK_STREAM;
114 hints.ai_protocol = IPPROTO_TCP;
115
116 err = getaddrinfo(tcp->address, tcp->port, &hints, &results);
117
118 if (err) {
119 sr_err("Address lookup failed: %s:%s: %s", tcp->address,
120 tcp->port, gai_strerror(err));
121 return SR_ERR;
122 }
123
124 for (res = results; res; res = res->ai_next) {
125 if ((tcp->socket = socket(res->ai_family, res->ai_socktype,
126 res->ai_protocol)) < 0)
127 continue;
128 if (connect(tcp->socket, res->ai_addr, res->ai_addrlen) != 0) {
129 close(tcp->socket);
130 tcp->socket = -1;
131 continue;
132 }
133 break;
134 }
135
136 freeaddrinfo(results);
137
138 if (tcp->socket < 0) {
139 sr_err("Failed to connect to %s:%s: %s", tcp->address, tcp->port,
140 g_strerror(errno));
141 return SR_ERR;
142 }
143
144 return SR_OK;
145}
146
147SR_PRIV int ipdbg_la_tcp_close(struct ipdbg_la_tcp *tcp)
148{
149 int ret = SR_OK;
150
151 if (close(tcp->socket) < 0)
152 ret = SR_ERR;
153
154 tcp->socket = -1;
155
156 return ret;
157}
158
159static int tcp_send(struct ipdbg_la_tcp *tcp, const uint8_t *buf, size_t len)
160{
161 int out;
162 out = send(tcp->socket, buf, len, 0);
163
164 if (out < 0) {
165 sr_err("Send error: %s", g_strerror(errno));
166 return SR_ERR;
167 }
168
169 if (out < (int)len)
170 sr_dbg("Only sent %d/%d bytes of data.", out, (int)len);
171
172 return SR_OK;
173}
174
175static int tcp_receive_blocking(struct ipdbg_la_tcp *tcp,
176 uint8_t *buf, int bufsize)
177{
178 int received = 0;
179 int error_count = 0;
180
181 /* Timeout after 500ms of not receiving data */
182 while ((received < bufsize) && (error_count < 500)) {
183 if (ipdbg_la_tcp_receive(tcp, buf) > 0) {
184 buf++;
185 received++;
186 } else {
187 error_count++;
188 g_usleep(1000); /* Sleep for 1ms */
189 }
190 }
191
192 return received;
193}
194
195SR_PRIV int ipdbg_la_tcp_receive(struct ipdbg_la_tcp *tcp,
196 uint8_t *buf)
197{
198 int received = 0;
199
200 if (data_available(tcp)) {
201 while (received < 1) {
202 int len = recv(tcp->socket, buf, 1, 0);
203
204 if (len < 0) {
205 sr_err("Receive error: %s", g_strerror(errno));
206 return SR_ERR;
207 } else
208 received += len;
209 }
210
211 return received;
212 } else
213 return -1;
214}
215
216SR_PRIV int ipdbg_la_convert_trigger(const struct sr_dev_inst *sdi)
217{
218 struct dev_context *devc;
219 struct sr_trigger *trigger;
220 struct sr_trigger_stage *stage;
221 struct sr_trigger_match *match;
222 const GSList *l, *m;
223
224 devc = sdi->priv;
225
226 devc->num_stages = 0;
227 devc->num_transfers = 0;
228 devc->raw_sample_buf = NULL;
229
230 for (uint64_t i = 0; i < devc->data_width_bytes; i++) {
231 devc->trigger_mask[i] = 0;
232 devc->trigger_value[i] = 0;
233 devc->trigger_mask_last[i] = 0;
234 devc->trigger_value_last[i] = 0;
235 devc->trigger_edge_mask[i] = 0;
236 }
237
238 if (!(trigger = sr_session_trigger_get(sdi->session)))
239 return SR_OK;
240
241 for (l = trigger->stages; l; l = l->next) {
242 stage = l->data;
243 for (m = stage->matches; m; m = m->next) {
244 match = m->data;
245 int byte_idx = match->channel->index / 8;
246 uint8_t match_bit = 1 << (match->channel->index % 8);
247
248 if (!match->channel->enabled)
249 /* Ignore disabled channels with a trigger. */
250 continue;
251
252 if (match->match == SR_TRIGGER_ONE) {
253 devc->trigger_value[byte_idx] |= match_bit;
254 devc->trigger_mask[byte_idx] |= match_bit;
255 devc->trigger_mask_last[byte_idx] &= ~match_bit;
256 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
257 } else if (match->match == SR_TRIGGER_ZERO) {
258 devc->trigger_value[byte_idx] &= ~match_bit;
259 devc->trigger_mask[byte_idx] |= match_bit;
260 devc->trigger_mask_last[byte_idx] &= ~match_bit;
261 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
262 } else if (match->match == SR_TRIGGER_RISING) {
263 devc->trigger_value[byte_idx] |= match_bit;
264 devc->trigger_value_last[byte_idx] &=
265 ~match_bit;
266 devc->trigger_mask[byte_idx] |= match_bit;
267 devc->trigger_mask_last[byte_idx] |= match_bit;
268 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
269 } else if (match->match == SR_TRIGGER_FALLING) {
270 devc->trigger_value[byte_idx] &= ~match_bit;
271 devc->trigger_value_last[byte_idx] |= match_bit;
272 devc->trigger_mask[byte_idx] |= match_bit;
273 devc->trigger_mask_last[byte_idx] |= match_bit;
274 devc->trigger_edge_mask[byte_idx] &= ~match_bit;
275 } else if (match->match == SR_TRIGGER_EDGE) {
276 devc->trigger_mask[byte_idx] &= ~match_bit;
277 devc->trigger_mask_last[byte_idx] &= ~match_bit;
278 devc->trigger_edge_mask[byte_idx] |= match_bit;
279 }
280 }
281 }
282
283 return SR_OK;
284}
285
286SR_PRIV int ipdbg_la_receive_data(int fd, int revents, void *cb_data)
287{
288 const struct sr_dev_inst *sdi;
289 struct dev_context *devc;
290
291 (void)fd;
292 (void)revents;
293
294 sdi = cb_data;
295 if (!sdi)
296 return FALSE;
297
298 if (!(devc = sdi->priv))
299 return FALSE;
300
301 struct ipdbg_la_tcp *tcp = sdi->conn;
302 struct sr_datafeed_packet packet;
303 struct sr_datafeed_logic logic;
304
305 if (!devc->raw_sample_buf) {
306 devc->raw_sample_buf =
307 g_try_malloc(devc->limit_samples * devc->data_width_bytes);
308 if (!devc->raw_sample_buf) {
309 sr_err("Sample buffer malloc failed.");
310 return FALSE;
311 }
312 }
313
314 if (devc->num_transfers <
315 (devc->limit_samples_max * devc->data_width_bytes)) {
316 uint8_t byte;
317
318 if (ipdbg_la_tcp_receive(tcp, &byte) == 1) {
319 if (devc->num_transfers <
320 (devc->limit_samples * devc->data_width_bytes))
321 devc->raw_sample_buf[devc->num_transfers] = byte;
322
323 devc->num_transfers++;
324 }
325 } else {
326 if (devc->delay_value > 0) {
327 /* There are pre-trigger samples, send those first. */
328 packet.type = SR_DF_LOGIC;
329 packet.payload = &logic;
330 logic.length = devc->delay_value * devc->data_width_bytes;
331 logic.unitsize = devc->data_width_bytes;
332 logic.data = devc->raw_sample_buf;
333 sr_session_send(cb_data, &packet);
334 }
335
336 /* Send the trigger. */
337 packet.type = SR_DF_TRIGGER;
338 sr_session_send(cb_data, &packet);
339
340 /* Send post-trigger samples. */
341 packet.type = SR_DF_LOGIC;
342 packet.payload = &logic;
343 logic.length = (devc->limit_samples - devc->delay_value) *
344 devc->data_width_bytes;
345 logic.unitsize = devc->data_width_bytes;
346 logic.data = devc->raw_sample_buf +
347 (devc->delay_value * devc->data_width_bytes);
348 sr_session_send(cb_data, &packet);
349
350 g_free(devc->raw_sample_buf);
351 devc->raw_sample_buf = NULL;
352
353 ipdbg_la_abort_acquisition(sdi);
354 }
355
356 return TRUE;
357}
358
359static int send_escaping(struct ipdbg_la_tcp *tcp, uint8_t *data_to_send,
360 uint32_t length)
361{
362 uint8_t escape = CMD_ESCAPE;
363
364 while (length--) {
365 uint8_t payload = *data_to_send++;
366
367 if (payload == CMD_RESET)
368 if (tcp_send(tcp, &escape, 1) != SR_OK)
369 sr_warn("Couldn't send escape");
370
371 if (payload == CMD_ESCAPE)
372 if (tcp_send(tcp, &escape, 1) != SR_OK)
373 sr_warn("Couldn't send escape");
374
375 if (tcp_send(tcp, &payload, 1) != SR_OK)
376 sr_warn("Couldn't send data");
377 }
378
379 return SR_OK;
380}
381
382SR_PRIV int ipdbg_la_send_delay(struct dev_context *devc,
383 struct ipdbg_la_tcp *tcp)
384{
385 devc->delay_value = (devc->limit_samples / 100.0) * devc->capture_ratio;
386
387 uint8_t buf;
388 buf = CMD_CFG_LA;
389 tcp_send(tcp, &buf, 1);
390 buf = CMD_LA_DELAY;
391 tcp_send(tcp, &buf, 1);
392
393 uint8_t delay_buf[4] = { devc->delay_value & 0x000000ff,
394 (devc->delay_value >> 8) & 0x000000ff,
395 (devc->delay_value >> 16) & 0x000000ff,
396 (devc->delay_value >> 24) & 0x000000ff
397 };
398
399 for (uint64_t i = 0; i < devc->addr_width_bytes; i++)
400 send_escaping(tcp, &(delay_buf[devc->addr_width_bytes - 1 - i]), 1);
401
402 return SR_OK;
403}
404
405SR_PRIV int ipdbg_la_send_trigger(struct dev_context *devc,
406 struct ipdbg_la_tcp *tcp)
407{
408 uint8_t buf;
409
410 /* Mask */
411 buf = CMD_CFG_TRIGGER;
412 tcp_send(tcp, &buf, 1);
413 buf = CMD_TRIG_MASKS;
414 tcp_send(tcp, &buf, 1);
415 buf = CMD_TRIG_MASK;
416 tcp_send(tcp, &buf, 1);
417
418 for (size_t i = 0; i < devc->data_width_bytes; i++)
419 send_escaping(tcp,
420 devc->trigger_mask + devc->data_width_bytes - 1 - i, 1);
421
422 /* Value */
423 buf = CMD_CFG_TRIGGER;
424 tcp_send(tcp, &buf, 1);
425 buf = CMD_TRIG_MASKS;
426 tcp_send(tcp, &buf, 1);
427 buf = CMD_TRIG_VALUE;
428 tcp_send(tcp, &buf, 1);
429
430 for (size_t i = 0; i < devc->data_width_bytes; i++)
431 send_escaping(tcp,
432 devc->trigger_value + devc->data_width_bytes - 1 - i, 1);
433
434 /* Mask_last */
435 buf = CMD_CFG_TRIGGER;
436 tcp_send(tcp, &buf, 1);
437 buf = CMD_TRIG_MASKS_LAST;
438 tcp_send(tcp, &buf, 1);
439 buf = CMD_TRIG_MASK_LAST;
440 tcp_send(tcp, &buf, 1);
441
442 for (size_t i = 0; i < devc->data_width_bytes; i++)
443 send_escaping(tcp,
444 devc->trigger_mask_last + devc->data_width_bytes - 1 - i, 1);
445
446 /* Value_last */
447 buf = CMD_CFG_TRIGGER;
448 tcp_send(tcp, &buf, 1);
449 buf = CMD_TRIG_MASKS_LAST;
450 tcp_send(tcp, &buf, 1);
451 buf = CMD_TRIG_VALUE_LAST;
452 tcp_send(tcp, &buf, 1);
453
454 for (size_t i = 0; i < devc->data_width_bytes; i++)
455 send_escaping(tcp,
456 devc->trigger_value_last + devc->data_width_bytes - 1 - i, 1);
457
458 /* Edge_mask */
459 buf = CMD_CFG_TRIGGER;
460 tcp_send(tcp, &buf, 1);
461 buf = CMD_TRIG_SELECT_EDGE_MASK;
462 tcp_send(tcp, &buf, 1);
463 buf = CMD_TRIG_SET_EDGE_MASK;
464 tcp_send(tcp, &buf, 1);
465
466 for (size_t i = 0; i < devc->data_width_bytes; i++)
467 send_escaping(tcp,
468 devc->trigger_edge_mask + devc->data_width_bytes - 1 - i, 1);
469
470 return SR_OK;
471}
472
473SR_PRIV void ipdbg_la_get_addrwidth_and_datawidth(
474 struct ipdbg_la_tcp *tcp, struct dev_context *devc)
475{
476 uint8_t buf[8];
477 uint8_t read_cmd = CMD_GET_BUS_WIDTHS;
478
479 if (tcp_send(tcp, &read_cmd, 1) != SR_OK)
480 sr_warn("Can't send read command");
481
482 if (tcp_receive_blocking(tcp, buf, 8) != 8)
483 sr_warn("Can't get address and data width from device");
484
485 devc->data_width = buf[0] & 0x000000FF;
486 devc->data_width |= (buf[1] << 8) & 0x0000FF00;
487 devc->data_width |= (buf[2] << 16) & 0x00FF0000;
488 devc->data_width |= (buf[3] << 24) & 0xFF000000;
489
490 devc->addr_width = buf[4] & 0x000000FF;
491 devc->addr_width |= (buf[5] << 8) & 0x0000FF00;
492 devc->addr_width |= (buf[6] << 16) & 0x00FF0000;
493 devc->addr_width |= (buf[7] << 24) & 0xFF000000;
494
495 const uint8_t host_word_size = 8;
496
497 devc->data_width_bytes =
498 (devc->data_width + host_word_size - 1) / host_word_size;
499 devc->addr_width_bytes =
500 (devc->addr_width + host_word_size - 1) / host_word_size;
501
502 devc->limit_samples_max = (0x01 << devc->addr_width);
503 devc->limit_samples = devc->limit_samples_max;
504
505 devc->trigger_mask = g_malloc0(devc->data_width_bytes);
506 devc->trigger_value = g_malloc0(devc->data_width_bytes);
507 devc->trigger_mask_last = g_malloc0(devc->data_width_bytes);
508 devc->trigger_value_last = g_malloc0(devc->data_width_bytes);
509 devc->trigger_edge_mask = g_malloc0(devc->data_width_bytes);
510}
511
512SR_PRIV struct dev_context *ipdbg_la_dev_new(void)
513{
514 struct dev_context *devc;
515
516 devc = g_malloc0(sizeof(struct dev_context));
517 devc->capture_ratio = 50;
518
519 return devc;
520}
521
522SR_PRIV int ipdbg_la_send_reset(struct ipdbg_la_tcp *tcp)
523{
524 uint8_t buf = CMD_RESET;
525 if (tcp_send(tcp, &buf, 1) != SR_OK)
526 sr_warn("Couldn't send reset");
527
528 return SR_OK;
529}
530
531SR_PRIV int ipdbg_la_request_id(struct ipdbg_la_tcp *tcp)
532{
533 uint8_t buf = CMD_GET_LA_ID;
534 if (tcp_send(tcp, &buf, 1) != SR_OK)
535 sr_warn("Couldn't send ID request");
536
537 char id[4];
538 if (tcp_receive_blocking(tcp, (uint8_t *)id, 4) != 4) {
539 sr_err("Couldn't read device ID");
540 return SR_ERR;
541 }
542
543 if (strncmp(id, "IDBG", 4)) {
544 sr_err("Invalid device ID: expected 'IDBG', got '%c%c%c%c'.",
545 id[0], id[1], id[2], id[3]);
546 return SR_ERR;
547 }
548
549 return SR_OK;
550}
551
552SR_PRIV void ipdbg_la_abort_acquisition(const struct sr_dev_inst *sdi)
553{
554 struct ipdbg_la_tcp *tcp = sdi->conn;
555
556 sr_session_source_remove(sdi->session, tcp->socket);
557
558 std_session_send_df_end(sdi);
559}
560
561SR_PRIV int ipdbg_la_send_start(struct ipdbg_la_tcp *tcp)
562{
563 uint8_t buf = CMD_START;
564
565 if (tcp_send(tcp, &buf, 1) != SR_OK)
566 sr_warn("Couldn't send start");
567
568 return SR_OK;
569}