]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/norma-dmm/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / norma-dmm / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <config.h>
21#include "protocol.h"
22
23static const uint32_t scanopts[] = {
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26};
27
28static const uint32_t drvopts[] = {
29 SR_CONF_MULTIMETER,
30};
31
32static const uint32_t devopts[] = {
33 SR_CONF_CONTINUOUS,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
36};
37
38#define BUF_MAX 50
39
40#define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
41
42static struct sr_dev_driver norma_dmm_driver_info;
43static struct sr_dev_driver siemens_b102x_driver_info;
44
45static const char *get_brandstr(struct sr_dev_driver *drv)
46{
47 return (drv == &norma_dmm_driver_info) ? "Norma" : "Siemens";
48}
49
50static const char *get_typestr(int type, struct sr_dev_driver *drv)
51{
52 static const char *nameref[5][2] = {
53 {"DM910", "B1024"},
54 {"DM920", "B1025"},
55 {"DM930", "B1026"},
56 {"DM940", "B1027"},
57 {"DM950", "B1028"},
58 };
59
60 if ((type < 1) || (type > 5))
61 return "Unknown type!";
62
63 return nameref[type - 1][(drv == &siemens_b102x_driver_info)];
64}
65
66static GSList *scan(struct sr_dev_driver *drv, GSList *options)
67{
68 struct sr_dev_inst *sdi;
69 struct dev_context *devc;
70 struct sr_config *src;
71 struct sr_serial_dev_inst *serial;
72 GSList *l, *devices;
73 int len, cnt, auxtype;
74 const char *conn, *serialcomm;
75 char *buf;
76 char req[10];
77
78 devices = NULL;
79 conn = serialcomm = NULL;
80
81 for (l = options; l; l = l->next) {
82 src = l->data;
83 switch (src->key) {
84 case SR_CONF_CONN:
85 conn = g_variant_get_string(src->data, NULL);
86 break;
87 case SR_CONF_SERIALCOMM:
88 serialcomm = g_variant_get_string(src->data, NULL);
89 break;
90 }
91 }
92 if (!conn)
93 return NULL;
94 if (!serialcomm)
95 serialcomm = SERIALCOMM;
96
97 serial = sr_serial_dev_inst_new(conn, serialcomm);
98
99 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
100 return NULL;
101
102 buf = g_malloc(BUF_MAX);
103
104 snprintf(req, sizeof(req), "%s\r\n",
105 nmadmm_requests[NMADMM_REQ_IDN].req_str);
106 g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
107 for (cnt = 0; cnt < 7; cnt++) {
108 if (serial_write_blocking(serial, req, strlen(req),
109 serial_timeout(serial, strlen(req))) < 0) {
110 sr_err("Unable to send identification request.");
111 g_free(buf);
112 return NULL;
113 }
114 len = BUF_MAX;
115 serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS);
116 if (!len)
117 continue;
118 buf[BUF_MAX - 1] = '\0';
119
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 sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
124
125 sdi = g_malloc0(sizeof(struct sr_dev_inst));
126 sdi->status = SR_ST_INACTIVE;
127 sdi->vendor = g_strdup(get_brandstr(drv));
128 sdi->model = g_strdup(get_typestr(auxtype, drv));
129 sdi->version = g_strdup(buf + 9);
130 devc = g_malloc0(sizeof(struct dev_context));
131 sr_sw_limits_init(&devc->limits);
132 devc->type = auxtype;
133 sdi->conn = serial;
134 sdi->priv = devc;
135 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
136 devices = g_slist_append(devices, sdi);
137 break;
138 }
139
140 /*
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
144 * chances.
145 */
146 if (cnt == 3) {
147 sr_info("Waiting 5s to allow interface to settle.");
148 g_usleep(5 * 1000 * 1000);
149 }
150 }
151
152 g_free(buf);
153
154 serial_close(serial);
155 if (!devices)
156 sr_serial_dev_inst_free(serial);
157
158 return std_scan_complete(drv, devices);
159}
160
161static int config_set(uint32_t key, GVariant *data,
162 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
163{
164 struct dev_context *devc;
165
166 (void)cg;
167
168 devc = sdi->priv;
169
170 return sr_sw_limits_config_set(&devc->limits, key, data);
171}
172
173static int config_list(uint32_t key, GVariant **data,
174 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
175{
176 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
177}
178
179static int dev_acquisition_start(const struct sr_dev_inst *sdi)
180{
181 struct dev_context *devc;
182 struct sr_serial_dev_inst *serial;
183
184 devc = sdi->priv;
185
186 sr_sw_limits_acquisition_start(&devc->limits);
187 std_session_send_df_header(sdi);
188
189 serial = sdi->conn;
190 serial_source_add(sdi->session, serial, G_IO_IN, 100,
191 norma_dmm_receive_data, (void *)sdi);
192
193 return SR_OK;
194}
195
196static struct sr_dev_driver norma_dmm_driver_info = {
197 .name = "norma-dmm",
198 .longname = "Norma DM9x0 DMMs",
199 .api_version = 1,
200 .init = std_init,
201 .cleanup = std_cleanup,
202 .scan = scan,
203 .dev_list = std_dev_list,
204 .dev_clear = std_dev_clear,
205 .config_get = NULL,
206 .config_set = config_set,
207 .config_list = config_list,
208 .dev_open = std_serial_dev_open,
209 .dev_close = std_serial_dev_close,
210 .dev_acquisition_start = dev_acquisition_start,
211 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
212 .context = NULL,
213};
214SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info);
215
216static struct sr_dev_driver siemens_b102x_driver_info = {
217 .name = "siemens-b102x",
218 .longname = "Siemens B102x DMMs",
219 .api_version = 1,
220 .init = std_init,
221 .cleanup = std_cleanup,
222 .scan = scan,
223 .dev_list = std_dev_list,
224 .dev_clear = std_dev_clear,
225 .config_get = NULL,
226 .config_set = config_set,
227 .config_list = config_list,
228 .dev_open = std_serial_dev_open,
229 .dev_close = std_serial_dev_close,
230 .dev_acquisition_start = dev_acquisition_start,
231 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
232 .context = NULL,
233};
234SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info);