]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
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 <glib.h> | |
21 | #include <sys/types.h> | |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <string.h> | |
25 | #include <errno.h> | |
26 | #include "libsigrok.h" | |
27 | #include "libsigrok-internal.h" | |
28 | #include "agilent-dmm.h" | |
29 | ||
30 | static const uint32_t scanopts[] = { | |
31 | SR_CONF_CONN, | |
32 | SR_CONF_SERIALCOMM, | |
33 | }; | |
34 | ||
35 | static const uint32_t drvopts[] = { | |
36 | SR_CONF_MULTIMETER, | |
37 | }; | |
38 | ||
39 | static const uint32_t devopts[] = { | |
40 | SR_CONF_CONTINUOUS | SR_CONF_SET, | |
41 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, | |
42 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
43 | }; | |
44 | ||
45 | extern const struct agdmm_job agdmm_jobs_u12xx[]; | |
46 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
47 | extern const struct agdmm_recv agdmm_recvs_u124x[]; | |
48 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
49 | ||
50 | /* This works on all the Agilent U12xxA series, although the | |
51 | * U127xA can apparently also run at 19200/8n1. */ | |
52 | #define SERIALCOMM "9600/8n1" | |
53 | ||
54 | static const struct agdmm_profile supported_agdmm[] = { | |
55 | { AGILENT_U1231, "U1231A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
56 | { AGILENT_U1232, "U1232A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
57 | { AGILENT_U1233, "U1233A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
58 | ||
59 | { AGILENT_U1241, "U1241A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
60 | { AGILENT_U1242, "U1242A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
61 | { AGILENT_U1241, "U1241B", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
62 | { AGILENT_U1242, "U1242B", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
63 | ||
64 | { AGILENT_U1251, "U1251A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
65 | { AGILENT_U1252, "U1252A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
66 | { AGILENT_U1253, "U1253A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
67 | { AGILENT_U1251, "U1251B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
68 | { AGILENT_U1252, "U1252B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
69 | { AGILENT_U1253, "U1253B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
70 | ALL_ZERO | |
71 | }; | |
72 | ||
73 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
74 | ||
75 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) | |
76 | { | |
77 | return std_init(sr_ctx, di, LOG_PREFIX); | |
78 | } | |
79 | ||
80 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
81 | { | |
82 | struct sr_dev_inst *sdi; | |
83 | struct drv_context *drvc; | |
84 | struct dev_context *devc; | |
85 | struct sr_config *src; | |
86 | struct sr_serial_dev_inst *serial; | |
87 | GSList *l, *devices; | |
88 | int len, i; | |
89 | const char *conn, *serialcomm; | |
90 | char *buf, **tokens; | |
91 | ||
92 | drvc = di->priv; | |
93 | drvc->instances = NULL; | |
94 | ||
95 | devices = NULL; | |
96 | conn = serialcomm = NULL; | |
97 | for (l = options; l; l = l->next) { | |
98 | src = l->data; | |
99 | switch (src->key) { | |
100 | case SR_CONF_CONN: | |
101 | conn = g_variant_get_string(src->data, NULL); | |
102 | break; | |
103 | case SR_CONF_SERIALCOMM: | |
104 | serialcomm = g_variant_get_string(src->data, NULL); | |
105 | break; | |
106 | } | |
107 | } | |
108 | if (!conn) | |
109 | return NULL; | |
110 | if (!serialcomm) | |
111 | serialcomm = SERIALCOMM; | |
112 | ||
113 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
114 | ||
115 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
116 | return NULL; | |
117 | ||
118 | serial_flush(serial); | |
119 | if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) { | |
120 | sr_err("Unable to send identification string."); | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | len = 128; | |
125 | if (!(buf = g_try_malloc(len))) { | |
126 | sr_err("Serial buffer malloc failed."); | |
127 | return NULL; | |
128 | } | |
129 | serial_readline(serial, &buf, &len, 250); | |
130 | if (!len) | |
131 | return NULL; | |
132 | ||
133 | tokens = g_strsplit(buf, ",", 4); | |
134 | if (!strcmp("Agilent Technologies", tokens[0]) | |
135 | && tokens[1] && tokens[2] && tokens[3]) { | |
136 | for (i = 0; supported_agdmm[i].model; i++) { | |
137 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
138 | continue; | |
139 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
140 | sdi->status = SR_ST_INACTIVE; | |
141 | sdi->vendor = g_strdup("Agilent"); | |
142 | sdi->model = g_strdup(tokens[1]); | |
143 | sdi->version = g_strdup(tokens[3]); | |
144 | devc = g_malloc0(sizeof(struct dev_context)); | |
145 | devc->profile = &supported_agdmm[i]; | |
146 | devc->cur_mq = -1; | |
147 | sdi->inst_type = SR_INST_SERIAL; | |
148 | sdi->conn = serial; | |
149 | sdi->priv = devc; | |
150 | sdi->driver = di; | |
151 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
152 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
153 | devices = g_slist_append(devices, sdi); | |
154 | break; | |
155 | } | |
156 | } | |
157 | g_strfreev(tokens); | |
158 | g_free(buf); | |
159 | ||
160 | serial_close(serial); | |
161 | if (!devices) | |
162 | sr_serial_dev_inst_free(serial); | |
163 | ||
164 | return devices; | |
165 | } | |
166 | ||
167 | static GSList *dev_list(const struct sr_dev_driver *di) | |
168 | { | |
169 | return ((struct drv_context *)(di->priv))->instances; | |
170 | } | |
171 | ||
172 | static int cleanup(const struct sr_dev_driver *di) | |
173 | { | |
174 | return std_dev_clear(di, NULL); | |
175 | } | |
176 | ||
177 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
178 | const struct sr_channel_group *cg) | |
179 | { | |
180 | struct dev_context *devc; | |
181 | ||
182 | (void)cg; | |
183 | ||
184 | if (sdi->status != SR_ST_ACTIVE) | |
185 | return SR_ERR_DEV_CLOSED; | |
186 | ||
187 | if (!(devc = sdi->priv)) { | |
188 | sr_err("sdi->priv was NULL."); | |
189 | return SR_ERR_BUG; | |
190 | } | |
191 | ||
192 | switch (key) { | |
193 | case SR_CONF_LIMIT_MSEC: | |
194 | /* TODO: not yet implemented */ | |
195 | if (g_variant_get_uint64(data) == 0) { | |
196 | sr_err("LIMIT_MSEC can't be 0."); | |
197 | return SR_ERR; | |
198 | } | |
199 | devc->limit_msec = g_variant_get_uint64(data); | |
200 | sr_dbg("Setting time limit to %" PRIu64 "ms.", | |
201 | devc->limit_msec); | |
202 | break; | |
203 | case SR_CONF_LIMIT_SAMPLES: | |
204 | devc->limit_samples = g_variant_get_uint64(data); | |
205 | sr_dbg("Setting sample limit to %" PRIu64 ".", | |
206 | devc->limit_samples); | |
207 | break; | |
208 | default: | |
209 | return SR_ERR_NA; | |
210 | } | |
211 | ||
212 | return SR_OK; | |
213 | } | |
214 | ||
215 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
216 | const struct sr_channel_group *cg) | |
217 | { | |
218 | (void)sdi; | |
219 | (void)cg; | |
220 | ||
221 | switch (key) { | |
222 | case SR_CONF_SCAN_OPTIONS: | |
223 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
224 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
225 | break; | |
226 | case SR_CONF_DEVICE_OPTIONS: | |
227 | if (!sdi) | |
228 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
229 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
230 | else | |
231 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
232 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
233 | break; | |
234 | default: | |
235 | return SR_ERR_NA; | |
236 | } | |
237 | ||
238 | return SR_OK; | |
239 | } | |
240 | ||
241 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) | |
242 | { | |
243 | struct dev_context *devc; | |
244 | struct sr_serial_dev_inst *serial; | |
245 | ||
246 | if (sdi->status != SR_ST_ACTIVE) | |
247 | return SR_ERR_DEV_CLOSED; | |
248 | ||
249 | if (!(devc = sdi->priv)) { | |
250 | sr_err("sdi->priv was NULL."); | |
251 | return SR_ERR_BUG; | |
252 | } | |
253 | ||
254 | devc->cb_data = cb_data; | |
255 | ||
256 | /* Send header packet to the session bus. */ | |
257 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
258 | ||
259 | /* Poll every 100ms, or whenever some data comes in. */ | |
260 | serial = sdi->conn; | |
261 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
262 | agdmm_receive_data, (void *)sdi); | |
263 | ||
264 | return SR_OK; | |
265 | } | |
266 | ||
267 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
268 | { | |
269 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, | |
270 | sdi->conn, LOG_PREFIX); | |
271 | } | |
272 | ||
273 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
274 | .name = "agilent-dmm", | |
275 | .longname = "Agilent U12xx series DMMs", | |
276 | .api_version = 1, | |
277 | .init = init, | |
278 | .cleanup = cleanup, | |
279 | .scan = scan, | |
280 | .dev_list = dev_list, | |
281 | .dev_clear = NULL, | |
282 | .config_get = NULL, | |
283 | .config_set = config_set, | |
284 | .config_list = config_list, | |
285 | .dev_open = std_serial_dev_open, | |
286 | .dev_close = std_serial_dev_close, | |
287 | .dev_acquisition_start = dev_acquisition_start, | |
288 | .dev_acquisition_stop = dev_acquisition_stop, | |
289 | .priv = NULL, | |
290 | }; |