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