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/>.
25 /* Submit an already filled-in USB transfer.
27 static int submit_transfer(struct dev_context *devc,
28 struct libusb_transfer *xfer)
32 ret = libusb_submit_transfer(xfer);
35 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
36 devc->transfer_error = TRUE;
43 /* Set up transfer for the next register in a write sequence.
45 static void next_reg_write(struct acquisition_state *acq)
47 struct regval *regval;
49 regval = &acq->reg_sequence[acq->reg_seq_pos];
51 acq->xfer_buf_out[0] = LWLA_WORD(CMD_WRITE_REG);
52 acq->xfer_buf_out[1] = LWLA_WORD(regval->reg);
53 acq->xfer_buf_out[2] = LWLA_WORD_0(regval->val);
54 acq->xfer_buf_out[3] = LWLA_WORD_1(regval->val);
56 acq->xfer_out->length = 4 * sizeof(acq->xfer_buf_out[0]);
59 /* Set up transfer for the next register in a read sequence.
61 static void next_reg_read(struct acquisition_state *acq)
65 addr = acq->reg_sequence[acq->reg_seq_pos].reg;
67 acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_REG);
68 acq->xfer_buf_out[1] = LWLA_WORD(addr);
70 acq->xfer_out->length = 2 * sizeof(acq->xfer_buf_out[0]);
73 /* Decode the response to a register read request.
75 static int read_reg_response(struct acquisition_state *acq)
79 if (acq->xfer_in->actual_length != 4) {
80 sr_err("Received size %d doesn't match expected size 4.",
81 acq->xfer_in->actual_length);
84 value = LWLA_TO_UINT32(acq->xfer_buf_in[0]);
85 acq->reg_sequence[acq->reg_seq_pos].val = value;
90 /* Enter a new state and submit the corresponding request to the device.
92 static int submit_request(const struct sr_dev_inst *sdi,
93 enum protocol_state state)
95 struct dev_context *devc;
96 struct acquisition_state *acq;
100 acq = devc->acquisition;
104 acq->xfer_out->length = 0;
105 acq->reg_seq_pos = 0;
106 acq->reg_seq_len = 0;
108 /* Perform the model-specific action for the new state. */
109 ret = (*devc->model->prepare_request)(sdi);
112 devc->transfer_error = TRUE;
116 if (acq->reg_seq_pos < acq->reg_seq_len) {
117 if ((state & STATE_EXPECT_RESPONSE) != 0)
123 return submit_transfer(devc, acq->xfer_out);
126 /* Evaluate and act on the response to a capture status request.
128 static void handle_status_response(const struct sr_dev_inst *sdi)
130 struct dev_context *devc;
131 struct acquisition_state *acq;
132 unsigned int old_status;
135 acq = devc->acquisition;
136 old_status = acq->status;
138 if ((*devc->model->handle_response)(sdi) != SR_OK) {
139 devc->transfer_error = TRUE;
142 devc->state = STATE_STATUS_WAIT;
144 sr_spew("Captured %u words, %" PRIu64 " ms, status 0x%02X.",
145 acq->mem_addr_fill, acq->duration_now, acq->status);
147 if ((~old_status & acq->status & STATUS_TRIGGERED) != 0)
148 sr_info("Capture triggered.");
150 if (acq->duration_now >= acq->duration_max) {
151 sr_dbg("Time limit reached, stopping capture.");
152 submit_request(sdi, STATE_STOP_CAPTURE);
153 } else if ((acq->status & STATUS_TRIGGERED) == 0) {
154 sr_spew("Waiting for trigger.");
155 } else if ((acq->status & STATUS_MEM_AVAIL) == 0) {
156 sr_dbg("Capture memory filled.");
157 submit_request(sdi, STATE_LENGTH_REQUEST);
158 } else if ((acq->status & STATUS_CAPTURING) != 0) {
159 sr_spew("Sampling in progress.");
163 /* Evaluate and act on the response to a capture length request.
165 static void handle_length_response(const struct sr_dev_inst *sdi)
167 struct dev_context *devc;
168 struct acquisition_state *acq;
171 acq = devc->acquisition;
173 if ((*devc->model->handle_response)(sdi) != SR_OK) {
174 devc->transfer_error = TRUE;
177 acq->rle = RLE_STATE_DATA;
180 acq->samples_done = 0;
181 acq->mem_addr_done = acq->mem_addr_next;
184 if (acq->mem_addr_next >= acq->mem_addr_stop) {
185 submit_request(sdi, STATE_READ_FINISH);
188 sr_dbg("%u words in capture buffer.",
189 acq->mem_addr_stop - acq->mem_addr_next);
191 submit_request(sdi, STATE_READ_PREPARE);
194 /* Evaluate and act on the response to a capture memory read request.
196 static void handle_read_response(const struct sr_dev_inst *sdi)
198 struct dev_context *devc;
199 struct acquisition_state *acq;
200 struct sr_datafeed_packet packet;
201 struct sr_datafeed_logic logic;
202 unsigned int end_addr;
205 acq = devc->acquisition;
207 /* Prepare session packet. */
208 packet.type = SR_DF_LOGIC;
209 packet.payload = &logic;
210 logic.unitsize = (devc->model->num_channels + 7) / 8;
211 logic.data = acq->out_packet;
213 end_addr = MIN(acq->mem_addr_next, acq->mem_addr_stop);
217 * Repeatedly call the model-specific read response handler until
218 * all data received in the transfer has been accounted for.
220 while (!devc->cancel_requested
221 && (acq->run_len > 0 || acq->mem_addr_done < end_addr)
222 && acq->samples_done < acq->samples_max) {
224 if ((*devc->model->handle_response)(sdi) != SR_OK) {
225 devc->transfer_error = TRUE;
228 if (acq->out_index * logic.unitsize >= PACKET_SIZE) {
229 /* Send off full logic packet. */
230 logic.length = acq->out_index * logic.unitsize;
231 sr_session_send(sdi, &packet);
236 if (!devc->cancel_requested
237 && acq->samples_done < acq->samples_max
238 && acq->mem_addr_next < acq->mem_addr_stop) {
239 /* Request the next block. */
240 submit_request(sdi, STATE_READ_REQUEST);
244 /* Send partially filled packet as it is the last one. */
245 if (!devc->cancel_requested && acq->out_index > 0) {
246 logic.length = acq->out_index * logic.unitsize;
247 sr_session_send(sdi, &packet);
250 submit_request(sdi, STATE_READ_FINISH);
253 /* Destroy and unset the acquisition state record.
255 static void clear_acquisition_state(const struct sr_dev_inst *sdi)
257 struct dev_context *devc;
258 struct acquisition_state *acq;
261 acq = devc->acquisition;
263 devc->acquisition = NULL;
266 libusb_free_transfer(acq->xfer_out);
267 libusb_free_transfer(acq->xfer_in);
272 /* USB I/O source callback.
274 static int transfer_event(int fd, int revents, void *cb_data)
276 const struct sr_dev_inst *sdi;
277 struct dev_context *devc;
278 struct drv_context *drvc;
286 drvc = sdi->driver->context;
289 return G_SOURCE_REMOVE;
291 /* Handle pending USB events without blocking. */
294 ret = libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
297 sr_err("Event handling failed: %s.", libusb_error_name(ret));
298 devc->transfer_error = TRUE;
301 if (!devc->transfer_error && devc->state == STATE_STATUS_WAIT) {
302 if (devc->cancel_requested)
303 submit_request(sdi, STATE_STOP_CAPTURE);
304 else if (revents == 0) /* status poll timeout */
305 submit_request(sdi, STATE_STATUS_REQUEST);
308 /* Stop processing events if an error occurred on a transfer. */
309 if (devc->transfer_error)
310 devc->state = STATE_IDLE;
312 if (devc->state != STATE_IDLE)
313 return G_SOURCE_CONTINUE;
315 sr_info("Acquisition stopped.");
317 /* We are done, clean up and send end packet to session bus. */
318 clear_acquisition_state(sdi);
319 std_session_send_df_end(sdi);
321 return G_SOURCE_REMOVE;
324 /* USB output transfer completion callback.
326 static void LIBUSB_CALL transfer_out_completed(struct libusb_transfer *transfer)
328 const struct sr_dev_inst *sdi;
329 struct dev_context *devc;
330 struct acquisition_state *acq;
332 sdi = transfer->user_data;
334 acq = devc->acquisition;
336 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
337 sr_err("Transfer to device failed (state %d): %s.",
338 devc->state, libusb_error_name(transfer->status));
339 devc->transfer_error = TRUE;
343 /* If this was a read request, wait for the response. */
344 if ((devc->state & STATE_EXPECT_RESPONSE) != 0) {
345 submit_transfer(devc, acq->xfer_in);
348 if (acq->reg_seq_pos < acq->reg_seq_len)
349 acq->reg_seq_pos++; /* register write completed */
351 /* Repeat until all queued registers have been written. */
352 if (acq->reg_seq_pos < acq->reg_seq_len && !devc->cancel_requested) {
354 submit_transfer(devc, acq->xfer_out);
358 switch (devc->state) {
359 case STATE_START_CAPTURE:
360 sr_info("Acquisition started.");
362 if (!devc->cancel_requested)
363 devc->state = STATE_STATUS_WAIT;
365 submit_request(sdi, STATE_STOP_CAPTURE);
367 case STATE_STOP_CAPTURE:
368 if (!devc->cancel_requested)
369 submit_request(sdi, STATE_LENGTH_REQUEST);
371 devc->state = STATE_IDLE;
373 case STATE_READ_PREPARE:
374 if (acq->mem_addr_next < acq->mem_addr_stop && !devc->cancel_requested)
375 submit_request(sdi, STATE_READ_REQUEST);
377 submit_request(sdi, STATE_READ_FINISH);
379 case STATE_READ_FINISH:
380 devc->state = STATE_IDLE;
383 sr_err("Unexpected device state %d.", devc->state);
384 devc->transfer_error = TRUE;
389 /* USB input transfer completion callback.
391 static void LIBUSB_CALL transfer_in_completed(struct libusb_transfer *transfer)
393 const struct sr_dev_inst *sdi;
394 struct dev_context *devc;
395 struct acquisition_state *acq;
397 sdi = transfer->user_data;
399 acq = devc->acquisition;
401 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
402 sr_err("Transfer from device failed (state %d): %s.",
403 devc->state, libusb_error_name(transfer->status));
404 devc->transfer_error = TRUE;
407 if ((devc->state & STATE_EXPECT_RESPONSE) == 0) {
408 sr_err("Unexpected completion of input transfer (state %d).",
410 devc->transfer_error = TRUE;
414 if (acq->reg_seq_pos < acq->reg_seq_len && !devc->cancel_requested) {
415 /* Complete register read sequence. */
416 if (read_reg_response(acq) != SR_OK) {
417 devc->transfer_error = TRUE;
420 /* Repeat until all queued registers have been read. */
421 if (++acq->reg_seq_pos < acq->reg_seq_len) {
423 submit_transfer(devc, acq->xfer_out);
428 switch (devc->state) {
429 case STATE_STATUS_REQUEST:
430 if (devc->cancel_requested)
431 submit_request(sdi, STATE_STOP_CAPTURE);
433 handle_status_response(sdi);
435 case STATE_LENGTH_REQUEST:
436 if (devc->cancel_requested)
437 submit_request(sdi, STATE_READ_FINISH);
439 handle_length_response(sdi);
441 case STATE_READ_REQUEST:
442 handle_read_response(sdi);
445 sr_err("Unexpected device state %d.", devc->state);
446 devc->transfer_error = TRUE;
451 /* Set up the acquisition state record.
453 static int init_acquisition_state(const struct sr_dev_inst *sdi)
455 struct dev_context *devc;
456 struct sr_usb_dev_inst *usb;
457 struct acquisition_state *acq;
462 if (devc->acquisition) {
463 sr_err("Acquisition still in progress?");
466 if (devc->cfg_clock_source == CLOCK_INTERNAL && devc->samplerate == 0) {
467 sr_err("Samplerate not set.");
471 acq = g_try_malloc0(sizeof(struct acquisition_state));
473 return SR_ERR_MALLOC;
475 acq->xfer_in = libusb_alloc_transfer(0);
478 return SR_ERR_MALLOC;
480 acq->xfer_out = libusb_alloc_transfer(0);
481 if (!acq->xfer_out) {
482 libusb_free_transfer(acq->xfer_in);
484 return SR_ERR_MALLOC;
487 libusb_fill_bulk_transfer(acq->xfer_out, usb->devhdl, EP_COMMAND,
488 (unsigned char *)acq->xfer_buf_out, 0,
489 &transfer_out_completed,
490 (struct sr_dev_inst *)sdi, USB_TIMEOUT_MS);
492 libusb_fill_bulk_transfer(acq->xfer_in, usb->devhdl, EP_REPLY,
493 (unsigned char *)acq->xfer_buf_in,
494 sizeof(acq->xfer_buf_in),
495 &transfer_in_completed,
496 (struct sr_dev_inst *)sdi, USB_TIMEOUT_MS);
498 if (devc->limit_msec > 0) {
499 acq->duration_max = devc->limit_msec;
500 sr_info("Acquisition time limit %" PRIu64 " ms.",
503 acq->duration_max = MAX_LIMIT_MSEC;
505 if (devc->limit_samples > 0) {
506 acq->samples_max = devc->limit_samples;
507 sr_info("Acquisition sample count limit %" PRIu64 ".",
508 devc->limit_samples);
510 acq->samples_max = MAX_LIMIT_SAMPLES;
512 if (devc->cfg_clock_source == CLOCK_INTERNAL) {
513 sr_info("Internal clock, samplerate %" PRIu64 ".",
515 /* Ramp up clock speed to enable samplerates above 100 MS/s. */
516 acq->clock_boost = (devc->samplerate > SR_MHZ(100));
518 /* If only one of the limits is set, derive the other one. */
519 if (devc->limit_msec == 0 && devc->limit_samples > 0)
520 acq->duration_max = devc->limit_samples
521 * 1000 / devc->samplerate + 1;
522 else if (devc->limit_samples == 0 && devc->limit_msec > 0)
523 acq->samples_max = devc->limit_msec
524 * devc->samplerate / 1000;
526 acq->clock_boost = TRUE;
528 if (devc->cfg_clock_edge == EDGE_POSITIVE)
529 sr_info("External clock, rising edge.");
531 sr_info("External clock, falling edge.");
534 acq->rle_enabled = devc->cfg_rle;
535 devc->acquisition = acq;
540 SR_PRIV int lwla_start_acquisition(const struct sr_dev_inst *sdi)
542 struct drv_context *drvc;
543 struct dev_context *devc;
545 const int poll_interval_ms = 100;
547 drvc = sdi->driver->context;
550 if (devc->state != STATE_IDLE) {
551 sr_err("Not in idle state, cannot start acquisition.");
554 devc->cancel_requested = FALSE;
555 devc->transfer_error = FALSE;
557 ret = init_acquisition_state(sdi);
561 ret = (*devc->model->setup_acquisition)(sdi);
563 sr_err("Failed to set up device for acquisition.");
564 clear_acquisition_state(sdi);
567 /* Register event source for asynchronous USB I/O. */
568 ret = usb_source_add(sdi->session, drvc->sr_ctx, poll_interval_ms,
569 &transfer_event, (struct sr_dev_inst *)sdi);
571 clear_acquisition_state(sdi);
574 ret = submit_request(sdi, STATE_START_CAPTURE);
577 ret = std_session_send_df_header(sdi);
580 usb_source_remove(sdi->session, drvc->sr_ctx);
581 clear_acquisition_state(sdi);