]> sigrok.org Git - libsigrok.git/blob - src/hardware/scpi-pps/profiles.c
strcasecmp() is defined in strings.h.
[libsigrok.git] / src / hardware / scpi-pps / profiles.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 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 <string.h>
21 #include <strings.h>
22 #include "protocol.h"
23
24 #define CH_IDX(x) (1 << x)
25
26 const char *pps_vendors[][2] = {
27         { "RIGOL TECHNOLOGIES", "Rigol" },
28         { "HEWLETT-PACKARD", "HP" },
29         { "PHILIPS", "Philips" },
30 };
31
32 const char *get_vendor(const char *raw_vendor)
33 {
34         unsigned int i;
35
36         for (i = 0; i < ARRAY_SIZE(pps_vendors); i++) {
37                 if (!strcasecmp(raw_vendor, pps_vendors[i][0]))
38                         return pps_vendors[i][1];
39         }
40
41         return raw_vendor;
42 }
43
44 static const uint32_t devopts_none[] = { };
45
46 /* Rigol DP800 series */
47 static const uint32_t rigol_dp800_devopts[] = {
48         SR_CONF_CONTINUOUS | SR_CONF_SET,
49         SR_CONF_OVER_TEMPERATURE_PROTECTION | SR_CONF_GET | SR_CONF_SET,
50 };
51
52 static const uint32_t rigol_dp800_devopts_cg[] = {
53         SR_CONF_OUTPUT_REGULATION | SR_CONF_GET,
54         SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET,
55         SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
56         SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
57         SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET,
58         SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
59         SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
60         SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
61         SR_CONF_OUTPUT_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
62         SR_CONF_OUTPUT_CURRENT | SR_CONF_GET,
63         SR_CONF_OUTPUT_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
64         SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
65 };
66
67 const struct channel_spec rigol_dp821a_ch[] = {
68         { "1", { 0, 60, 0.001 }, { 0, 1, 0.0001 } },
69         { "2", { 0, 8, 0.001 }, { 0, 10, 0.001 } },
70 };
71
72 const struct channel_spec rigol_dp831_ch[] = {
73         { "1", { 0, 8, 0.001 }, { 0, 5, 0.0003 } },
74         { "2", { 0, 30, 0.001 }, { 0, 2, 0.0001 } },
75         { "3", { 0, -30, 0.001 }, { 0, 2, 0.0001 } },
76 };
77
78 const struct channel_spec rigol_dp832_ch[] = {
79         { "1", { 0, 30, 0.001 }, { 0, 3, 0.001 } },
80         { "2", { 0, 30, 0.001 }, { 0, 3, 0.001 } },
81         { "3", { 0, 5, 0.001 }, { 0, 3, 0.001 } },
82 };
83
84 const struct channel_group_spec rigol_dp820_cg[] = {
85         { "1", CH_IDX(0), PPS_OVP | PPS_OCP },
86         { "2", CH_IDX(1), PPS_OVP | PPS_OCP },
87 };
88
89 const struct channel_group_spec rigol_dp830_cg[] = {
90         { "1", CH_IDX(0), PPS_OVP | PPS_OCP },
91         { "2", CH_IDX(1), PPS_OVP | PPS_OCP },
92         { "3", CH_IDX(2), PPS_OVP | PPS_OCP },
93 };
94
95 const struct scpi_command rigol_dp800_cmd[] = {
96         { SCPI_CMD_REMOTE, "SYST:REMOTE" },
97         { SCPI_CMD_LOCAL, "SYST:LOCAL" },
98         { SCPI_CMD_BEEPER, "SYST:BEEP:STAT?" },
99         { SCPI_CMD_BEEPER_ENABLE, "SYST:BEEP:STAT ON" },
100         { SCPI_CMD_BEEPER_DISABLE, "SYST:BEEP:STAT OFF" },
101         { SCPI_CMD_SELECT_CHANNEL, ":INST:NSEL %s" },
102         { SCPI_CMD_GET_MEAS_VOLTAGE, ":MEAS:VOLT?" },
103         { SCPI_CMD_GET_MEAS_CURRENT, ":MEAS:CURR?" },
104         { SCPI_CMD_GET_MEAS_POWER, ":MEAS:POWE?" },
105         { SCPI_CMD_GET_VOLTAGE_TARGET, ":SOUR:VOLT?" },
106         { SCPI_CMD_SET_VOLTAGE_TARGET, ":SOUR:VOLT %.6f" },
107         { SCPI_CMD_GET_CURRENT_LIMIT, ":SOUR:CURR?" },
108         { SCPI_CMD_SET_CURRENT_LIMIT, ":SOUR:CURR %.6f" },
109         { SCPI_CMD_GET_OUTPUT_ENABLED, ":OUTP?" },
110         { SCPI_CMD_SET_OUTPUT_ENABLE, ":OUTP ON" },
111         { SCPI_CMD_SET_OUTPUT_DISABLE, ":OUTP OFF" },
112         { SCPI_CMD_GET_OUTPUT_REGULATION, ":OUTP:MODE?" },
113         { SCPI_CMD_GET_OVER_TEMPERATURE_PROTECTION, ":SYST:OTP?" },
114         { SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_ENABLE, ":SYST:OTP ON" },
115         { SCPI_CMD_SET_OVER_TEMPERATURE_PROTECTION_DISABLE, ":SYST:OTP OFF" },
116         { SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ENABLED, ":OUTP:OVP?" },
117         { SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_ENABLE, ":OUTP:OVP ON" },
118         { SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_DISABLE, ":OUTP:OVP OFF" },
119         { SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE, ":OUTP:OVP:QUES?" },
120         { SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD, ":OUTP:OVP:VAL?" },
121         { SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, ":OUTP:OVP:VAL %.6f" },
122         { SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED, ":OUTP:OCP?" },
123         { SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE, ":OUTP:OCP:STAT ON" },
124         { SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE, ":OUTP:OCP:STAT OFF" },
125         { SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE, ":OUTP:OCP:QUES?" },
126         { SCPI_CMD_GET_OVER_CURRENT_PROTECTION_THRESHOLD, ":OUTP:OCP:VAL?" },
127         { SCPI_CMD_SET_OVER_CURRENT_PROTECTION_THRESHOLD, ":OUTP:OCP:VAL %.6f" },
128 };
129
130 /* HP 663xx series */
131 static const uint32_t hp_6632b_devopts[] = {
132         SR_CONF_CONTINUOUS | SR_CONF_SET,
133         SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
134         SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
135         SR_CONF_OUTPUT_CURRENT | SR_CONF_GET,
136         SR_CONF_OUTPUT_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
137         SR_CONF_OUTPUT_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
138 };
139
140 const struct channel_spec hp_6632b_ch[] = {
141         { "1", { 0, 20.475, 0.005 }, { 0, 5.1188, 0.00132 } },
142 };
143
144 const struct channel_group_spec hp_6632b_cg[] = {
145         { "1", CH_IDX(0), 0 },
146 };
147
148 const struct scpi_command hp_6632b_cmd[] = {
149         { SCPI_CMD_GET_OUTPUT_ENABLED, "OUTP:STAT?" },
150         { SCPI_CMD_SET_OUTPUT_ENABLE, "OUTP:STAT ON" },
151         { SCPI_CMD_SET_OUTPUT_DISABLE, "OUTP:STAT OFF" },
152         { SCPI_CMD_GET_MEAS_VOLTAGE, ":MEAS:VOLT?" },
153         { SCPI_CMD_GET_MEAS_CURRENT, ":MEAS:CURR?" },
154         { SCPI_CMD_GET_VOLTAGE_TARGET, ":SOUR:VOLT?" },
155         { SCPI_CMD_SET_VOLTAGE_TARGET, ":SOUR:VOLT %.6f" },
156         { SCPI_CMD_GET_CURRENT_LIMIT, ":SOUR:CURR?" },
157         { SCPI_CMD_SET_CURRENT_LIMIT, ":SOUR:CURR %.6f" },
158 };
159
160 /* Philips/Fluke PM2800 series */
161 static const uint32_t philips_pm2800_devopts[] = {
162         SR_CONF_CONTINUOUS | SR_CONF_SET,
163 };
164
165 static const uint32_t philips_pm2800_devopts_cg[] = {
166         SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
167         SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
168         SR_CONF_OUTPUT_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
169         SR_CONF_OUTPUT_CURRENT | SR_CONF_GET,
170         SR_CONF_OUTPUT_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
171         SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
172         SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
173         SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET,
174         SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
175         SR_CONF_OUTPUT_REGULATION | SR_CONF_GET,
176 };
177
178 enum philips_pm2800_modules {
179         PM2800_MOD_30V_10A = 1,
180         PM2800_MOD_60V_5A,
181         PM2800_MOD_60V_10A,
182         PM2800_MOD_8V_15A,
183         PM2800_MOD_60V_2A,
184         PM2800_MOD_120V_1A,
185 };
186
187 static const struct philips_pm2800_module_spec {
188         /* Min, max, programming resolution. */
189         float voltage[3];
190         float current[3];
191 } philips_pm2800_module_specs[] = {
192         /* Autoranging modules. */
193         [PM2800_MOD_30V_10A] = { { 0, 30, 0.0075 }, { 0, 10, 0.0025 } },
194         [PM2800_MOD_60V_5A] = { { 0, 60, 0.015 }, { 0, 5, 0.00125 } },
195         [PM2800_MOD_60V_10A] = { { 0, 60, 0.015 }, { 0, 10, 0.0025 } },
196         /* Linear modules. */
197         [PM2800_MOD_8V_15A] = { { 0, 8, 0.002 }, { -15, 15, 0.00375 } },
198         [PM2800_MOD_60V_2A] = { { 0, 60, 0.015 }, { -2, 2, 0.0005 } },
199         [PM2800_MOD_120V_1A] = { { 0, 120, 0.030 }, { -1, 1, 0.00025 } },
200 };
201
202 static const struct philips_pm2800_model {
203         unsigned int chassis;
204         unsigned int num_modules;
205         unsigned int set;
206         unsigned int modules[3];
207 } philips_pm2800_matrix[] = {
208         /* Autoranging chassis. */
209         { 1, 1, 0, { PM2800_MOD_30V_10A, 0, 0 } },
210         { 1, 1, 1, { PM2800_MOD_60V_5A, 0, 0 } },
211         { 1, 2, 0, { PM2800_MOD_30V_10A, PM2800_MOD_30V_10A, 0 } },
212         { 1, 2, 1, { PM2800_MOD_60V_5A, PM2800_MOD_60V_5A, 0 } },
213         { 1, 2, 2, { PM2800_MOD_30V_10A, PM2800_MOD_60V_5A, 0 } },
214         { 1, 2, 3, { PM2800_MOD_30V_10A, PM2800_MOD_60V_10A, 0 } },
215         { 1, 2, 4, { PM2800_MOD_60V_5A, PM2800_MOD_60V_10A, 0 } },
216         { 1, 3, 0, { PM2800_MOD_30V_10A, PM2800_MOD_30V_10A, PM2800_MOD_30V_10A } },
217         { 1, 3, 1, { PM2800_MOD_60V_5A, PM2800_MOD_60V_5A, PM2800_MOD_60V_5A } },
218         { 1, 3, 2, { PM2800_MOD_30V_10A, PM2800_MOD_30V_10A, PM2800_MOD_60V_5A } },
219         { 1, 3, 3, { PM2800_MOD_30V_10A, PM2800_MOD_60V_5A, PM2800_MOD_60V_5A } },
220         /* Linear chassis. */
221         { 3, 1, 0, { PM2800_MOD_60V_2A, 0, 0 } },
222         { 3, 1, 1, { PM2800_MOD_120V_1A, 0, 0 } },
223         { 3, 1, 2, { PM2800_MOD_8V_15A, 0, 0 } },
224         { 3, 2, 0, { PM2800_MOD_60V_2A, 0, 0 } },
225         { 3, 2, 1, { PM2800_MOD_120V_1A, 0, 0 } },
226         { 3, 2, 2, { PM2800_MOD_60V_2A, PM2800_MOD_120V_1A, 0 } },
227         { 3, 2, 3, { PM2800_MOD_8V_15A, PM2800_MOD_8V_15A, 0 } },
228 };
229
230 static const char *philips_pm2800_names[] = { "1", "2", "3" };
231
232 static int philips_pm2800_probe_channels(struct sr_dev_inst *sdi,
233                 struct sr_scpi_hw_info *hw_info,
234                 struct channel_spec **channels, unsigned int *num_channels,
235                 struct channel_group_spec **channel_groups, unsigned int *num_channel_groups)
236 {
237         const struct philips_pm2800_model *model;
238         const struct philips_pm2800_module_spec *spec;
239         unsigned int chassis, num_modules, set, module, m, i;
240
241         (void)sdi;
242
243         /*
244          * The model number as reported by *IDN? looks like e.g. PM2813/11,
245          * Where "PM28" is fixed, followed by the chassis code (1 = autoranging,
246          * 3 = linear series) and the number of modules: 1-3 for autoranging,
247          * 1-2 for linear.
248          * After the slash, the first digit denotes the module set. The
249          * digit after that denotes front (5) or rear (1) binding posts.
250          */
251         chassis = hw_info->model[4] - 0x30;
252         num_modules = hw_info->model[5] - 0x30;
253         set = hw_info->model[7] - 0x30;
254         for (m = 0; m < ARRAY_SIZE(philips_pm2800_matrix); m++) {
255                 model = &philips_pm2800_matrix[m];
256                 if (model->chassis == chassis && model->num_modules == num_modules
257                                 && model->set == set)
258                         break;
259         }
260         if (m == ARRAY_SIZE(philips_pm2800_matrix)) {
261                 sr_dbg("Model %s not found in matrix.", hw_info->model);
262                 return SR_ERR;
263         }
264
265         sr_dbg("Found %d output channel%s:", num_modules, num_modules > 1 ? "s" : "");
266         *channels = g_malloc0(sizeof(struct channel_spec) * num_modules);
267         *channel_groups = g_malloc0(sizeof(struct channel_group_spec) * num_modules);
268         for (i = 0; i < num_modules; i++) {
269                 module = model->modules[i];
270                 spec = &philips_pm2800_module_specs[module];
271                 sr_dbg("output %d: %.0f - %.0fV, %.0f - %.0fA", i + 1,
272                                 spec->voltage[0], spec->voltage[1],
273                                 spec->current[0], spec->current[1]);
274                 (*channels)[i].name = (char *)philips_pm2800_names[i];
275                 memcpy(&((*channels)[i].voltage), spec, sizeof(float) * 6);
276                 (*channel_groups)[i].name = (char *)philips_pm2800_names[i];
277                 (*channel_groups)[i].channel_index_mask = 1 << i;
278                 (*channel_groups)[i].features = PPS_OTP | PPS_OVP | PPS_OCP;
279         }
280         *num_channels = *num_channel_groups = num_modules;
281
282         return SR_OK;
283 }
284
285 const struct scpi_command philips_pm2800_cmd[] = {
286         { SCPI_CMD_SELECT_CHANNEL, ":INST:NSEL %s" },
287         { SCPI_CMD_GET_MEAS_VOLTAGE, ":MEAS:VOLT?" },
288         { SCPI_CMD_GET_MEAS_CURRENT, ":MEAS:CURR?" },
289         { SCPI_CMD_GET_VOLTAGE_TARGET, ":SOUR:VOLT?" },
290         { SCPI_CMD_SET_VOLTAGE_TARGET, ":SOUR:VOLT %.6f" },
291         { SCPI_CMD_GET_CURRENT_LIMIT, ":SOUR:CURR?" },
292         { SCPI_CMD_SET_CURRENT_LIMIT, ":SOUR:CURR %.6f" },
293         { SCPI_CMD_GET_OUTPUT_ENABLED, ":OUTP?" },
294         { SCPI_CMD_SET_OUTPUT_ENABLE, ":OUTP ON" },
295         { SCPI_CMD_SET_OUTPUT_DISABLE, ":OUTP OFF" },
296         { SCPI_CMD_GET_OUTPUT_REGULATION, ":SOUR:FUNC:MODE?" },
297         { SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_ACTIVE, ":SOUR:VOLT:PROT:TRIP?" },
298         { SCPI_CMD_GET_OVER_VOLTAGE_PROTECTION_THRESHOLD, ":SOUR:VOLT:PROT:LEV?" },
299         { SCPI_CMD_SET_OVER_VOLTAGE_PROTECTION_THRESHOLD, ":SOUR:VOLT:PROT:LEV %.6f" },
300         { SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ENABLED, ":SOUR:CURR:PROT:STAT?" },
301         { SCPI_CMD_SET_OVER_CURRENT_PROTECTION_ENABLE, ":SOUR:CURR:PROT:STAT ON" },
302         { SCPI_CMD_SET_OVER_CURRENT_PROTECTION_DISABLE, ":SOUR:CURR:PROT:STAT OFF" },
303         { SCPI_CMD_GET_OVER_CURRENT_PROTECTION_ACTIVE, ":SOUR:CURR:PROT:TRIP?" },
304 };
305
306 SR_PRIV const struct scpi_pps pps_profiles[] = {
307         /* HP 6632B */
308         { "HP", "6632B", 0,
309                 ARRAY_AND_SIZE(hp_6632b_devopts),
310                 ARRAY_AND_SIZE(devopts_none),
311                 ARRAY_AND_SIZE(hp_6632b_ch),
312                 ARRAY_AND_SIZE(hp_6632b_cg),
313                 ARRAY_AND_SIZE(hp_6632b_cmd),
314                 .probe_channels = NULL,
315         },
316
317         /* Rigol DP800 series */
318         { "Rigol", "^DP821A$", PPS_OTP,
319                 ARRAY_AND_SIZE(rigol_dp800_devopts),
320                 ARRAY_AND_SIZE(rigol_dp800_devopts_cg),
321                 ARRAY_AND_SIZE(rigol_dp821a_ch),
322                 ARRAY_AND_SIZE(rigol_dp820_cg),
323                 ARRAY_AND_SIZE(rigol_dp800_cmd),
324                 .probe_channels = NULL,
325         },
326         { "Rigol", "^DP831A$", PPS_OTP,
327                 ARRAY_AND_SIZE(rigol_dp800_devopts),
328                 ARRAY_AND_SIZE(rigol_dp800_devopts_cg),
329                 ARRAY_AND_SIZE(rigol_dp831_ch),
330                 ARRAY_AND_SIZE(rigol_dp830_cg),
331                 ARRAY_AND_SIZE(rigol_dp800_cmd),
332                 .probe_channels = NULL,
333         },
334         { "Rigol", "^(DP832|DP832A)$", PPS_OTP,
335                 ARRAY_AND_SIZE(rigol_dp800_devopts),
336                 ARRAY_AND_SIZE(rigol_dp800_devopts_cg),
337                 ARRAY_AND_SIZE(rigol_dp832_ch),
338                 ARRAY_AND_SIZE(rigol_dp830_cg),
339                 ARRAY_AND_SIZE(rigol_dp800_cmd),
340                 .probe_channels = NULL,
341         },
342
343         /* Philips/Fluke PM2800 series */
344         { "Philips", "^PM28[13][123]/[01234]{1,2}$", 0,
345                 ARRAY_AND_SIZE(philips_pm2800_devopts),
346                 ARRAY_AND_SIZE(philips_pm2800_devopts_cg),
347                 NULL, 0,
348                 NULL, 0,
349                 ARRAY_AND_SIZE(philips_pm2800_cmd),
350                 philips_pm2800_probe_channels,
351         },
352 };
353
354 SR_PRIV unsigned int num_pps_profiles = ARRAY_SIZE(pps_profiles);