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