]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/uni-t-ut32x/api.c
uni-t-ut32x: migrate from USB transfers to serial-over-HID communication
[libsigrok.git] / src / hardware / uni-t-ut32x / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.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 "protocol.h"
23
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
26 SR_CONF_SERIALCOMM,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_THERMOMETER,
31};
32
33static const uint32_t devopts[] = {
34 SR_CONF_CONTINUOUS,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38};
39
40/*
41 * BEWARE! "T1-T2" looks like a range, and is probably not a good
42 * channel name. Using it in sigrok-cli -C specs is troublesome. Use
43 * "delta" instead? -- But OTOH channels are not selected by the
44 * software. Instead received packets just reflect the one channel
45 * that manually was selected by the user via the device's buttons.
46 * So the name is not a blocker, and it matches the labels on the
47 * device and in the manual. So we can get away with it.
48 */
49static const char *channel_names[] = {
50 "T1", "T2", "T1-T2",
51};
52
53static const char *data_sources[] = {
54 "Live", "Memory",
55};
56
57static GSList *scan(struct sr_dev_driver *di, GSList *options)
58{
59 const char *conn, *serialcomm;
60 struct sr_config *src;
61 GSList *l, *devices;
62 struct sr_serial_dev_inst *serial;
63 int rc;
64 struct sr_dev_inst *sdi;
65 struct dev_context *devc;
66 size_t i;
67
68 conn = "hid/ch9325";
69 serialcomm = "2400/8n1";
70 for (l = options; l; l = l->next) {
71 src = l->data;
72 switch (src->key) {
73 case SR_CONF_CONN:
74 conn = g_variant_get_string(src->data, NULL);
75 break;
76 case SR_CONF_SERIALCOMM:
77 serialcomm = g_variant_get_string(src->data, NULL);
78 break;
79 }
80 }
81
82 devices = NULL;
83 serial = sr_serial_dev_inst_new(conn, serialcomm);
84 rc = serial_open(serial, SERIAL_RDWR);
85 serial_flush(serial);
86 /* Cannot query/identify the device. Successful open shall suffice. */
87 serial_close(serial);
88 if (rc != SR_OK) {
89 sr_serial_dev_inst_free(serial);
90 return devices;
91 }
92
93 sdi = g_malloc0(sizeof(*sdi));
94 sdi->status = SR_ST_INACTIVE;
95 sdi->vendor = g_strdup("UNI-T");
96 sdi->model = g_strdup("UT32x");
97 sdi->inst_type = SR_INST_SERIAL;
98 sdi->conn = serial;
99 devc = g_malloc0(sizeof(*devc));
100 sdi->priv = devc;
101 sr_sw_limits_init(&devc->limits);
102 devc->data_source = DEFAULT_DATA_SOURCE;
103 for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
104 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
105 channel_names[i]);
106 }
107 devices = g_slist_append(devices, sdi);
108
109 serial_close(serial);
110 if (!devices)
111 sr_serial_dev_inst_free(serial);
112
113 return std_scan_complete(di, devices);
114}
115
116static int config_get(uint32_t key, GVariant **data,
117 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
118{
119 struct dev_context *devc;
120
121 (void)cg;
122
123 devc = sdi->priv;
124 switch (key) {
125 case SR_CONF_LIMIT_SAMPLES:
126 case SR_CONF_LIMIT_MSEC:
127 return sr_sw_limits_config_get(&devc->limits, key, data);
128 case SR_CONF_DATA_SOURCE:
129 *data = g_variant_new_string(data_sources[devc->data_source]);
130 break;
131 default:
132 return SR_ERR_NA;
133 }
134
135 return SR_OK;
136}
137
138static int config_set(uint32_t key, GVariant *data,
139 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
140{
141 struct dev_context *devc;
142 int idx;
143
144 (void)cg;
145
146 devc = sdi->priv;
147
148 switch (key) {
149 case SR_CONF_LIMIT_SAMPLES:
150 case SR_CONF_LIMIT_MSEC:
151 return sr_sw_limits_config_set(&devc->limits, key, data);
152 case SR_CONF_DATA_SOURCE:
153 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0)
154 return SR_ERR_ARG;
155 devc->data_source = idx;
156 break;
157 default:
158 return SR_ERR_NA;
159 }
160
161 return SR_OK;
162}
163
164static int config_list(uint32_t key, GVariant **data,
165 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
166{
167 switch (key) {
168 case SR_CONF_SCAN_OPTIONS:
169 case SR_CONF_DEVICE_OPTIONS:
170 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
171 case SR_CONF_DATA_SOURCE:
172 *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
173 break;
174 default:
175 return SR_ERR_NA;
176 }
177
178 return SR_OK;
179}
180
181static int dev_acquisition_start(const struct sr_dev_inst *sdi)
182{
183 struct dev_context *devc;
184 struct sr_serial_dev_inst *serial;
185 uint8_t cmd;
186
187 devc = sdi->priv;
188 serial = sdi->conn;
189
190 sr_sw_limits_acquisition_start(&devc->limits);
191 devc->packet_len = 0;
192 std_session_send_df_header(sdi);
193
194 if (devc->data_source == DATA_SOURCE_LIVE)
195 cmd = CMD_GET_LIVE;
196 else
197 cmd = CMD_GET_STORED;
198 serial_write_blocking(serial, &cmd, sizeof(cmd), 0);
199
200 serial_source_add(sdi->session, serial, G_IO_IN, 10,
201 ut32x_handle_events, (void *)sdi);
202
203 return SR_OK;
204}
205
206static int dev_acquisition_stop(struct sr_dev_inst *sdi)
207{
208 /* Have the reception routine stop the acquisition. */
209 sdi->status = SR_ST_STOPPING;
210
211 return SR_OK;
212}
213
214static struct sr_dev_driver uni_t_ut32x_driver_info = {
215 .name = "uni-t-ut32x",
216 .longname = "UNI-T UT32x",
217 .api_version = 1,
218 .init = std_init,
219 .cleanup = std_cleanup,
220 .scan = scan,
221 .dev_list = std_dev_list,
222 .dev_clear = std_dev_clear,
223 .config_get = config_get,
224 .config_set = config_set,
225 .config_list = config_list,
226 .dev_open = std_serial_dev_open,
227 .dev_close = std_serial_dev_close,
228 .dev_acquisition_start = dev_acquisition_start,
229 .dev_acquisition_stop = dev_acquisition_stop,
230 .context = NULL,
231};
232SR_REGISTER_DEV_DRIVER(uni_t_ut32x_driver_info);