2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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.
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.
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/>.
21 * Norma DM9x0/Siemens B102x DMMs driver.
28 static const uint32_t scanopts[] = {
33 static const uint32_t devopts[] = {
36 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
37 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
42 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
44 SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
45 SR_PRIV struct sr_dev_driver siemens_b102x_driver_info;
47 static const char *get_brandstr(struct sr_dev_driver *drv)
49 if (drv == &norma_dmm_driver_info)
55 static const char *get_typestr(int type, struct sr_dev_driver *drv)
57 static const char *nameref[5][2] = {
64 if ((type < 1) || (type > 5))
65 return "Unknown type!";
67 return nameref[type-1][(drv == &siemens_b102x_driver_info)];
70 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
72 return std_init(sr_ctx, di, LOG_PREFIX);
75 static GSList *scan(struct sr_dev_driver *drv, GSList *options)
77 struct sr_dev_inst *sdi;
78 struct drv_context *drvc;
79 struct dev_context *devc;
80 struct sr_config *src;
81 struct sr_serial_dev_inst *serial;
84 const char *conn, *serialcomm;
91 drvc->instances = NULL;
92 conn = serialcomm = NULL;
94 for (l = options; l; l = l->next) {
98 conn = g_variant_get_string(src->data, NULL);
100 case SR_CONF_SERIALCOMM:
101 serialcomm = g_variant_get_string(src->data, NULL);
108 serialcomm = SERIALCOMM;
110 serial = sr_serial_dev_inst_new(conn, serialcomm);
112 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
115 serial_flush(serial);
117 buf = g_malloc(BUF_MAX);
119 snprintf(req, sizeof(req), "%s\r\n",
120 nmadmm_requests[NMADMM_REQ_IDN].req_str);
121 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
122 for (cnt = 0; cnt < 7; cnt++) {
123 if (serial_write_blocking(serial, req, strlen(req),
124 serial_timeout(serial, strlen(req))) < 0) {
125 sr_err("Unable to send identification request.");
129 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
132 buf[BUF_MAX - 1] = '\0';
134 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
135 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
136 auxtype = xgittoint(buf[7]);
137 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
139 sdi = g_malloc0(sizeof(struct sr_dev_inst));
140 sdi->status = SR_ST_INACTIVE;
141 sdi->vendor = g_strdup(get_brandstr(drv));
142 sdi->model = g_strdup(get_typestr(auxtype, drv));
143 sdi->version = g_strdup(buf + 9);
144 devc = g_malloc0(sizeof(struct dev_context));
145 devc->type = auxtype;
146 devc->version = g_strdup(&buf[9]);
147 devc->elapsed_msec = g_timer_new();
151 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
152 drvc->instances = g_slist_append(drvc->instances, sdi);
153 devices = g_slist_append(devices, sdi);
158 * The interface of the DM9x0 contains a cap that needs to
159 * charge for up to 10s before the interface works, if not
160 * powered externally. Therefore wait a little to improve
164 sr_info("Waiting 5s to allow interface to settle.");
165 g_usleep(5 * 1000 * 1000);
171 serial_close(serial);
173 sr_serial_dev_inst_free(serial);
178 static GSList *dev_list(const struct sr_dev_driver *di)
180 return ((struct drv_context *)(di->context))->instances;
183 static int dev_close(struct sr_dev_inst *sdi)
185 struct dev_context *devc;
187 std_serial_dev_close(sdi);
189 /* Free dynamically allocated resources. */
190 if ((devc = sdi->priv) && devc->version) {
191 g_free(devc->version);
192 devc->version = NULL;
193 g_timer_destroy(devc->elapsed_msec);
199 static int cleanup(const struct sr_dev_driver *di)
201 return std_dev_clear(di, NULL);
204 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
205 const struct sr_channel_group *cg)
207 struct dev_context *devc;
211 if (sdi->status != SR_ST_ACTIVE)
212 return SR_ERR_DEV_CLOSED;
214 if (!(devc = sdi->priv)) {
215 sr_err("sdi->priv was NULL.");
220 case SR_CONF_LIMIT_MSEC:
221 devc->limit_msec = g_variant_get_uint64(data);
223 case SR_CONF_LIMIT_SAMPLES:
224 devc->limit_samples = g_variant_get_uint64(data);
233 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
234 const struct sr_channel_group *cg)
240 case SR_CONF_SCAN_OPTIONS:
241 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
242 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
244 case SR_CONF_DEVICE_OPTIONS:
245 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
246 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
255 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
257 struct dev_context *devc;
258 struct sr_serial_dev_inst *serial;
260 if (!sdi || !cb_data || !(devc = sdi->priv))
263 if (sdi->status != SR_ST_ACTIVE)
264 return SR_ERR_DEV_CLOSED;
266 devc->cb_data = cb_data;
268 /* Send header packet to the session bus. */
269 std_session_send_df_header(cb_data, LOG_PREFIX);
271 /* Start timer, if required. */
272 if (devc->limit_msec)
273 g_timer_start(devc->elapsed_msec);
275 /* Poll every 100ms, or whenever some data comes in. */
277 serial_source_add(sdi->session, serial, G_IO_IN, 100,
278 norma_dmm_receive_data, (void *)sdi);
283 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
285 struct dev_context *devc;
287 /* Stop timer, if required. */
288 if (sdi && (devc = sdi->priv) && devc->limit_msec)
289 g_timer_stop(devc->elapsed_msec);
291 return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
292 sdi->conn, LOG_PREFIX);
295 SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
297 .longname = "Norma DM9x0 DMMs",
302 .dev_list = dev_list,
305 .config_set = config_set,
306 .config_list = config_list,
307 .dev_open = std_serial_dev_open,
308 .dev_close = dev_close,
309 .dev_acquisition_start = dev_acquisition_start,
310 .dev_acquisition_stop = dev_acquisition_stop,
314 SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
315 .name = "siemens-b102x",
316 .longname = "Siemens B102x DMMs",
321 .dev_list = dev_list,
324 .config_set = config_set,
325 .config_list = config_list,
326 .dev_open = std_serial_dev_open,
327 .dev_close = dev_close,
328 .dev_acquisition_start = dev_acquisition_start,
329 .dev_acquisition_stop = dev_acquisition_stop,