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.
27 static const uint32_t scanopts[] = {
32 static const uint32_t devopts[] = {
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
41 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
43 SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
44 SR_PRIV struct sr_dev_driver siemens_b102x_driver_info;
46 static const char* get_brandstr(struct sr_dev_driver* drv)
48 if (drv == &norma_dmm_driver_info)
54 static const char* get_typestr(int type, struct sr_dev_driver* drv)
56 static const char* nameref[5][2] = {
63 if ((type < 1) || (type > 5))
64 return "Unknown type!";
66 return nameref[type-1][(drv == &siemens_b102x_driver_info)];
69 static int init_norma_dmm(struct sr_context *sr_ctx)
71 return std_init(sr_ctx, &norma_dmm_driver_info, LOG_PREFIX);
74 static int init_siemens_b102x(struct sr_context *sr_ctx)
76 return std_init(sr_ctx, &siemens_b102x_driver_info, LOG_PREFIX);
79 static GSList *do_scan(struct sr_dev_driver* drv, GSList *options)
81 struct sr_dev_inst *sdi;
82 struct drv_context *drvc;
83 struct dev_context *devc;
84 struct sr_config *src;
85 struct sr_channel *ch;
86 struct sr_serial_dev_inst *serial;
89 const char *conn, *serialcomm;
96 drvc->instances = NULL;
97 conn = serialcomm = NULL;
99 for (l = options; l; l = l->next) {
103 conn = g_variant_get_string(src->data, NULL);
105 case SR_CONF_SERIALCOMM:
106 serialcomm = g_variant_get_string(src->data, NULL);
113 serialcomm = SERIALCOMM;
115 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
118 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
121 serial_flush(serial);
123 if (!(buf = g_try_malloc(BUF_MAX))) {
124 sr_err("Serial buffer malloc failed.");
128 snprintf(req, sizeof(req), "%s\r\n",
129 nmadmm_requests[NMADMM_REQ_IDN].req_str);
130 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
131 for (cnt = 0; cnt < 7; cnt++) {
132 if (serial_write_blocking(serial, req, strlen(req), 0) < 0) {
133 sr_err("Unable to send identification request.");
137 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
140 buf[BUF_MAX - 1] = '\0';
142 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
143 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
144 auxtype = xgittoint(buf[7]);
145 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
147 sdi = g_malloc0(sizeof(struct sr_dev_inst));
148 sdi->status = SR_ST_INACTIVE;
149 sdi->vendor = g_strdup(get_brandstr(drv));
150 sdi->model = g_strdup(get_typestr(auxtype, drv));
151 sdi->version = g_strdup(buf + 9);
152 devc = g_malloc0(sizeof(struct dev_context));
153 devc->type = auxtype;
154 devc->version = g_strdup(&buf[9]);
155 devc->elapsed_msec = g_timer_new();
159 if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE,
162 sdi->channels = g_slist_append(sdi->channels, ch);
163 drvc->instances = g_slist_append(drvc->instances, sdi);
164 devices = g_slist_append(devices, sdi);
169 * The interface of the DM9x0 contains a cap that needs to
170 * charge for up to 10s before the interface works, if not
171 * powered externally. Therefore wait a little to improve
175 sr_info("Waiting 5s to allow interface to settle.");
176 g_usleep(5 * 1000 * 1000);
182 serial_close(serial);
184 sr_serial_dev_inst_free(serial);
189 static GSList *scan_norma_dmm(GSList *options)
191 return do_scan(&norma_dmm_driver_info, options);
194 static GSList *scan_siemens_b102x(GSList *options)
196 return do_scan(&siemens_b102x_driver_info, options);
199 static GSList *dev_list_norma_dmm(void)
201 return ((struct drv_context *)(norma_dmm_driver_info.priv))->instances;
204 static GSList *dev_list_siemens_b102x(void)
206 return ((struct drv_context *)(siemens_b102x_driver_info.priv))->instances;
209 static int dev_close(struct sr_dev_inst *sdi)
211 struct dev_context *devc;
213 std_serial_dev_close(sdi);
215 /* Free dynamically allocated resources. */
216 if ((devc = sdi->priv) && devc->version) {
217 g_free(devc->version);
218 devc->version = NULL;
219 g_timer_destroy(devc->elapsed_msec);
225 static int cleanup_norma_dmm(void)
227 return std_dev_clear(&norma_dmm_driver_info, NULL);
230 static int cleanup_siemens_b102x(void)
232 return std_dev_clear(&siemens_b102x_driver_info, NULL);
235 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
236 const struct sr_channel_group *cg)
238 struct dev_context *devc;
242 if (sdi->status != SR_ST_ACTIVE)
243 return SR_ERR_DEV_CLOSED;
245 if (!(devc = sdi->priv)) {
246 sr_err("sdi->priv was NULL.");
251 case SR_CONF_LIMIT_MSEC:
252 if (g_variant_get_uint64(data) == 0) {
253 sr_err("LIMIT_MSEC can't be 0.");
256 devc->limit_msec = g_variant_get_uint64(data);
257 sr_dbg("Setting time limit to %" PRIu64 "ms.",
260 case SR_CONF_LIMIT_SAMPLES:
261 devc->limit_samples = g_variant_get_uint64(data);
262 sr_dbg("Setting sample limit to %" PRIu64 ".",
263 devc->limit_samples);
272 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
273 const struct sr_channel_group *cg)
279 case SR_CONF_SCAN_OPTIONS:
280 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
281 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
283 case SR_CONF_DEVICE_OPTIONS:
284 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
285 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
294 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
297 struct dev_context *devc;
298 struct sr_serial_dev_inst *serial;
300 if (!sdi || !cb_data || !(devc = sdi->priv))
303 if (sdi->status != SR_ST_ACTIVE)
304 return SR_ERR_DEV_CLOSED;
306 devc->cb_data = cb_data;
308 /* Send header packet to the session bus. */
309 std_session_send_df_header(cb_data, LOG_PREFIX);
311 /* Start timer, if required. */
312 if (devc->limit_msec)
313 g_timer_start(devc->elapsed_msec);
315 /* Poll every 100ms, or whenever some data comes in. */
317 serial_source_add(sdi->session, serial, G_IO_IN, 100,
318 norma_dmm_receive_data, (void *)sdi);
323 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
325 struct dev_context *devc;
327 /* Stop timer, if required. */
328 if (sdi && (devc = sdi->priv) && devc->limit_msec)
329 g_timer_stop(devc->elapsed_msec);
331 return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
332 sdi->conn, LOG_PREFIX);
335 SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
337 .longname = "Norma DM9x0 DMMs",
339 .init = init_norma_dmm,
340 .cleanup = cleanup_norma_dmm,
341 .scan = scan_norma_dmm,
342 .dev_list = dev_list_norma_dmm,
345 .config_set = config_set,
346 .config_list = config_list,
347 .dev_open = std_serial_dev_open,
348 .dev_close = dev_close,
349 .dev_acquisition_start = dev_acquisition_start,
350 .dev_acquisition_stop = dev_acquisition_stop,
355 SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
356 .name = "siemens-b102x",
357 .longname = "Siemens B102x DMMs",
359 .init = init_siemens_b102x,
360 .cleanup = cleanup_siemens_b102x,
361 .scan = scan_siemens_b102x,
362 .dev_list = dev_list_siemens_b102x,
365 .config_set = config_set,
366 .config_list = config_list,
367 .dev_open = std_serial_dev_open,
368 .dev_close = dev_close,
369 .dev_acquisition_start = dev_acquisition_start,
370 .dev_acquisition_stop = dev_acquisition_stop,