]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-4032l/protocol.c
hantek-4032l: Increase speed of data getting.
[libsigrok.git] / src / hardware / hantek-4032l / protocol.c
CommitLineData
6a25fa42
AZ
1/*
2 * This file is part of the libsigrok project.
3 *
5089a143
AZ
4 * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
5 * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
6 * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
6a25fa42
AZ
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <config.h>
23#include "protocol.h"
24
5089a143
AZ
25#define H4032L_USB_TIMEOUT 500
26
27enum h4032l_cmd {
74c4c174
AV
28 CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */
29 CMD_CONFIGURE = 0x2b1a,
5089a143
AZ
30 CMD_STATUS = 0x4b3a,
31 CMD_GET = 0x6b5a
32};
33
e80e1858 34struct h4032l_status_packet {
5089a143
AZ
35 uint32_t magic;
36 uint32_t values;
37 uint32_t status;
2bd5d17c
UH
38 uint32_t usbxi_data;
39 uint32_t fpga_version;
5089a143
AZ
40};
41
2958315d
AV
42static void abort_acquisition(struct dev_context *devc)
43{
44 int i;
45
46 devc->acq_aborted = TRUE;
47
48 for (i = devc->num_transfers - 1; i >= 0; i--) {
49 if (devc->transfers[i])
50 libusb_cancel_transfer(devc->transfers[i]);
51 }
52
53 devc->status = H4032L_STATUS_IDLE;
54}
55
a5b9880e
AV
56static void finish_acquisition(struct sr_dev_inst *sdi)
57{
2958315d 58 struct dev_context *devc = sdi->priv;
a5b9880e
AV
59 struct drv_context *drvc = sdi->driver->context;
60
61 std_session_send_df_end(sdi);
62 usb_source_remove(sdi->session, drvc->sr_ctx);
2958315d
AV
63
64 devc->num_transfers = 0;
65 g_free(devc->transfers);
a5b9880e
AV
66}
67
68static void free_transfer(struct libusb_transfer *transfer)
69{
70 struct sr_dev_inst *sdi = transfer->user_data;
71 struct dev_context *devc = sdi->priv;
2958315d
AV
72 unsigned int i;
73
74 if ((transfer->buffer != (unsigned char *)&devc->cmd_pkt) &&
75 (transfer->buffer != devc->buffer)) {
76 g_free(transfer->buffer);
77 }
a5b9880e
AV
78
79 transfer->buffer = NULL;
80 libusb_free_transfer(transfer);
a5b9880e 81
2958315d
AV
82 for (i = 0; i < devc->num_transfers; i++) {
83 if (devc->transfers[i] == transfer) {
84 devc->transfers[i] = NULL;
85 break;
86 }
87 }
88
89 if (--devc->submitted_transfers == 0)
90 finish_acquisition(sdi);
a5b9880e
AV
91}
92
43d86035
AV
93static void resubmit_transfer(struct libusb_transfer *transfer)
94{
95 int ret;
96
97 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
98 return;
99
100 sr_err("%s: %s", __func__, libusb_error_name(ret));
101 free_transfer(transfer);
102}
103
5089a143 104SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
6a25fa42 105{
5089a143
AZ
106 struct timeval tv;
107 struct drv_context *drvc;
6a25fa42
AZ
108
109 (void)fd;
5089a143
AZ
110 (void)revents;
111
112 drvc = (struct drv_context *)cb_data;
113
114 tv.tv_sec = tv.tv_usec = 0;
115 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
6a25fa42 116
5089a143
AZ
117 return TRUE;
118}
6a25fa42 119
43d86035 120void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer)
5089a143
AZ
121{
122 const struct sr_dev_inst *sdi = transfer->user_data;
123 struct dev_context *devc = sdi->priv;
43d86035
AV
124 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
125 struct sr_datafeed_packet packet;
126 struct sr_datafeed_logic logic;
127 uint32_t *buffer;
128 uint32_t number_samples;
129
130 /*
131 * If acquisition has already ended, just free any queued up
132 * transfer that come in.
133 */
134 if (devc->acq_aborted) {
135 free_transfer(transfer);
136 return;
137 }
138
139 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
140 sr_dbg("%s error: %d.", __func__, transfer->status);
141
142 /* Cancel pending transfers. */
143 if (transfer->actual_length == 0) {
144 resubmit_transfer(transfer);
145 return;
146 }
147
148 buffer = (uint32_t *)transfer->buffer;
149
150 number_samples = (devc->remaining_samples < max_samples) ?
151 devc->remaining_samples : max_samples;
152 devc->remaining_samples -= number_samples;
153 packet.type = SR_DF_LOGIC;
154 packet.payload = &logic;
155 logic.length = number_samples * sizeof(uint32_t);
156 logic.unitsize = sizeof(uint32_t);
157 logic.data = buffer;
158 sr_session_send(sdi, &packet);
159 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
160 buffer[0], buffer[1]);
161
162 /* Close data receiving. */
163 if (devc->remaining_samples == 0) {
43d86035
AV
164 if (buffer[number_samples] != H4032L_END_PACKET_MAGIC)
165 sr_err("Mismatch magic number of end poll.");
2958315d
AV
166
167 abort_acquisition(devc);
168 free_transfer(transfer);
43d86035 169 } else {
2958315d
AV
170 if (((devc->submitted_transfers - 1) * H4032L_DATA_BUFFER_SIZE) <
171 (int32_t)(devc->remaining_samples * sizeof(uint32_t)))
172 resubmit_transfer(transfer);
173 else
174 free_transfer(transfer);
43d86035
AV
175 }
176}
177
178void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
179{
180 const struct sr_dev_inst *sdi = transfer->user_data;
181 struct dev_context *devc = sdi->priv;
5089a143
AZ
182 struct sr_usb_dev_inst *usb = sdi->conn;
183 gboolean cmd = FALSE;
43d86035 184 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
5089a143
AZ
185 uint32_t *buffer;
186 struct h4032l_status_packet *status;
187 struct sr_datafeed_packet packet;
188 struct sr_datafeed_logic logic;
189 uint32_t number_samples;
190 int ret;
6a25fa42 191
a5b9880e
AV
192 /*
193 * If acquisition has already ended, just free any queued up
194 * transfers that come in.
195 */
196 if (devc->acq_aborted) {
197 free_transfer(transfer);
198 return;
199 }
200
74c4c174
AV
201 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
202 sr_dbg("%s error: %d.", __func__, transfer->status);
6a25fa42 203
5089a143
AZ
204 buffer = (uint32_t *)transfer->buffer;
205
206 switch (devc->status) {
207 case H4032L_STATUS_IDLE:
208 sr_err("USB callback called in idle.");
209 break;
210 case H4032L_STATUS_CMD_CONFIGURE:
211 /* Select status request as next. */
212 cmd = TRUE;
213 devc->cmd_pkt.cmd = CMD_STATUS;
214 devc->status = H4032L_STATUS_CMD_STATUS;
215 break;
216 case H4032L_STATUS_CMD_STATUS:
217 /* Select status request as next. */
218 devc->status = H4032L_STATUS_RESPONSE_STATUS;
219 break;
220 case H4032L_STATUS_RESPONSE_STATUS:
221 /*
222 * Check magic and if status is complete, then select
223 * First Transfer as next.
224 */
225 status = (struct h4032l_status_packet *)transfer->buffer;
28f2d07f 226 if (status->magic != H4032L_STATUS_PACKET_MAGIC)
74c4c174 227 devc->status = H4032L_STATUS_RESPONSE_STATUS;
28f2d07f 228 else if (status->status == 2)
5089a143 229 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
28f2d07f 230 else
5089a143 231 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
5089a143
AZ
232 break;
233 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
234 devc->status = H4032L_STATUS_CMD_STATUS;
235 devc->cmd_pkt.cmd = CMD_STATUS;
236 cmd = TRUE;
237 break;
238 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
239 devc->status = H4032L_STATUS_CMD_GET;
240 devc->cmd_pkt.cmd = CMD_GET;
241 cmd = TRUE;
242 break;
243 case H4032L_STATUS_CMD_GET:
244 devc->status = H4032L_STATUS_FIRST_TRANSFER;
43d86035
AV
245 /* Trigger has been captured. */
246 std_session_send_df_header(sdi);
5089a143
AZ
247 break;
248 case H4032L_STATUS_FIRST_TRANSFER:
74c4c174 249 /* Drop packets until H4032L_START_PACKET_MAGIC. */
5089a143 250 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
74c4c174 251 sr_dbg("Mismatch magic number of start poll.");
5089a143
AZ
252 break;
253 }
254 devc->status = H4032L_STATUS_TRANSFER;
255 max_samples--;
256 buffer++;
97200156 257 /* Fallthrough. */
5089a143 258 case H4032L_STATUS_TRANSFER:
28f2d07f 259 number_samples = (devc->remaining_samples < max_samples) ?
43d86035 260 devc->remaining_samples : max_samples;
5089a143
AZ
261 devc->remaining_samples -= number_samples;
262 packet.type = SR_DF_LOGIC;
263 packet.payload = &logic;
264 logic.length = number_samples * sizeof(uint32_t);
265 logic.unitsize = sizeof(uint32_t);
266 logic.data = buffer;
267 sr_session_send(sdi, &packet);
268 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
28f2d07f 269 buffer[0], buffer[1]);
5089a143
AZ
270 break;
271 }
272
43d86035
AV
273 /* Start data receiving. */
274 if (devc->status == H4032L_STATUS_TRANSFER) {
275 if ((ret = h4032l_start_data_transfers(sdi)) != SR_OK) {
276 sr_err("Can not start data transfers: %d", ret);
277 devc->status = H4032L_STATUS_IDLE;
278 }
279 } else if (devc->status != H4032L_STATUS_IDLE) {
5089a143
AZ
280 if (cmd) {
281 /* Setup new USB cmd packet, reuse transfer object. */
282 sr_dbg("New command: %d.", devc->status);
283 libusb_fill_bulk_transfer(transfer, usb->devhdl,
284 2 | LIBUSB_ENDPOINT_OUT,
285 (unsigned char *)&devc->cmd_pkt,
286 sizeof(struct h4032l_cmd_pkt),
28f2d07f
AV
287 h4032l_usb_callback,
288 (void *)sdi, H4032L_USB_TIMEOUT);
5089a143
AZ
289 } else {
290 /* Setup new USB poll packet, reuse transfer object. */
291 sr_dbg("Poll: %d.", devc->status);
292 libusb_fill_bulk_transfer(transfer, usb->devhdl,
293 6 | LIBUSB_ENDPOINT_IN,
294 devc->buffer, ARRAY_SIZE(devc->buffer),
295 h4032l_usb_callback,
296 (void *)sdi, H4032L_USB_TIMEOUT);
297 }
4868f15a 298 /* Send prepared USB packet. */
5089a143
AZ
299 if ((ret = libusb_submit_transfer(transfer)) != 0) {
300 sr_err("Failed to submit transfer: %s.",
301 libusb_error_name(ret));
302 devc->status = H4032L_STATUS_IDLE;
303 }
304 } else {
305 sr_dbg("Now idle.");
306 }
307
308 if (devc->status == H4032L_STATUS_IDLE)
a5b9880e 309 free_transfer(transfer);
5089a143
AZ
310}
311
312uint16_t h4032l_voltage2pwm(double voltage)
313{
314 /*
315 * word PwmA - channel A Vref PWM value, pseudocode:
316 * -6V < ThresholdVoltage < +6V
317 * Vref = 1.8 - ThresholdVoltage
318 * if Vref > 10.0
319 * Vref = 10.0
320 * if Vref < -5.0
321 * Vref = -5.0
322 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
323 * if pwm > 4095
324 * pwm = 4095
325 */
326 voltage = 1.8 - voltage;
327 if (voltage > 10.0)
328 voltage = 10.0;
329 else if (voltage < -5.0)
330 voltage = -5.0;
331
332 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
333}
334
43d86035
AV
335SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi)
336{
337 struct dev_context *devc = sdi->priv;
338 struct sr_usb_dev_inst *usb = sdi->conn;
339 struct libusb_transfer *transfer;
2958315d
AV
340 uint8_t *buffer;
341 unsigned int num_transfers;
342 unsigned int i;
43d86035
AV
343 int ret;
344
2958315d 345 devc->submitted_transfers = 0;
43d86035 346
2958315d
AV
347 /*
348 * Set number of data transfers regarding to size of buffer.
349 * FPGA version 0 can't transfer multiple transfers at once.
350 */
351 if ((num_transfers = MIN(devc->remaining_samples * sizeof(uint32_t) /
352 H4032L_DATA_BUFFER_SIZE, devc->fpga_version ?
353 H4032L_DATA_TRANSFER_MAX_NUM : 1)) == 0)
354 num_transfers = 1;
355
356 g_free(devc->transfers);
357 devc->transfers = g_try_malloc(sizeof(*devc->transfers) * num_transfers);
358 if (!devc->transfers) {
359 sr_err("USB transfers malloc failed.");
360 return SR_ERR_MALLOC;
43d86035
AV
361 }
362
2958315d
AV
363 devc->num_transfers = num_transfers;
364 for (i = 0; i < num_transfers; i++) {
365 if (!(buffer = g_malloc(H4032L_DATA_BUFFER_SIZE))) {
366 sr_err("USB transfer buffer malloc failed.");
367 return SR_ERR_MALLOC;
368 }
369 transfer = libusb_alloc_transfer(0);
370
371 libusb_fill_bulk_transfer(transfer, usb->devhdl,
372 6 | LIBUSB_ENDPOINT_IN,
373 buffer, H4032L_DATA_BUFFER_SIZE,
374 h4032l_data_transfer_callback,
375 (void *)sdi, H4032L_USB_TIMEOUT);
376
377 /* Send prepared usb packet. */
378 if ((ret = libusb_submit_transfer(transfer)) != 0) {
379 sr_err("Failed to submit transfer: %s.",
380 libusb_error_name(ret));
381 libusb_free_transfer(transfer);
382 g_free(buffer);
383 abort_acquisition(devc);
384 return SR_ERR;
385 }
386 devc->transfers[i] = transfer;
387 devc->submitted_transfers++;
388 }
43d86035 389
2958315d 390 return SR_OK;
43d86035
AV
391}
392
5089a143
AZ
393SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
394{
395 struct dev_context *devc = sdi->priv;
396 struct sr_usb_dev_inst *usb = sdi->conn;
397 struct libusb_transfer *transfer;
74c4c174 398 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5089a143
AZ
399 int ret;
400
74c4c174
AV
401 /* Send reset command to arm the logic analyzer. */
402 if ((ret = libusb_control_transfer(usb->devhdl,
403 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET,
404 0x00, 0x00, buffer, ARRAY_SIZE(buffer), H4032L_USB_TIMEOUT)) < 0) {
28f2d07f
AV
405 sr_err("Failed to send vendor request %s.",
406 libusb_error_name(ret));
74c4c174
AV
407 return SR_ERR;
408 }
409
410 /* Wait for reset vendor request. */
411 g_usleep(20 * 1000);
412
413 /* Send configure command. */
5089a143
AZ
414 devc->cmd_pkt.cmd = CMD_CONFIGURE;
415 devc->status = H4032L_STATUS_CMD_CONFIGURE;
416 devc->remaining_samples = devc->cmd_pkt.sample_size;
417
418 transfer = libusb_alloc_transfer(0);
419
420 libusb_fill_bulk_transfer(transfer, usb->devhdl,
421 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
422 sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
423 (void *)sdi, H4032L_USB_TIMEOUT);
424
425 if ((ret = libusb_submit_transfer(transfer)) != 0) {
28f2d07f
AV
426 sr_err("Failed to submit transfer: %s.",
427 libusb_error_name(ret));
5089a143
AZ
428 libusb_free_transfer(transfer);
429 return SR_ERR;
430 }
431
2958315d
AV
432 devc->transfers = g_malloc0(sizeof(*devc->transfers));
433 if (!devc->transfers) {
434 sr_err("USB start transfer malloc failed.");
435 return SR_ERR_MALLOC;
436 }
437
438 devc->submitted_transfers++;
439 devc->num_transfers = 1;
440 devc->transfers[0] = transfer;
441
442 return SR_OK;
443}
444
445SR_PRIV int h4032l_stop(struct sr_dev_inst *sdi)
446{
447 abort_acquisition(sdi->priv);
448
5089a143
AZ
449 return SR_OK;
450}
451
452SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
453{
454 struct drv_context *drvc = sdi->driver->context;
455 struct sr_usb_dev_inst *usb = sdi->conn;
456 struct libusb_device_descriptor des;
457 libusb_device **devlist;
458 int ret = SR_ERR, i, device_count;
459 char connection_id[64];
460
461 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
462 if (device_count < 0) {
463 sr_err("Failed to get device list: %s.",
464 libusb_error_name(device_count));
465 return SR_ERR;
466 }
467
468 for (i = 0; i < device_count; i++) {
469 libusb_get_device_descriptor(devlist[i], &des);
470
471 if (des.idVendor != H4032L_USB_VENDOR ||
472 des.idProduct != H4032L_USB_PRODUCT)
473 continue;
474
475 if ((sdi->status == SR_ST_INITIALIZING) ||
476 (sdi->status == SR_ST_INACTIVE)) {
477 /* Check device by its physical USB bus/port address. */
28f2d07f
AV
478 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
479 continue;
480
5089a143
AZ
481 if (strcmp(sdi->connection_id, connection_id))
482 /* This is not the one. */
483 continue;
484 }
485
486 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
487 if (usb->address == 0xff)
488 /*
489 * First time we touch this device after FW
490 * upload, so we don't know the address yet.
491 */
492 usb->address =
28f2d07f 493 libusb_get_device_address(devlist[i]);
5089a143
AZ
494 } else {
495 sr_err("Failed to open device: %s.",
496 libusb_error_name(ret));
497 ret = SR_ERR;
498 break;
499 }
500
501 ret = SR_OK;
502 break;
503 }
504
505 libusb_free_device_list(devlist, 1);
506 return ret;
6a25fa42 507}
7a7afc00
AV
508
509SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi)
510{
511 struct dev_context *devc = sdi->priv;
512 struct sr_usb_dev_inst *usb = sdi->conn;
513 struct h4032l_status_packet *status;
514 int transferred;
515 int i, ret;
516
517 /* Set command to status. */
518 devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
519 devc->cmd_pkt.cmd = CMD_STATUS;
520
521 /* Send status request. */
522 if ((ret = libusb_bulk_transfer(usb->devhdl,
523 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
524 sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) {
525 sr_err("Unable to send FPGA version request: %s.",
526 libusb_error_name(ret));
527 return SR_ERR;
528 }
529
530 /* Attempt to get FGPA version. */
531 for (i = 0; i < 10; i++) {
532 if ((ret = libusb_bulk_transfer(usb->devhdl,
533 6 | LIBUSB_ENDPOINT_IN, devc->buffer,
534 ARRAY_SIZE(devc->buffer), &transferred, H4032L_USB_TIMEOUT)) < 0) {
535 sr_err("Unable to receive FPGA version: %s.",
536 libusb_error_name(ret));
537 return SR_ERR;
538 }
539 status = (struct h4032l_status_packet *)devc->buffer;
540 if (status->magic == H4032L_STATUS_PACKET_MAGIC) {
541 sr_dbg("FPGA version: 0x%x.", status->fpga_version);
542 devc->fpga_version = status->fpga_version;
543 return SR_OK;
544 }
545 }
546
547 sr_err("Unable to get FPGA version.");
548
549 return SR_ERR;
550}