]> sigrok.org Git - libsigrok.git/blob - src/hardware/uni-t-ut32x/api.c
rigol-ds: Fix reading data from internal memory
[libsigrok.git] / src / hardware / uni-t-ut32x / api.c
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
24 static const uint32_t scanopts[] = {
25         SR_CONF_CONN,
26         SR_CONF_SERIALCOMM,
27 };
28
29 static const uint32_t drvopts[] = {
30         SR_CONF_THERMOMETER,
31 };
32
33 static 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  */
49 static const char *channel_names[] = {
50         "T1", "T2", "T1-T2",
51 };
52
53 static const char *data_sources[] = {
54         "Live", "Memory",
55 };
56
57 static 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         /*
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          */
74         conn = NULL;
75         serialcomm = "2400/8n1";
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;
82                 case SR_CONF_SERIALCOMM:
83                         serialcomm = g_variant_get_string(src->data, NULL);
84                         break;
85                 }
86         }
87         if (!conn)
88                 return NULL;
89
90         devices = NULL;
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;
99         }
100
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]);
114         }
115         devices = g_slist_append(devices, sdi);
116
117         return std_scan_complete(di, devices);
118 }
119
120 static int config_get(uint32_t key, GVariant **data,
121         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
122 {
123         struct dev_context *devc;
124
125         (void)cg;
126
127         devc = sdi->priv;
128         switch (key) {
129         case SR_CONF_LIMIT_SAMPLES:
130         case SR_CONF_LIMIT_MSEC:
131                 return sr_sw_limits_config_get(&devc->limits, key, data);
132         case SR_CONF_DATA_SOURCE:
133                 *data = g_variant_new_string(data_sources[devc->data_source]);
134                 break;
135         default:
136                 return SR_ERR_NA;
137         }
138
139         return SR_OK;
140 }
141
142 static int config_set(uint32_t key, GVariant *data,
143         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
144 {
145         struct dev_context *devc;
146         int idx;
147
148         (void)cg;
149
150         devc = sdi->priv;
151
152         switch (key) {
153         case SR_CONF_LIMIT_SAMPLES:
154         case SR_CONF_LIMIT_MSEC:
155                 return sr_sw_limits_config_set(&devc->limits, key, data);
156         case SR_CONF_DATA_SOURCE:
157                 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0)
158                         return SR_ERR_ARG;
159                 devc->data_source = idx;
160                 break;
161         default:
162                 return SR_ERR_NA;
163         }
164
165         return SR_OK;
166 }
167
168 static int config_list(uint32_t key, GVariant **data,
169         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
170 {
171         switch (key) {
172         case SR_CONF_SCAN_OPTIONS:
173         case SR_CONF_DEVICE_OPTIONS:
174                 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
175         case SR_CONF_DATA_SOURCE:
176                 *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
177                 break;
178         default:
179                 return SR_ERR_NA;
180         }
181
182         return SR_OK;
183 }
184
185 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
186 {
187         struct dev_context *devc;
188         struct sr_serial_dev_inst *serial;
189         uint8_t cmd;
190
191         devc = sdi->priv;
192         serial = sdi->conn;
193
194         sr_sw_limits_acquisition_start(&devc->limits);
195         devc->packet_len = 0;
196         std_session_send_df_header(sdi);
197
198         if (devc->data_source == DATA_SOURCE_LIVE)
199                 cmd = CMD_GET_LIVE;
200         else
201                 cmd = CMD_GET_STORED;
202         serial_write_blocking(serial, &cmd, sizeof(cmd), 0);
203
204         serial_source_add(sdi->session, serial, G_IO_IN, 10,
205                         ut32x_handle_events, (void *)sdi);
206
207         return SR_OK;
208 }
209
210 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
211 {
212         /* Have the reception routine stop the acquisition. */
213         sdi->status = SR_ST_STOPPING;
214
215         return SR_OK;
216 }
217
218 static struct sr_dev_driver uni_t_ut32x_driver_info = {
219         .name = "uni-t-ut32x",
220         .longname = "UNI-T UT32x",
221         .api_version = 1,
222         .init = std_init,
223         .cleanup = std_cleanup,
224         .scan = scan,
225         .dev_list = std_dev_list,
226         .dev_clear = std_dev_clear,
227         .config_get = config_get,
228         .config_set = config_set,
229         .config_list = config_list,
230         .dev_open = std_serial_dev_open,
231         .dev_close = std_serial_dev_close,
232         .dev_acquisition_start = dev_acquisition_start,
233         .dev_acquisition_stop = dev_acquisition_stop,
234         .context = NULL,
235 };
236 SR_REGISTER_DEV_DRIVER(uni_t_ut32x_driver_info);