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 finish_acquisition(struct sr_dev_inst *sdi)
44 struct drv_context *drvc = sdi->driver->context;
46 std_session_send_df_end(sdi);
47 usb_source_remove(sdi->session, drvc->sr_ctx);
50 static void free_transfer(struct libusb_transfer *transfer)
52 struct sr_dev_inst *sdi = transfer->user_data;
53 struct dev_context *devc = sdi->priv;
55 transfer->buffer = NULL;
56 libusb_free_transfer(transfer);
57 devc->usb_transfer = NULL;
59 finish_acquisition(sdi);
62 static void resubmit_transfer(struct libusb_transfer *transfer)
66 if ((ret = libusb_submit_transfer(transfer)) == LIBUSB_SUCCESS)
69 sr_err("%s: %s", __func__, libusb_error_name(ret));
70 free_transfer(transfer);
73 SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
76 struct drv_context *drvc;
81 drvc = (struct drv_context *)cb_data;
83 tv.tv_sec = tv.tv_usec = 0;
84 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
89 void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer)
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;
94 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
95 struct sr_datafeed_packet packet;
96 struct sr_datafeed_logic logic;
98 uint32_t number_samples;
101 * If acquisition has already ended, just free any queued up
102 * transfer that come in.
104 if (devc->acq_aborted) {
105 free_transfer(transfer);
109 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
110 sr_dbg("%s error: %d.", __func__, transfer->status);
112 /* Cancel pending transfers. */
113 if (transfer->actual_length == 0) {
114 resubmit_transfer(transfer);
118 buffer = (uint32_t *)transfer->buffer;
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);
128 sr_session_send(sdi, &packet);
129 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
130 buffer[0], buffer[1]);
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.");
140 resubmit_transfer(transfer);
144 void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
146 const struct sr_dev_inst *sdi = transfer->user_data;
147 struct dev_context *devc = sdi->priv;
148 struct sr_usb_dev_inst *usb = sdi->conn;
149 gboolean cmd = FALSE;
150 uint32_t max_samples = transfer->actual_length / sizeof(uint32_t);
152 struct h4032l_status_packet *status;
153 struct sr_datafeed_packet packet;
154 struct sr_datafeed_logic logic;
155 uint32_t number_samples;
159 * If acquisition has already ended, just free any queued up
160 * transfers that come in.
162 if (devc->acq_aborted) {
163 free_transfer(transfer);
167 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
168 sr_dbg("%s error: %d.", __func__, transfer->status);
170 buffer = (uint32_t *)transfer->buffer;
172 switch (devc->status) {
173 case H4032L_STATUS_IDLE:
174 sr_err("USB callback called in idle.");
176 case H4032L_STATUS_CMD_CONFIGURE:
177 /* Select status request as next. */
179 devc->cmd_pkt.cmd = CMD_STATUS;
180 devc->status = H4032L_STATUS_CMD_STATUS;
182 case H4032L_STATUS_CMD_STATUS:
183 /* Select status request as next. */
184 devc->status = H4032L_STATUS_RESPONSE_STATUS;
186 case H4032L_STATUS_RESPONSE_STATUS:
188 * Check magic and if status is complete, then select
189 * First Transfer as next.
191 status = (struct h4032l_status_packet *)transfer->buffer;
192 if (status->magic != H4032L_STATUS_PACKET_MAGIC)
193 devc->status = H4032L_STATUS_RESPONSE_STATUS;
194 else if (status->status == 2)
195 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
197 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
199 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
200 devc->status = H4032L_STATUS_CMD_STATUS;
201 devc->cmd_pkt.cmd = CMD_STATUS;
204 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
205 devc->status = H4032L_STATUS_CMD_GET;
206 devc->cmd_pkt.cmd = CMD_GET;
209 case H4032L_STATUS_CMD_GET:
210 devc->status = H4032L_STATUS_FIRST_TRANSFER;
211 /* Trigger has been captured. */
212 std_session_send_df_header(sdi);
214 case H4032L_STATUS_FIRST_TRANSFER:
215 /* Drop packets until H4032L_START_PACKET_MAGIC. */
216 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
217 sr_dbg("Mismatch magic number of start poll.");
220 devc->status = H4032L_STATUS_TRANSFER;
224 case H4032L_STATUS_TRANSFER:
225 number_samples = (devc->remaining_samples < max_samples) ?
226 devc->remaining_samples : max_samples;
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);
233 sr_session_send(sdi, &packet);
234 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
235 buffer[0], buffer[1]);
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;
245 } else if (devc->status != H4032L_STATUS_IDLE) {
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),
254 (void *)sdi, H4032L_USB_TIMEOUT);
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),
262 (void *)sdi, H4032L_USB_TIMEOUT);
264 /* Send prepared USB packet. */
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;
274 if (devc->status == H4032L_STATUS_IDLE)
275 free_transfer(transfer);
278 uint16_t h4032l_voltage2pwm(double voltage)
281 * word PwmA - channel A Vref PWM value, pseudocode:
282 * -6V < ThresholdVoltage < +6V
283 * Vref = 1.8 - ThresholdVoltage
288 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
292 voltage = 1.8 - voltage;
295 else if (voltage < -5.0)
298 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
301 SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi)
303 struct dev_context *devc = sdi->priv;
304 struct sr_usb_dev_inst *usb = sdi->conn;
305 struct libusb_transfer *transfer;
308 transfer = libusb_alloc_transfer(0);
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);
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;
323 if (devc->status == H4032L_STATUS_IDLE)
324 free_transfer(transfer);
326 return (ret ? SR_ERR : SR_OK);
329 SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
331 struct dev_context *devc = sdi->priv;
332 struct sr_usb_dev_inst *usb = sdi->conn;
333 struct libusb_transfer *transfer;
334 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
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) {
341 sr_err("Failed to send vendor request %s.",
342 libusb_error_name(ret));
346 /* Wait for reset vendor request. */
349 /* Send configure command. */
350 devc->cmd_pkt.cmd = CMD_CONFIGURE;
351 devc->status = H4032L_STATUS_CMD_CONFIGURE;
352 devc->remaining_samples = devc->cmd_pkt.sample_size;
354 transfer = libusb_alloc_transfer(0);
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);
361 if ((ret = libusb_submit_transfer(transfer)) != 0) {
362 sr_err("Failed to submit transfer: %s.",
363 libusb_error_name(ret));
364 libusb_free_transfer(transfer);
371 SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
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];
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));
387 for (i = 0; i < device_count; i++) {
388 libusb_get_device_descriptor(devlist[i], &des);
390 if (des.idVendor != H4032L_USB_VENDOR ||
391 des.idProduct != H4032L_USB_PRODUCT)
394 if ((sdi->status == SR_ST_INITIALIZING) ||
395 (sdi->status == SR_ST_INACTIVE)) {
396 /* Check device by its physical USB bus/port address. */
397 if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
400 if (strcmp(sdi->connection_id, connection_id))
401 /* This is not the one. */
405 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
406 if (usb->address == 0xff)
408 * First time we touch this device after FW
409 * upload, so we don't know the address yet.
412 libusb_get_device_address(devlist[i]);
414 sr_err("Failed to open device: %s.",
415 libusb_error_name(ret));
424 libusb_free_device_list(devlist, 1);
428 SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi)
430 struct dev_context *devc = sdi->priv;
431 struct sr_usb_dev_inst *usb = sdi->conn;
432 struct h4032l_status_packet *status;
436 /* Set command to status. */
437 devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC;
438 devc->cmd_pkt.cmd = CMD_STATUS;
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));
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));
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;
466 sr_err("Unable to get FPGA version.");