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/>.
22 static const int32_t hwopts[] = {
27 static const int32_t hwcaps[] = {
29 SR_CONF_LIMIT_SAMPLES,
36 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
38 SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
39 static struct sr_dev_driver *di = &norma_dmm_driver_info;
41 static int init(struct sr_context *sr_ctx)
43 return std_init(sr_ctx, di, LOG_PREFIX);
46 static GSList *scan(GSList *options)
48 struct sr_dev_inst *sdi;
49 struct drv_context *drvc;
50 struct dev_context *devc;
51 struct sr_config *src;
52 struct sr_probe *probe;
53 struct sr_serial_dev_inst *serial;
56 const char *conn, *serialcomm;
64 drvc->instances = NULL;
65 conn = serialcomm = NULL;
67 for (l = options; l; l = l->next) {
71 conn = g_variant_get_string(src->data, NULL);
73 case SR_CONF_SERIALCOMM:
74 serialcomm = g_variant_get_string(src->data, NULL);
81 serialcomm = SERIALCOMM;
83 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
86 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
91 if (!(buf = g_try_malloc(BUF_MAX))) {
92 sr_err("Serial buffer malloc failed.");
96 snprintf(req, sizeof(req), "%s\r\n",
97 nmadmm_requests[NMADMM_REQ_IDN].req_str);
98 for (cnt = 0; cnt < 7; cnt++) {
99 if (serial_write(serial, req, strlen(req)) == -1) {
100 sr_err("Unable to send identification request: %d %s.",
101 errno, strerror(errno));
105 serial_readline(serial, &buf, &len, 1500);
108 buf[BUF_MAX - 1] = '\0';
110 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
111 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
112 auxtype = xgittoint(buf[7]);
113 // TODO: Will this work with non-DM950?
114 snprintf(fmttype, sizeof(fmttype), "DM9%d0", auxtype);
115 sr_spew("Norma %s DMM %s detected!", fmttype, &buf[9]);
117 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
118 "Norma", fmttype, buf + 9)))
120 if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
121 sr_err("Device context malloc failed.");
124 devc->type = auxtype;
125 devc->version = g_strdup(&buf[9]);
126 devc->elapsed_msec = g_timer_new();
131 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
134 sdi->probes = g_slist_append(sdi->probes, probe);
135 drvc->instances = g_slist_append(drvc->instances, sdi);
136 devices = g_slist_append(devices, sdi);
141 * The interface of the DM9x0 contains a cap that needs to
142 * charge for up to 10s before the interface works, if not
143 * powered externally. Therefore wait a little to improve
147 sr_info("Waiting 5s to allow interface to settle.");
148 g_usleep(5 * 1000 * 1000);
154 serial_close(serial);
156 sr_serial_dev_inst_free(serial);
161 static GSList *dev_list(void)
163 return ((struct drv_context *)(di->priv))->instances;
166 static int dev_clear(void)
168 return std_dev_clear(di, NULL);
171 static int dev_close(struct sr_dev_inst *sdi)
173 struct dev_context *devc;
175 std_serial_dev_close(sdi);
177 /* Free dynamically allocated resources. */
178 if ((devc = sdi->priv) && devc->version) {
179 g_free(devc->version);
180 devc->version = NULL;
181 g_timer_destroy(devc->elapsed_msec);
187 static int cleanup(void)
192 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
193 const struct sr_probe_group *probe_group)
195 struct dev_context *devc;
199 if (sdi->status != SR_ST_ACTIVE)
200 return SR_ERR_DEV_CLOSED;
202 if (!(devc = sdi->priv)) {
203 sr_err("sdi->priv was NULL.");
208 case SR_CONF_LIMIT_MSEC:
209 /* TODO: not yet implemented */
210 if (g_variant_get_uint64(data) == 0) {
211 sr_err("LIMIT_MSEC can't be 0.");
214 devc->limit_msec = g_variant_get_uint64(data);
215 sr_dbg("Setting time limit to %" PRIu64 "ms.",
218 case SR_CONF_LIMIT_SAMPLES:
219 devc->limit_samples = g_variant_get_uint64(data);
220 sr_dbg("Setting sample limit to %" PRIu64 ".",
221 devc->limit_samples);
230 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
231 const struct sr_probe_group *probe_group)
237 case SR_CONF_SCAN_OPTIONS:
238 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
239 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
241 case SR_CONF_DEVICE_OPTIONS:
242 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
243 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
252 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
255 struct dev_context *devc;
256 struct sr_serial_dev_inst *serial;
261 if (!sdi || !cb_data || !(devc = sdi->priv))
264 if (sdi->status != SR_ST_ACTIVE)
265 return SR_ERR_DEV_CLOSED;
267 devc->cb_data = cb_data;
269 /* Send header packet to the session bus. */
270 std_session_send_df_header(cb_data, LOG_PREFIX);
272 /* Start timer, if required. */
273 if (devc->limit_msec)
274 g_timer_start(devc->elapsed_msec);
276 /* Poll every 100ms, or whenever some data comes in. */
278 serial_source_add(serial, G_IO_IN, 100, norma_dmm_receive_data,
284 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
286 struct dev_context *devc;
288 /* Stop timer, if required. */
289 if (sdi && (devc = sdi->priv) && devc->limit_msec)
290 g_timer_stop(devc->elapsed_msec);
292 return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
293 sdi->conn, LOG_PREFIX);
296 SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
298 .longname = "Norma DM9x0 / Siemens B102x DMMs",
303 .dev_list = dev_list,
304 .dev_clear = dev_clear,
306 .config_set = config_set,
307 .config_list = config_list,
308 .dev_open = std_serial_dev_open,
309 .dev_close = dev_close,
310 .dev_acquisition_start = dev_acquisition_start,
311 .dev_acquisition_stop = dev_acquisition_stop,