]>
Commit | Line | Data |
---|---|---|
7b4edcb6 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 | ||
f5792417 MH |
20 | #include <string.h> |
21 | ||
7b4edcb6 MH |
22 | #include "protocol.h" |
23 | ||
f5792417 MH |
24 | /* Serial communication parameters for Metrahit 1x/2x with 'RS232' adaptor */ |
25 | #define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */ | |
26 | #define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0" | |
27 | #define VENDOR_GMC "Gossen Metrawatt" | |
28 | ||
7b4edcb6 | 29 | SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info; |
7b4edcb6 | 30 | |
f5792417 MH |
31 | static const int32_t hwopts[] = { |
32 | SR_CONF_CONN, | |
33 | SR_CONF_SERIALCOMM, | |
34 | }; | |
35 | ||
36 | static const int32_t hwcaps[] = { | |
37 | SR_CONF_MULTIMETER, | |
38 | SR_CONF_LIMIT_SAMPLES, | |
39 | SR_CONF_LIMIT_MSEC, | |
40 | SR_CONF_CONTINUOUS, | |
41 | }; | |
42 | ||
43 | /** Driver init function */ | |
44 | static int init_1x_2x_rs232(struct sr_context *sr_ctx) | |
7b4edcb6 | 45 | { |
f5792417 | 46 | return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX); |
7b4edcb6 MH |
47 | } |
48 | ||
f5792417 MH |
49 | |
50 | /** Read single byte from serial port. | |
51 | * \retval -1 Timeout or error. | |
52 | * \retval other Byte. | |
53 | */ | |
54 | static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout) | |
7b4edcb6 | 55 | { |
f5792417 MH |
56 | guint8 result = 0; |
57 | int rc = 0; | |
58 | ||
59 | for (;;) { | |
60 | rc = serial_read(serial, &result, 1); | |
61 | if (rc == 1) { | |
62 | sr_spew("read: 0x%02x/%d", result, result); | |
63 | return result; | |
64 | } | |
65 | if (g_get_monotonic_time() > timeout) | |
66 | return -1; | |
67 | g_usleep(2000); | |
68 | } | |
69 | } | |
70 | ||
71 | /** Try to detect GMC 1x/2x multimeter model in send mode for max. 1 second. | |
72 | * \param serial Configured, open serial port. | |
73 | * | |
74 | * \retval NULL Detection failed. | |
75 | * \retval other Model code. | |
76 | */ | |
77 | static enum model scan_model_sm(struct sr_serial_dev_inst *serial) | |
78 | { | |
79 | int byte, bytecnt, cnt; | |
80 | enum model model; | |
81 | gint64 timeout_us; | |
82 | ||
83 | model = SR_METRAHIT_NONE; | |
84 | timeout_us = g_get_monotonic_time() + 1*1000*1000; | |
85 | ||
86 | /* Try to find message consisting of device code and several | |
87 | * (at least 4) data bytes. */ | |
88 | for (bytecnt = 0; bytecnt < 100; bytecnt++) { | |
89 | byte = read_byte(serial, timeout_us); | |
90 | if ((byte == -1) || (timeout_us < g_get_monotonic_time())) | |
91 | break; | |
92 | if ((byte & MSGID_MASK) == MSGID_INF) { | |
93 | if (!(model = sr_gmc_decode_model_sm(byte & MSGC_MASK))) | |
94 | break; | |
95 | /* Now expect (at least) 4 data bytes */ | |
96 | for (cnt = 0; cnt < 4; cnt++) { | |
97 | byte = read_byte(serial, timeout_us); | |
98 | if ((byte == -1) || | |
99 | ((byte & MSGID_MASK) != MSGID_DATA)) | |
100 | { | |
101 | model = SR_METRAHIT_NONE; | |
102 | bytecnt = 100; | |
103 | break; | |
104 | } | |
105 | } | |
106 | break; | |
107 | } | |
108 | } | |
7b4edcb6 | 109 | |
f5792417 MH |
110 | return model; |
111 | } | |
112 | ||
113 | /** Scan for Metrahit 1x and Metrahit 2x in send mode using Gossen Metrawatt | |
114 | * 'RS232' interface. The older 1x models use 8192 and the newer 2x 9600 baud. | |
115 | * The DMM usually sends up to about 20 messages per second. However, depending | |
116 | * on configuration and measurement mode the intervals can be much larger and | |
117 | * then the detection might not work. | |
118 | */ | |
119 | static GSList *scan_1x_2x_rs232(GSList *options) | |
120 | { | |
121 | struct sr_dev_inst *sdi; | |
122 | struct drv_context *drvc; | |
123 | struct dev_context *devc; | |
124 | struct sr_config *src; | |
125 | struct sr_probe *probe; | |
126 | struct sr_serial_dev_inst *serial; | |
127 | GSList *l, *devices; | |
128 | const char *conn, *serialcomm; | |
129 | enum model model; | |
130 | gboolean serialcomm_given; | |
131 | int cnt; | |
7b4edcb6 MH |
132 | |
133 | devices = NULL; | |
f5792417 | 134 | drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv; |
7b4edcb6 | 135 | drvc->instances = NULL; |
f5792417 MH |
136 | conn = serialcomm = NULL; |
137 | model = SR_METRAHIT_NONE; | |
138 | serialcomm_given = FALSE; | |
139 | ||
140 | sr_spew("scan_1x_2x_rs232() called!"); | |
141 | ||
142 | for (l = options; l; l = l->next) { | |
143 | src = l->data; | |
144 | switch (src->key) { | |
145 | case SR_CONF_CONN: | |
146 | conn = g_variant_get_string(src->data, NULL); | |
147 | break; | |
148 | case SR_CONF_SERIALCOMM: | |
149 | serialcomm = g_variant_get_string(src->data, NULL); | |
150 | serialcomm_given = TRUE; | |
151 | break; | |
152 | } | |
153 | } | |
154 | if (!conn) | |
155 | return NULL; | |
156 | if (!serialcomm) | |
157 | serialcomm = SERIALCOMM_2X_RS232; | |
158 | ||
159 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) | |
160 | return NULL; | |
161 | ||
162 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) { | |
163 | sr_serial_dev_inst_free(serial); | |
164 | return NULL; | |
165 | } | |
7b4edcb6 | 166 | |
f5792417 MH |
167 | serial_flush(serial); |
168 | ||
169 | model = scan_model_sm(serial); | |
170 | ||
171 | /* If detection failed and no user-supplied parameters, | |
172 | * try second baud rate. */ | |
173 | if ((model == SR_METRAHIT_NONE) && !serialcomm_given) { | |
174 | serialcomm = SERIALCOMM_1X_RS232; | |
175 | g_free(serial->serialcomm); | |
176 | serial->serialcomm = g_strdup(serialcomm); | |
177 | if (serial_set_paramstr(serial, serialcomm) == SR_OK) { | |
178 | serial_flush(serial); | |
179 | model = scan_model_sm(serial); | |
180 | } | |
181 | } | |
182 | ||
183 | if (model != SR_METRAHIT_NONE) { | |
184 | sr_spew("%s %s detected!", VENDOR_GMC, sr_gmc_model_str(model)); | |
185 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC, | |
186 | sr_gmc_model_str(model), ""))) | |
187 | return NULL; | |
188 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
189 | sr_err("Device context malloc failed."); | |
190 | return NULL; | |
191 | } | |
192 | devc->model = model; | |
193 | devc->limit_samples = 0; | |
194 | devc->limit_msec = 0; | |
195 | devc->num_samples = 0; | |
196 | devc->elapsed_msec = g_timer_new(); | |
197 | devc->settings_ok = FALSE; | |
198 | ||
199 | sdi->conn = serial; | |
200 | sdi->priv = devc; | |
201 | sdi->driver = &gmc_mh_1x_2x_rs232_driver_info; | |
202 | if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) | |
203 | return NULL; | |
204 | sdi->probes = g_slist_append(sdi->probes, probe); | |
205 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
206 | devices = g_slist_append(devices, sdi); | |
207 | } | |
7b4edcb6 MH |
208 | |
209 | return devices; | |
210 | } | |
211 | ||
f5792417 MH |
212 | /** Driver device list function */ |
213 | static GSList *dev_list_1x_2x_rs232(void) | |
7b4edcb6 | 214 | { |
f5792417 MH |
215 | return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv)) |
216 | ->instances; | |
7b4edcb6 MH |
217 | } |
218 | ||
f5792417 | 219 | static int dev_clear_1x_2x_rs232(void) |
7b4edcb6 | 220 | { |
f5792417 | 221 | return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL); |
7b4edcb6 MH |
222 | } |
223 | ||
7b4edcb6 MH |
224 | static int dev_close(struct sr_dev_inst *sdi) |
225 | { | |
f5792417 | 226 | struct dev_context *devc; |
7b4edcb6 | 227 | |
bf2c987f | 228 | std_serial_dev_close(sdi); |
7b4edcb6 MH |
229 | |
230 | sdi->status = SR_ST_INACTIVE; | |
231 | ||
f5792417 MH |
232 | /* Free dynamically allocated resources. */ |
233 | if ((devc = sdi->priv) && devc->elapsed_msec) { | |
234 | g_timer_destroy(devc->elapsed_msec); | |
235 | devc->elapsed_msec = NULL; | |
236 | devc->model = SR_METRAHIT_NONE; | |
237 | } | |
238 | ||
7b4edcb6 MH |
239 | return SR_OK; |
240 | } | |
241 | ||
f5792417 | 242 | static int cleanup_sm_rs232(void) |
7b4edcb6 | 243 | { |
f5792417 | 244 | return dev_clear_1x_2x_rs232(); |
7b4edcb6 MH |
245 | } |
246 | ||
f5792417 | 247 | /** TODO */ |
7b4edcb6 MH |
248 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, |
249 | const struct sr_probe_group *probe_group) | |
250 | { | |
251 | int ret; | |
252 | ||
253 | (void)sdi; | |
254 | (void)data; | |
255 | (void)probe_group; | |
256 | ||
257 | ret = SR_OK; | |
258 | switch (key) { | |
259 | /* TODO */ | |
260 | default: | |
261 | return SR_ERR_NA; | |
262 | } | |
263 | ||
264 | return ret; | |
265 | } | |
266 | ||
267 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
268 | const struct sr_probe_group *probe_group) | |
269 | { | |
f5792417 | 270 | struct dev_context *devc; |
7b4edcb6 | 271 | |
7b4edcb6 MH |
272 | (void)probe_group; |
273 | ||
274 | if (sdi->status != SR_ST_ACTIVE) | |
275 | return SR_ERR_DEV_CLOSED; | |
276 | ||
f5792417 MH |
277 | if (!(devc = sdi->priv)) { |
278 | sr_err("sdi->priv was NULL."); | |
279 | return SR_ERR_BUG; | |
280 | } | |
281 | ||
7b4edcb6 | 282 | switch (key) { |
f5792417 MH |
283 | case SR_CONF_LIMIT_MSEC: |
284 | if (g_variant_get_uint64(data) == 0) { | |
285 | sr_err("LIMIT_MSEC can't be 0."); | |
286 | return SR_ERR; | |
287 | } | |
288 | devc->limit_msec = g_variant_get_uint64(data); | |
289 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
290 | devc->limit_msec); | |
291 | break; | |
292 | case SR_CONF_LIMIT_SAMPLES: | |
293 | devc->limit_samples = g_variant_get_uint64(data); | |
294 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
295 | devc->limit_samples); | |
296 | break; | |
7b4edcb6 | 297 | default: |
f5792417 | 298 | return SR_ERR_NA; |
7b4edcb6 MH |
299 | } |
300 | ||
f5792417 | 301 | return SR_OK; |
7b4edcb6 MH |
302 | } |
303 | ||
304 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
305 | const struct sr_probe_group *probe_group) | |
306 | { | |
7b4edcb6 | 307 | (void)sdi; |
7b4edcb6 | 308 | |
7b4edcb6 | 309 | switch (key) { |
f5792417 MH |
310 | case SR_CONF_SCAN_OPTIONS: |
311 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
312 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
313 | break; | |
314 | case SR_CONF_DEVICE_OPTIONS: | |
315 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
316 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
317 | break; | |
7b4edcb6 MH |
318 | default: |
319 | return SR_ERR_NA; | |
320 | } | |
321 | ||
f5792417 | 322 | return SR_OK; |
7b4edcb6 MH |
323 | } |
324 | ||
f5792417 | 325 | static int dev_acq_start_1x_2x_rs232(const struct sr_dev_inst *sdi, |
7b4edcb6 MH |
326 | void *cb_data) |
327 | { | |
f5792417 MH |
328 | struct dev_context *devc; |
329 | struct sr_serial_dev_inst *serial; | |
330 | ||
331 | if (!sdi || !cb_data || !(devc = sdi->priv)) | |
332 | return SR_ERR_BUG; | |
7b4edcb6 MH |
333 | |
334 | if (sdi->status != SR_ST_ACTIVE) | |
335 | return SR_ERR_DEV_CLOSED; | |
336 | ||
f5792417 MH |
337 | devc->cb_data = cb_data; |
338 | devc->settings_ok = FALSE; | |
339 | devc->buflen = 0; | |
340 | ||
341 | /* Send header packet to the session bus. */ | |
342 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
343 | ||
344 | /* Start timer, if required. */ | |
345 | if (devc->limit_msec) | |
346 | g_timer_start(devc->elapsed_msec); | |
347 | ||
348 | /* Poll every 40ms, or whenever some data comes in. */ | |
349 | serial = sdi->conn; | |
abc4b335 | 350 | serial_source_add(serial, G_IO_IN, 40, gmc_mh_1x_2x_receive_data, |
f5792417 | 351 | (void *)sdi); |
7b4edcb6 MH |
352 | |
353 | return SR_OK; | |
354 | } | |
355 | ||
f5792417 | 356 | static int dev_acq_stop(struct sr_dev_inst *sdi, void *cb_data) |
7b4edcb6 | 357 | { |
f5792417 | 358 | struct dev_context *devc; |
7b4edcb6 | 359 | |
f5792417 MH |
360 | /* Stop timer, if required. */ |
361 | if (sdi && (devc = sdi->priv) && devc->limit_msec) | |
362 | g_timer_stop(devc->elapsed_msec); | |
7b4edcb6 | 363 | |
d43b0908 | 364 | return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close, |
f5792417 | 365 | sdi->conn, LOG_PREFIX); |
7b4edcb6 MH |
366 | } |
367 | ||
368 | SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = { | |
369 | .name = "gmc-mh-1x-2x-rs232", | |
370 | .longname = | |
371 | "Gossen Metrawatt Metrahit 1x/2x DMMs, 'RS232' Interface", | |
372 | .api_version = 1, | |
f5792417 MH |
373 | .init = init_1x_2x_rs232, |
374 | .cleanup = cleanup_sm_rs232, | |
375 | .scan = scan_1x_2x_rs232, | |
376 | .dev_list = dev_list_1x_2x_rs232, | |
377 | .dev_clear = dev_clear_1x_2x_rs232, | |
7b4edcb6 MH |
378 | .config_get = config_get, |
379 | .config_set = config_set, | |
380 | .config_list = config_list, | |
854434de | 381 | .dev_open = std_serial_dev_open, |
7b4edcb6 | 382 | .dev_close = dev_close, |
f5792417 MH |
383 | .dev_acquisition_start = dev_acq_start_1x_2x_rs232, |
384 | .dev_acquisition_stop = dev_acq_stop, | |
7b4edcb6 MH |
385 | .priv = NULL, |
386 | }; |