2 * This file is part of the libsigrok project.
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>
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.
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.
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/>.
25 #define H4032L_USB_TIMEOUT 500
28 CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */
29 CMD_CONFIGURE = 0x2b1a,
34 struct h4032l_status_packet {
39 uint32_t fpga_version;
42 static void abort_acquisition(struct dev_context *devc)
46 devc->acq_aborted = TRUE;
48 for (i = devc->num_transfers - 1; i >= 0; i--) {
49 if (devc->transfers[i])
50 libusb_cancel_transfer(devc->transfers[i]);
53 devc->status = H4032L_STATUS_IDLE;
56 static void finish_acquisition(struct sr_dev_inst *sdi)
58 struct dev_context *devc = sdi->priv;
59 struct drv_context *drvc = sdi->driver->context;
61 std_session_send_df_end(sdi);
62 usb_source_remove(sdi->session, drvc->sr_ctx);
64 devc->num_transfers = 0;
65 g_free(devc->transfers);
68 static void free_transfer(struct libusb_transfer *transfer)
70 struct sr_dev_inst *sdi = transfer->user_data;
71 struct dev_context *devc = sdi->priv;
74 if ((transfer->buffer != (unsigned char *)&devc->cmd_pkt) &&
75 (transfer->buffer != devc->buffer)) {
76 g_free(transfer->buffer);
79 transfer->buffer = NULL;
80 libusb_free_transfer(transfer);
82 for (i = 0; i < devc->num_transfers; i++) {
83 if (devc->transfers[i] == transfer) {
84 devc->transfers[i] = NULL;
89 if (--devc->submitted_transfers == 0)
90 finish_acquisition(sdi);
93 static void resubmit_transfer(struct libusb_transfer *transfer)
97 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
100 sr_err("%s: %s", __func__, libusb_error_name(ret));
101 free_transfer(transfer);
104 static void send_data(struct sr_dev_inst *sdi,
105 uint32_t *data, size_t sample_count)
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),
113 const struct sr_datafeed_packet packet = {
117 const struct sr_datafeed_packet trig = {
118 .type = SR_DF_TRIGGER,
121 size_t trigger_offset;
123 if (devc->trigger_pos >= devc->sent_samples &&
124 devc->trigger_pos < (devc->sent_samples + sample_count)) {
125 /* Get trigger position. */
126 trigger_offset = devc->trigger_pos - devc->sent_samples;
127 logic.length = trigger_offset * sizeof(uint32_t);
129 sr_session_send(sdi, &packet);
131 /* Send trigger position. */
132 sr_session_send(sdi, &trig);
134 /* Send rest of data. */
135 logic.length = (sample_count-trigger_offset) * sizeof(uint32_t);
136 logic.data = data + trigger_offset;
138 sr_session_send(sdi, &packet);
140 sr_session_send(sdi, &packet);
143 devc->sent_samples += sample_count;
146 SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
149 struct drv_context *drvc;
154 drvc = (struct drv_context *)cb_data;
156 tv.tv_sec = tv.tv_usec = 0;
157 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
162 void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer)
164 struct sr_dev_inst *const sdi = transfer->user_data;
165 struct dev_context *const devc = sdi->priv;
166 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
168 uint32_t number_samples;
171 * If acquisition has already ended, just free any queued up
172 * transfer that come in.
174 if (devc->acq_aborted) {
175 free_transfer(transfer);
179 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
180 sr_dbg("%s error: %d.", __func__, transfer->status);
182 /* Cancel pending transfers. */
183 if (transfer->actual_length == 0) {
184 resubmit_transfer(transfer);
188 buffer = (uint32_t *)transfer->buffer;
190 number_samples = (devc->remaining_samples < max_samples) ?
191 devc->remaining_samples : max_samples;
192 devc->remaining_samples -= number_samples;
193 send_data(sdi, buffer, number_samples);
194 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
195 buffer[0], buffer[1]);
197 /* Close data receiving. */
198 if (devc->remaining_samples == 0) {
199 if (buffer[number_samples] != H4032L_END_PACKET_MAGIC)
200 sr_err("Mismatch magic number of end poll.");
202 abort_acquisition(devc);
203 free_transfer(transfer);
205 if (((devc->submitted_transfers - 1) * H4032L_DATA_BUFFER_SIZE) <
206 (int32_t)(devc->remaining_samples * sizeof(uint32_t)))
207 resubmit_transfer(transfer);
209 free_transfer(transfer);
213 void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
215 struct sr_dev_inst *const sdi = transfer->user_data;
216 struct dev_context *const devc = sdi->priv;
217 struct sr_usb_dev_inst *usb = sdi->conn;
218 gboolean cmd = FALSE;
219 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
221 struct h4032l_status_packet *status;
222 uint32_t number_samples;
226 * If acquisition has already ended, just free any queued up
227 * transfers that come in.
229 if (devc->acq_aborted) {
230 free_transfer(transfer);
234 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
235 sr_dbg("%s error: %d.", __func__, transfer->status);
237 buffer = (uint32_t *)transfer->buffer;
239 switch (devc->status) {
240 case H4032L_STATUS_IDLE:
241 sr_err("USB callback called in idle.");
243 case H4032L_STATUS_CMD_CONFIGURE:
244 /* Select status request as next. */
246 devc->cmd_pkt.cmd = CMD_STATUS;
247 devc->status = H4032L_STATUS_CMD_STATUS;
249 case H4032L_STATUS_CMD_STATUS:
250 /* Select status request as next. */
251 devc->status = H4032L_STATUS_RESPONSE_STATUS;
253 case H4032L_STATUS_RESPONSE_STATUS:
255 * Check magic and if status is complete, then select
256 * First Transfer as next.
258 status = (struct h4032l_status_packet *)transfer->buffer;
259 if (status->magic != H4032L_STATUS_PACKET_MAGIC)
260 devc->status = H4032L_STATUS_RESPONSE_STATUS;
261 else if (status->status == 2)
262 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
264 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
266 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
267 devc->status = H4032L_STATUS_CMD_STATUS;
268 devc->cmd_pkt.cmd = CMD_STATUS;
271 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
272 devc->status = H4032L_STATUS_CMD_GET;
273 devc->cmd_pkt.cmd = CMD_GET;
276 case H4032L_STATUS_CMD_GET:
277 devc->status = H4032L_STATUS_FIRST_TRANSFER;
278 /* Trigger has been captured. */
279 std_session_send_df_header(sdi);
281 case H4032L_STATUS_FIRST_TRANSFER:
282 /* Drop packets until H4032L_START_PACKET_MAGIC. */
283 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
284 sr_dbg("Mismatch magic number of start poll.");
287 devc->status = H4032L_STATUS_TRANSFER;
291 case H4032L_STATUS_TRANSFER:
292 number_samples = (devc->remaining_samples < max_samples) ?
293 devc->remaining_samples : max_samples;
294 devc->remaining_samples -= number_samples;
295 send_data(sdi, buffer, number_samples);
296 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
297 buffer[0], buffer[1]);
301 /* Start data receiving. */
302 if (devc->status == H4032L_STATUS_TRANSFER) {
303 if ((ret = h4032l_start_data_transfers(sdi)) != SR_OK) {
304 sr_err("Can not start data transfers: %d", ret);
305 devc->status = H4032L_STATUS_IDLE;
307 } else if (devc->status != H4032L_STATUS_IDLE) {
309 /* Setup new USB cmd packet, reuse transfer object. */
310 sr_dbg("New command: %d.", devc->status);
311 libusb_fill_bulk_transfer(transfer, usb->devhdl,
312 2 | LIBUSB_ENDPOINT_OUT,
313 (unsigned char *)&devc->cmd_pkt,
314 sizeof(struct h4032l_cmd_pkt),
316 (void *)sdi, H4032L_USB_TIMEOUT);
318 /* Setup new USB poll packet, reuse transfer object. */
319 sr_dbg("Poll: %d.", devc->status);
320 libusb_fill_bulk_transfer(transfer, usb->devhdl,
321 6 | LIBUSB_ENDPOINT_IN,
322 devc->buffer, ARRAY_SIZE(devc->buffer),
324 (void *)sdi, H4032L_USB_TIMEOUT);
326 /* Send prepared USB packet. */
327 if ((ret = libusb_submit_transfer(transfer)) != 0) {
328 sr_err("Failed to submit transfer: %s.",
329 libusb_error_name(ret));
330 devc->status = H4032L_STATUS_IDLE;
336 if (devc->status == H4032L_STATUS_IDLE)
337 free_transfer(transfer);
340 uint16_t h4032l_voltage2pwm(double voltage)
343 * word PwmA - channel A Vref PWM value, pseudocode:
344 * -6V < ThresholdVoltage < +6V
345 * Vref = 1.8 - ThresholdVoltage
350 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
354 voltage = 1.8 - voltage;
357 else if (voltage < -5.0)
360 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
363 SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi)
365 struct dev_context *devc = sdi->priv;
366 struct sr_usb_dev_inst *usb = sdi->conn;
367 struct libusb_transfer *transfer;
369 unsigned int num_transfers;
373 devc->submitted_transfers = 0;
376 * Set number of data transfers regarding to size of buffer.
377 * FPGA version 0 can't transfer multiple transfers at once.
379 if ((num_transfers = MIN(devc->remaining_samples * sizeof(uint32_t) /
380 H4032L_DATA_BUFFER_SIZE, devc->fpga_version ?
381 H4032L_DATA_TRANSFER_MAX_NUM : 1)) == 0)
384 g_free(devc->transfers);
385 devc->transfers = g_try_malloc(sizeof(*devc->transfers) * num_transfers);
386 if (!devc->transfers) {
387 sr_err("USB transfers malloc failed.");
388 return SR_ERR_MALLOC;
391 devc->num_transfers = num_transfers;
392 for (i = 0; i < num_transfers; i++) {
393 if (!(buffer = g_malloc(H4032L_DATA_BUFFER_SIZE))) {
394 sr_err("USB transfer buffer malloc failed.");
395 return SR_ERR_MALLOC;
397 transfer = libusb_alloc_transfer(0);
399 libusb_fill_bulk_transfer(transfer, usb->devhdl,
400 6 | LIBUSB_ENDPOINT_IN,
401 buffer, H4032L_DATA_BUFFER_SIZE,
402 h4032l_data_transfer_callback,
403 (void *)sdi, H4032L_USB_TIMEOUT);
405 /* Send prepared usb packet. */
406 if ((ret = libusb_submit_transfer(transfer)) != 0) {
407 sr_err("Failed to submit transfer: %s.",
408 libusb_error_name(ret));
409 libusb_free_transfer(transfer);
411 abort_acquisition(devc);
414 devc->transfers[i] = transfer;
415 devc->submitted_transfers++;
421 SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
423 struct dev_context *devc = sdi->priv;
424 struct sr_usb_dev_inst *usb = sdi->conn;
425 struct libusb_transfer *transfer;
426 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
429 /* Send reset command to arm the logic analyzer. */
430 if ((ret = libusb_control_transfer(usb->devhdl,
431 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET,
432 0x00, 0x00, buffer, ARRAY_SIZE(buffer), H4032L_USB_TIMEOUT)) < 0) {
433 sr_err("Failed to send vendor request %s.",
434 libusb_error_name(ret));
438 /* Wait for reset vendor request. */
441 /* Send configure command. */
442 devc->cmd_pkt.cmd = CMD_CONFIGURE;
443 devc->status = H4032L_STATUS_CMD_CONFIGURE;
444 devc->remaining_samples = devc->cmd_pkt.sample_size;
446 transfer = libusb_alloc_transfer(0);
448 libusb_fill_bulk_transfer(transfer, usb->devhdl,
449 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
450 sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
451 (void *)sdi, H4032L_USB_TIMEOUT);
453 if ((ret = libusb_submit_transfer(transfer)) != 0) {
454 sr_err("Failed to submit transfer: %s.",
455 libusb_error_name(ret));
456 libusb_free_transfer(transfer);
460 devc->transfers = g_malloc0(sizeof(*devc->transfers));
461 if (!devc->transfers) {
462 sr_err("USB start transfer malloc failed.");
463 return SR_ERR_MALLOC;
466 devc->submitted_transfers++;
467 devc->num_transfers = 1;
468 devc->transfers[0] = transfer;
473 SR_PRIV int h4032l_stop(struct sr_dev_inst *sdi)
475 abort_acquisition(sdi->priv);
480 SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
482 struct drv_context *drvc = sdi->driver->context;
483 struct sr_usb_dev_inst *usb = sdi->conn;
484 struct libusb_device_descriptor des;
485 libusb_device **devlist;
486 int ret = SR_ERR, i, device_count;
487 char connection_id[64];
489 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
490 if (device_count < 0) {
491 sr_err("Failed to get device list: %s.",
492 libusb_error_name(device_count));
496 for (i = 0; i < device_count; i++) {
497 libusb_get_device_descriptor(devlist[i], &des);
499 if (des.idVendor != H4032L_USB_VENDOR ||
500 des.idProduct != H4032L_USB_PRODUCT)
503 if ((sdi->status == SR_ST_INITIALIZING) ||
504 (sdi->status == SR_ST_INACTIVE)) {
505 /* Check device by its physical USB bus/port address. */
506 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
509 if (strcmp(sdi->connection_id, connection_id))
510 /* This is not the one. */
514 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
515 if (usb->address == 0xff)
517 * First time we touch this device after FW
518 * upload, so we don't know the address yet.
521 libusb_get_device_address(devlist[i]);
523 sr_err("Failed to open device: %s.",
524 libusb_error_name(ret));
533 libusb_free_device_list(devlist, 1);
537 SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi)
539 struct dev_context *devc = sdi->priv;
540 struct sr_usb_dev_inst *usb = sdi->conn;
541 struct h4032l_status_packet *status;
545 /* Set command to status. */
546 devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
547 devc->cmd_pkt.cmd = CMD_STATUS;
549 /* Send status request. */
550 if ((ret = libusb_bulk_transfer(usb->devhdl,
551 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
552 sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) {
553 sr_err("Unable to send FPGA version request: %s.",
554 libusb_error_name(ret));
558 /* Attempt to get FGPA version. */
559 for (i = 0; i < 10; i++) {
560 if ((ret = libusb_bulk_transfer(usb->devhdl,
561 6 | LIBUSB_ENDPOINT_IN, devc->buffer,
562 ARRAY_SIZE(devc->buffer), &transferred, H4032L_USB_TIMEOUT)) < 0) {
563 sr_err("Unable to receive FPGA version: %s.",
564 libusb_error_name(ret));
567 status = (struct h4032l_status_packet *)devc->buffer;
568 if (status->magic == H4032L_STATUS_PACKET_MAGIC) {
569 sr_dbg("FPGA version: 0x%x.", status->fpga_version);
570 devc->fpga_version = status->fpga_version;
575 sr_err("Unable to get FPGA version.");