]>
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 | |
e44ac12a | 30 | static const int32_t hwopts[] = { |
1953564a BV |
31 | SR_CONF_CONN, |
32 | SR_CONF_SERIALCOMM, | |
e93cdf42 BV |
33 | }; |
34 | ||
e44ac12a | 35 | static const int32_t hwcaps[] = { |
1953564a BV |
36 | SR_CONF_MULTIMETER, |
37 | SR_CONF_LIMIT_SAMPLES, | |
38 | SR_CONF_LIMIT_MSEC, | |
39 | SR_CONF_CONTINUOUS, | |
e93cdf42 BV |
40 | }; |
41 | ||
8c0152f2 BV |
42 | extern const struct agdmm_job agdmm_jobs_u123x[]; |
43 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
44 | extern const struct agdmm_job agdmm_jobs_u125x[]; | |
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[] = { |
74ac7d7f BV |
52 | { AGILENT_U1231A, "U1231A", agdmm_jobs_u123x, agdmm_recvs_u123x }, |
53 | { AGILENT_U1232A, "U1232A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
54 | { AGILENT_U1233A, "U1233A", agdmm_jobs_u123x, agdmm_recvs_u123x }, | |
55 | { AGILENT_U1251A, "U1251A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
56 | { AGILENT_U1252A, "U1252A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
57 | { AGILENT_U1253A, "U1253A", agdmm_jobs_u125x, agdmm_recvs_u125x }, | |
58 | { 0, NULL, NULL, NULL } | |
e93cdf42 BV |
59 | }; |
60 | ||
61 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
62 | static struct sr_dev_driver *di = &agdmm_driver_info; | |
63 | ||
6078d2c9 | 64 | static int init(struct sr_context *sr_ctx) |
e93cdf42 | 65 | { |
f6beaac5 | 66 | return std_init(sr_ctx, di, LOG_PREFIX); |
e93cdf42 BV |
67 | } |
68 | ||
6078d2c9 | 69 | static GSList *scan(GSList *options) |
e93cdf42 BV |
70 | { |
71 | struct sr_dev_inst *sdi; | |
72 | struct drv_context *drvc; | |
73 | struct dev_context *devc; | |
1987b8d6 | 74 | struct sr_config *src; |
ba7dd8bb | 75 | struct sr_channel *ch; |
109a3ba4 | 76 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 77 | GSList *l, *devices; |
109a3ba4 | 78 | int len, i; |
e93cdf42 BV |
79 | const char *conn, *serialcomm; |
80 | char *buf, **tokens; | |
81 | ||
82 | drvc = di->priv; | |
83 | drvc->instances = NULL; | |
84 | ||
85 | devices = NULL; | |
86 | conn = serialcomm = NULL; | |
87 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
88 | src = l->data; |
89 | switch (src->key) { | |
1953564a | 90 | case SR_CONF_CONN: |
e44ac12a | 91 | conn = g_variant_get_string(src->data, NULL); |
e93cdf42 | 92 | break; |
1953564a | 93 | case SR_CONF_SERIALCOMM: |
e44ac12a | 94 | serialcomm = g_variant_get_string(src->data, NULL); |
e93cdf42 BV |
95 | break; |
96 | } | |
97 | } | |
74ac7d7f | 98 | if (!conn) |
e93cdf42 | 99 | return NULL; |
109a3ba4 BV |
100 | if (!serialcomm) |
101 | serialcomm = SERIALCOMM; | |
e93cdf42 | 102 | |
109a3ba4 | 103 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
e93cdf42 | 104 | return NULL; |
e93cdf42 | 105 | |
a54dd31e | 106 | if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) |
e93cdf42 | 107 | return NULL; |
e93cdf42 | 108 | |
109a3ba4 BV |
109 | serial_flush(serial); |
110 | if (serial_write(serial, "*IDN?\r\n", 7) == -1) { | |
38d326e8 UH |
111 | sr_err("Unable to send identification string: %s.", |
112 | strerror(errno)); | |
e93cdf42 BV |
113 | return NULL; |
114 | } | |
115 | ||
116 | len = 128; | |
886a52b6 UH |
117 | if (!(buf = g_try_malloc(len))) { |
118 | sr_err("Serial buffer malloc failed."); | |
119 | return NULL; | |
120 | } | |
109a3ba4 | 121 | serial_readline(serial, &buf, &len, 150); |
e93cdf42 BV |
122 | if (!len) |
123 | return NULL; | |
124 | ||
125 | tokens = g_strsplit(buf, ",", 4); | |
126 | if (!strcmp("Agilent Technologies", tokens[0]) | |
fbf07e02 | 127 | && tokens[1] && tokens[2] && tokens[3]) { |
e93cdf42 BV |
128 | for (i = 0; supported_agdmm[i].model; i++) { |
129 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
130 | continue; | |
67d6f6fc | 131 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Agilent", |
e93cdf42 BV |
132 | tokens[1], tokens[3]))) |
133 | return NULL; | |
134 | if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { | |
886a52b6 | 135 | sr_err("Device context malloc failed."); |
e93cdf42 BV |
136 | return NULL; |
137 | } | |
138 | devc->profile = &supported_agdmm[i]; | |
e93cdf42 | 139 | devc->cur_mq = -1; |
fb3a1505 BV |
140 | sdi->inst_type = SR_INST_SERIAL; |
141 | sdi->conn = serial; | |
e93cdf42 BV |
142 | sdi->priv = devc; |
143 | sdi->driver = di; | |
3f239f08 | 144 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) |
e93cdf42 | 145 | return NULL; |
ba7dd8bb | 146 | sdi->channels = g_slist_append(sdi->channels, ch); |
e93cdf42 BV |
147 | drvc->instances = g_slist_append(drvc->instances, sdi); |
148 | devices = g_slist_append(devices, sdi); | |
149 | break; | |
150 | } | |
151 | } | |
152 | g_strfreev(tokens); | |
153 | g_free(buf); | |
154 | ||
109a3ba4 BV |
155 | serial_close(serial); |
156 | if (!devices) | |
157 | sr_serial_dev_inst_free(serial); | |
e93cdf42 BV |
158 | |
159 | return devices; | |
160 | } | |
161 | ||
6078d2c9 | 162 | static GSList *dev_list(void) |
e93cdf42 | 163 | { |
0e94d524 | 164 | return ((struct drv_context *)(di->priv))->instances; |
e93cdf42 BV |
165 | } |
166 | ||
6078d2c9 | 167 | static int cleanup(void) |
e93cdf42 | 168 | { |
a6630742 | 169 | return std_dev_clear(di, NULL); |
e93cdf42 BV |
170 | } |
171 | ||
8f996b89 | 172 | static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 173 | const struct sr_channel_group *cg) |
e93cdf42 BV |
174 | { |
175 | struct dev_context *devc; | |
176 | ||
53b4680f | 177 | (void)cg; |
8f996b89 | 178 | |
e93cdf42 | 179 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 180 | return SR_ERR_DEV_CLOSED; |
e93cdf42 BV |
181 | |
182 | if (!(devc = sdi->priv)) { | |
38d326e8 | 183 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
184 | return SR_ERR_BUG; |
185 | } | |
186 | ||
035a1078 | 187 | switch (id) { |
1953564a | 188 | case SR_CONF_LIMIT_MSEC: |
e93cdf42 | 189 | /* TODO: not yet implemented */ |
e44ac12a | 190 | if (g_variant_get_uint64(data) == 0) { |
38d326e8 | 191 | sr_err("LIMIT_MSEC can't be 0."); |
e93cdf42 BV |
192 | return SR_ERR; |
193 | } | |
e44ac12a | 194 | devc->limit_msec = g_variant_get_uint64(data); |
38d326e8 | 195 | sr_dbg("Setting time limit to %" PRIu64 "ms.", |
e93cdf42 BV |
196 | devc->limit_msec); |
197 | break; | |
1953564a | 198 | case SR_CONF_LIMIT_SAMPLES: |
e44ac12a | 199 | devc->limit_samples = g_variant_get_uint64(data); |
38d326e8 | 200 | sr_dbg("Setting sample limit to %" PRIu64 ".", |
e93cdf42 BV |
201 | devc->limit_samples); |
202 | break; | |
203 | default: | |
bd6fbf62 | 204 | return SR_ERR_NA; |
e93cdf42 BV |
205 | } |
206 | ||
207 | return SR_OK; | |
208 | } | |
209 | ||
8f996b89 | 210 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 211 | const struct sr_channel_group *cg) |
a1c743fc | 212 | { |
a1c743fc | 213 | (void)sdi; |
53b4680f | 214 | (void)cg; |
a1c743fc BV |
215 | |
216 | switch (key) { | |
0d485e30 | 217 | case SR_CONF_SCAN_OPTIONS: |
e44ac12a BV |
218 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
219 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
0d485e30 | 220 | break; |
9a6517d1 | 221 | case SR_CONF_DEVICE_OPTIONS: |
e44ac12a BV |
222 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, |
223 | hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t)); | |
9a6517d1 | 224 | break; |
a1c743fc | 225 | default: |
bd6fbf62 | 226 | return SR_ERR_NA; |
a1c743fc BV |
227 | } |
228 | ||
229 | return SR_OK; | |
230 | } | |
231 | ||
6078d2c9 | 232 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 233 | { |
e93cdf42 | 234 | struct dev_context *devc; |
fb3a1505 | 235 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 236 | |
e73ffd42 BV |
237 | if (sdi->status != SR_ST_ACTIVE) |
238 | return SR_ERR_DEV_CLOSED; | |
239 | ||
e93cdf42 | 240 | if (!(devc = sdi->priv)) { |
38d326e8 | 241 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
242 | return SR_ERR_BUG; |
243 | } | |
244 | ||
e93cdf42 BV |
245 | devc->cb_data = cb_data; |
246 | ||
247 | /* Send header packet to the session bus. */ | |
29a27196 | 248 | std_session_send_df_header(cb_data, LOG_PREFIX); |
e93cdf42 | 249 | |
e93cdf42 | 250 | /* Poll every 100ms, or whenever some data comes in. */ |
fb3a1505 | 251 | serial = sdi->conn; |
abc4b335 | 252 | serial_source_add(serial, G_IO_IN, 100, agdmm_receive_data, (void *)sdi); |
e93cdf42 BV |
253 | |
254 | return SR_OK; | |
255 | } | |
256 | ||
6078d2c9 | 257 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 258 | { |
d43b0908 | 259 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, |
29a27196 | 260 | sdi->conn, LOG_PREFIX); |
e93cdf42 BV |
261 | } |
262 | ||
263 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
264 | .name = "agilent-dmm", | |
265 | .longname = "Agilent U12xx series DMMs", | |
266 | .api_version = 1, | |
6078d2c9 UH |
267 | .init = init, |
268 | .cleanup = cleanup, | |
269 | .scan = scan, | |
270 | .dev_list = dev_list, | |
a6630742 | 271 | .dev_clear = NULL, |
6fab7b8f | 272 | .config_get = NULL, |
035a1078 | 273 | .config_set = config_set, |
a1c743fc | 274 | .config_list = config_list, |
854434de | 275 | .dev_open = std_serial_dev_open, |
bf2c987f | 276 | .dev_close = std_serial_dev_close, |
6078d2c9 UH |
277 | .dev_acquisition_start = dev_acquisition_start, |
278 | .dev_acquisition_stop = dev_acquisition_stop, | |
e93cdf42 BV |
279 | .priv = NULL, |
280 | }; |