]> sigrok.org Git - libsigrok.git/blame - src/hardware/scpi-pps/protocol.c
scpi-pps: Add infrastructure for controlling output frequency
[libsigrok.git] / src / hardware / scpi-pps / protocol.c
CommitLineData
ca1a7cb5
BV
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
9e45cd41 20#include <string.h>
ba464a12 21#include <strings.h>
9e45cd41 22#include <stdarg.h>
ca1a7cb5
BV
23#include "protocol.h"
24
329733d9 25SR_PRIV const char *scpi_cmd_get(const struct sr_dev_inst *sdi, int command)
9e45cd41 26{
9e45cd41 27 struct dev_context *devc;
9e45cd41 28 unsigned int i;
329733d9 29 const char *cmd;
9e45cd41
BV
30
31 devc = sdi->priv;
32 cmd = NULL;
33 for (i = 0; i < devc->device->num_commands; i++) {
34 if (devc->device->commands[i].command == command) {
35 cmd = devc->device->commands[i].string;
36 break;
37 }
38 }
478c8d92
BV
39
40 return cmd;
41}
42
43SR_PRIV int scpi_cmd(const struct sr_dev_inst *sdi, int command, ...)
44{
45 struct sr_scpi_dev_inst *scpi;
46 va_list args;
47 int ret;
329733d9 48 const char *cmd;
478c8d92
BV
49
50 if (!(cmd = scpi_cmd_get(sdi, command))) {
9e45cd41
BV
51 /* Device does not implement this command, that's OK. */
52 return SR_OK_CONTINUE;
53 }
54
55 scpi = sdi->conn;
56 va_start(args, command);
57 ret = sr_scpi_send_variadic(scpi, cmd, args);
58 va_end(args);
59
60 return ret;
61}
62
478c8d92
BV
63SR_PRIV int scpi_cmd_resp(const struct sr_dev_inst *sdi, GVariant **gvar,
64 const GVariantType *gvtype, int command, ...)
65{
66 struct sr_scpi_dev_inst *scpi;
67 va_list args;
68 double d;
69 int ret;
329733d9
UH
70 char *s;
71 const char *cmd;
478c8d92
BV
72
73 if (!(cmd = scpi_cmd_get(sdi, command))) {
74 /* Device does not implement this command, that's OK. */
75 return SR_OK_CONTINUE;
76 }
77
78 scpi = sdi->conn;
79 va_start(args, command);
80 ret = sr_scpi_send_variadic(scpi, cmd, args);
81 va_end(args);
82 if (ret != SR_OK)
83 return ret;
84
c1d56d20
BV
85 /* Non-standard data type responses. */
86 if (command == SCPI_CMD_GET_OUTPUT_REGULATION) {
87 /*
88 * The Rigol DP800 series return CV/CC/UR, Philips PM2800
89 * return VOLT/CURR. We always return a GVariant string in
90 * the Rigol notation.
91 */
478c8d92
BV
92 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
93 return ret;
c1d56d20
BV
94 if (!strcmp(s, "CV") || !strcmp(s, "VOLT")) {
95 *gvar = g_variant_new_string("CV");
96 } else if (!strcmp(s, "CC") || !strcmp(s, "CURR")) {
97 *gvar = g_variant_new_string("CC");
98 } else if (!strcmp(s, "UR")) {
99 *gvar = g_variant_new_string("UR");
100 } else {
101 sr_dbg("Unknown response to SCPI_CMD_GET_OUTPUT_REGULATION: %s", s);
102 ret = SR_ERR_DATA;
103 }
104 g_free(s);
105 } else {
106 /* Straight SCPI getters to GVariant types. */
107 if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_BOOLEAN)) {
108 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) != SR_OK)
109 return ret;
110 if (!strcasecmp(s, "ON") || !strcasecmp(s, "1") || !strcasecmp(s, "YES"))
111 *gvar = g_variant_new_boolean(TRUE);
112 else if (!strcasecmp(s, "OFF") || !strcasecmp(s, "0") || !strcasecmp(s, "NO"))
113 *gvar = g_variant_new_boolean(FALSE);
114 else
115 ret = SR_ERR;
116 g_free(s);
117 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_DOUBLE)) {
118 if ((ret = sr_scpi_get_double(scpi, NULL, &d)) == SR_OK)
119 *gvar = g_variant_new_double(d);
120 } if (g_variant_type_equal(gvtype, G_VARIANT_TYPE_STRING)) {
121 if ((ret = sr_scpi_get_string(scpi, NULL, &s)) == SR_OK)
122 *gvar = g_variant_new_string(s);
123 }
478c8d92
BV
124 }
125
126 return ret;
127}
128
60475cd7
BV
129SR_PRIV int select_channel(const struct sr_dev_inst *sdi, struct sr_channel *ch)
130{
131 struct dev_context *devc;
624503ae 132 struct pps_channel *cur_pch, *new_pch;
60475cd7
BV
133 int ret;
134
135 if (g_slist_length(sdi->channels) == 1)
136 return SR_OK;
137
138 devc = sdi->priv;
139 if (ch == devc->cur_channel)
140 return SR_OK;
141
624503ae
BV
142 new_pch = ch->priv;
143 if (devc->cur_channel) {
144 cur_pch = devc->cur_channel->priv;
145 if (cur_pch->hw_output_idx == new_pch->hw_output_idx) {
146 /* Same underlying output channel. */
147 devc->cur_channel = ch;
148 return SR_OK;
149 }
150 }
151
152 if ((ret = scpi_cmd(sdi, SCPI_CMD_SELECT_CHANNEL, new_pch->hwname)) == SR_OK)
60475cd7
BV
153 devc->cur_channel = ch;
154
155 return ret;
156}
157
624503ae
BV
158SR_PRIV struct sr_channel *next_enabled_channel(const struct sr_dev_inst *sdi,
159 struct sr_channel *cur_channel)
160{
161 struct sr_channel *next_channel;
162 GSList *l;
163
164 next_channel = cur_channel;
165 do {
166 l = g_slist_find(sdi->channels, next_channel);
167 if (l && l->next)
168 next_channel = l->next->data;
169 else
170 next_channel = sdi->channels->data;
171 } while (!next_channel->enabled);
172
173 return next_channel;
174}
175
ca1a7cb5
BV
176SR_PRIV int scpi_pps_receive_data(int fd, int revents, void *cb_data)
177{
ca1a7cb5 178 struct dev_context *devc;
9e45cd41
BV
179 struct sr_datafeed_packet packet;
180 struct sr_datafeed_analog analog;
181 const struct sr_dev_inst *sdi;
60475cd7 182 struct sr_channel *next_channel;
9e45cd41 183 struct sr_scpi_dev_inst *scpi;
01b0257a 184 struct pps_channel *pch;
9e45cd41 185 float f;
01b0257a 186 int cmd;
ca1a7cb5
BV
187
188 (void)fd;
9e45cd41 189 (void)revents;
ca1a7cb5
BV
190
191 if (!(sdi = cb_data))
192 return TRUE;
193
194 if (!(devc = sdi->priv))
195 return TRUE;
196
9e45cd41
BV
197 scpi = sdi->conn;
198
199 /* Retrieve requested value for this state. */
200 if (sr_scpi_get_float(scpi, NULL, &f) == SR_OK) {
01b0257a 201 pch = devc->cur_channel->priv;
9e45cd41
BV
202 packet.type = SR_DF_ANALOG;
203 packet.payload = &analog;
204 analog.channels = g_slist_append(NULL, devc->cur_channel);
205 analog.num_samples = 1;
01b0257a
BV
206 analog.mq = pch->mq;
207 if (pch->mq == SR_MQ_VOLTAGE)
9e45cd41 208 analog.unit = SR_UNIT_VOLT;
01b0257a 209 else if (pch->mq == SR_MQ_CURRENT)
9e45cd41 210 analog.unit = SR_UNIT_AMPERE;
01b0257a
BV
211 else if (pch->mq == SR_MQ_POWER)
212 analog.unit = SR_UNIT_WATT;
9e45cd41
BV
213 analog.mqflags = SR_MQFLAG_DC;
214 analog.data = &f;
215 sr_session_send(sdi, &packet);
216 g_slist_free(analog.channels);
217 }
218
60475cd7 219 if (g_slist_length(sdi->channels) > 1) {
624503ae
BV
220 next_channel = next_enabled_channel(sdi, devc->cur_channel);
221 if (select_channel(sdi, next_channel) != SR_OK) {
60475cd7
BV
222 sr_err("Failed to select channel %s", next_channel->name);
223 return FALSE;
224 }
225 }
01b0257a
BV
226
227 pch = devc->cur_channel->priv;
228 if (pch->mq == SR_MQ_VOLTAGE)
229 cmd = SCPI_CMD_GET_MEAS_VOLTAGE;
4264f1c0
AG
230 else if (pch->mq == SR_MQ_FREQUENCY)
231 cmd = SCPI_CMD_GET_MEAS_FREQUENCY;
01b0257a
BV
232 else if (pch->mq == SR_MQ_CURRENT)
233 cmd = SCPI_CMD_GET_MEAS_CURRENT;
234 else if (pch->mq == SR_MQ_POWER)
235 cmd = SCPI_CMD_GET_MEAS_POWER;
236 else
237 return SR_ERR;
624503ae 238 scpi_cmd(sdi, cmd);
ca1a7cb5
BV
239
240 return TRUE;
241}