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