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