]> sigrok.org Git - libsigrok.git/blob - src/hardware/sysclk-lwla/protocol.c
sysclk-lwla: Implement support for LWLA1016
[libsigrok.git] / src / hardware / sysclk-lwla / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Daniel Elstner <daniel.kitta@gmail.com>
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21 #include <string.h>
22 #include "lwla.h"
23 #include "protocol.h"
24
25 /* Status polling interval during acquisition.
26  */
27 #define POLL_INTERVAL 100 /* ms */
28
29 /* Submit an already filled-in USB transfer.
30  */
31 static int submit_transfer(struct dev_context *devc,
32                            struct libusb_transfer *xfer)
33 {
34         int ret;
35
36         ret = libusb_submit_transfer(xfer);
37
38         if (ret != 0) {
39                 sr_err("Submit transfer failed: %s.", libusb_error_name(ret));
40                 devc->transfer_error = TRUE;
41                 return SR_ERR;
42         }
43
44         return SR_OK;
45 }
46
47 /* Set up transfer for the next register in a write sequence.
48  */
49 static void next_reg_write(struct acquisition_state *acq)
50 {
51         struct regval *regval;
52
53         regval = &acq->reg_sequence[acq->reg_seq_pos];
54
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);
59
60         acq->xfer_out->length = 4 * sizeof(acq->xfer_buf_out[0]);
61 }
62
63 /* Set up transfer for the next register in a read sequence.
64  */
65 static void next_reg_read(struct acquisition_state *acq)
66 {
67         unsigned int addr;
68
69         addr = acq->reg_sequence[acq->reg_seq_pos].reg;
70
71         acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_REG);
72         acq->xfer_buf_out[1] = LWLA_WORD(addr);
73
74         acq->xfer_out->length = 2 * sizeof(acq->xfer_buf_out[0]);
75 }
76
77 /* Decode the response to a register read request.
78  */
79 static int read_reg_response(struct acquisition_state *acq)
80 {
81         uint32_t value;
82
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);
86                 return SR_ERR;
87         }
88         value = LWLA_TO_UINT32(acq->xfer_buf_in[0]);
89         acq->reg_sequence[acq->reg_seq_pos].val = value;
90
91         return SR_OK;
92 }
93
94 /* Enter a new state and submit the corresponding request to the device.
95  */
96 static int submit_request(const struct sr_dev_inst *sdi,
97                           enum protocol_state state)
98 {
99         struct dev_context *devc;
100         struct acquisition_state *acq;
101         int ret;
102
103         devc = sdi->priv;
104         acq  = devc->acquisition;
105
106         devc->state = state;
107
108         acq->xfer_out->length = 0;
109         acq->reg_seq_pos = 0;
110         acq->reg_seq_len = 0;
111
112         /* Perform the model-specific action for the new state. */
113         ret = (*devc->model->prepare_request)(sdi);
114
115         if (ret != SR_OK) {
116                 devc->transfer_error = TRUE;
117                 return ret;
118         }
119
120         if (acq->reg_seq_pos < acq->reg_seq_len) {
121                 if ((state & STATE_EXPECT_RESPONSE) != 0)
122                         next_reg_read(acq);
123                 else
124                         next_reg_write(acq);
125         }
126
127         return submit_transfer(devc, acq->xfer_out);
128 }
129
130 /* Evaluate and act on the response to a capture status request.
131  */
132 static void handle_status_response(const struct sr_dev_inst *sdi)
133 {
134         struct dev_context *devc;
135         struct acquisition_state *acq;
136         unsigned int old_status;
137
138         devc = sdi->priv;
139         acq  = devc->acquisition;
140         old_status = acq->status;
141
142         if ((*devc->model->handle_response)(sdi) != SR_OK) {
143                 devc->transfer_error = TRUE;
144                 return;
145         }
146         devc->state = STATE_STATUS_WAIT;
147
148         sr_spew("Captured %zu words, %" PRIu64 " ms, status 0x%02X.",
149                 acq->mem_addr_fill, acq->duration_now, acq->status);
150
151         if ((~old_status & acq->status & STATUS_TRIGGERED) != 0)
152                 sr_info("Capture triggered.");
153
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.");
164         }
165 }
166
167 /* Evaluate and act on the response to a capture length request.
168  */
169 static void handle_length_response(const struct sr_dev_inst *sdi)
170 {
171         struct dev_context *devc;
172         struct acquisition_state *acq;
173
174         devc = sdi->priv;
175         acq  = devc->acquisition;
176
177         if ((*devc->model->handle_response)(sdi) != SR_OK) {
178                 devc->transfer_error = TRUE;
179                 return;
180         }
181         acq->rle = RLE_STATE_DATA;
182         acq->sample = 0;
183         acq->run_len = 0;
184         acq->samples_done = 0;
185         acq->mem_addr_done = acq->mem_addr_next;
186         acq->out_index = 0;
187
188         if (acq->mem_addr_next >= acq->mem_addr_stop) {
189                 submit_request(sdi, STATE_READ_FINISH);
190                 return;
191         }
192         sr_dbg("%zu words in capture buffer.",
193                acq->mem_addr_stop - acq->mem_addr_next);
194
195         submit_request(sdi, STATE_READ_PREPARE);
196 }
197
198 /* Evaluate and act on the response to a capture length request.
199  */
200 static void handle_read_response(const struct sr_dev_inst *sdi)
201 {
202         struct dev_context *devc;
203         struct acquisition_state *acq;
204         struct sr_datafeed_packet packet;
205         struct sr_datafeed_logic logic;
206         size_t end_addr;
207
208         devc = sdi->priv;
209         acq  = devc->acquisition;
210
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;
216
217         end_addr = MIN(acq->mem_addr_next, acq->mem_addr_stop);
218         acq->in_index = 0;
219         /*
220          * Repeatedly call the model-specific read response handler until
221          * all data received in the transfer has been accounted for.
222          */
223         while (!devc->cancel_requested
224                         && (acq->run_len > 0 || acq->mem_addr_done < end_addr)
225                         && acq->samples_done < acq->samples_max) {
226
227                 if ((*devc->model->handle_response)(sdi) != SR_OK) {
228                         devc->transfer_error = TRUE;
229                         return;
230                 }
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);
235                         acq->out_index = 0;
236                 }
237         }
238
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);
244                 return;
245         }
246
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);
251                 acq->out_index = 0;
252         }
253         submit_request(sdi, STATE_READ_FINISH);
254 }
255
256 /* Destroy and unset the acquisition state record.
257  */
258 static void clear_acquisition_state(const struct sr_dev_inst *sdi)
259 {
260         struct dev_context *devc;
261         struct acquisition_state *acq;
262
263         devc = sdi->priv;
264         acq  = devc->acquisition;
265
266         devc->acquisition = NULL;
267
268         if (acq) {
269                 libusb_free_transfer(acq->xfer_out);
270                 libusb_free_transfer(acq->xfer_in);
271                 g_free(acq);
272         }
273 }
274
275 /* USB I/O source callback.
276  */
277 static int transfer_event(int fd, int revents, void *cb_data)
278 {
279         const struct sr_dev_inst *sdi;
280         struct dev_context *devc;
281         struct drv_context *drvc;
282         struct timeval tv;
283         struct sr_datafeed_packet packet;
284         int ret;
285
286         (void)fd;
287
288         sdi  = cb_data;
289         devc = sdi->priv;
290         drvc = sdi->driver->context;
291
292         if (!devc || !drvc)
293                 return G_SOURCE_REMOVE;
294
295         /* Handle pending USB events without blocking. */
296         tv.tv_sec  = 0;
297         tv.tv_usec = 0;
298         ret = libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx,
299                                                      &tv, NULL);
300         if (ret != 0) {
301                 sr_err("Event handling failed: %s.", libusb_error_name(ret));
302                 devc->transfer_error = TRUE;
303         }
304
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);
310         }
311
312         /* Stop processing events if an error occurred on a transfer. */
313         if (devc->transfer_error)
314                 devc->state = STATE_IDLE;
315
316         if (devc->state != STATE_IDLE)
317                 return G_SOURCE_CONTINUE;
318
319         sr_info("Acquisition stopped.");
320
321         /* We are done, clean up and send end packet to session bus. */
322         clear_acquisition_state(sdi);
323
324         packet.type = SR_DF_END;
325         packet.payload = NULL;
326         sr_session_send(sdi, &packet);
327
328         return G_SOURCE_REMOVE;
329 }
330
331 /* USB output transfer completion callback.
332  */
333 static void LIBUSB_CALL transfer_out_completed(struct libusb_transfer *transfer)
334 {
335         const struct sr_dev_inst *sdi;
336         struct dev_context *devc;
337         struct acquisition_state *acq;
338
339         sdi  = transfer->user_data;
340         devc = sdi->priv;
341         acq  = devc->acquisition;
342
343         if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
344                 sr_err("Transfer to device failed: %d.", transfer->status);
345                 devc->transfer_error = TRUE;
346                 return;
347         }
348
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);
352                 return;
353         }
354         if (acq->reg_seq_pos < acq->reg_seq_len)
355                 acq->reg_seq_pos++; /* register write completed */
356
357         /* Repeat until all queued registers have been written. */
358         if (acq->reg_seq_pos < acq->reg_seq_len && !devc->cancel_requested) {
359                 next_reg_write(acq);
360                 submit_transfer(devc, acq->xfer_out);
361                 return;
362         }
363
364         switch (devc->state) {
365         case STATE_START_CAPTURE:
366                 sr_info("Acquisition started.");
367
368                 if (!devc->cancel_requested)
369                         devc->state = STATE_STATUS_WAIT;
370                 else
371                         submit_request(sdi, STATE_STOP_CAPTURE);
372                 break;
373         case STATE_STOP_CAPTURE:
374                 if (!devc->cancel_requested)
375                         submit_request(sdi, STATE_LENGTH_REQUEST);
376                 else
377                         devc->state = STATE_IDLE;
378                 break;
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);
382                 else
383                         submit_request(sdi, STATE_READ_FINISH);
384                 break;
385         case STATE_READ_FINISH:
386                 devc->state = STATE_IDLE;
387                 break;
388         default:
389                 sr_err("Unexpected device state %d.", devc->state);
390                 devc->transfer_error = TRUE;
391                 break;
392         }
393 }
394
395 /* USB input transfer completion callback.
396  */
397 static void LIBUSB_CALL transfer_in_completed(struct libusb_transfer *transfer)
398 {
399         const struct sr_dev_inst *sdi;
400         struct dev_context *devc;
401         struct acquisition_state *acq;
402
403         sdi  = transfer->user_data;
404         devc = sdi->priv;
405         acq  = devc->acquisition;
406
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;
411                 return;
412         }
413         if ((devc->state & STATE_EXPECT_RESPONSE) == 0) {
414                 sr_err("Unexpected completion of input transfer (state %d).",
415                        devc->state);
416                 devc->transfer_error = TRUE;
417                 return;
418         }
419
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;
424                         return;
425                 }
426                 /* Repeat until all queued registers have been read. */
427                 if (++acq->reg_seq_pos < acq->reg_seq_len) {
428                         next_reg_read(acq);
429                         submit_transfer(devc, acq->xfer_out);
430                         return;
431                 }
432         }
433
434         switch (devc->state) {
435         case STATE_STATUS_REQUEST:
436                 if (devc->cancel_requested)
437                         submit_request(sdi, STATE_STOP_CAPTURE);
438                 else
439                         handle_status_response(sdi);
440                 break;
441         case STATE_LENGTH_REQUEST:
442                 if (devc->cancel_requested)
443                         submit_request(sdi, STATE_READ_FINISH);
444                 else
445                         handle_length_response(sdi);
446                 break;
447         case STATE_READ_REQUEST:
448                 handle_read_response(sdi);
449                 break;
450         default:
451                 sr_err("Unexpected device state %d.", devc->state);
452                 devc->transfer_error = TRUE;
453                 break;
454         }
455 }
456
457 /* Set up the acquisition state record.
458  */
459 static int init_acquisition_state(const struct sr_dev_inst *sdi)
460 {
461         struct dev_context *devc;
462         struct sr_usb_dev_inst *usb;
463         struct acquisition_state *acq;
464
465         devc = sdi->priv;
466         usb  = sdi->conn;
467
468         if (devc->acquisition) {
469                 sr_err("Acquisition still in progress?");
470                 return SR_ERR;
471         }
472         if (devc->cfg_clock_source == CLOCK_INTERNAL && devc->samplerate == 0) {
473                 sr_err("Samplerate not set.");
474                 return SR_ERR;
475         }
476
477         acq = g_try_malloc0(sizeof(struct acquisition_state));
478         if (!acq)
479                 return SR_ERR_MALLOC;
480
481         acq->xfer_in = libusb_alloc_transfer(0);
482         if (!acq->xfer_in) {
483                 g_free(acq);
484                 return SR_ERR_MALLOC;
485         }
486         acq->xfer_out = libusb_alloc_transfer(0);
487         if (!acq->xfer_out) {
488                 libusb_free_transfer(acq->xfer_in);
489                 g_free(acq);
490                 return SR_ERR_MALLOC;
491         }
492
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);
497
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);
503
504         if (devc->limit_msec > 0) {
505                 acq->duration_max = devc->limit_msec;
506                 sr_info("Acquisition time limit %" PRIu64 " ms.",
507                         devc->limit_msec);
508         } else
509                 acq->duration_max = MAX_LIMIT_MSEC;
510
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);
515         } else
516                 acq->samples_max = MAX_LIMIT_SAMPLES;
517
518         if (devc->cfg_clock_source == CLOCK_INTERNAL) {
519                 sr_info("Internal clock, samplerate %" PRIu64 ".",
520                         devc->samplerate);
521                 /* Ramp up clock speed to enable samplerates above 100 MS/s. */
522                 acq->clock_boost = (devc->samplerate > SR_MHZ(100));
523
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;
531         } else {
532                 acq->clock_boost = TRUE;
533
534                 if (devc->cfg_clock_edge == EDGE_POSITIVE)
535                         sr_info("External clock, rising edge.");
536                 else
537                         sr_info("External clock, falling edge.");
538         }
539
540         acq->rle_enabled = devc->cfg_rle;
541         devc->acquisition = acq;
542
543         return SR_OK;
544 }
545
546 SR_PRIV int lwla_start_acquisition(const struct sr_dev_inst *sdi)
547 {
548         struct drv_context *drvc;
549         struct dev_context *devc;
550         int ret;
551
552         drvc = sdi->driver->context;
553         devc = sdi->priv;
554
555         if (devc->state != STATE_IDLE) {
556                 sr_err("Not in idle state, cannot start acquisition.");
557                 return SR_ERR;
558         }
559         devc->cancel_requested = FALSE;
560         devc->transfer_error = FALSE;
561
562         ret = init_acquisition_state(sdi);
563         if (ret != SR_OK)
564                 return ret;
565
566         ret = (*devc->model->setup_acquisition)(sdi);
567         if (ret != SR_OK) {
568                 sr_err("Failed to set up device for acquisition.");
569                 clear_acquisition_state(sdi);
570                 return ret;
571         }
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);
575         if (ret != SR_OK) {
576                 clear_acquisition_state(sdi);
577                 return ret;
578         }
579         ret = submit_request(sdi, STATE_START_CAPTURE);
580
581         if (ret == SR_OK) {
582                 /* Send header packet to the session bus. */
583                 ret = std_session_send_df_header(sdi, LOG_PREFIX);
584         }
585         if (ret != SR_OK) {
586                 usb_source_remove(sdi->session, drvc->sr_ctx);
587                 clear_acquisition_state(sdi);
588         }
589         return ret;
590 }