]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/agilent-dmm/api.c
agilent-dmm: File naming consistency changes.
[libsigrok.git] / src / hardware / agilent-dmm / api.c
... / ...
CommitLineData
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 <config.h>
21#include <glib.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <fcntl.h>
25#include <string.h>
26#include <libsigrok/libsigrok.h>
27#include "libsigrok-internal.h"
28#include "protocol.h"
29
30static const uint32_t scanopts[] = {
31 SR_CONF_CONN,
32 SR_CONF_SERIALCOMM,
33};
34
35static const uint32_t drvopts[] = {
36 SR_CONF_MULTIMETER,
37};
38
39static const uint32_t devopts[] = {
40 SR_CONF_CONTINUOUS,
41 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
42 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
43};
44
45extern const struct agdmm_job agdmm_jobs_u12xx[];
46extern const struct agdmm_recv agdmm_recvs_u123x[];
47extern const struct agdmm_recv agdmm_recvs_u124x[];
48extern const struct agdmm_recv agdmm_recvs_u125x[];
49extern const struct agdmm_recv agdmm_recvs_u128x[];
50
51/* This works on all the Agilent U12xxA series, although the
52 * U127xA can apparently also run at 19200/8n1. */
53#define SERIALCOMM "9600/8n1"
54
55static const struct agdmm_profile supported_agdmm[] = {
56 { AGILENT_U1231, "U1231A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
57 { AGILENT_U1232, "U1232A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
58 { AGILENT_U1233, "U1233A", agdmm_jobs_u12xx, agdmm_recvs_u123x },
59
60 { AGILENT_U1241, "U1241A", agdmm_jobs_u12xx, agdmm_recvs_u124x },
61 { AGILENT_U1242, "U1242A", agdmm_jobs_u12xx, agdmm_recvs_u124x },
62 { AGILENT_U1241, "U1241B", agdmm_jobs_u12xx, agdmm_recvs_u124x },
63 { AGILENT_U1242, "U1242B", agdmm_jobs_u12xx, agdmm_recvs_u124x },
64
65 { AGILENT_U1251, "U1251A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
66 { AGILENT_U1252, "U1252A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
67 { AGILENT_U1253, "U1253A", agdmm_jobs_u12xx, agdmm_recvs_u125x },
68 { AGILENT_U1251, "U1251B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
69 { AGILENT_U1252, "U1252B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
70 { AGILENT_U1253, "U1253B", agdmm_jobs_u12xx, agdmm_recvs_u125x },
71
72 { KEYSIGHT_U1281, "U1281A", agdmm_jobs_u12xx, agdmm_recvs_u128x },
73 { KEYSIGHT_U1282, "U1282A", agdmm_jobs_u12xx, agdmm_recvs_u128x },
74 ALL_ZERO
75};
76
77static GSList *scan(struct sr_dev_driver *di, GSList *options)
78{
79 struct sr_dev_inst *sdi;
80 struct dev_context *devc;
81 struct sr_config *src;
82 struct sr_serial_dev_inst *serial;
83 GSList *l, *devices;
84 int len, i;
85 const char *conn, *serialcomm;
86 char *buf, **tokens;
87
88 devices = NULL;
89 conn = serialcomm = NULL;
90 for (l = options; l; l = l->next) {
91 src = l->data;
92 switch (src->key) {
93 case SR_CONF_CONN:
94 conn = g_variant_get_string(src->data, NULL);
95 break;
96 case SR_CONF_SERIALCOMM:
97 serialcomm = g_variant_get_string(src->data, NULL);
98 break;
99 }
100 }
101 if (!conn)
102 return NULL;
103 if (!serialcomm)
104 serialcomm = SERIALCOMM;
105
106 serial = sr_serial_dev_inst_new(conn, serialcomm);
107
108 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
109 return NULL;
110
111 serial_flush(serial);
112 if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) {
113 sr_err("Unable to send identification string.");
114 return NULL;
115 }
116
117 len = 128;
118 buf = g_malloc(len);
119 serial_readline(serial, &buf, &len, 250);
120 if (!len)
121 return NULL;
122
123 tokens = g_strsplit(buf, ",", 4);
124 if ((!strcmp("Agilent Technologies", tokens[0]) ||
125 !strcmp("Keysight Technologies", tokens[0]))
126 && tokens[1] && tokens[2] && tokens[3]) {
127 for (i = 0; supported_agdmm[i].model; i++) {
128 if (strcmp(supported_agdmm[i].modelname, tokens[1]))
129 continue;
130 sdi = g_malloc0(sizeof(struct sr_dev_inst));
131 sdi->status = SR_ST_INACTIVE;
132 sdi->vendor = g_strdup(tokens[0][0] == 'A' ? "Agilent" : "Keysight");
133 sdi->model = g_strdup(tokens[1]);
134 sdi->version = g_strdup(tokens[3]);
135 devc = g_malloc0(sizeof(struct dev_context));
136 sr_sw_limits_init(&devc->limits);
137 devc->profile = &supported_agdmm[i];
138 devc->cur_mq = 0;
139 sdi->inst_type = SR_INST_SERIAL;
140 sdi->conn = serial;
141 sdi->priv = devc;
142 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
143 devices = g_slist_append(devices, sdi);
144 break;
145 }
146 }
147 g_strfreev(tokens);
148 g_free(buf);
149
150 serial_close(serial);
151 if (!devices)
152 sr_serial_dev_inst_free(serial);
153
154 return std_scan_complete(di, devices);
155}
156
157static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
158 const struct sr_channel_group *cg)
159{
160 struct dev_context *devc;
161
162 (void)cg;
163
164 if (sdi->status != SR_ST_ACTIVE)
165 return SR_ERR_DEV_CLOSED;
166
167 devc = sdi->priv;
168
169 return sr_sw_limits_config_set(&devc->limits, key, data);
170}
171
172static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
173 const struct sr_channel_group *cg)
174{
175 (void)cg;
176
177 switch (key) {
178 case SR_CONF_SCAN_OPTIONS:
179 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
180 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
181 break;
182 case SR_CONF_DEVICE_OPTIONS:
183 if (!sdi)
184 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
185 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
186 else
187 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
188 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
189 break;
190 default:
191 return SR_ERR_NA;
192 }
193
194 return SR_OK;
195}
196
197static int dev_acquisition_start(const struct sr_dev_inst *sdi)
198{
199 struct dev_context *devc = sdi->priv;
200 struct sr_serial_dev_inst *serial;
201
202 if (sdi->status != SR_ST_ACTIVE)
203 return SR_ERR_DEV_CLOSED;
204
205 sr_sw_limits_acquisition_start(&devc->limits);
206 std_session_send_df_header(sdi);
207
208 /* Poll every 100ms, or whenever some data comes in. */
209 serial = sdi->conn;
210 serial_source_add(sdi->session, serial, G_IO_IN, 100,
211 agdmm_receive_data, (void *)sdi);
212
213 return SR_OK;
214}
215
216static struct sr_dev_driver agdmm_driver_info = {
217 .name = "agilent-dmm",
218 .longname = "Agilent U12xx series DMMs",
219 .api_version = 1,
220 .init = std_init,
221 .cleanup = std_cleanup,
222 .scan = scan,
223 .dev_list = std_dev_list,
224 .dev_clear = NULL,
225 .config_get = NULL,
226 .config_set = config_set,
227 .config_list = config_list,
228 .dev_open = std_serial_dev_open,
229 .dev_close = std_serial_dev_close,
230 .dev_acquisition_start = dev_acquisition_start,
231 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
232 .context = NULL,
233};
234SR_REGISTER_DEV_DRIVER(agdmm_driver_info);