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