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