]>
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> | |
26 | #endif | |
7f4c9a04 | 27 | |
a4210e18 EK |
28 | #include <string.h> |
29 | #include <unistd.h> | |
7f4c9a04 | 30 | |
a4210e18 EK |
31 | #ifndef _WIN32 |
32 | #include <sys/socket.h> | |
33 | #include <netinet/in.h> | |
34 | #include <arpa/inet.h> | |
35 | #include <netdb.h> | |
36 | #endif | |
7f4c9a04 | 37 | |
a4210e18 EK |
38 | #include <errno.h> |
39 | #include "protocol.h" | |
40 | ||
b51288e3 EK |
41 | #include <sys/ioctl.h> |
42 | ||
7f4c9a04 | 43 | #define BUFFER_SIZE 4 |
2f15f5bf | 44 | |
7f4c9a04 SA |
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 | |
2f15f5bf | 51 | |
7f4c9a04 SA |
52 | #define CMD_GET_BUS_WIDTHS 0xAA |
53 | #define CMD_GET_LA_ID 0xBB | |
54 | #define CMD_ESCAPE 0x55 | |
2f15f5bf | 55 | |
7f4c9a04 SA |
56 | /* Trigger subfunction command opcodes */ |
57 | #define CMD_TRIG_MASKS 0xF1 | |
58 | #define CMD_TRIG_MASK 0xF3 | |
59 | #define CMD_TRIG_VALUE 0xF7 | |
2f15f5bf | 60 | |
7f4c9a04 SA |
61 | #define CMD_TRIG_MASKS_LAST 0xF9 |
62 | #define CMD_TRIG_MASK_LAST 0xFB | |
63 | #define CMD_TRIG_VALUE_LAST 0xFF | |
2f15f5bf | 64 | |
7f4c9a04 SA |
65 | #define CMD_TRIG_SELECT_EDGE_MASK 0xF5 |
66 | #define CMD_TRIG_SET_EDGE_MASK 0xF6 | |
2f15f5bf | 67 | |
7f4c9a04 SA |
68 | /* LA subfunction command opcodes */ |
69 | #define CMD_LA_DELAY 0x1F | |
a66099c6 | 70 | |
4d33f5e1 | 71 | static gboolean data_available(struct ipdbg_la_tcp *tcp) |
b51288e3 EK |
72 | { |
73 | #ifdef __WIN32__ | |
9d2e5483 | 74 | ioctlsocket(tcp->socket, FIONREAD, &bytes_available); |
b51288e3 | 75 | #else |
9d2e5483 | 76 | int status; |
b51288e3 | 77 | |
9d2e5483 | 78 | if (ioctl(tcp->socket, FIONREAD, &status) < 0) { // TIOCMGET |
4838c6ca | 79 | sr_err("FIONREAD failed: %s\n", g_strerror(errno)); |
4465837b | 80 | return FALSE; |
9d2e5483 | 81 | } |
b51288e3 | 82 | |
4465837b | 83 | return (status < 1) ? FALSE : TRUE; |
7f4c9a04 | 84 | #endif // __WIN32__ |
b51288e3 EK |
85 | } |
86 | ||
8a9788e2 | 87 | SR_PRIV struct ipdbg_la_tcp *ipdbg_la_tcp_new(void) |
a4210e18 | 88 | { |
8a9788e2 | 89 | struct ipdbg_la_tcp *tcp; |
a4210e18 | 90 | |
8a9788e2 | 91 | tcp = g_malloc0(sizeof(struct ipdbg_la_tcp)); |
9d2e5483 SA |
92 | tcp->address = NULL; |
93 | tcp->port = NULL; | |
94 | tcp->socket = -1; | |
a4210e18 | 95 | |
9d2e5483 | 96 | return tcp; |
a4210e18 EK |
97 | } |
98 | ||
8a9788e2 | 99 | SR_PRIV void ipdbg_la_tcp_free(struct ipdbg_la_tcp *tcp) |
7f4c9a04 | 100 | { |
9d2e5483 SA |
101 | g_free(tcp->address); |
102 | g_free(tcp->port); | |
7f4c9a04 SA |
103 | } |
104 | ||
8a9788e2 | 105 | SR_PRIV int ipdbg_la_tcp_open(struct ipdbg_la_tcp *tcp) |
a4210e18 | 106 | { |
9d2e5483 SA |
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; | |
a4210e18 EK |
145 | } |
146 | ||
8a9788e2 | 147 | SR_PRIV int ipdbg_la_tcp_close(struct ipdbg_la_tcp *tcp) |
7f4c9a04 | 148 | { |
9d2e5483 | 149 | int ret = SR_OK; |
7f4c9a04 | 150 | |
9d2e5483 SA |
151 | if (close(tcp->socket) < 0) |
152 | ret = SR_ERR; | |
7f4c9a04 | 153 | |
9d2e5483 | 154 | tcp->socket = -1; |
7f4c9a04 | 155 | |
9d2e5483 | 156 | return ret; |
7f4c9a04 SA |
157 | } |
158 | ||
4d33f5e1 | 159 | static int tcp_send(struct ipdbg_la_tcp *tcp, const uint8_t *buf, size_t len) |
a4210e18 | 160 | { |
9d2e5483 | 161 | int out; |
703fb454 | 162 | out = send(tcp->socket, buf, len, 0); |
a4210e18 | 163 | |
9d2e5483 SA |
164 | if (out < 0) { |
165 | sr_err("Send error: %s", g_strerror(errno)); | |
166 | return SR_ERR; | |
167 | } | |
a4210e18 | 168 | |
9d2e5483 SA |
169 | if (out < (int)len) |
170 | sr_dbg("Only sent %d/%d bytes of data.", out, (int)len); | |
a4210e18 | 171 | |
9d2e5483 | 172 | return SR_OK; |
a4210e18 EK |
173 | } |
174 | ||
4d33f5e1 | 175 | static int tcp_receive_blocking(struct ipdbg_la_tcp *tcp, |
7f4c9a04 | 176 | uint8_t *buf, int bufsize) |
a4210e18 | 177 | { |
9d2e5483 | 178 | int received = 0; |
ed186484 | 179 | int error_count = 0; |
7f4c9a04 | 180 | |
ed186484 SA |
181 | /* Timeout after 500ms of not receiving data */ |
182 | while ((received < bufsize) && (error_count < 500)) { | |
8a9788e2 | 183 | if (ipdbg_la_tcp_receive(tcp, buf) > 0) { |
9d2e5483 SA |
184 | buf++; |
185 | received++; | |
ed186484 SA |
186 | } else { |
187 | error_count++; | |
188 | g_usleep(1000); /* Sleep for 1ms */ | |
9d2e5483 SA |
189 | } |
190 | } | |
7f4c9a04 | 191 | |
9d2e5483 | 192 | return received; |
a4210e18 | 193 | } |
7f4c9a04 | 194 | |
8a9788e2 | 195 | SR_PRIV int ipdbg_la_tcp_receive(struct ipdbg_la_tcp *tcp, |
9d2e5483 | 196 | uint8_t *buf) |
b51288e3 | 197 | { |
9d2e5483 SA |
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; | |
a4210e18 EK |
214 | } |
215 | ||
8a9788e2 | 216 | SR_PRIV int ipdbg_la_convert_trigger(const struct sr_dev_inst *sdi) |
a4210e18 | 217 | { |
1f15efc1 | 218 | struct dev_context *devc; |
9d2e5483 SA |
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 | ||
ac3625be | 230 | for (uint64_t i = 0; i < devc->data_width_bytes; i++) { |
9d2e5483 SA |
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; | |
a4210e18 EK |
284 | } |
285 | ||
8a9788e2 | 286 | SR_PRIV int ipdbg_la_receive_data(int fd, int revents, void *cb_data) |
a4210e18 | 287 | { |
9d2e5483 | 288 | const struct sr_dev_inst *sdi; |
1f15efc1 | 289 | struct dev_context *devc; |
9d2e5483 SA |
290 | |
291 | (void)fd; | |
292 | (void)revents; | |
293 | ||
703fb454 | 294 | sdi = cb_data; |
9d2e5483 SA |
295 | if (!sdi) |
296 | return FALSE; | |
297 | ||
298 | if (!(devc = sdi->priv)) | |
299 | return FALSE; | |
300 | ||
8a9788e2 | 301 | struct ipdbg_la_tcp *tcp = sdi->conn; |
9d2e5483 SA |
302 | struct sr_datafeed_packet packet; |
303 | struct sr_datafeed_logic logic; | |
304 | ||
305 | if (!devc->raw_sample_buf) { | |
306 | devc->raw_sample_buf = | |
ac3625be | 307 | g_try_malloc(devc->limit_samples * devc->data_width_bytes); |
9d2e5483 | 308 | if (!devc->raw_sample_buf) { |
fac36d0a | 309 | sr_err("Sample buffer malloc failed."); |
9d2e5483 SA |
310 | return FALSE; |
311 | } | |
312 | } | |
313 | ||
314 | if (devc->num_transfers < | |
ac3625be | 315 | (devc->limit_samples_max * devc->data_width_bytes)) { |
9d2e5483 SA |
316 | uint8_t byte; |
317 | ||
8a9788e2 | 318 | if (ipdbg_la_tcp_receive(tcp, &byte) == 1) { |
9d2e5483 | 319 | if (devc->num_transfers < |
ac3625be | 320 | (devc->limit_samples * devc->data_width_bytes)) |
9d2e5483 SA |
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; | |
ac3625be UH |
330 | logic.length = devc->delay_value * devc->data_width_bytes; |
331 | logic.unitsize = devc->data_width_bytes; | |
9d2e5483 SA |
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) * | |
ac3625be UH |
344 | devc->data_width_bytes; |
345 | logic.unitsize = devc->data_width_bytes; | |
9d2e5483 | 346 | logic.data = devc->raw_sample_buf + |
ac3625be | 347 | (devc->delay_value * devc->data_width_bytes); |
9d2e5483 SA |
348 | sr_session_send(cb_data, &packet); |
349 | ||
350 | g_free(devc->raw_sample_buf); | |
351 | devc->raw_sample_buf = NULL; | |
352 | ||
8a9788e2 | 353 | ipdbg_la_abort_acquisition(sdi); |
9d2e5483 SA |
354 | } |
355 | ||
356 | return TRUE; | |
a4210e18 EK |
357 | } |
358 | ||
4d33f5e1 UH |
359 | static 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 | ||
1f15efc1 | 382 | SR_PRIV int ipdbg_la_send_delay(struct dev_context *devc, |
8a9788e2 | 383 | struct ipdbg_la_tcp *tcp) |
a4210e18 | 384 | { |
9d2e5483 | 385 | devc->delay_value = (devc->limit_samples / 100.0) * devc->capture_ratio; |
a4210e18 | 386 | |
9d2e5483 SA |
387 | uint8_t buf; |
388 | buf = CMD_CFG_LA; | |
4d33f5e1 | 389 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 390 | buf = CMD_LA_DELAY; |
4d33f5e1 | 391 | tcp_send(tcp, &buf, 1); |
a4210e18 | 392 | |
9d2e5483 SA |
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 | }; | |
a4210e18 | 398 | |
ac3625be UH |
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); | |
a4210e18 | 401 | |
9d2e5483 | 402 | return SR_OK; |
a4210e18 EK |
403 | } |
404 | ||
1f15efc1 | 405 | SR_PRIV int ipdbg_la_send_trigger(struct dev_context *devc, |
8a9788e2 | 406 | struct ipdbg_la_tcp *tcp) |
a4210e18 | 407 | { |
9d2e5483 SA |
408 | uint8_t buf; |
409 | ||
410 | /* Mask */ | |
411 | buf = CMD_CFG_TRIGGER; | |
4d33f5e1 | 412 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 413 | buf = CMD_TRIG_MASKS; |
4d33f5e1 | 414 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 415 | buf = CMD_TRIG_MASK; |
4d33f5e1 | 416 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 417 | |
ac3625be | 418 | for (size_t i = 0; i < devc->data_width_bytes; i++) |
9d2e5483 | 419 | send_escaping(tcp, |
ac3625be | 420 | devc->trigger_mask + devc->data_width_bytes - 1 - i, 1); |
9d2e5483 SA |
421 | |
422 | /* Value */ | |
423 | buf = CMD_CFG_TRIGGER; | |
4d33f5e1 | 424 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 425 | buf = CMD_TRIG_MASKS; |
4d33f5e1 | 426 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 427 | buf = CMD_TRIG_VALUE; |
4d33f5e1 | 428 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 429 | |
ac3625be | 430 | for (size_t i = 0; i < devc->data_width_bytes; i++) |
9d2e5483 | 431 | send_escaping(tcp, |
ac3625be | 432 | devc->trigger_value + devc->data_width_bytes - 1 - i, 1); |
9d2e5483 SA |
433 | |
434 | /* Mask_last */ | |
435 | buf = CMD_CFG_TRIGGER; | |
4d33f5e1 | 436 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 437 | buf = CMD_TRIG_MASKS_LAST; |
4d33f5e1 | 438 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 439 | buf = CMD_TRIG_MASK_LAST; |
4d33f5e1 | 440 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 441 | |
ac3625be | 442 | for (size_t i = 0; i < devc->data_width_bytes; i++) |
9d2e5483 | 443 | send_escaping(tcp, |
ac3625be | 444 | devc->trigger_mask_last + devc->data_width_bytes - 1 - i, 1); |
9d2e5483 SA |
445 | |
446 | /* Value_last */ | |
447 | buf = CMD_CFG_TRIGGER; | |
4d33f5e1 | 448 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 449 | buf = CMD_TRIG_MASKS_LAST; |
4d33f5e1 | 450 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 451 | buf = CMD_TRIG_VALUE_LAST; |
4d33f5e1 | 452 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 453 | |
ac3625be | 454 | for (size_t i = 0; i < devc->data_width_bytes; i++) |
9d2e5483 | 455 | send_escaping(tcp, |
ac3625be | 456 | devc->trigger_value_last + devc->data_width_bytes - 1 - i, 1); |
9d2e5483 SA |
457 | |
458 | /* Edge_mask */ | |
459 | buf = CMD_CFG_TRIGGER; | |
4d33f5e1 | 460 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 461 | buf = CMD_TRIG_SELECT_EDGE_MASK; |
4d33f5e1 | 462 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 463 | buf = CMD_TRIG_SET_EDGE_MASK; |
4d33f5e1 | 464 | tcp_send(tcp, &buf, 1); |
9d2e5483 | 465 | |
ac3625be | 466 | for (size_t i = 0; i < devc->data_width_bytes; i++) |
9d2e5483 | 467 | send_escaping(tcp, |
ac3625be | 468 | devc->trigger_edge_mask + devc->data_width_bytes - 1 - i, 1); |
9d2e5483 SA |
469 | |
470 | return SR_OK; | |
a4210e18 EK |
471 | } |
472 | ||
8a9788e2 | 473 | SR_PRIV void ipdbg_la_get_addrwidth_and_datawidth( |
1f15efc1 | 474 | struct ipdbg_la_tcp *tcp, struct dev_context *devc) |
a4210e18 | 475 | { |
9d2e5483 SA |
476 | uint8_t buf[8]; |
477 | uint8_t read_cmd = CMD_GET_BUS_WIDTHS; | |
a4210e18 | 478 | |
4d33f5e1 | 479 | if (tcp_send(tcp, &read_cmd, 1) != SR_OK) |
9d2e5483 | 480 | sr_warn("Can't send read command"); |
a4210e18 | 481 | |
4d33f5e1 | 482 | if (tcp_receive_blocking(tcp, buf, 8) != 8) |
9d2e5483 | 483 | sr_warn("Can't get address and data width from device"); |
b51288e3 | 484 | |
ac3625be UH |
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; | |
a4210e18 | 489 | |
ac3625be UH |
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; | |
a4210e18 | 494 | |
ac3625be | 495 | const uint8_t host_word_size = 8; |
a4210e18 | 496 | |
ac3625be UH |
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; | |
a4210e18 | 501 | |
ac3625be | 502 | devc->limit_samples_max = (0x01 << devc->addr_width); |
9d2e5483 SA |
503 | devc->limit_samples = devc->limit_samples_max; |
504 | ||
ac3625be UH |
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); | |
a4210e18 EK |
510 | } |
511 | ||
1f15efc1 | 512 | SR_PRIV struct dev_context *ipdbg_la_dev_new(void) |
a4210e18 | 513 | { |
1f15efc1 | 514 | struct dev_context *devc; |
a4210e18 | 515 | |
1f15efc1 | 516 | devc = g_malloc0(sizeof(struct dev_context)); |
9d2e5483 | 517 | devc->capture_ratio = 50; |
a4210e18 | 518 | |
9d2e5483 | 519 | return devc; |
a4210e18 EK |
520 | } |
521 | ||
8a9788e2 | 522 | SR_PRIV int ipdbg_la_send_reset(struct ipdbg_la_tcp *tcp) |
a4210e18 | 523 | { |
9d2e5483 | 524 | uint8_t buf = CMD_RESET; |
4d33f5e1 | 525 | if (tcp_send(tcp, &buf, 1) != SR_OK) |
9d2e5483 | 526 | sr_warn("Couldn't send reset"); |
7f4c9a04 | 527 | |
9d2e5483 | 528 | return SR_OK; |
a4210e18 EK |
529 | } |
530 | ||
8a9788e2 | 531 | SR_PRIV int ipdbg_la_request_id(struct ipdbg_la_tcp *tcp) |
a4210e18 | 532 | { |
9d2e5483 | 533 | uint8_t buf = CMD_GET_LA_ID; |
4d33f5e1 | 534 | if (tcp_send(tcp, &buf, 1) != SR_OK) |
9d2e5483 | 535 | sr_warn("Couldn't send ID request"); |
a4210e18 | 536 | |
9d2e5483 | 537 | char id[4]; |
4d33f5e1 | 538 | if (tcp_receive_blocking(tcp, (uint8_t *)id, 4) != 4) { |
ed186484 SA |
539 | sr_err("Couldn't read device ID"); |
540 | return SR_ERR; | |
541 | } | |
a4210e18 | 542 | |
9d2e5483 SA |
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 | } | |
a4210e18 | 548 | |
9d2e5483 | 549 | return SR_OK; |
a4210e18 EK |
550 | } |
551 | ||
8a9788e2 | 552 | SR_PRIV void ipdbg_la_abort_acquisition(const struct sr_dev_inst *sdi) |
a4210e18 | 553 | { |
8a9788e2 | 554 | struct ipdbg_la_tcp *tcp = sdi->conn; |
a4210e18 | 555 | |
9d2e5483 | 556 | sr_session_source_remove(sdi->session, tcp->socket); |
a4210e18 | 557 | |
9d2e5483 | 558 | std_session_send_df_end(sdi); |
a4210e18 EK |
559 | } |
560 | ||
8a9788e2 | 561 | SR_PRIV int ipdbg_la_send_start(struct ipdbg_la_tcp *tcp) |
a4210e18 | 562 | { |
9d2e5483 | 563 | uint8_t buf = CMD_START; |
a4210e18 | 564 | |
4d33f5e1 | 565 | if (tcp_send(tcp, &buf, 1) != SR_OK) |
9d2e5483 | 566 | sr_warn("Couldn't send start"); |
7f4c9a04 | 567 | |
9d2e5483 | 568 | return SR_OK; |
a4210e18 | 569 | } |