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/>.
23 static const uint32_t scanopts[] = {
28 static const uint32_t drvopts[] = {
32 static const uint32_t devopts[] = {
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
40 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
42 static struct sr_dev_driver norma_dmm_driver_info;
43 static struct sr_dev_driver siemens_b102x_driver_info;
45 static const char *get_brandstr(struct sr_dev_driver *drv)
47 return (drv == &norma_dmm_driver_info) ? "Norma" : "Siemens";
50 static const char *get_typestr(int type, struct sr_dev_driver *drv)
52 static const char *nameref[5][2] = {
60 if ((type < 1) || (type > 5))
61 return "Unknown type!";
63 return nameref[type - 1][(drv == &siemens_b102x_driver_info)];
66 static GSList *scan(struct sr_dev_driver *drv, GSList *options)
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_config *src;
71 struct sr_serial_dev_inst *serial;
73 int len, cnt, auxtype;
74 const char *conn, *serialcomm;
79 conn = serialcomm = NULL;
81 for (l = options; l; l = l->next) {
85 conn = g_variant_get_string(src->data, NULL);
87 case SR_CONF_SERIALCOMM:
88 serialcomm = g_variant_get_string(src->data, NULL);
95 serialcomm = SERIALCOMM;
97 serial = sr_serial_dev_inst_new(conn, serialcomm);
99 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
102 serial_flush(serial);
104 buf = g_malloc(BUF_MAX);
106 snprintf(req, sizeof(req), "%s\r\n",
107 nmadmm_requests[NMADMM_REQ_IDN].req_str);
108 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
109 for (cnt = 0; cnt < 7; cnt++) {
110 if (serial_write_blocking(serial, req, strlen(req),
111 serial_timeout(serial, strlen(req))) < 0) {
112 sr_err("Unable to send identification request.");
117 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
120 buf[BUF_MAX - 1] = '\0';
122 /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950). */
123 if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
124 auxtype = xgittoint(buf[7]);
125 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
127 sdi = g_malloc0(sizeof(struct sr_dev_inst));
128 sdi->status = SR_ST_INACTIVE;
129 sdi->vendor = g_strdup(get_brandstr(drv));
130 sdi->model = g_strdup(get_typestr(auxtype, drv));
131 sdi->version = g_strdup(buf + 9);
132 devc = g_malloc0(sizeof(struct dev_context));
133 sr_sw_limits_init(&devc->limits);
134 devc->type = auxtype;
137 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
138 devices = g_slist_append(devices, sdi);
143 * The interface of the DM9x0 contains a cap that needs to
144 * charge for up to 10s before the interface works, if not
145 * powered externally. Therefore wait a little to improve
149 sr_info("Waiting 5s to allow interface to settle.");
150 g_usleep(5 * 1000 * 1000);
156 serial_close(serial);
158 sr_serial_dev_inst_free(serial);
160 return std_scan_complete(drv, devices);
163 static int config_set(uint32_t key, GVariant *data,
164 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
166 struct dev_context *devc;
172 return sr_sw_limits_config_set(&devc->limits, key, data);
175 static int config_list(uint32_t key, GVariant **data,
176 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
178 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
181 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
183 struct dev_context *devc;
184 struct sr_serial_dev_inst *serial;
188 sr_sw_limits_acquisition_start(&devc->limits);
189 std_session_send_df_header(sdi);
192 serial_source_add(sdi->session, serial, G_IO_IN, 100,
193 norma_dmm_receive_data, (void *)sdi);
198 static struct sr_dev_driver norma_dmm_driver_info = {
200 .longname = "Norma DM9x0 DMMs",
203 .cleanup = std_cleanup,
205 .dev_list = std_dev_list,
206 .dev_clear = std_dev_clear,
208 .config_set = config_set,
209 .config_list = config_list,
210 .dev_open = std_serial_dev_open,
211 .dev_close = std_serial_dev_close,
212 .dev_acquisition_start = dev_acquisition_start,
213 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
216 SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info);
218 static struct sr_dev_driver siemens_b102x_driver_info = {
219 .name = "siemens-b102x",
220 .longname = "Siemens B102x DMMs",
223 .cleanup = std_cleanup,
225 .dev_list = std_dev_list,
226 .dev_clear = std_dev_clear,
228 .config_set = config_set,
229 .config_list = config_list,
230 .dev_open = std_serial_dev_open,
231 .dev_close = std_serial_dev_close,
232 .dev_acquisition_start = dev_acquisition_start,
233 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
236 SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info);