]> sigrok.org Git - libsigrok.git/blob - src/hardware/norma-dmm/api.c
Add helper function for scan completion
[libsigrok.git] / src / hardware / norma-dmm / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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 /** @file
21  *  Norma DM9x0/Siemens B102x DMMs driver.
22  *  @internal
23  */
24
25 #include <config.h>
26 #include "protocol.h"
27
28 static const uint32_t scanopts[] = {
29         SR_CONF_CONN,
30         SR_CONF_SERIALCOMM,
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 #define BUF_MAX 50
41
42 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
43
44 static struct sr_dev_driver norma_dmm_driver_info;
45 static struct sr_dev_driver siemens_b102x_driver_info;
46
47 static const char *get_brandstr(struct sr_dev_driver *drv)
48 {
49         return (drv == &norma_dmm_driver_info) ? "Norma" : "Siemens";
50 }
51
52 static const char *get_typestr(int type, struct sr_dev_driver *drv)
53 {
54         static const char *nameref[5][2] = {
55                 {"DM910", "B1024"},
56                 {"DM920", "B1025"},
57                 {"DM930", "B1026"},
58                 {"DM940", "B1027"},
59                 {"DM950", "B1028"},
60         };
61
62         if ((type < 1) || (type > 5))
63                 return "Unknown type!";
64
65         return nameref[type - 1][(drv == &siemens_b102x_driver_info)];
66 }
67
68 static GSList *scan(struct sr_dev_driver *drv, GSList *options)
69 {
70         struct sr_dev_inst *sdi;
71         struct dev_context *devc;
72         struct sr_config *src;
73         struct sr_serial_dev_inst *serial;
74         GSList *l, *devices;
75         int len, cnt, auxtype;
76         const char *conn, *serialcomm;
77         char *buf;
78         char req[10];
79
80         devices = NULL;
81         conn = serialcomm = NULL;
82
83         for (l = options; l; l = l->next) {
84                 src = l->data;
85                 switch (src->key) {
86                 case SR_CONF_CONN:
87                         conn = g_variant_get_string(src->data, NULL);
88                         break;
89                 case SR_CONF_SERIALCOMM:
90                         serialcomm = g_variant_get_string(src->data, NULL);
91                         break;
92                 }
93         }
94         if (!conn)
95                 return NULL;
96         if (!serialcomm)
97                 serialcomm = SERIALCOMM;
98
99         serial = sr_serial_dev_inst_new(conn, serialcomm);
100
101         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
102                 return NULL;
103
104         serial_flush(serial);
105
106         buf = g_malloc(BUF_MAX);
107
108         snprintf(req, sizeof(req), "%s\r\n",
109                  nmadmm_requests[NMADMM_REQ_IDN].req_str);
110         g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
111         for (cnt = 0; cnt < 7; cnt++) {
112                 if (serial_write_blocking(serial, req, strlen(req),
113                                 serial_timeout(serial, strlen(req))) < 0) {
114                         sr_err("Unable to send identification request.");
115                         return NULL;
116                 }
117                 len = BUF_MAX;
118                 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
119                 if (!len)
120                         continue;
121                 buf[BUF_MAX - 1] = '\0';
122
123                 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950). */
124                 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
125                         auxtype = xgittoint(buf[7]);
126                         sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
127
128                         sdi = g_malloc0(sizeof(struct sr_dev_inst));
129                         sdi->status = SR_ST_INACTIVE;
130                         sdi->vendor = g_strdup(get_brandstr(drv));
131                         sdi->model = g_strdup(get_typestr(auxtype, drv));
132                         sdi->version = g_strdup(buf + 9);
133                         devc = g_malloc0(sizeof(struct dev_context));
134                         sr_sw_limits_init(&devc->limits);
135                         devc->type = auxtype;
136                         devc->version = g_strdup(&buf[9]);
137                         sdi->conn = serial;
138                         sdi->priv = devc;
139                         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
140                         devices = g_slist_append(devices, sdi);
141                         break;
142                 }
143
144                 /*
145                  * The interface of the DM9x0 contains a cap that needs to
146                  * charge for up to 10s before the interface works, if not
147                  * powered externally. Therefore wait a little to improve
148                  * chances.
149                  */
150                 if (cnt == 3) {
151                         sr_info("Waiting 5s to allow interface to settle.");
152                         g_usleep(5 * 1000 * 1000);
153                 }
154         }
155
156         g_free(buf);
157
158         serial_close(serial);
159         if (!devices)
160                 sr_serial_dev_inst_free(serial);
161
162         return std_scan_complete(drv, devices);
163 }
164
165 static int dev_close(struct sr_dev_inst *sdi)
166 {
167         struct dev_context *devc;
168
169         std_serial_dev_close(sdi);
170
171         /* Free dynamically allocated resources. */
172         if ((devc = sdi->priv) && devc->version) {
173                 g_free(devc->version);
174                 devc->version = NULL;
175         }
176
177         return SR_OK;
178 }
179
180 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
181                 const struct sr_channel_group *cg)
182 {
183         struct dev_context *devc;
184
185         (void)cg;
186
187         if (sdi->status != SR_ST_ACTIVE)
188                 return SR_ERR_DEV_CLOSED;
189
190         devc = sdi->priv;
191
192         return sr_sw_limits_config_set(&devc->limits, key, data);
193 }
194
195 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
196                 const struct sr_channel_group *cg)
197 {
198         (void)sdi;
199         (void)cg;
200
201         switch (key) {
202         case SR_CONF_SCAN_OPTIONS:
203                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
204                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
205                 break;
206         case SR_CONF_DEVICE_OPTIONS:
207                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
208                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
209                 break;
210         default:
211                 return SR_ERR_NA;
212         }
213
214         return SR_OK;
215 }
216
217 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
218 {
219         struct dev_context *devc;
220         struct sr_serial_dev_inst *serial;
221
222         if (sdi->status != SR_ST_ACTIVE)
223                 return SR_ERR_DEV_CLOSED;
224
225         devc = sdi->priv;
226
227         sr_sw_limits_acquisition_start(&devc->limits);
228         std_session_send_df_header(sdi, LOG_PREFIX);
229
230         /* Poll every 100ms, or whenever some data comes in. */
231         serial = sdi->conn;
232         serial_source_add(sdi->session, serial, G_IO_IN, 100,
233                         norma_dmm_receive_data, (void *)sdi);
234
235         return SR_OK;
236 }
237
238 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
239 {
240         return std_serial_dev_acquisition_stop(sdi, dev_close,
241                         sdi->conn, LOG_PREFIX);
242 }
243
244 static struct sr_dev_driver norma_dmm_driver_info = {
245         .name = "norma-dmm",
246         .longname = "Norma DM9x0 DMMs",
247         .api_version = 1,
248         .init = std_init,
249         .cleanup = std_cleanup,
250         .scan = scan,
251         .dev_list = std_dev_list,
252         .dev_clear = NULL,
253         .config_get = NULL,
254         .config_set = config_set,
255         .config_list = config_list,
256         .dev_open = std_serial_dev_open,
257         .dev_close = dev_close,
258         .dev_acquisition_start = dev_acquisition_start,
259         .dev_acquisition_stop = dev_acquisition_stop,
260         .context = NULL,
261 };
262 SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info);
263
264 static struct sr_dev_driver siemens_b102x_driver_info = {
265         .name = "siemens-b102x",
266         .longname = "Siemens B102x DMMs",
267         .api_version = 1,
268         .init = std_init,
269         .cleanup = std_cleanup,
270         .scan = scan,
271         .dev_list = std_dev_list,
272         .dev_clear = NULL,
273         .config_get = NULL,
274         .config_set = config_set,
275         .config_list = config_list,
276         .dev_open = std_serial_dev_open,
277         .dev_close = dev_close,
278         .dev_acquisition_start = dev_acquisition_start,
279         .dev_acquisition_stop = dev_acquisition_stop,
280         .context = NULL,
281 };
282 SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info);