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