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/>.
27 static const int32_t hwopts[] = {
32 static const int32_t hwcaps[] = {
34 SR_CONF_LIMIT_SAMPLES,
40 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
43 SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
44 static struct sr_dev_driver *di = &norma_dmm_driver_info;
47 static int init(struct sr_context *sr_ctx)
49 return std_init(sr_ctx, di, LOG_PREFIX);
53 static GSList *scan(GSList *options)
55 struct sr_dev_inst *sdi;
56 struct drv_context *drvc;
57 struct dev_context *devc;
58 struct sr_config *src;
59 struct sr_probe *probe;
60 struct sr_serial_dev_inst *serial;
63 const char *conn, *serialcomm;
73 drvc->instances = NULL;
74 conn = serialcomm = NULL;
76 for (l = options; l; l = l->next) {
80 conn = g_variant_get_string(src->data, NULL);
82 case SR_CONF_SERIALCOMM:
83 serialcomm = g_variant_get_string(src->data, NULL);
90 serialcomm = SERIALCOMM;
92 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
95 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
101 if (!(buf = g_try_malloc(len))) {
102 sr_err("Serial buffer malloc failed.");
106 snprintf(req, sizeof(req), "%s\r\n",
107 nmadmm_requests[NMADMM_REQ_IDN].reqstr);
108 for (cnt = 0; cnt < 7; cnt++) {
109 if (serial_write(serial, req, strlen(req)) == -1) {
110 sr_err("Unable to send identification request: %d %s.",
111 errno, strerror(errno));
115 serial_readline(serial, &buf, &len, 1500);
118 buf[BUF_MAX-1] = '\0';
120 /* Match id string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
121 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char*)buf,0,0)) {
122 auxtype = xgittoint(buf[7]);
123 // TODO: Will this work with non-DM950?
124 snprintf(fmttype, sizeof(fmttype), "DM9%d0", auxtype);
125 sr_spew("Norma %s DMM %s detected!", fmttype, &buf[9]);
127 if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE,
128 "Norma", fmttype, buf + 9)))
130 if (!(devc = g_try_malloc0(sizeof(*devc)))) {
131 sr_err("Device context malloc failed.");
134 devc->type = auxtype;
135 devc->version = g_strdup(&buf[9]);
136 devc->elapsed_msec = g_timer_new();
141 if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE,
144 sdi->probes = g_slist_append(sdi->probes, probe);
145 drvc->instances = g_slist_append(drvc->instances, sdi);
146 devices = g_slist_append(devices, sdi);
149 /* The interface of the DM9x0 contains a cap that needs to
150 charge for up to 10s before the interface works, if not powered
151 externally. Therefore wait a little to improve chances. */
153 sr_info("Waiting 5s to allow interface to settle.");
154 g_usleep(5 * 1000 * 1000);
160 serial_close(serial);
162 sr_serial_dev_inst_free(serial);
168 static GSList *dev_list(void)
170 return ((struct drv_context *)(di->priv))->instances;
174 static int dev_clear(void)
176 return std_dev_clear(di, NULL);
180 static int dev_open(struct sr_dev_inst *sdi)
182 struct sr_serial_dev_inst *serial;
185 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
188 sdi->status = SR_ST_ACTIVE;
194 static int dev_close(struct sr_dev_inst *sdi)
196 struct sr_serial_dev_inst *serial;
197 struct dev_context *devc;
200 if (serial && serial->fd != -1) {
201 serial_close(serial);
202 sdi->status = SR_ST_INACTIVE;
205 // Free dynamically allocated resources.
206 if ((devc = sdi->priv) && devc->version) {
207 g_free(devc->version);
208 devc->version = NULL;
209 g_timer_destroy(devc->elapsed_msec);
216 static int cleanup(void)
222 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi)
224 struct dev_context *devc;
226 if (sdi->status != SR_ST_ACTIVE)
227 return SR_ERR_DEV_CLOSED;
229 if (!(devc = sdi->priv)) {
230 sr_err("sdi->priv was NULL.");
235 case SR_CONF_LIMIT_MSEC:
236 /* TODO: not yet implemented */
237 if (g_variant_get_uint64(data) == 0) {
238 sr_err("LIMIT_MSEC can't be 0.");
241 devc->limit_msec = g_variant_get_uint64(data);
242 sr_dbg("Setting time limit to %" PRIu64 "ms.",
245 case SR_CONF_LIMIT_SAMPLES:
246 devc->limit_samples = g_variant_get_uint64(data);
247 sr_dbg("Setting sample limit to %" PRIu64 ".",
248 devc->limit_samples);
258 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi)
263 case SR_CONF_SCAN_OPTIONS:
264 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
265 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
267 case SR_CONF_DEVICE_OPTIONS:
268 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
269 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
279 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
282 struct dev_context *devc;
283 struct sr_serial_dev_inst *serial;
288 if (!sdi || !cb_data || !(devc = sdi->priv))
291 if (sdi->status != SR_ST_ACTIVE)
292 return SR_ERR_DEV_CLOSED;
294 devc->cb_data = cb_data;
296 /* Send header packet to the session bus. */
297 std_session_send_df_header(cb_data, LOG_PREFIX);
299 /* Start timer, if required. */
300 if (devc->limit_msec)
301 g_timer_start(devc->elapsed_msec);
303 /* Poll every 100ms, or whenever some data comes in. */
305 sr_source_add(serial->fd, G_IO_IN, 100, norma_dmm_receive_data,
312 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
314 struct dev_context *devc;
316 /* Stop timer, if required. */
317 if (sdi && (devc = sdi->priv) && devc->limit_msec)
318 g_timer_stop(devc->elapsed_msec);
320 return std_dev_acquisition_stop_serial(sdi, cb_data, dev_close,
321 sdi->conn, LOG_PREFIX);
325 SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
327 .longname = "Norma DM910..950, Siemens B1024..1028 DMMs",
332 .dev_list = dev_list,
333 .dev_clear = dev_clear,
335 .config_set = config_set,
336 .config_list = config_list,
337 .dev_open = dev_open,
338 .dev_close = dev_close,
339 .dev_acquisition_start = dev_acquisition_start,
340 .dev_acquisition_stop = dev_acquisition_stop,