]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-4032l/protocol.c
hantek-4032l: Separate USB receive callbacks.
[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
a5b9880e
AV
42static void finish_acquisition(struct sr_dev_inst *sdi)
43{
44 struct drv_context *drvc = sdi->driver->context;
45
46 std_session_send_df_end(sdi);
47 usb_source_remove(sdi->session, drvc->sr_ctx);
48}
49
50static void free_transfer(struct libusb_transfer *transfer)
51{
52 struct sr_dev_inst *sdi = transfer->user_data;
53 struct dev_context *devc = sdi->priv;
54
55 transfer->buffer = NULL;
56 libusb_free_transfer(transfer);
57 devc->usb_transfer = NULL;
58
59 finish_acquisition(sdi);
60}
61
43d86035
AV
62static void resubmit_transfer(struct libusb_transfer *transfer)
63{
64 int ret;
65
66 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
67 return;
68
69 sr_err("%s: %s", __func__, libusb_error_name(ret));
70 free_transfer(transfer);
71}
72
5089a143 73SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
6a25fa42 74{
5089a143
AZ
75 struct timeval tv;
76 struct drv_context *drvc;
6a25fa42
AZ
77
78 (void)fd;
5089a143
AZ
79 (void)revents;
80
81 drvc = (struct drv_context *)cb_data;
82
83 tv.tv_sec = tv.tv_usec = 0;
84 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
6a25fa42 85
5089a143
AZ
86 return TRUE;
87}
6a25fa42 88
43d86035 89void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer)
5089a143
AZ
90{
91 const struct sr_dev_inst *sdi = transfer->user_data;
92 struct dev_context *devc = sdi->priv;
93 struct drv_context *drvc = sdi->driver->context;
43d86035
AV
94 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
95 struct sr_datafeed_packet packet;
96 struct sr_datafeed_logic logic;
97 uint32_t *buffer;
98 uint32_t number_samples;
99
100 /*
101 * If acquisition has already ended, just free any queued up
102 * transfer that come in.
103 */
104 if (devc->acq_aborted) {
105 free_transfer(transfer);
106 return;
107 }
108
109 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
110 sr_dbg("%s error: %d.", __func__, transfer->status);
111
112 /* Cancel pending transfers. */
113 if (transfer->actual_length == 0) {
114 resubmit_transfer(transfer);
115 return;
116 }
117
118 buffer = (uint32_t *)transfer->buffer;
119
120 number_samples = (devc->remaining_samples < max_samples) ?
121 devc->remaining_samples : max_samples;
122 devc->remaining_samples -= number_samples;
123 packet.type = SR_DF_LOGIC;
124 packet.payload = &logic;
125 logic.length = number_samples * sizeof(uint32_t);
126 logic.unitsize = sizeof(uint32_t);
127 logic.data = buffer;
128 sr_session_send(sdi, &packet);
129 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
130 buffer[0], buffer[1]);
131
132 /* Close data receiving. */
133 if (devc->remaining_samples == 0) {
134 std_session_send_df_end(sdi);
135 usb_source_remove(sdi->session, drvc->sr_ctx);
136 devc->status = H4032L_STATUS_IDLE;
137 if (buffer[number_samples] != H4032L_END_PACKET_MAGIC)
138 sr_err("Mismatch magic number of end poll.");
139 } else {
140 resubmit_transfer(transfer);
141 }
142}
143
144void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
145{
146 const struct sr_dev_inst *sdi = transfer->user_data;
147 struct dev_context *devc = sdi->priv;
5089a143
AZ
148 struct sr_usb_dev_inst *usb = sdi->conn;
149 gboolean cmd = FALSE;
43d86035 150 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
5089a143
AZ
151 uint32_t *buffer;
152 struct h4032l_status_packet *status;
153 struct sr_datafeed_packet packet;
154 struct sr_datafeed_logic logic;
155 uint32_t number_samples;
156 int ret;
6a25fa42 157
a5b9880e
AV
158 /*
159 * If acquisition has already ended, just free any queued up
160 * transfers that come in.
161 */
162 if (devc->acq_aborted) {
163 free_transfer(transfer);
164 return;
165 }
166
74c4c174
AV
167 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
168 sr_dbg("%s error: %d.", __func__, transfer->status);
6a25fa42 169
5089a143
AZ
170 buffer = (uint32_t *)transfer->buffer;
171
172 switch (devc->status) {
173 case H4032L_STATUS_IDLE:
174 sr_err("USB callback called in idle.");
175 break;
176 case H4032L_STATUS_CMD_CONFIGURE:
177 /* Select status request as next. */
178 cmd = TRUE;
179 devc->cmd_pkt.cmd = CMD_STATUS;
180 devc->status = H4032L_STATUS_CMD_STATUS;
181 break;
182 case H4032L_STATUS_CMD_STATUS:
183 /* Select status request as next. */
184 devc->status = H4032L_STATUS_RESPONSE_STATUS;
185 break;
186 case H4032L_STATUS_RESPONSE_STATUS:
187 /*
188 * Check magic and if status is complete, then select
189 * First Transfer as next.
190 */
191 status = (struct h4032l_status_packet *)transfer->buffer;
28f2d07f 192 if (status->magic != H4032L_STATUS_PACKET_MAGIC)
74c4c174 193 devc->status = H4032L_STATUS_RESPONSE_STATUS;
28f2d07f 194 else if (status->status == 2)
5089a143 195 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
28f2d07f 196 else
5089a143 197 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
5089a143
AZ
198 break;
199 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
200 devc->status = H4032L_STATUS_CMD_STATUS;
201 devc->cmd_pkt.cmd = CMD_STATUS;
202 cmd = TRUE;
203 break;
204 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
205 devc->status = H4032L_STATUS_CMD_GET;
206 devc->cmd_pkt.cmd = CMD_GET;
207 cmd = TRUE;
208 break;
209 case H4032L_STATUS_CMD_GET:
210 devc->status = H4032L_STATUS_FIRST_TRANSFER;
43d86035
AV
211 /* Trigger has been captured. */
212 std_session_send_df_header(sdi);
5089a143
AZ
213 break;
214 case H4032L_STATUS_FIRST_TRANSFER:
74c4c174 215 /* Drop packets until H4032L_START_PACKET_MAGIC. */
5089a143 216 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
74c4c174 217 sr_dbg("Mismatch magic number of start poll.");
5089a143
AZ
218 break;
219 }
220 devc->status = H4032L_STATUS_TRANSFER;
221 max_samples--;
222 buffer++;
97200156 223 /* Fallthrough. */
5089a143 224 case H4032L_STATUS_TRANSFER:
28f2d07f 225 number_samples = (devc->remaining_samples < max_samples) ?
43d86035 226 devc->remaining_samples : max_samples;
5089a143
AZ
227 devc->remaining_samples -= number_samples;
228 packet.type = SR_DF_LOGIC;
229 packet.payload = &logic;
230 logic.length = number_samples * sizeof(uint32_t);
231 logic.unitsize = sizeof(uint32_t);
232 logic.data = buffer;
233 sr_session_send(sdi, &packet);
234 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
28f2d07f 235 buffer[0], buffer[1]);
5089a143
AZ
236 break;
237 }
238
43d86035
AV
239 /* Start data receiving. */
240 if (devc->status == H4032L_STATUS_TRANSFER) {
241 if ((ret = h4032l_start_data_transfers(sdi)) != SR_OK) {
242 sr_err("Can not start data transfers: %d", ret);
243 devc->status = H4032L_STATUS_IDLE;
244 }
245 } else if (devc->status != H4032L_STATUS_IDLE) {
5089a143
AZ
246 if (cmd) {
247 /* Setup new USB cmd packet, reuse transfer object. */
248 sr_dbg("New command: %d.", devc->status);
249 libusb_fill_bulk_transfer(transfer, usb->devhdl,
250 2 | LIBUSB_ENDPOINT_OUT,
251 (unsigned char *)&devc->cmd_pkt,
252 sizeof(struct h4032l_cmd_pkt),
28f2d07f
AV
253 h4032l_usb_callback,
254 (void *)sdi, H4032L_USB_TIMEOUT);
5089a143
AZ
255 } else {
256 /* Setup new USB poll packet, reuse transfer object. */
257 sr_dbg("Poll: %d.", devc->status);
258 libusb_fill_bulk_transfer(transfer, usb->devhdl,
259 6 | LIBUSB_ENDPOINT_IN,
260 devc->buffer, ARRAY_SIZE(devc->buffer),
261 h4032l_usb_callback,
262 (void *)sdi, H4032L_USB_TIMEOUT);
263 }
4868f15a 264 /* Send prepared USB packet. */
5089a143
AZ
265 if ((ret = libusb_submit_transfer(transfer)) != 0) {
266 sr_err("Failed to submit transfer: %s.",
267 libusb_error_name(ret));
268 devc->status = H4032L_STATUS_IDLE;
269 }
270 } else {
271 sr_dbg("Now idle.");
272 }
273
274 if (devc->status == H4032L_STATUS_IDLE)
a5b9880e 275 free_transfer(transfer);
5089a143
AZ
276}
277
278uint16_t h4032l_voltage2pwm(double voltage)
279{
280 /*
281 * word PwmA - channel A Vref PWM value, pseudocode:
282 * -6V < ThresholdVoltage < +6V
283 * Vref = 1.8 - ThresholdVoltage
284 * if Vref > 10.0
285 * Vref = 10.0
286 * if Vref < -5.0
287 * Vref = -5.0
288 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
289 * if pwm > 4095
290 * pwm = 4095
291 */
292 voltage = 1.8 - voltage;
293 if (voltage > 10.0)
294 voltage = 10.0;
295 else if (voltage < -5.0)
296 voltage = -5.0;
297
298 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
299}
300
43d86035
AV
301SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi)
302{
303 struct dev_context *devc = sdi->priv;
304 struct sr_usb_dev_inst *usb = sdi->conn;
305 struct libusb_transfer *transfer;
306 int ret;
307
308 transfer = libusb_alloc_transfer(0);
309
310 libusb_fill_bulk_transfer(transfer, usb->devhdl,
311 6 | LIBUSB_ENDPOINT_IN,
312 devc->buffer, ARRAY_SIZE(devc->buffer),
313 h4032l_data_transfer_callback,
314 (void *)sdi, H4032L_USB_TIMEOUT);
315
316 /* Send prepared usb packet. */
317 if ((ret = libusb_submit_transfer(transfer)) != 0) {
318 sr_err("Failed to submit transfer: %s.",
319 libusb_error_name(ret));
320 devc->status = H4032L_STATUS_IDLE;
321 }
322
323 if (devc->status == H4032L_STATUS_IDLE)
324 free_transfer(transfer);
325
326 return (ret ? SR_ERR : SR_OK);
327}
328
5089a143
AZ
329SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
330{
331 struct dev_context *devc = sdi->priv;
332 struct sr_usb_dev_inst *usb = sdi->conn;
333 struct libusb_transfer *transfer;
74c4c174 334 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5089a143
AZ
335 int ret;
336
74c4c174
AV
337 /* Send reset command to arm the logic analyzer. */
338 if ((ret = libusb_control_transfer(usb->devhdl,
339 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET,
340 0x00, 0x00, buffer, ARRAY_SIZE(buffer), H4032L_USB_TIMEOUT)) < 0) {
28f2d07f
AV
341 sr_err("Failed to send vendor request %s.",
342 libusb_error_name(ret));
74c4c174
AV
343 return SR_ERR;
344 }
345
346 /* Wait for reset vendor request. */
347 g_usleep(20 * 1000);
348
349 /* Send configure command. */
5089a143
AZ
350 devc->cmd_pkt.cmd = CMD_CONFIGURE;
351 devc->status = H4032L_STATUS_CMD_CONFIGURE;
352 devc->remaining_samples = devc->cmd_pkt.sample_size;
353
354 transfer = libusb_alloc_transfer(0);
355
356 libusb_fill_bulk_transfer(transfer, usb->devhdl,
357 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
358 sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
359 (void *)sdi, H4032L_USB_TIMEOUT);
360
361 if ((ret = libusb_submit_transfer(transfer)) != 0) {
28f2d07f
AV
362 sr_err("Failed to submit transfer: %s.",
363 libusb_error_name(ret));
5089a143
AZ
364 libusb_free_transfer(transfer);
365 return SR_ERR;
366 }
367
5089a143
AZ
368 return SR_OK;
369}
370
371SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
372{
373 struct drv_context *drvc = sdi->driver->context;
374 struct sr_usb_dev_inst *usb = sdi->conn;
375 struct libusb_device_descriptor des;
376 libusb_device **devlist;
377 int ret = SR_ERR, i, device_count;
378 char connection_id[64];
379
380 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
381 if (device_count < 0) {
382 sr_err("Failed to get device list: %s.",
383 libusb_error_name(device_count));
384 return SR_ERR;
385 }
386
387 for (i = 0; i < device_count; i++) {
388 libusb_get_device_descriptor(devlist[i], &des);
389
390 if (des.idVendor != H4032L_USB_VENDOR ||
391 des.idProduct != H4032L_USB_PRODUCT)
392 continue;
393
394 if ((sdi->status == SR_ST_INITIALIZING) ||
395 (sdi->status == SR_ST_INACTIVE)) {
396 /* Check device by its physical USB bus/port address. */
28f2d07f
AV
397 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
398 continue;
399
5089a143
AZ
400 if (strcmp(sdi->connection_id, connection_id))
401 /* This is not the one. */
402 continue;
403 }
404
405 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
406 if (usb->address == 0xff)
407 /*
408 * First time we touch this device after FW
409 * upload, so we don't know the address yet.
410 */
411 usb->address =
28f2d07f 412 libusb_get_device_address(devlist[i]);
5089a143
AZ
413 } else {
414 sr_err("Failed to open device: %s.",
415 libusb_error_name(ret));
416 ret = SR_ERR;
417 break;
418 }
419
420 ret = SR_OK;
421 break;
422 }
423
424 libusb_free_device_list(devlist, 1);
425 return ret;
6a25fa42 426}
7a7afc00
AV
427
428SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi)
429{
430 struct dev_context *devc = sdi->priv;
431 struct sr_usb_dev_inst *usb = sdi->conn;
432 struct h4032l_status_packet *status;
433 int transferred;
434 int i, ret;
435
436 /* Set command to status. */
437 devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
438 devc->cmd_pkt.cmd = CMD_STATUS;
439
440 /* Send status request. */
441 if ((ret = libusb_bulk_transfer(usb->devhdl,
442 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
443 sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) {
444 sr_err("Unable to send FPGA version request: %s.",
445 libusb_error_name(ret));
446 return SR_ERR;
447 }
448
449 /* Attempt to get FGPA version. */
450 for (i = 0; i < 10; i++) {
451 if ((ret = libusb_bulk_transfer(usb->devhdl,
452 6 | LIBUSB_ENDPOINT_IN, devc->buffer,
453 ARRAY_SIZE(devc->buffer), &transferred, H4032L_USB_TIMEOUT)) < 0) {
454 sr_err("Unable to receive FPGA version: %s.",
455 libusb_error_name(ret));
456 return SR_ERR;
457 }
458 status = (struct h4032l_status_packet *)devc->buffer;
459 if (status->magic == H4032L_STATUS_PACKET_MAGIC) {
460 sr_dbg("FPGA version: 0x%x.", status->fpga_version);
461 devc->fpga_version = status->fpga_version;
462 return SR_OK;
463 }
464 }
465
466 sr_err("Unable to get FPGA version.");
467
468 return SR_ERR;
469}