]>
Commit | Line | Data |
---|---|---|
bfd48770 MH |
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 | ||
a4e435eb MH |
20 | /** @file |
21 | * Norma DM9x0/Siemens B102x DMMs driver. | |
22 | * @internal | |
23 | */ | |
24 | ||
6ec6c43b | 25 | #include <config.h> |
bfd48770 MH |
26 | #include "protocol.h" |
27 | ||
a0e0bb41 | 28 | static const uint32_t scanopts[] = { |
f8e76e2e MH |
29 | SR_CONF_CONN, |
30 | SR_CONF_SERIALCOMM, | |
31 | }; | |
32 | ||
05199c0a | 33 | static const uint32_t drvopts[] = { |
f8e76e2e | 34 | SR_CONF_MULTIMETER, |
05199c0a UH |
35 | }; |
36 | ||
37 | static const uint32_t devopts[] = { | |
f8e76e2e | 38 | SR_CONF_CONTINUOUS, |
5827f61b BV |
39 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
40 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
f8e76e2e MH |
41 | }; |
42 | ||
e790bd5c | 43 | #define BUF_MAX 50 |
f8e76e2e MH |
44 | |
45 | #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1" | |
46 | ||
dd5c48a6 LPC |
47 | static struct sr_dev_driver norma_dmm_driver_info; |
48 | static struct sr_dev_driver siemens_b102x_driver_info; | |
bfd48770 | 49 | |
c442ffda | 50 | static const char *get_brandstr(struct sr_dev_driver *drv) |
bfd48770 | 51 | { |
0cadb8a3 | 52 | return (drv == &norma_dmm_driver_info) ? "Norma" : "Siemens"; |
bfd48770 MH |
53 | } |
54 | ||
c442ffda | 55 | static const char *get_typestr(int type, struct sr_dev_driver *drv) |
49c06128 | 56 | { |
c442ffda | 57 | static const char *nameref[5][2] = { |
49c06128 MH |
58 | {"DM910", "B1024"}, |
59 | {"DM920", "B1025"}, | |
60 | {"DM930", "B1026"}, | |
61 | {"DM940", "B1027"}, | |
0cadb8a3 UH |
62 | {"DM950", "B1028"}, |
63 | }; | |
49c06128 MH |
64 | |
65 | if ((type < 1) || (type > 5)) | |
66 | return "Unknown type!"; | |
67 | ||
0cadb8a3 | 68 | return nameref[type - 1][(drv == &siemens_b102x_driver_info)]; |
49c06128 MH |
69 | } |
70 | ||
c442ffda | 71 | static GSList *scan(struct sr_dev_driver *drv, GSList *options) |
bfd48770 | 72 | { |
f8e76e2e | 73 | struct sr_dev_inst *sdi; |
f8e76e2e MH |
74 | struct dev_context *devc; |
75 | struct sr_config *src; | |
f8e76e2e MH |
76 | struct sr_serial_dev_inst *serial; |
77 | GSList *l, *devices; | |
0cadb8a3 | 78 | int len, cnt, auxtype; |
f8e76e2e MH |
79 | const char *conn, *serialcomm; |
80 | char *buf; | |
f8e76e2e | 81 | char req[10]; |
f8e76e2e | 82 | |
bfd48770 | 83 | devices = NULL; |
f8e76e2e MH |
84 | conn = serialcomm = NULL; |
85 | ||
86 | for (l = options; l; l = l->next) { | |
87 | src = l->data; | |
88 | switch (src->key) { | |
89 | case SR_CONF_CONN: | |
90 | conn = g_variant_get_string(src->data, NULL); | |
91 | break; | |
92 | case SR_CONF_SERIALCOMM: | |
93 | serialcomm = g_variant_get_string(src->data, NULL); | |
94 | break; | |
95 | } | |
96 | } | |
97 | if (!conn) | |
98 | return NULL; | |
99 | if (!serialcomm) | |
100 | serialcomm = SERIALCOMM; | |
101 | ||
91219afc | 102 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
f8e76e2e | 103 | |
d12ef776 | 104 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
f8e76e2e MH |
105 | return NULL; |
106 | ||
107 | serial_flush(serial); | |
bfd48770 | 108 | |
a95f142e | 109 | buf = g_malloc(BUF_MAX); |
f8e76e2e MH |
110 | |
111 | snprintf(req, sizeof(req), "%s\r\n", | |
e790bd5c | 112 | nmadmm_requests[NMADMM_REQ_IDN].req_str); |
a4e435eb | 113 | g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */ |
f8e76e2e | 114 | for (cnt = 0; cnt < 7; cnt++) { |
d545c81c UH |
115 | if (serial_write_blocking(serial, req, strlen(req), |
116 | serial_timeout(serial, strlen(req))) < 0) { | |
8849f45a | 117 | sr_err("Unable to send identification request."); |
f8e76e2e MH |
118 | return NULL; |
119 | } | |
120 | len = BUF_MAX; | |
a4e435eb | 121 | serial_readline(serial, &buf, &len, NMADMM_TIMEOUT_MS); |
f8e76e2e MH |
122 | if (!len) |
123 | continue; | |
e790bd5c | 124 | buf[BUF_MAX - 1] = '\0'; |
f8e76e2e | 125 | |
0cadb8a3 | 126 | /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950). */ |
e790bd5c | 127 | if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) { |
f8e76e2e | 128 | auxtype = xgittoint(buf[7]); |
49c06128 | 129 | sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9); |
f8e76e2e | 130 | |
aac29cc1 | 131 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
132 | sdi->status = SR_ST_INACTIVE; |
133 | sdi->vendor = g_strdup(get_brandstr(drv)); | |
134 | sdi->model = g_strdup(get_typestr(auxtype, drv)); | |
135 | sdi->version = g_strdup(buf + 9); | |
f57d8ffe | 136 | devc = g_malloc0(sizeof(struct dev_context)); |
37dbffd1 | 137 | sr_sw_limits_init(&devc->limits); |
f8e76e2e | 138 | devc->type = auxtype; |
f8e76e2e MH |
139 | sdi->conn = serial; |
140 | sdi->priv = devc; | |
5e23fcab | 141 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
f8e76e2e MH |
142 | devices = g_slist_append(devices, sdi); |
143 | break; | |
144 | } | |
e790bd5c UH |
145 | |
146 | /* | |
147 | * The interface of the DM9x0 contains a cap that needs to | |
148 | * charge for up to 10s before the interface works, if not | |
149 | * powered externally. Therefore wait a little to improve | |
150 | * chances. | |
151 | */ | |
f8e76e2e MH |
152 | if (cnt == 3) { |
153 | sr_info("Waiting 5s to allow interface to settle."); | |
154 | g_usleep(5 * 1000 * 1000); | |
155 | } | |
156 | } | |
157 | ||
158 | g_free(buf); | |
159 | ||
160 | serial_close(serial); | |
161 | if (!devices) | |
162 | sr_serial_dev_inst_free(serial); | |
bfd48770 | 163 | |
15a5bfe4 | 164 | return std_scan_complete(drv, devices); |
bfd48770 MH |
165 | } |
166 | ||
584560f1 | 167 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 168 | const struct sr_channel_group *cg) |
bfd48770 | 169 | { |
f8e76e2e | 170 | struct dev_context *devc; |
bfd48770 | 171 | |
53b4680f | 172 | (void)cg; |
d3c74a6f | 173 | |
b0baddef | 174 | devc = sdi->priv; |
f8e76e2e | 175 | |
37dbffd1 | 176 | return sr_sw_limits_config_set(&devc->limits, key, data); |
bfd48770 MH |
177 | } |
178 | ||
584560f1 | 179 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 180 | const struct sr_channel_group *cg) |
bfd48770 | 181 | { |
05199c0a | 182 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
bfd48770 MH |
183 | } |
184 | ||
695dc859 | 185 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
bfd48770 | 186 | { |
f8e76e2e MH |
187 | struct dev_context *devc; |
188 | struct sr_serial_dev_inst *serial; | |
189 | ||
208c1d35 | 190 | devc = sdi->priv; |
f8e76e2e | 191 | |
37dbffd1 | 192 | sr_sw_limits_acquisition_start(&devc->limits); |
bee2b016 | 193 | std_session_send_df_header(sdi); |
f8e76e2e | 194 | |
f8e76e2e MH |
195 | /* Poll every 100ms, or whenever some data comes in. */ |
196 | serial = sdi->conn; | |
102f1239 BV |
197 | serial_source_add(sdi->session, serial, G_IO_IN, 100, |
198 | norma_dmm_receive_data, (void *)sdi); | |
bfd48770 MH |
199 | |
200 | return SR_OK; | |
201 | } | |
202 | ||
dd5c48a6 | 203 | static struct sr_dev_driver norma_dmm_driver_info = { |
bfd48770 | 204 | .name = "norma-dmm", |
49c06128 MH |
205 | .longname = "Norma DM9x0 DMMs", |
206 | .api_version = 1, | |
c2fdcc25 | 207 | .init = std_init, |
700d6b64 | 208 | .cleanup = std_cleanup, |
4f840ce9 | 209 | .scan = scan, |
c01bf34c | 210 | .dev_list = std_dev_list, |
f778bf02 | 211 | .dev_clear = std_dev_clear, |
49c06128 MH |
212 | .config_get = NULL, |
213 | .config_set = config_set, | |
214 | .config_list = config_list, | |
215 | .dev_open = std_serial_dev_open, | |
eabfed20 | 216 | .dev_close = std_serial_dev_close, |
49c06128 | 217 | .dev_acquisition_start = dev_acquisition_start, |
4b1a9d5d | 218 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
41812aca | 219 | .context = NULL, |
49c06128 | 220 | }; |
dd5c48a6 | 221 | SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info); |
49c06128 | 222 | |
dd5c48a6 | 223 | static struct sr_dev_driver siemens_b102x_driver_info = { |
49c06128 MH |
224 | .name = "siemens-b102x", |
225 | .longname = "Siemens B102x DMMs", | |
bfd48770 | 226 | .api_version = 1, |
c2fdcc25 | 227 | .init = std_init, |
700d6b64 | 228 | .cleanup = std_cleanup, |
4f840ce9 | 229 | .scan = scan, |
c01bf34c | 230 | .dev_list = std_dev_list, |
f778bf02 | 231 | .dev_clear = std_dev_clear, |
f8e76e2e | 232 | .config_get = NULL, |
bfd48770 MH |
233 | .config_set = config_set, |
234 | .config_list = config_list, | |
854434de | 235 | .dev_open = std_serial_dev_open, |
eabfed20 | 236 | .dev_close = std_serial_dev_close, |
bfd48770 | 237 | .dev_acquisition_start = dev_acquisition_start, |
4b1a9d5d | 238 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
41812aca | 239 | .context = NULL, |
bfd48770 | 240 | }; |
dd5c48a6 | 241 | SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info); |