]> sigrok.org Git - libsigrok.git/blob - src/hardware/uni-t-dmm/api.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / hardware / uni-t-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <libsigrok/libsigrok.h>
24 #include "libsigrok-internal.h"
25 #include "protocol.h"
26
27 #define UNI_T_UT_D04_NEW "1a86.e008"
28
29 static const uint32_t scanopts[] = {
30         SR_CONF_CONN,
31 };
32
33 static const uint32_t devopts[] = {
34         SR_CONF_MULTIMETER,
35         SR_CONF_CONTINUOUS,
36         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
37         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
38 };
39
40 /*
41  * Note 1: The actual baudrate of the Cyrustek ES519xx chip used in this DMM
42  * is 19230. However, the WCH CH9325 chip (UART to USB/HID) used in (some
43  * versions of) the UNI-T UT-D04 cable doesn't support 19230 baud. It only
44  * supports 19200, and setting an unsupported baudrate will result in the
45  * default of 2400 being used (which will not work with this DMM, of course).
46  */
47
48 static int dev_clear(const struct sr_dev_driver *di)
49 {
50         return std_dev_clear(di, NULL);
51 }
52
53 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
54 {
55         return std_init(sr_ctx, di, LOG_PREFIX);
56 }
57
58 static GSList *scan(struct sr_dev_driver *di, GSList *options)
59 {
60         GSList *usb_devices, *devices, *l;
61         struct sr_dev_inst *sdi;
62         struct dev_context *devc;
63         struct drv_context *drvc;
64         struct dmm_info *dmm;
65         struct sr_usb_dev_inst *usb;
66         struct sr_config *src;
67         const char *conn;
68
69         drvc = di->context;
70         dmm = (struct dmm_info *)di;
71
72         conn = NULL;
73         for (l = options; l; l = l->next) {
74                 src = l->data;
75                 switch (src->key) {
76                 case SR_CONF_CONN:
77                         conn = g_variant_get_string(src->data, NULL);
78                         break;
79                 }
80         }
81         if (!conn)
82                 return NULL;
83
84         devices = NULL;
85         if (!(usb_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn))) {
86                 g_slist_free_full(usb_devices, g_free);
87                 return NULL;
88         }
89
90         for (l = usb_devices; l; l = l->next) {
91                 usb = l->data;
92                 devc = g_malloc0(sizeof(struct dev_context));
93                 devc->first_run = TRUE;
94                 sdi = g_malloc0(sizeof(struct sr_dev_inst));
95                 sdi->status = SR_ST_INACTIVE;
96                 sdi->vendor = g_strdup(dmm->vendor);
97                 sdi->model = g_strdup(dmm->device);
98                 sdi->priv = devc;
99                 sdi->driver = di;
100                 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
101                 sdi->inst_type = SR_INST_USB;
102                 sdi->conn = usb;
103                 drvc->instances = g_slist_append(drvc->instances, sdi);
104                 devices = g_slist_append(devices, sdi);
105         }
106
107         return devices;
108 }
109
110 static GSList *dev_list(const struct sr_dev_driver *di)
111 {
112         return ((struct drv_context *)(di->context))->instances;
113 }
114
115 static int dev_open(struct sr_dev_inst *sdi)
116 {
117         struct sr_dev_driver *di;
118         struct drv_context *drvc;
119         struct sr_usb_dev_inst *usb;
120         int ret;
121
122         di = sdi->driver;
123         drvc = di->context;
124         usb = sdi->conn;
125
126         if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
127                 sdi->status = SR_ST_ACTIVE;
128
129         return ret;
130 }
131
132 static int dev_close(struct sr_dev_inst *sdi)
133 {
134         /* TODO */
135
136         sdi->status = SR_ST_INACTIVE;
137
138         return SR_OK;
139 }
140
141 static int cleanup(const struct sr_dev_driver *di)
142 {
143         return dev_clear(di);
144 }
145
146 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
147                 const struct sr_channel_group *cg)
148 {
149         struct dev_context *devc;
150
151         (void)cg;
152
153         devc = sdi->priv;
154
155         switch (key) {
156         case SR_CONF_LIMIT_MSEC:
157                 devc->limit_msec = g_variant_get_uint64(data);
158                 break;
159         case SR_CONF_LIMIT_SAMPLES:
160                 devc->limit_samples = g_variant_get_uint64(data);
161                 break;
162         default:
163                 return SR_ERR_NA;
164         }
165
166         return SR_OK;
167 }
168
169 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
170                 const struct sr_channel_group *cg)
171 {
172         (void)sdi;
173         (void)cg;
174
175         switch (key) {
176         case SR_CONF_SCAN_OPTIONS:
177                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
178                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
179                 break;
180         case SR_CONF_DEVICE_OPTIONS:
181                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
182                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
183                 break;
184         default:
185                 return SR_ERR_NA;
186         }
187
188         return SR_OK;
189 }
190
191 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
192 {
193         struct dev_context *devc;
194
195         devc = sdi->priv;
196
197         devc->cb_data = cb_data;
198
199         devc->starttime = g_get_monotonic_time();
200
201         /* Send header packet to the session bus. */
202         std_session_send_df_header(sdi, LOG_PREFIX);
203
204         sr_session_source_add(sdi->session, 0, 0, 10 /* poll_timeout */,
205                       uni_t_dmm_receive_data, (void *)sdi);
206
207         return SR_OK;
208 }
209
210 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
211 {
212         struct sr_datafeed_packet packet;
213
214         (void)cb_data;
215
216         sr_dbg("Stopping acquisition.");
217
218         /* Send end packet to the session bus. */
219         sr_dbg("Sending SR_DF_END.");
220         packet.type = SR_DF_END;
221         sr_session_send(sdi, &packet);
222
223         sr_session_source_remove(sdi->session, 0);
224
225         return SR_OK;
226 }
227
228 #define DMM(ID, CHIPSET, VENDOR, MODEL, BAUDRATE, PACKETSIZE, \
229                         VALID, PARSE, DETAILS) \
230     &(struct dmm_info) { \
231                 { \
232                         .name = ID, \
233                         .longname = VENDOR " " MODEL, \
234                         .api_version = 1, \
235                         .init = init, \
236                         .cleanup = cleanup, \
237                         .scan = scan, \
238                         .dev_list = dev_list, \
239                         .dev_clear = dev_clear, \
240                         .config_get = NULL, \
241                         .config_set = config_set, \
242                         .config_list = config_list, \
243                         .dev_open = dev_open, \
244                         .dev_close = dev_close, \
245                         .dev_acquisition_start = dev_acquisition_start, \
246                         .dev_acquisition_stop = dev_acquisition_stop, \
247                         .context = NULL, \
248                 }, \
249                 VENDOR, MODEL, BAUDRATE, PACKETSIZE, \
250                 VALID, PARSE, DETAILS, sizeof(struct CHIPSET##_info) \
251         }
252
253 SR_PRIV const struct dmm_info *uni_t_dmm_drivers[] = {
254         DMM(
255                 "tecpel-dmm-8061", fs9721,
256                 "Tecpel", "DMM-8061", 2400,
257                 FS9721_PACKET_SIZE,
258                 sr_fs9721_packet_valid, sr_fs9721_parse,
259                 sr_fs9721_00_temp_c
260         ),
261         DMM(
262                 "uni-t-ut372", ut372,
263                 "UNI-T", "UT372", 2400,
264                 UT372_PACKET_SIZE,
265                 sr_ut372_packet_valid, sr_ut372_parse,
266                 NULL
267         ),
268         DMM(
269                 "uni-t-ut60a", fs9721,
270                 "UNI-T", "UT60A", 2400,
271                 FS9721_PACKET_SIZE,
272                 sr_fs9721_packet_valid, sr_fs9721_parse,
273                 NULL
274         ),
275         DMM(
276                 "uni-t-ut60e", fs9721,
277                 "UNI-T", "UT60E", 2400,
278                 FS9721_PACKET_SIZE,
279                 sr_fs9721_packet_valid, sr_fs9721_parse,
280                 sr_fs9721_00_temp_c
281         ),
282         DMM(
283                 "uni-t-ut60g", es519xx,
284                 /* The baudrate is actually 19230, see "Note 1" below. */
285                 "UNI-T", "UT60G", 19200,
286                 ES519XX_11B_PACKET_SIZE,
287                 sr_es519xx_19200_11b_packet_valid, sr_es519xx_19200_11b_parse,
288                 NULL
289         ),
290         DMM(
291                 "uni-t-ut61b", fs9922,
292                 "UNI-T", "UT61B", 2400,
293                 FS9922_PACKET_SIZE,
294                 sr_fs9922_packet_valid, sr_fs9922_parse,
295                 NULL
296         ),
297         DMM(
298                 "uni-t-ut61c", fs9922,
299                 "UNI-T", "UT61C", 2400,
300                 FS9922_PACKET_SIZE,
301                 sr_fs9922_packet_valid, sr_fs9922_parse,
302                 NULL
303         ),
304         DMM(
305                 "uni-t-ut61d", fs9922,
306                 "UNI-T", "UT61D", 2400,
307                 FS9922_PACKET_SIZE,
308                 sr_fs9922_packet_valid, sr_fs9922_parse,
309                 NULL
310         ),
311         DMM(
312                 "uni-t-ut61e", es519xx,
313                 /* The baudrate is actually 19230, see "Note 1" below. */
314                 "UNI-T", "UT61E", 19200,
315                 ES519XX_14B_PACKET_SIZE,
316                 sr_es519xx_19200_14b_packet_valid, sr_es519xx_19200_14b_parse,
317                 NULL
318         ),
319         DMM(
320                 "uni-t-ut71a", ut71x,
321                 "UNI-T", "UT71A", 2400, UT71X_PACKET_SIZE,
322                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
323         ),
324         DMM(
325                 "uni-t-ut71b", ut71x,
326                 "UNI-T", "UT71B", 2400, UT71X_PACKET_SIZE,
327                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
328         ),
329         DMM(
330                 "uni-t-ut71c", ut71x,
331                 "UNI-T", "UT71C", 2400, UT71X_PACKET_SIZE,
332                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
333         ),
334         DMM(
335                 "uni-t-ut71d", ut71x,
336                 "UNI-T", "UT71D", 2400, UT71X_PACKET_SIZE,
337                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
338         ),
339         DMM(
340                 "uni-t-ut71e", ut71x,
341                 "UNI-T", "UT71E", 2400, UT71X_PACKET_SIZE,
342                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
343         ),
344         DMM(
345                 "voltcraft-vc820", fs9721,
346                 "Voltcraft", "VC-820", 2400,
347                 FS9721_PACKET_SIZE,
348                 sr_fs9721_packet_valid, sr_fs9721_parse,
349                 NULL
350         ),
351         DMM(
352                 "voltcraft-vc830", fs9922,
353                 /*
354                  * Note: The VC830 doesn't set the 'volt' and 'diode' bits of
355                  * the FS9922 protocol. Instead, it only sets the user-defined
356                  * bit "z1" to indicate "diode mode" and "voltage".
357                  */
358                 "Voltcraft", "VC-830", 2400,
359                 FS9922_PACKET_SIZE,
360                 sr_fs9922_packet_valid, sr_fs9922_parse,
361                 &sr_fs9922_z1_diode
362         ),
363         DMM(
364                 "voltcraft-vc840", fs9721,
365                 "Voltcraft", "VC-840", 2400,
366                 FS9721_PACKET_SIZE,
367                 sr_fs9721_packet_valid, sr_fs9721_parse,
368                 sr_fs9721_00_temp_c
369         ),
370         DMM(
371                 "voltcraft-vc870", vc870,
372                 "Voltcraft", "VC-870", 9600, VC870_PACKET_SIZE,
373                 sr_vc870_packet_valid, sr_vc870_parse, NULL
374         ),
375         DMM(
376                 "voltcraft-vc920", ut71x,
377                 "Voltcraft", "VC-920", 2400, UT71X_PACKET_SIZE,
378                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
379         ),
380         DMM(
381                 "voltcraft-vc940", ut71x,
382                 "Voltcraft", "VC-940", 2400, UT71X_PACKET_SIZE,
383                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
384         ),
385         DMM(
386                 "voltcraft-vc960", ut71x,
387                 "Voltcraft", "VC-960", 2400, UT71X_PACKET_SIZE,
388                 sr_ut71x_packet_valid, sr_ut71x_parse, NULL
389         ),
390         DMM(
391                 "tenma-72-7745", es519xx,
392                 "Tenma", "72-7745", 2400,
393                 FS9721_PACKET_SIZE,
394                 sr_fs9721_packet_valid, sr_fs9721_parse,
395                 sr_fs9721_00_temp_c
396         ),
397         DMM(
398                 "tenma-72-7750", es519xx,
399                 /* The baudrate is actually 19230, see "Note 1" below. */
400                 "Tenma", "72-7750", 19200,
401                 ES519XX_11B_PACKET_SIZE,
402                 sr_es519xx_19200_11b_packet_valid, sr_es519xx_19200_11b_parse,
403                 NULL
404         ),
405         NULL
406 };