X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fhantek-4032l%2Fprotocol.c;h=a5b90ac56227f8128ec7cffdc6108a6dafc82817;hb=deb7615262ac4f9cc0750a08351afa7cbf9c34d5;hp=ccd2256bf545be9a28daf1ca64745c4042d95909;hpb=4868f15a86168969de4e3c68544e88502e0e8fb7;p=libsigrok.git diff --git a/src/hardware/hantek-4032l/protocol.c b/src/hardware/hantek-4032l/protocol.c index ccd2256b..a5b90ac5 100644 --- a/src/hardware/hantek-4032l/protocol.c +++ b/src/hardware/hantek-4032l/protocol.c @@ -25,17 +25,120 @@ #define H4032L_USB_TIMEOUT 500 enum h4032l_cmd { - CMD_CONFIGURE = 0x2b1a, /* Also arms the logic analyzer. */ + CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */ + CMD_CONFIGURE = 0x2b1a, CMD_STATUS = 0x4b3a, CMD_GET = 0x6b5a }; -struct __attribute__((__packed__)) h4032l_status_packet { +struct h4032l_status_packet { uint32_t magic; uint32_t values; uint32_t status; + uint32_t usbxi_data; + uint32_t fpga_version; }; +static void abort_acquisition(struct dev_context *devc) +{ + int i; + + devc->acq_aborted = TRUE; + + for (i = devc->num_transfers - 1; i >= 0; i--) { + if (devc->transfers[i]) + libusb_cancel_transfer(devc->transfers[i]); + } + + devc->status = H4032L_STATUS_IDLE; +} + +static void finish_acquisition(struct sr_dev_inst *sdi) +{ + struct dev_context *devc = sdi->priv; + struct drv_context *drvc = sdi->driver->context; + + std_session_send_df_end(sdi); + usb_source_remove(sdi->session, drvc->sr_ctx); + + devc->num_transfers = 0; + g_free(devc->transfers); +} + +static void free_transfer(struct libusb_transfer *transfer) +{ + struct sr_dev_inst *sdi = transfer->user_data; + struct dev_context *devc = sdi->priv; + unsigned int i; + + if ((transfer->buffer != (unsigned char *)&devc->cmd_pkt) && + (transfer->buffer != devc->buf)) { + g_free(transfer->buffer); + } + + transfer->buffer = NULL; + libusb_free_transfer(transfer); + + for (i = 0; i < devc->num_transfers; i++) { + if (devc->transfers[i] == transfer) { + devc->transfers[i] = NULL; + break; + } + } + + if (--devc->submitted_transfers == 0) + finish_acquisition(sdi); +} + +static void resubmit_transfer(struct libusb_transfer *transfer) +{ + int ret; + + if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS) + return; + + sr_err("%s: %s", __func__, libusb_error_name(ret)); + free_transfer(transfer); +} + +static void send_data(struct sr_dev_inst *sdi, + uint32_t *data, size_t sample_count) +{ + struct dev_context *devc = sdi->priv; + struct sr_datafeed_logic logic = { + .length = sample_count * sizeof(uint32_t), + .unitsize = sizeof(uint32_t), + .data = data + }; + const struct sr_datafeed_packet packet = { + .type = SR_DF_LOGIC, + .payload = &logic + }; + size_t trigger_offset; + + if (devc->trigger_pos >= devc->sent_samples && + devc->trigger_pos < (devc->sent_samples + sample_count)) { + /* Get trigger position. */ + trigger_offset = devc->trigger_pos - devc->sent_samples; + logic.length = trigger_offset * sizeof(uint32_t); + if (logic.length) + sr_session_send(sdi, &packet); + + /* Send trigger position. */ + std_session_send_df_trigger(sdi); + + /* Send rest of data. */ + logic.length = (sample_count - trigger_offset) * sizeof(uint32_t); + logic.data = data + trigger_offset; + if (logic.length) + sr_session_send(sdi, &packet); + } else { + sr_session_send(sdi, &packet); + } + + devc->sent_samples += sample_count; +} + SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data) { struct timeval tv; @@ -52,27 +155,81 @@ SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data) return TRUE; } +void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer) +{ + struct sr_dev_inst *const sdi = transfer->user_data; + struct dev_context *const devc = sdi->priv; + uint32_t max_samples = transfer->actual_length / sizeof(uint32_t); + uint32_t *buf; + uint32_t num_samples; + + /* + * If acquisition has already ended, just free any queued up + * transfer that come in. + */ + if (devc->acq_aborted) { + free_transfer(transfer); + return; + } + + if (transfer->status != LIBUSB_TRANSFER_COMPLETED) + sr_dbg("%s error: %d.", __func__, transfer->status); + + /* Cancel pending transfers. */ + if (transfer->actual_length == 0) { + resubmit_transfer(transfer); + return; + } + + buf = (uint32_t *)transfer->buffer; + + num_samples = MIN(devc->remaining_samples, max_samples); + devc->remaining_samples -= num_samples; + send_data(sdi, buf, num_samples); + sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples, + buf[0], buf[1]); + + /* Close data receiving. */ + if (devc->remaining_samples == 0) { + if (buf[num_samples] != H4032L_END_PACKET_MAGIC) + sr_err("Mismatch magic number of end poll."); + + abort_acquisition(devc); + free_transfer(transfer); + } else { + if (((devc->submitted_transfers - 1) * H4032L_DATA_BUFFER_SIZE) < + (int32_t)(devc->remaining_samples * sizeof(uint32_t))) + resubmit_transfer(transfer); + else + free_transfer(transfer); + } +} + void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) { - const struct sr_dev_inst *sdi = transfer->user_data; - struct dev_context *devc = sdi->priv; - struct drv_context *drvc = sdi->driver->context; + struct sr_dev_inst *const sdi = transfer->user_data; + struct dev_context *const devc = sdi->priv; struct sr_usb_dev_inst *usb = sdi->conn; gboolean cmd = FALSE; - uint32_t max_samples = 512 / sizeof(uint32_t); - uint32_t *buffer; + uint32_t max_samples = transfer->actual_length / sizeof(uint32_t); + uint32_t *buf; struct h4032l_status_packet *status; - struct sr_datafeed_packet packet; - struct sr_datafeed_logic logic; - uint32_t number_samples; + uint32_t num_samples; int ret; - if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { - sr_err("%s error: %d.", __func__, transfer->status); + /* + * If acquisition has already ended, just free any queued up + * transfers that come in. + */ + if (devc->acq_aborted) { + free_transfer(transfer); return; } - buffer = (uint32_t *)transfer->buffer; + if (transfer->status != LIBUSB_TRANSFER_COMPLETED) + sr_dbg("%s error: %d.", __func__, transfer->status); + + buf = (uint32_t *)transfer->buffer; switch (devc->status) { case H4032L_STATUS_IDLE: @@ -94,15 +251,12 @@ void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) * First Transfer as next. */ status = (struct h4032l_status_packet *)transfer->buffer; - if (status->magic != H4032L_STATUS_PACKET_MAGIC) { - devc->status = H4032L_STATUS_CMD_STATUS; - devc->cmd_pkt.cmd = CMD_STATUS; - cmd = TRUE; - } else if (status->status == 2) { + if (status->magic != H4032L_STATUS_PACKET_MAGIC) + devc->status = H4032L_STATUS_RESPONSE_STATUS; + else if (status->status == 2) devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE; - } else { + else devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY; - } break; case H4032L_STATUS_RESPONSE_STATUS_RETRY: devc->status = H4032L_STATUS_CMD_STATUS; @@ -116,39 +270,35 @@ void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) break; case H4032L_STATUS_CMD_GET: devc->status = H4032L_STATUS_FIRST_TRANSFER; + /* Trigger has been captured. */ + std_session_send_df_header(sdi); break; case H4032L_STATUS_FIRST_TRANSFER: - if (buffer[0] != H4032L_START_PACKET_MAGIC) { - sr_err("Mismatch magic number of start poll."); - devc->status = H4032L_STATUS_IDLE; + /* Drop packets until H4032L_START_PACKET_MAGIC. */ + if (buf[0] != H4032L_START_PACKET_MAGIC) { + sr_dbg("Mismatch magic number of start poll."); break; } devc->status = H4032L_STATUS_TRANSFER; max_samples--; - buffer++; - break; + buf++; + /* Fallthrough. */ case H4032L_STATUS_TRANSFER: - number_samples = (devc->remaining_samples < max_samples) ? devc->remaining_samples : max_samples; - devc->remaining_samples -= number_samples; - packet.type = SR_DF_LOGIC; - packet.payload = &logic; - logic.length = number_samples * sizeof(uint32_t); - logic.unitsize = sizeof(uint32_t); - logic.data = buffer; - sr_session_send(sdi, &packet); + num_samples = MIN(devc->remaining_samples, max_samples); + devc->remaining_samples -= num_samples; + send_data(sdi, buf, num_samples); sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples, - buffer[0], buffer[1]); - if (devc->remaining_samples == 0) { - std_session_send_df_end(sdi); - usb_source_remove(sdi->session, drvc->sr_ctx); - devc->status = H4032L_STATUS_IDLE; - if (buffer[number_samples] != H4032L_END_PACKET_MAGIC) - sr_err("Mismatch magic number of end poll."); - } + buf[0], buf[1]); break; } - if (devc->status != H4032L_STATUS_IDLE) { + /* Start data receiving. */ + if (devc->status == H4032L_STATUS_TRANSFER) { + if ((ret = h4032l_start_data_transfers(sdi)) != SR_OK) { + sr_err("Can not start data transfers: %d", ret); + devc->status = H4032L_STATUS_IDLE; + } + } else if (devc->status != H4032L_STATUS_IDLE) { if (cmd) { /* Setup new USB cmd packet, reuse transfer object. */ sr_dbg("New command: %d.", devc->status); @@ -156,14 +306,14 @@ void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt, sizeof(struct h4032l_cmd_pkt), - h4032l_usb_callback, (void *)sdi, - H4032L_USB_TIMEOUT); + h4032l_usb_callback, + (void *)sdi, H4032L_USB_TIMEOUT); } else { /* Setup new USB poll packet, reuse transfer object. */ sr_dbg("Poll: %d.", devc->status); libusb_fill_bulk_transfer(transfer, usb->devhdl, 6 | LIBUSB_ENDPOINT_IN, - devc->buffer, ARRAY_SIZE(devc->buffer), + devc->buf, ARRAY_SIZE(devc->buf), h4032l_usb_callback, (void *)sdi, H4032L_USB_TIMEOUT); } @@ -178,7 +328,7 @@ void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) } if (devc->status == H4032L_STATUS_IDLE) - libusb_free_transfer(transfer); + free_transfer(transfer); } uint16_t h4032l_voltage2pwm(double voltage) @@ -204,14 +354,78 @@ uint16_t h4032l_voltage2pwm(double voltage) return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0)); } +SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi) +{ + struct dev_context *devc = sdi->priv; + struct sr_usb_dev_inst *usb = sdi->conn; + struct libusb_transfer *transfer; + uint8_t *buf; + unsigned int num_transfers; + unsigned int i; + int ret; + + devc->submitted_transfers = 0; + + /* + * Set number of data transfers regarding to size of buffer. + * FPGA version 0 can't transfer multiple transfers at once. + */ + if ((num_transfers = MIN(devc->remaining_samples * sizeof(uint32_t) / + H4032L_DATA_BUFFER_SIZE, devc->fpga_version ? + H4032L_DATA_TRANSFER_MAX_NUM : 1)) == 0) + num_transfers = 1; + + g_free(devc->transfers); + devc->transfers = g_malloc(sizeof(*devc->transfers) * num_transfers); + devc->num_transfers = num_transfers; + + for (i = 0; i < num_transfers; i++) { + buf = g_malloc(H4032L_DATA_BUFFER_SIZE); + transfer = libusb_alloc_transfer(0); + + libusb_fill_bulk_transfer(transfer, usb->devhdl, + 6 | LIBUSB_ENDPOINT_IN, + buf, H4032L_DATA_BUFFER_SIZE, + h4032l_data_transfer_callback, + (void *)sdi, H4032L_USB_TIMEOUT); + + /* Send prepared usb packet. */ + if ((ret = libusb_submit_transfer(transfer)) != 0) { + sr_err("Failed to submit transfer: %s.", + libusb_error_name(ret)); + libusb_free_transfer(transfer); + g_free(buf); + abort_acquisition(devc); + return SR_ERR; + } + devc->transfers[i] = transfer; + devc->submitted_transfers++; + } + + return SR_OK; +} + SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi) { struct dev_context *devc = sdi->priv; struct sr_usb_dev_inst *usb = sdi->conn; struct libusb_transfer *transfer; + unsigned char buf[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; int ret; - /* Send configure command to arm the logic analyzer. */ + /* Send reset command to arm the logic analyzer. */ + if ((ret = libusb_control_transfer(usb->devhdl, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET, + 0x00, 0x00, buf, ARRAY_SIZE(buf), H4032L_USB_TIMEOUT)) < 0) { + sr_err("Failed to send vendor request %s.", + libusb_error_name(ret)); + return SR_ERR; + } + + /* Wait for reset vendor request. */ + g_usleep(20 * 1000); + + /* Send configure command. */ devc->cmd_pkt.cmd = CMD_CONFIGURE; devc->status = H4032L_STATUS_CMD_CONFIGURE; devc->remaining_samples = devc->cmd_pkt.sample_size; @@ -224,12 +438,23 @@ SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi) (void *)sdi, H4032L_USB_TIMEOUT); if ((ret = libusb_submit_transfer(transfer)) != 0) { - sr_err("Failed to submit transfer: %s.", libusb_error_name(ret)); + sr_err("Failed to submit transfer: %s.", + libusb_error_name(ret)); libusb_free_transfer(transfer); return SR_ERR; } - std_session_send_df_header(sdi); + devc->transfers = g_malloc0(sizeof(*devc->transfers)); + devc->submitted_transfers++; + devc->num_transfers = 1; + devc->transfers[0] = transfer; + + return SR_OK; +} + +SR_PRIV int h4032l_stop(struct sr_dev_inst *sdi) +{ + abort_acquisition(sdi->priv); return SR_OK; } @@ -260,8 +485,9 @@ SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi) if ((sdi->status == SR_ST_INITIALIZING) || (sdi->status == SR_ST_INACTIVE)) { /* Check device by its physical USB bus/port address. */ - usb_get_port_path(devlist[i], connection_id, - sizeof(connection_id)); + if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) + continue; + if (strcmp(sdi->connection_id, connection_id)) /* This is not the one. */ continue; @@ -274,7 +500,7 @@ SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi) * upload, so we don't know the address yet. */ usb->address = - libusb_get_device_address(devlist[i]); + libusb_get_device_address(devlist[i]); } else { sr_err("Failed to open device: %s.", libusb_error_name(ret)); @@ -289,3 +515,46 @@ SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi) libusb_free_device_list(devlist, 1); return ret; } + +SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi) +{ + struct dev_context *devc = sdi->priv; + struct sr_usb_dev_inst *usb = sdi->conn; + struct h4032l_status_packet *status; + int transferred; + int i, ret; + + /* Set command to status. */ + devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC; + devc->cmd_pkt.cmd = CMD_STATUS; + + /* Send status request. */ + if ((ret = libusb_bulk_transfer(usb->devhdl, + 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt, + sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) { + sr_err("Unable to send FPGA version request: %s.", + libusb_error_name(ret)); + return SR_ERR; + } + + /* Attempt to get FGPA version. */ + for (i = 0; i < 10; i++) { + if ((ret = libusb_bulk_transfer(usb->devhdl, + 6 | LIBUSB_ENDPOINT_IN, devc->buf, + ARRAY_SIZE(devc->buf), &transferred, H4032L_USB_TIMEOUT)) < 0) { + sr_err("Unable to receive FPGA version: %s.", + libusb_error_name(ret)); + return SR_ERR; + } + status = (struct h4032l_status_packet *)devc->buf; + if (status->magic == H4032L_STATUS_PACKET_MAGIC) { + sr_dbg("FPGA version: 0x%x.", status->fpga_version); + devc->fpga_version = status->fpga_version; + return SR_OK; + } + } + + sr_err("Unable to get FPGA version."); + + return SR_ERR; +}