2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* Bit mask covering all 34 channels. */
24 #define ALL_CHANNELS_MASK (((uint64_t)1 << NUM_PROBES) - 1)
26 /* Bit mask for the RLE repeat-count-follows flag. */
27 #define RLE_FLAG_LEN_FOLLOWS ((uint64_t)1 << 35)
29 /* Start address of capture status memory area to read. */
30 #define CAP_STAT_ADDR 5
32 /* Number of 64-bit words read from the capture status memory. */
33 #define CAP_STAT_LEN 5
35 /* The bitstream filenames are indexed by the clock source enumeration.
37 static const char *const bitstream_map[] = {
38 FIRMWARE_DIR "/sysclk-lwla1034-off.bitstream",
39 FIRMWARE_DIR "/sysclk-lwla1034-int.bitstream",
40 FIRMWARE_DIR "/sysclk-lwla1034-extpos.bitstream",
41 FIRMWARE_DIR "/sysclk-lwla1034-extneg.bitstream",
44 /* Submit an already filled-in USB transfer.
46 static int submit_transfer(struct dev_context *devc,
47 struct libusb_transfer *xfer)
51 ret = libusb_submit_transfer(xfer);
54 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
55 devc->transfer_error = TRUE;
62 /* Set up the LWLA in preparation for an acquisition session.
64 static int capture_setup(const struct sr_dev_inst *sdi)
66 struct dev_context *devc;
67 uint64_t divider_count;
68 uint64_t memory_limit;
69 uint16_t command[3 + 10*4];
73 command[0] = LWLA_WORD(CMD_CAP_SETUP);
74 command[1] = LWLA_WORD(0); /* address */
75 command[2] = LWLA_WORD(10); /* length */
77 command[3] = LWLA_WORD_0(devc->channel_mask);
78 command[4] = LWLA_WORD_1(devc->channel_mask);
79 command[5] = LWLA_WORD_2(devc->channel_mask);
80 command[6] = LWLA_WORD_3(devc->channel_mask);
82 /* Set the clock divide counter maximum for samplerates of up to
83 * 100 MHz. At the highest samplerate of 125 MHz the clock divider
86 if (devc->samplerate > 0 && devc->samplerate < SR_MHZ(100))
87 divider_count = SR_MHZ(100) / devc->samplerate - 1;
91 command[7] = LWLA_WORD_0(divider_count);
92 command[8] = LWLA_WORD_1(divider_count);
93 command[9] = LWLA_WORD_2(divider_count);
94 command[10] = LWLA_WORD_3(divider_count);
96 command[11] = LWLA_WORD_0(devc->trigger_values);
97 command[12] = LWLA_WORD_1(devc->trigger_values);
98 command[13] = LWLA_WORD_2(devc->trigger_values);
99 command[14] = LWLA_WORD_3(devc->trigger_values);
101 command[15] = LWLA_WORD_0(devc->trigger_edge_mask);
102 command[16] = LWLA_WORD_1(devc->trigger_edge_mask);
103 command[17] = LWLA_WORD_2(devc->trigger_edge_mask);
104 command[18] = LWLA_WORD_3(devc->trigger_edge_mask);
106 command[19] = LWLA_WORD_0(devc->trigger_mask);
107 command[20] = LWLA_WORD_1(devc->trigger_mask);
108 command[21] = LWLA_WORD_2(devc->trigger_mask);
109 command[22] = LWLA_WORD_3(devc->trigger_mask);
111 /* Set the capture memory full threshold. This is slightly less
112 * than the actual maximum, most likely in order to compensate for
115 memory_limit = MEMORY_DEPTH - 16;
117 command[23] = LWLA_WORD_0(memory_limit);
118 command[24] = LWLA_WORD_1(memory_limit);
119 command[25] = LWLA_WORD_2(memory_limit);
120 command[26] = LWLA_WORD_3(memory_limit);
122 /* Fill remaining 64-bit words with zeroes. */
123 memset(&command[27], 0, 16 * sizeof(uint16_t));
125 return lwla_send_command(sdi->conn, command, G_N_ELEMENTS(command));
128 /* Issue a register write command as an asynchronous USB transfer.
130 static int issue_write_reg(const struct sr_dev_inst *sdi,
131 unsigned int reg, unsigned int value)
133 struct dev_context *devc;
134 struct acquisition_state *acq;
137 acq = devc->acquisition;
139 acq->xfer_buf_out[0] = LWLA_WORD(CMD_WRITE_REG);
140 acq->xfer_buf_out[1] = LWLA_WORD(reg);
141 acq->xfer_buf_out[2] = LWLA_WORD_0(value);
142 acq->xfer_buf_out[3] = LWLA_WORD_1(value);
144 acq->xfer_out->length = 4 * sizeof(uint16_t);
146 return submit_transfer(devc, acq->xfer_out);
149 /* Issue a register write command as an asynchronous USB transfer for the
150 * next register/value pair of the currently active register write sequence.
152 static int issue_next_write_reg(const struct sr_dev_inst *sdi)
154 struct dev_context *devc;
155 struct regval_pair *regval;
160 if (devc->reg_write_pos >= devc->reg_write_len) {
161 sr_err("Already written all registers in sequence.");
164 regval = &devc->reg_write_seq[devc->reg_write_pos];
166 ret = issue_write_reg(sdi, regval->reg, regval->val);
170 ++devc->reg_write_pos;
174 /* Issue a capture status request as an asynchronous USB transfer.
176 static void request_capture_status(const struct sr_dev_inst *sdi)
178 struct dev_context *devc;
179 struct acquisition_state *acq;
182 acq = devc->acquisition;
184 acq->xfer_buf_out[0] = LWLA_WORD(CMD_CAP_STATUS);
185 acq->xfer_buf_out[1] = LWLA_WORD(CAP_STAT_ADDR);
186 acq->xfer_buf_out[2] = LWLA_WORD(CAP_STAT_LEN);
188 acq->xfer_out->length = 3 * sizeof(uint16_t);
190 if (submit_transfer(devc, acq->xfer_out) == SR_OK)
191 devc->state = STATE_STATUS_REQUEST;
194 /* Issue a request for the capture buffer fill level as
195 * an asynchronous USB transfer.
197 static void request_capture_length(const struct sr_dev_inst *sdi)
199 struct dev_context *devc;
200 struct acquisition_state *acq;
203 acq = devc->acquisition;
205 acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_REG);
206 acq->xfer_buf_out[1] = LWLA_WORD(REG_MEM_FILL);
208 acq->xfer_out->length = 2 * sizeof(uint16_t);
210 if (submit_transfer(devc, acq->xfer_out) == SR_OK)
211 devc->state = STATE_LENGTH_REQUEST;
214 /* Initiate the capture memory read operation: Reset the acquisition state
215 * and start a sequence of register writes in order to set up the device for
216 * reading from the capture buffer.
218 static void issue_read_start(const struct sr_dev_inst *sdi)
220 struct dev_context *devc;
221 struct acquisition_state *acq;
222 struct regval_pair *regvals;
225 acq = devc->acquisition;
227 /* Reset RLE state. */
228 acq->rle = RLE_STATE_DATA;
232 acq->captured_samples = 0;
233 acq->transferred_samples = 0;
235 /* For some reason, the start address is 4 rather than 0. */
236 acq->mem_addr_done = 4;
237 acq->mem_addr_next = 4;
238 acq->mem_addr_stop = acq->mem_addr_fill;
240 /* Byte offset into the packet output buffer. */
243 regvals = devc->reg_write_seq;
245 regvals[0].reg = REG_DIV_BYPASS;
248 regvals[1].reg = REG_MEM_CTRL2;
251 regvals[2].reg = REG_MEM_CTRL4;
254 devc->reg_write_pos = 0;
255 devc->reg_write_len = 3;
257 if (issue_next_write_reg(sdi) == SR_OK)
258 devc->state = STATE_READ_PREPARE;
261 static void issue_read_end(const struct sr_dev_inst *sdi)
263 struct dev_context *devc;
267 if (issue_write_reg(sdi, REG_DIV_BYPASS, 0) == SR_OK)
268 devc->state = STATE_READ_END;
271 /* Decode an incoming reponse to a buffer fill level request and act on it
272 * as appropriate. Note that this function changes the device context state.
274 static void process_capture_length(const struct sr_dev_inst *sdi)
276 struct dev_context *devc;
277 struct acquisition_state *acq;
280 acq = devc->acquisition;
282 if (acq->xfer_in->actual_length != 4) {
283 sr_err("Received size %d doesn't match expected size 4.",
284 acq->xfer_in->actual_length);
285 devc->transfer_error = TRUE;
288 acq->mem_addr_fill = LWLA_READ32(acq->xfer_buf_in);
290 sr_dbg("%lu words in capture buffer.",
291 (unsigned long)acq->mem_addr_fill);
293 if (acq->mem_addr_fill > 0 && sdi->status == SR_ST_ACTIVE)
294 issue_read_start(sdi);
299 /* Initiate a sequence of register write commands with the effect of
300 * cancelling a running capture operation. This sets a new device state
301 * if issuing the first command succeeds.
303 static void issue_stop_capture(const struct sr_dev_inst *sdi)
305 struct dev_context *devc;
306 struct regval_pair *regvals;
310 if (devc->stopping_in_progress)
313 regvals = devc->reg_write_seq;
315 regvals[0].reg = REG_CMD_CTRL2;
318 regvals[1].reg = REG_CMD_CTRL3;
321 regvals[2].reg = REG_CMD_CTRL4;
324 regvals[3].reg = REG_CMD_CTRL1;
327 regvals[4].reg = REG_DIV_BYPASS;
330 devc->reg_write_pos = 0;
331 devc->reg_write_len = 5;
333 if (issue_next_write_reg(sdi) == SR_OK) {
334 devc->stopping_in_progress = TRUE;
335 devc->state = STATE_STOP_CAPTURE;
339 /* Decode an incoming capture status reponse and act on it as appropriate.
340 * Note that this function changes the device state.
342 static void process_capture_status(const struct sr_dev_inst *sdi)
344 struct dev_context *devc;
345 struct acquisition_state *acq;
348 acq = devc->acquisition;
350 if (acq->xfer_in->actual_length != CAP_STAT_LEN * 8) {
351 sr_err("Received size %d doesn't match expected size %d.",
352 acq->xfer_in->actual_length, CAP_STAT_LEN * 8);
353 devc->transfer_error = TRUE;
357 /* TODO: Find out the actual bit width of these fields as stored
358 * in the FPGA. These fields are definitely less than 64 bit wide
359 * internally, and the unused bits occasionally even contain garbage.
361 acq->mem_addr_fill = LWLA_READ32(&acq->xfer_buf_in[0]);
362 acq->captured_samples = LWLA_READ32(&acq->xfer_buf_in[8])
364 acq->capture_flags = LWLA_READ32(&acq->xfer_buf_in[16])
367 sr_spew("Captured %lu words, %" PRIu64 " samples, flags 0x%02X",
368 (unsigned long)acq->mem_addr_fill,
369 acq->captured_samples, acq->capture_flags);
371 if (acq->captured_samples >= devc->limit_samples) {
372 issue_stop_capture(sdi);
375 devc->state = STATE_STATUS_WAIT;
377 if ((acq->capture_flags & STATUS_TRIGGERED) == 0) {
378 sr_spew("Waiting for trigger.");
379 } else if ((acq->capture_flags & STATUS_MEM_AVAIL) == 0) {
380 sr_dbg("Capture memory filled.");
381 request_capture_length(sdi);
382 } else if ((acq->capture_flags & STATUS_CAPTURING) != 0) {
383 sr_spew("Sampling in progress.");
387 /* Issue a capture buffer read request as an asynchronous USB transfer.
388 * The address and size of the memory area to read are derived from the
389 * current acquisition state.
391 static void request_read_mem(const struct sr_dev_inst *sdi)
393 struct dev_context *devc;
394 struct acquisition_state *acq;
398 acq = devc->acquisition;
400 if (acq->mem_addr_next >= acq->mem_addr_stop)
403 /* Always read a multiple of 8 device words. */
404 count = (acq->mem_addr_stop - acq->mem_addr_next + 7) / 8 * 8;
405 count = MIN(count, READ_CHUNK_LEN);
407 acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_MEM);
408 acq->xfer_buf_out[1] = LWLA_WORD_0(acq->mem_addr_next);
409 acq->xfer_buf_out[2] = LWLA_WORD_1(acq->mem_addr_next);
410 acq->xfer_buf_out[3] = LWLA_WORD_0(count);
411 acq->xfer_buf_out[4] = LWLA_WORD_1(count);
413 acq->xfer_out->length = 5 * sizeof(uint16_t);
415 if (submit_transfer(devc, acq->xfer_out) == SR_OK) {
416 acq->mem_addr_next += count;
417 devc->state = STATE_READ_REQUEST;
421 /* Send a packet of logic samples to the session bus. The payload is taken
422 * from the acquisition state. The return value indicates whether to stop
423 * reading more samples.
425 static gboolean send_logic_packet(const struct sr_dev_inst *sdi)
428 struct dev_context *devc;
429 struct acquisition_state *acq;
430 struct sr_datafeed_packet packet;
431 struct sr_datafeed_logic logic;
435 acq = devc->acquisition;
437 if (acq->transferred_samples >= devc->limit_samples)
440 packet.type = SR_DF_LOGIC;
441 packet.payload = &logic;
442 logic.unitsize = UNIT_SIZE;
443 logic.data = acq->out_packet;
444 logic.length = acq->out_offset;
446 samples = acq->out_offset / UNIT_SIZE;
449 /* Cut the packet short if necessary. */
450 if (acq->transferred_samples + samples >= devc->limit_samples) {
451 samples = devc->limit_samples - acq->transferred_samples;
452 logic.length = samples * UNIT_SIZE;
455 acq->transferred_samples += samples;
458 /* Send off logic datafeed packet. */
459 sr_session_send(sdi, &packet);
464 /* Demangle and decompress incoming sample data from the capture buffer.
465 * The data chunk is taken from the acquisition state, and is expected to
466 * contain a multiple of 8 device words.
467 * All data currently in the acquisition buffer will be processed. Packets
468 * of decoded samples are sent off to the session bus whenever the output
469 * buffer becomes full while decoding.
471 static int process_sample_data(const struct sr_dev_inst *sdi)
475 uint64_t high_nibbles;
477 struct dev_context *devc;
478 struct acquisition_state *acq;
483 size_t in_words_left;
487 acq = devc->acquisition;
489 if (acq->mem_addr_done >= acq->mem_addr_stop
490 || acq->transferred_samples >= devc->limit_samples)
493 in_words_left = MIN(acq->mem_addr_stop - acq->mem_addr_done,
495 expect_len = LWLA1034_MEMBUF_LEN(in_words_left) * sizeof(uint16_t);
496 actual_len = acq->xfer_in->actual_length;
498 if (actual_len != expect_len) {
499 sr_err("Received size %lu does not match expected size %lu.",
500 (unsigned long)actual_len, (unsigned long)expect_len);
501 devc->transfer_error = TRUE;
504 acq->mem_addr_done += in_words_left;
505 slice = acq->xfer_buf_in;
506 si = 0; /* word index within slice */
509 sample = acq->sample;
510 /* Expand run-length samples into session packet. */
511 for (run_len = acq->run_len; run_len > 0; --run_len) {
512 out_p = &acq->out_packet[acq->out_offset];
513 out_p[0] = sample & 0xFF;
514 out_p[1] = (sample >> 8) & 0xFF;
515 out_p[2] = (sample >> 16) & 0xFF;
516 out_p[3] = (sample >> 24) & 0xFF;
517 out_p[4] = (sample >> 32) & 0xFF;
518 acq->out_offset += UNIT_SIZE;
520 /* Send out packet if it is full. */
521 if (acq->out_offset > PACKET_SIZE - UNIT_SIZE)
522 if (send_logic_packet(sdi))
523 return SR_OK; /* sample limit reached */
527 if (in_words_left == 0)
528 break; /* done with current chunk */
530 /* Now work on the current slice. */
531 high_nibbles = LWLA_READ32(&slice[8 * 2]);
532 word = LWLA_READ32(&slice[si * 2]);
533 word |= (high_nibbles << (4 * si + 4)) & ((uint64_t)0xF << 32);
535 if (acq->rle == RLE_STATE_DATA) {
536 acq->sample = word & ALL_CHANNELS_MASK;
537 acq->run_len = ((word >> NUM_PROBES) & 1) + 1;
538 if (word & RLE_FLAG_LEN_FOLLOWS)
539 acq->rle = RLE_STATE_LEN;
541 acq->run_len += word << 1;
542 acq->rle = RLE_STATE_DATA;
545 /* Move to next word. */
553 /* Send out partially filled packet if it is the last one. */
554 if (acq->mem_addr_done >= acq->mem_addr_stop && acq->out_offset > 0)
555 send_logic_packet(sdi);
560 /* Finish an acquisition session. This sends the end packet to the session
561 * bus and removes the listener for asynchronous USB transfers.
563 static void end_acquisition(struct sr_dev_inst *sdi)
565 struct drv_context *drvc;
566 struct dev_context *devc;
567 struct sr_datafeed_packet packet;
569 drvc = sdi->driver->priv;
572 if (devc->state == STATE_IDLE)
575 devc->state = STATE_IDLE;
577 /* Remove USB file descriptors from polling. */
578 usb_source_remove(drvc->sr_ctx);
580 packet.type = SR_DF_END;
581 sr_session_send(sdi, &packet);
583 lwla_free_acquisition_state(devc->acquisition);
584 devc->acquisition = NULL;
586 sdi->status = SR_ST_ACTIVE;
589 /* USB output transfer completion callback.
591 static void receive_transfer_out(struct libusb_transfer *transfer)
593 struct sr_dev_inst *sdi;
594 struct dev_context *devc;
596 sdi = transfer->user_data;
599 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
600 sr_err("Transfer to device failed: %d.", transfer->status);
601 devc->transfer_error = TRUE;
605 if (devc->reg_write_pos < devc->reg_write_len) {
606 issue_next_write_reg(sdi);
608 switch (devc->state) {
609 case STATE_START_CAPTURE:
610 devc->state = STATE_STATUS_WAIT;
612 case STATE_STATUS_REQUEST:
613 devc->state = STATE_STATUS_RESPONSE;
614 submit_transfer(devc, devc->acquisition->xfer_in);
616 case STATE_STOP_CAPTURE:
617 if (sdi->status == SR_ST_ACTIVE)
618 request_capture_length(sdi);
620 end_acquisition(sdi);
622 case STATE_LENGTH_REQUEST:
623 devc->state = STATE_LENGTH_RESPONSE;
624 submit_transfer(devc, devc->acquisition->xfer_in);
626 case STATE_READ_PREPARE:
627 request_read_mem(sdi);
629 case STATE_READ_REQUEST:
630 devc->state = STATE_READ_RESPONSE;
631 submit_transfer(devc, devc->acquisition->xfer_in);
634 end_acquisition(sdi);
637 sr_err("Unexpected device state %d.", devc->state);
643 /* USB input transfer completion callback.
645 static void receive_transfer_in(struct libusb_transfer *transfer)
647 struct sr_dev_inst *sdi;
648 struct dev_context *devc;
649 struct acquisition_state *acq;
651 sdi = transfer->user_data;
653 acq = devc->acquisition;
655 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
656 sr_err("Transfer from device failed: %d.", transfer->status);
657 devc->transfer_error = TRUE;
661 switch (devc->state) {
662 case STATE_STATUS_RESPONSE:
663 process_capture_status(sdi);
665 case STATE_LENGTH_RESPONSE:
666 process_capture_length(sdi);
668 case STATE_READ_RESPONSE:
669 if (process_sample_data(sdi) == SR_OK
670 && acq->mem_addr_next < acq->mem_addr_stop
671 && acq->transferred_samples < devc->limit_samples)
672 request_read_mem(sdi);
677 sr_err("Unexpected device state %d.", devc->state);
682 /* Initialize the LWLA. This downloads a bitstream into the FPGA
683 * and executes a simple device test sequence.
685 SR_PRIV int lwla_init_device(const struct sr_dev_inst *sdi)
687 struct dev_context *devc;
693 /* Select internal clock if it hasn't been set yet */
694 if (devc->selected_clock_source == CLOCK_SOURCE_NONE)
695 devc->selected_clock_source = CLOCK_SOURCE_INT;
697 /* Force reload of bitstream */
698 devc->cur_clock_source = CLOCK_SOURCE_NONE;
700 ret = lwla_set_clock_source(sdi);
705 ret = lwla_write_reg(sdi->conn, REG_CMD_CTRL2, 100);
709 ret = lwla_read_reg(sdi->conn, REG_CMD_CTRL1, &value);
712 sr_info("Received test word 0x%08X back.", value);
713 if (value != 0x12345678)
716 ret = lwla_read_reg(sdi->conn, REG_CMD_CTRL4, &value);
719 sr_info("Received test word 0x%08X back.", value);
720 if (value != 0x12345678)
723 ret = lwla_read_reg(sdi->conn, REG_CMD_CTRL3, &value);
726 sr_info("Received test word 0x%08X back.", value);
727 if (value != 0x87654321)
733 /* Select the LWLA clock source. If the clock source changed from the
734 * previous setting, this will download a new bitstream to the FPGA.
736 SR_PRIV int lwla_set_clock_source(const struct sr_dev_inst *sdi)
738 struct dev_context *devc;
740 enum clock_source selected;
743 selected = devc->selected_clock_source;
745 if (devc->cur_clock_source != selected) {
746 devc->cur_clock_source = CLOCK_SOURCE_NONE;
748 if (selected >= 0 && selected < G_N_ELEMENTS(bitstream_map)) {
749 ret = lwla_send_bitstream(sdi->conn,
750 bitstream_map[selected]);
752 devc->cur_clock_source = selected;
759 /* Configure the LWLA in preparation for an acquisition session.
761 SR_PRIV int lwla_setup_acquisition(const struct sr_dev_inst *sdi)
763 struct dev_context *devc;
764 struct sr_usb_dev_inst *usb;
765 struct regval_pair regvals[7];
771 regvals[0].reg = REG_MEM_CTRL2;
774 regvals[1].reg = REG_MEM_CTRL2;
777 regvals[2].reg = REG_CMD_CTRL2;
780 regvals[3].reg = REG_CMD_CTRL3;
781 regvals[3].val = 0x74;
783 regvals[4].reg = REG_CMD_CTRL4;
786 regvals[5].reg = REG_CMD_CTRL1;
789 regvals[6].reg = REG_DIV_BYPASS;
790 regvals[6].val = (devc->samplerate > SR_MHZ(100)) ? 1 : 0;
792 ret = lwla_write_regs(usb, regvals, G_N_ELEMENTS(regvals));
796 return capture_setup(sdi);
799 /* Start the capture operation on the LWLA device. Beginning with this
800 * function, all USB transfers will be asynchronous until the end of the
801 * acquisition session.
803 SR_PRIV int lwla_start_acquisition(const struct sr_dev_inst *sdi)
805 struct dev_context *devc;
806 struct sr_usb_dev_inst *usb;
807 struct acquisition_state *acq;
808 struct regval_pair *regvals;
812 acq = devc->acquisition;
814 libusb_fill_bulk_transfer(acq->xfer_out, usb->devhdl, EP_COMMAND,
815 (unsigned char *)acq->xfer_buf_out, 0,
816 &receive_transfer_out,
817 (struct sr_dev_inst *)sdi, USB_TIMEOUT);
819 libusb_fill_bulk_transfer(acq->xfer_in, usb->devhdl, EP_REPLY,
820 (unsigned char *)acq->xfer_buf_in,
821 sizeof acq->xfer_buf_in,
822 &receive_transfer_in,
823 (struct sr_dev_inst *)sdi, USB_TIMEOUT);
825 regvals = devc->reg_write_seq;
827 regvals[0].reg = REG_CMD_CTRL2;
830 regvals[1].reg = REG_CMD_CTRL3;
833 regvals[2].reg = REG_CMD_CTRL4;
836 regvals[3].reg = REG_CMD_CTRL1;
839 devc->reg_write_pos = 0;
840 devc->reg_write_len = 4;
842 devc->state = STATE_START_CAPTURE;
844 return issue_next_write_reg(sdi);
847 /* Allocate an acquisition state object.
849 SR_PRIV struct acquisition_state *lwla_alloc_acquisition_state(void)
851 struct acquisition_state *acq;
853 acq = g_try_new0(struct acquisition_state, 1);
855 sr_err("Acquisition state malloc failed.");
859 acq->xfer_in = libusb_alloc_transfer(0);
861 sr_err("Transfer malloc failed.");
866 acq->xfer_out = libusb_alloc_transfer(0);
867 if (!acq->xfer_out) {
868 sr_err("Transfer malloc failed.");
869 libusb_free_transfer(acq->xfer_in);
877 /* Deallocate an acquisition state object.
879 SR_PRIV void lwla_free_acquisition_state(struct acquisition_state *acq)
882 libusb_free_transfer(acq->xfer_out);
883 libusb_free_transfer(acq->xfer_in);
888 /* USB I/O source callback.
890 SR_PRIV int lwla_receive_data(int fd, int revents, void *cb_data)
892 struct sr_dev_inst *sdi;
893 struct dev_context *devc;
894 struct drv_context *drvc;
902 drvc = sdi->driver->priv;
907 /* No timeout: return immediately. */
911 ret = libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
914 sr_err("Event handling failed: %s.", libusb_error_name(ret));
916 /* If no event flags are set the timeout must have expired. */
917 if (revents == 0 && devc->state == STATE_STATUS_WAIT) {
918 if (sdi->status == SR_ST_STOPPING)
919 issue_stop_capture(sdi);
921 request_capture_status(sdi);
924 /* Check if an error occurred on a transfer. */
925 if (devc->transfer_error)
926 end_acquisition(sdi);