]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Sergey Alirzaev <zl29ah@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 <ftdi.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | static void send_samples(struct sr_dev_inst *sdi, uint64_t samples_to_send) | |
25 | { | |
26 | struct sr_datafeed_packet packet; | |
27 | struct sr_datafeed_logic logic; | |
28 | struct dev_context *devc; | |
29 | ||
30 | sr_spew("Sending %" PRIu64 " samples.", samples_to_send); | |
31 | ||
32 | devc = sdi->priv; | |
33 | ||
34 | packet.type = SR_DF_LOGIC; | |
35 | packet.payload = &logic; | |
36 | logic.length = samples_to_send; | |
37 | logic.unitsize = 1; | |
38 | logic.data = devc->data_buf; | |
39 | sr_session_send(sdi, &packet); | |
40 | ||
41 | devc->samples_sent += samples_to_send; | |
42 | devc->bytes_received -= samples_to_send; | |
43 | } | |
44 | ||
45 | SR_PRIV int ftdi_la_set_samplerate(struct dev_context *devc) | |
46 | { | |
47 | int ret; | |
48 | ||
49 | ret = ftdi_set_baudrate(devc->ftdic, | |
50 | devc->cur_samplerate / devc->desc->samplerate_div); | |
51 | if (ret < 0) { | |
52 | sr_err("Failed to set baudrate (%d): %s.", devc->cur_samplerate, | |
53 | ftdi_get_error_string(devc->ftdic)); | |
54 | return SR_ERR; | |
55 | } | |
56 | return SR_OK; | |
57 | } | |
58 | ||
59 | SR_PRIV int ftdi_la_receive_data(int fd, int revents, void *cb_data) | |
60 | { | |
61 | struct sr_dev_inst *sdi; | |
62 | struct dev_context *devc; | |
63 | int bytes_read; | |
64 | uint64_t n; | |
65 | ||
66 | (void)fd; | |
67 | (void)revents; | |
68 | ||
69 | if (!(sdi = cb_data)) | |
70 | return TRUE; | |
71 | if (!(devc = sdi->priv)) | |
72 | return TRUE; | |
73 | if (!(revents == G_IO_IN || revents == 0)) | |
74 | return TRUE; | |
75 | if (!devc->ftdic) | |
76 | return TRUE; | |
77 | ||
78 | /* Get a block of data. */ | |
79 | bytes_read = ftdi_read_data(devc->ftdic, devc->data_buf, DATA_BUF_SIZE); | |
80 | if (bytes_read < 0) { | |
81 | sr_err("Failed to read FTDI data (%d): %s.", | |
82 | bytes_read, ftdi_get_error_string(devc->ftdic)); | |
83 | sr_dev_acquisition_stop(sdi); | |
84 | return FALSE; | |
85 | } | |
86 | if (bytes_read == 0) { | |
87 | sr_spew("Received 0 bytes, nothing to do."); | |
88 | return TRUE; | |
89 | } | |
90 | sr_spew("Got some data."); | |
91 | devc->bytes_received += bytes_read; | |
92 | ||
93 | n = devc->samples_sent + devc->bytes_received; | |
94 | ||
95 | if (devc->limit_samples && (n >= devc->limit_samples)) { | |
96 | send_samples(sdi, devc->limit_samples - devc->samples_sent); | |
97 | sr_info("Requested number of samples reached."); | |
98 | sr_dev_acquisition_stop(sdi); | |
99 | return TRUE; | |
100 | } else { | |
101 | send_samples(sdi, devc->bytes_received); | |
102 | } | |
103 | ||
104 | return TRUE; | |
105 | } |