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