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 /* Status polling interval during acquisition.
27 #define POLL_INTERVAL 100 /* ms */
29 /* Submit an already filled-in USB transfer.
31 static int submit_transfer(struct dev_context *devc,
32 struct libusb_transfer *xfer)
36 ret = libusb_submit_transfer(xfer);
39 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
40 devc->transfer_error = TRUE;
47 /* Set up transfer for the next register in a write sequence.
49 static void next_reg_write(struct acquisition_state *acq)
51 struct regval *regval;
53 regval = &acq->reg_sequence[acq->reg_seq_pos];
55 acq->xfer_buf_out[0] = LWLA_WORD(CMD_WRITE_REG);
56 acq->xfer_buf_out[1] = LWLA_WORD(regval->reg);
57 acq->xfer_buf_out[2] = LWLA_WORD_0(regval->val);
58 acq->xfer_buf_out[3] = LWLA_WORD_1(regval->val);
60 acq->xfer_out->length = 4 * sizeof(acq->xfer_buf_out[0]);
63 /* Set up transfer for the next register in a read sequence.
65 static void next_reg_read(struct acquisition_state *acq)
69 addr = acq->reg_sequence[acq->reg_seq_pos].reg;
71 acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_REG);
72 acq->xfer_buf_out[1] = LWLA_WORD(addr);
74 acq->xfer_out->length = 2 * sizeof(acq->xfer_buf_out[0]);
77 /* Decode the response to a register read request.
79 static int read_reg_response(struct acquisition_state *acq)
83 if (acq->xfer_in->actual_length != 4) {
84 sr_err("Received size %d doesn't match expected size 4.",
85 acq->xfer_in->actual_length);
88 value = LWLA_TO_UINT32(acq->xfer_buf_in[0]);
89 acq->reg_sequence[acq->reg_seq_pos].val = value;
94 /* Enter a new state and submit the corresponding request to the device.
96 static int submit_request(const struct sr_dev_inst *sdi,
97 enum protocol_state state)
99 struct dev_context *devc;
100 struct acquisition_state *acq;
104 acq = devc->acquisition;
108 acq->xfer_out->length = 0;
109 acq->reg_seq_pos = 0;
110 acq->reg_seq_len = 0;
112 /* Perform the model-specific action for the new state. */
113 ret = (*devc->model->prepare_request)(sdi);
116 devc->transfer_error = TRUE;
120 if (acq->reg_seq_pos < acq->reg_seq_len) {
121 if ((state & STATE_EXPECT_RESPONSE) != 0)
127 return submit_transfer(devc, acq->xfer_out);
130 /* Evaluate and act on the response to a capture status request.
132 static void handle_status_response(const struct sr_dev_inst *sdi)
134 struct dev_context *devc;
135 struct acquisition_state *acq;
136 unsigned int old_status;
139 acq = devc->acquisition;
140 old_status = acq->status;
142 if ((*devc->model->handle_response)(sdi) != SR_OK) {
143 devc->transfer_error = TRUE;
146 devc->state = STATE_STATUS_WAIT;
148 sr_spew("Captured %u words, %" PRIu64 " ms, status 0x%02X.",
149 acq->mem_addr_fill, acq->duration_now, acq->status);
151 if ((~old_status & acq->status & STATUS_TRIGGERED) != 0)
152 sr_info("Capture triggered.");
154 if (acq->duration_now >= acq->duration_max) {
155 sr_dbg("Time limit reached, stopping capture.");
156 submit_request(sdi, STATE_STOP_CAPTURE);
157 } else if ((acq->status & STATUS_TRIGGERED) == 0) {
158 sr_spew("Waiting for trigger.");
159 } else if ((acq->status & STATUS_MEM_AVAIL) == 0) {
160 sr_dbg("Capture memory filled.");
161 submit_request(sdi, STATE_LENGTH_REQUEST);
162 } else if ((acq->status & STATUS_CAPTURING) != 0) {
163 sr_spew("Sampling in progress.");
167 /* Evaluate and act on the response to a capture length request.
169 static void handle_length_response(const struct sr_dev_inst *sdi)
171 struct dev_context *devc;
172 struct acquisition_state *acq;
175 acq = devc->acquisition;
177 if ((*devc->model->handle_response)(sdi) != SR_OK) {
178 devc->transfer_error = TRUE;
181 acq->rle = RLE_STATE_DATA;
184 acq->samples_done = 0;
185 acq->mem_addr_done = acq->mem_addr_next;
188 if (acq->mem_addr_next >= acq->mem_addr_stop) {
189 submit_request(sdi, STATE_READ_FINISH);
192 sr_dbg("%u words in capture buffer.",
193 acq->mem_addr_stop - acq->mem_addr_next);
195 submit_request(sdi, STATE_READ_PREPARE);
198 /* Evaluate and act on the response to a capture memory read request.
200 static void handle_read_response(const struct sr_dev_inst *sdi)
202 struct dev_context *devc;
203 struct acquisition_state *acq;
204 struct sr_datafeed_packet packet;
205 struct sr_datafeed_logic logic;
206 unsigned int end_addr;
209 acq = devc->acquisition;
211 /* Prepare session packet. */
212 packet.type = SR_DF_LOGIC;
213 packet.payload = &logic;
214 logic.unitsize = (devc->model->num_channels + 7) / 8;
215 logic.data = acq->out_packet;
217 end_addr = MIN(acq->mem_addr_next, acq->mem_addr_stop);
220 * Repeatedly call the model-specific read response handler until
221 * all data received in the transfer has been accounted for.
223 while (!devc->cancel_requested
224 && (acq->run_len > 0 || acq->mem_addr_done < end_addr)
225 && acq->samples_done < acq->samples_max) {
227 if ((*devc->model->handle_response)(sdi) != SR_OK) {
228 devc->transfer_error = TRUE;
231 if (acq->out_index * logic.unitsize >= PACKET_SIZE) {
232 /* Send off full logic packet. */
233 logic.length = acq->out_index * logic.unitsize;
234 sr_session_send(sdi, &packet);
239 if (!devc->cancel_requested
240 && acq->samples_done < acq->samples_max
241 && acq->mem_addr_next < acq->mem_addr_stop) {
242 /* Request the next block. */
243 submit_request(sdi, STATE_READ_REQUEST);
247 /* Send partially filled packet as it is the last one. */
248 if (!devc->cancel_requested && acq->out_index > 0) {
249 logic.length = acq->out_index * logic.unitsize;
250 sr_session_send(sdi, &packet);
253 submit_request(sdi, STATE_READ_FINISH);
256 /* Destroy and unset the acquisition state record.
258 static void clear_acquisition_state(const struct sr_dev_inst *sdi)
260 struct dev_context *devc;
261 struct acquisition_state *acq;
264 acq = devc->acquisition;
266 devc->acquisition = NULL;
269 libusb_free_transfer(acq->xfer_out);
270 libusb_free_transfer(acq->xfer_in);
275 /* USB I/O source callback.
277 static int transfer_event(int fd, int revents, void *cb_data)
279 const struct sr_dev_inst *sdi;
280 struct dev_context *devc;
281 struct drv_context *drvc;
283 struct sr_datafeed_packet packet;
290 drvc = sdi->driver->context;
293 return G_SOURCE_REMOVE;
295 /* Handle pending USB events without blocking. */
298 ret = libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
301 sr_err("Event handling failed: %s.", libusb_error_name(ret));
302 devc->transfer_error = TRUE;
305 if (!devc->transfer_error && devc->state == STATE_STATUS_WAIT) {
306 if (devc->cancel_requested)
307 submit_request(sdi, STATE_STOP_CAPTURE);
308 else if (revents == 0) /* status poll timeout */
309 submit_request(sdi, STATE_STATUS_REQUEST);
312 /* Stop processing events if an error occurred on a transfer. */
313 if (devc->transfer_error)
314 devc->state = STATE_IDLE;
316 if (devc->state != STATE_IDLE)
317 return G_SOURCE_CONTINUE;
319 sr_info("Acquisition stopped.");
321 /* We are done, clean up and send end packet to session bus. */
322 clear_acquisition_state(sdi);
324 packet.type = SR_DF_END;
325 packet.payload = NULL;
326 sr_session_send(sdi, &packet);
328 return G_SOURCE_REMOVE;
331 /* USB output transfer completion callback.
333 static void LIBUSB_CALL transfer_out_completed(struct libusb_transfer *transfer)
335 const struct sr_dev_inst *sdi;
336 struct dev_context *devc;
337 struct acquisition_state *acq;
339 sdi = transfer->user_data;
341 acq = devc->acquisition;
343 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
344 sr_err("Transfer to device failed: %d.", transfer->status);
345 devc->transfer_error = TRUE;
349 /* If this was a read request, wait for the response. */
350 if ((devc->state & STATE_EXPECT_RESPONSE) != 0) {
351 submit_transfer(devc, acq->xfer_in);
354 if (acq->reg_seq_pos < acq->reg_seq_len)
355 acq->reg_seq_pos++; /* register write completed */
357 /* Repeat until all queued registers have been written. */
358 if (acq->reg_seq_pos < acq->reg_seq_len && !devc->cancel_requested) {
360 submit_transfer(devc, acq->xfer_out);
364 switch (devc->state) {
365 case STATE_START_CAPTURE:
366 sr_info("Acquisition started.");
368 if (!devc->cancel_requested)
369 devc->state = STATE_STATUS_WAIT;
371 submit_request(sdi, STATE_STOP_CAPTURE);
373 case STATE_STOP_CAPTURE:
374 if (!devc->cancel_requested)
375 submit_request(sdi, STATE_LENGTH_REQUEST);
377 devc->state = STATE_IDLE;
379 case STATE_READ_PREPARE:
380 if (acq->mem_addr_next < acq->mem_addr_stop && !devc->cancel_requested)
381 submit_request(sdi, STATE_READ_REQUEST);
383 submit_request(sdi, STATE_READ_FINISH);
385 case STATE_READ_FINISH:
386 devc->state = STATE_IDLE;
389 sr_err("Unexpected device state %d.", devc->state);
390 devc->transfer_error = TRUE;
395 /* USB input transfer completion callback.
397 static void LIBUSB_CALL transfer_in_completed(struct libusb_transfer *transfer)
399 const struct sr_dev_inst *sdi;
400 struct dev_context *devc;
401 struct acquisition_state *acq;
403 sdi = transfer->user_data;
405 acq = devc->acquisition;
407 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
408 sr_err("Transfer from device failed (state %d): %s.",
409 devc->state, libusb_error_name(transfer->status));
410 devc->transfer_error = TRUE;
413 if ((devc->state & STATE_EXPECT_RESPONSE) == 0) {
414 sr_err("Unexpected completion of input transfer (state %d).",
416 devc->transfer_error = TRUE;
420 if (acq->reg_seq_pos < acq->reg_seq_len && !devc->cancel_requested) {
421 /* Complete register read sequence. */
422 if (read_reg_response(acq) != SR_OK) {
423 devc->transfer_error = TRUE;
426 /* Repeat until all queued registers have been read. */
427 if (++acq->reg_seq_pos < acq->reg_seq_len) {
429 submit_transfer(devc, acq->xfer_out);
434 switch (devc->state) {
435 case STATE_STATUS_REQUEST:
436 if (devc->cancel_requested)
437 submit_request(sdi, STATE_STOP_CAPTURE);
439 handle_status_response(sdi);
441 case STATE_LENGTH_REQUEST:
442 if (devc->cancel_requested)
443 submit_request(sdi, STATE_READ_FINISH);
445 handle_length_response(sdi);
447 case STATE_READ_REQUEST:
448 handle_read_response(sdi);
451 sr_err("Unexpected device state %d.", devc->state);
452 devc->transfer_error = TRUE;
457 /* Set up the acquisition state record.
459 static int init_acquisition_state(const struct sr_dev_inst *sdi)
461 struct dev_context *devc;
462 struct sr_usb_dev_inst *usb;
463 struct acquisition_state *acq;
468 if (devc->acquisition) {
469 sr_err("Acquisition still in progress?");
472 if (devc->cfg_clock_source == CLOCK_INTERNAL && devc->samplerate == 0) {
473 sr_err("Samplerate not set.");
477 acq = g_try_malloc0(sizeof(struct acquisition_state));
479 return SR_ERR_MALLOC;
481 acq->xfer_in = libusb_alloc_transfer(0);
484 return SR_ERR_MALLOC;
486 acq->xfer_out = libusb_alloc_transfer(0);
487 if (!acq->xfer_out) {
488 libusb_free_transfer(acq->xfer_in);
490 return SR_ERR_MALLOC;
493 libusb_fill_bulk_transfer(acq->xfer_out, usb->devhdl, EP_COMMAND,
494 (unsigned char *)acq->xfer_buf_out, 0,
495 &transfer_out_completed,
496 (struct sr_dev_inst *)sdi, USB_TIMEOUT_MS);
498 libusb_fill_bulk_transfer(acq->xfer_in, usb->devhdl, EP_REPLY,
499 (unsigned char *)acq->xfer_buf_in,
500 sizeof(acq->xfer_buf_in),
501 &transfer_in_completed,
502 (struct sr_dev_inst *)sdi, USB_TIMEOUT_MS);
504 if (devc->limit_msec > 0) {
505 acq->duration_max = devc->limit_msec;
506 sr_info("Acquisition time limit %" PRIu64 " ms.",
509 acq->duration_max = MAX_LIMIT_MSEC;
511 if (devc->limit_samples > 0) {
512 acq->samples_max = devc->limit_samples;
513 sr_info("Acquisition sample count limit %" PRIu64 ".",
514 devc->limit_samples);
516 acq->samples_max = MAX_LIMIT_SAMPLES;
518 if (devc->cfg_clock_source == CLOCK_INTERNAL) {
519 sr_info("Internal clock, samplerate %" PRIu64 ".",
521 /* Ramp up clock speed to enable samplerates above 100 MS/s. */
522 acq->clock_boost = (devc->samplerate > SR_MHZ(100));
524 /* If only one of the limits is set, derive the other one. */
525 if (devc->limit_msec == 0 && devc->limit_samples > 0)
526 acq->duration_max = devc->limit_samples
527 * 1000 / devc->samplerate + 1;
528 else if (devc->limit_samples == 0 && devc->limit_msec > 0)
529 acq->samples_max = devc->limit_msec
530 * devc->samplerate / 1000;
532 acq->clock_boost = TRUE;
534 if (devc->cfg_clock_edge == EDGE_POSITIVE)
535 sr_info("External clock, rising edge.");
537 sr_info("External clock, falling edge.");
540 acq->rle_enabled = devc->cfg_rle;
541 devc->acquisition = acq;
546 SR_PRIV int lwla_start_acquisition(const struct sr_dev_inst *sdi)
548 struct drv_context *drvc;
549 struct dev_context *devc;
552 drvc = sdi->driver->context;
555 if (devc->state != STATE_IDLE) {
556 sr_err("Not in idle state, cannot start acquisition.");
559 devc->cancel_requested = FALSE;
560 devc->transfer_error = FALSE;
562 ret = init_acquisition_state(sdi);
566 ret = (*devc->model->setup_acquisition)(sdi);
568 sr_err("Failed to set up device for acquisition.");
569 clear_acquisition_state(sdi);
572 /* Register event source for asynchronous USB I/O. */
573 ret = usb_source_add(sdi->session, drvc->sr_ctx, POLL_INTERVAL,
574 &transfer_event, (struct sr_dev_inst *)sdi);
576 clear_acquisition_state(sdi);
579 ret = submit_request(sdi, STATE_START_CAPTURE);
582 /* Send header packet to the session bus. */
583 ret = std_session_send_df_header(sdi, LOG_PREFIX);
586 usb_source_remove(sdi->session, drvc->sr_ctx);
587 clear_acquisition_state(sdi);