]> sigrok.org Git - libsigrok.git/blame - src/hardware/uni-t-ut32x/api.c
Backport recent changes from mainline.
[libsigrok.git] / src / hardware / uni-t-ut32x / api.c
CommitLineData
3877dde4
BV
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
6ec6c43b 20#include <config.h>
6513f97f 21#include <string.h>
515ab088 22#include "protocol.h"
6513f97f 23
40bbf635
BV
24static const uint32_t scanopts[] = {
25 SR_CONF_CONN,
e4204b17 26 SR_CONF_SERIALCOMM,
40bbf635
BV
27};
28
8cd15dd4 29static const uint32_t drvopts[] = {
6513f97f 30 SR_CONF_THERMOMETER,
8cd15dd4
UH
31};
32
33static const uint32_t devopts[] = {
6513f97f 34 SR_CONF_CONTINUOUS,
5827f61b 35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
e4204b17 36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
5827f61b 37 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
6513f97f
BV
38};
39
e4204b17
UH
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 */
0f34cb47
UH
49static const char *channel_names[] = {
50 "T1", "T2", "T1-T2",
6513f97f
BV
51};
52
53static const char *data_sources[] = {
8cd15dd4 54 "Live", "Memory",
6513f97f
BV
55};
56
4f840ce9 57static GSList *scan(struct sr_dev_driver *di, GSList *options)
3877dde4 58{
e4204b17 59 const char *conn, *serialcomm;
6513f97f 60 struct sr_config *src;
e4204b17
UH
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 /*
69 * Implementor's note: Do _not_ add a default conn value here,
70 * always expect users to specify the connection. Otherwise the
71 * UT32x driver's scan routine results in false positives, will
72 * match _any_ UT-D04 cable which uses the same USB HID chip.
73 */
6513f97f 74 conn = NULL;
e4204b17 75 serialcomm = "2400/8n1";
6513f97f
BV
76 for (l = options; l; l = l->next) {
77 src = l->data;
78 switch (src->key) {
79 case SR_CONF_CONN:
80 conn = g_variant_get_string(src->data, NULL);
81 break;
e4204b17
UH
82 case SR_CONF_SERIALCOMM:
83 serialcomm = g_variant_get_string(src->data, NULL);
84 break;
6513f97f
BV
85 }
86 }
87 if (!conn)
88 return NULL;
89
90 devices = NULL;
e4204b17
UH
91 serial = sr_serial_dev_inst_new(conn, serialcomm);
92 rc = serial_open(serial, SERIAL_RDWR);
93 serial_flush(serial);
94 /* Cannot query/identify the device. Successful open shall suffice. */
95 serial_close(serial);
96 if (rc != SR_OK) {
97 sr_serial_dev_inst_free(serial);
98 return devices;
6513f97f 99 }
3877dde4 100
e4204b17
UH
101 sdi = g_malloc0(sizeof(*sdi));
102 sdi->status = SR_ST_INACTIVE;
103 sdi->vendor = g_strdup("UNI-T");
104 sdi->model = g_strdup("UT32x");
105 sdi->inst_type = SR_INST_SERIAL;
106 sdi->conn = serial;
107 devc = g_malloc0(sizeof(*devc));
108 sdi->priv = devc;
109 sr_sw_limits_init(&devc->limits);
110 devc->data_source = DEFAULT_DATA_SOURCE;
111 for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
112 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE,
113 channel_names[i]);
6513f97f 114 }
e4204b17 115 devices = g_slist_append(devices, sdi);
3877dde4 116
e4204b17
UH
117 serial_close(serial);
118 if (!devices)
119 sr_serial_dev_inst_free(serial);
3877dde4 120
e4204b17 121 return std_scan_complete(di, devices);
3877dde4
BV
122}
123
8cd15dd4
UH
124static int config_get(uint32_t key, GVariant **data,
125 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3877dde4 126{
6513f97f 127 struct dev_context *devc;
3877dde4 128
53b4680f 129 (void)cg;
d3c74a6f 130
6513f97f 131 devc = sdi->priv;
3877dde4 132 switch (key) {
6513f97f 133 case SR_CONF_LIMIT_SAMPLES:
e4204b17
UH
134 case SR_CONF_LIMIT_MSEC:
135 return sr_sw_limits_config_get(&devc->limits, key, data);
6513f97f 136 case SR_CONF_DATA_SOURCE:
e4204b17 137 *data = g_variant_new_string(data_sources[devc->data_source]);
6513f97f 138 break;
3877dde4
BV
139 default:
140 return SR_ERR_NA;
141 }
142
6513f97f 143 return SR_OK;
3877dde4
BV
144}
145
8cd15dd4
UH
146static int config_set(uint32_t key, GVariant *data,
147 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3877dde4 148{
6513f97f 149 struct dev_context *devc;
8cd15dd4 150 int idx;
3877dde4 151
53b4680f 152 (void)cg;
d3c74a6f 153
6513f97f 154 devc = sdi->priv;
dcd438ee 155
3877dde4 156 switch (key) {
6513f97f 157 case SR_CONF_LIMIT_SAMPLES:
e4204b17
UH
158 case SR_CONF_LIMIT_MSEC:
159 return sr_sw_limits_config_set(&devc->limits, key, data);
6513f97f 160 case SR_CONF_DATA_SOURCE:
8cd15dd4
UH
161 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0)
162 return SR_ERR_ARG;
163 devc->data_source = idx;
6513f97f 164 break;
3877dde4 165 default:
dcd438ee 166 return SR_ERR_NA;
3877dde4
BV
167 }
168
dcd438ee 169 return SR_OK;
3877dde4
BV
170}
171
8cd15dd4
UH
172static int config_list(uint32_t key, GVariant **data,
173 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
3877dde4 174{
3877dde4 175 switch (key) {
40bbf635 176 case SR_CONF_SCAN_OPTIONS:
6513f97f 177 case SR_CONF_DEVICE_OPTIONS:
8cd15dd4 178 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
6513f97f 179 case SR_CONF_DATA_SOURCE:
8cd15dd4 180 *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
6513f97f 181 break;
3877dde4
BV
182 default:
183 return SR_ERR_NA;
184 }
185
6513f97f 186 return SR_OK;
3877dde4
BV
187}
188
695dc859 189static int dev_acquisition_start(const struct sr_dev_inst *sdi)
3877dde4 190{
6513f97f 191 struct dev_context *devc;
e4204b17
UH
192 struct sr_serial_dev_inst *serial;
193 uint8_t cmd;
3877dde4 194
6513f97f 195 devc = sdi->priv;
e4204b17 196 serial = sdi->conn;
6513f97f 197
e4204b17 198 sr_sw_limits_acquisition_start(&devc->limits);
d6ff054a 199 devc->packet_len = 0;
bee2b016 200 std_session_send_df_header(sdi);
6513f97f 201
6513f97f 202 if (devc->data_source == DATA_SOURCE_LIVE)
e4204b17 203 cmd = CMD_GET_LIVE;
6513f97f 204 else
e4204b17
UH
205 cmd = CMD_GET_STORED;
206 serial_write_blocking(serial, &cmd, sizeof(cmd), 0);
3877dde4 207
e4204b17
UH
208 serial_source_add(sdi->session, serial, G_IO_IN, 10,
209 ut32x_handle_events, (void *)sdi);
d6ff054a 210
3877dde4
BV
211 return SR_OK;
212}
213
695dc859 214static int dev_acquisition_stop(struct sr_dev_inst *sdi)
3877dde4 215{
e4204b17 216 /* Have the reception routine stop the acquisition. */
d6ff054a 217 sdi->status = SR_ST_STOPPING;
3877dde4
BV
218
219 return SR_OK;
220}
221
dd5c48a6 222static struct sr_dev_driver uni_t_ut32x_driver_info = {
3877dde4
BV
223 .name = "uni-t-ut32x",
224 .longname = "UNI-T UT32x",
225 .api_version = 1,
c2fdcc25 226 .init = std_init,
700d6b64 227 .cleanup = std_cleanup,
3877dde4 228 .scan = scan,
c01bf34c 229 .dev_list = std_dev_list,
8cd15dd4 230 .dev_clear = std_dev_clear,
3877dde4
BV
231 .config_get = config_get,
232 .config_set = config_set,
233 .config_list = config_list,
e4204b17
UH
234 .dev_open = std_serial_dev_open,
235 .dev_close = std_serial_dev_close,
3877dde4
BV
236 .dev_acquisition_start = dev_acquisition_start,
237 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 238 .context = NULL,
3877dde4 239};
dd5c48a6 240SR_REGISTER_DEV_DRIVER(uni_t_ut32x_driver_info);