2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * <em>Motech LPS-30x series</em> power supply driver
31 /** Send data packets for current measurements. */
32 static void send_data(struct sr_dev_inst *sdi)
34 struct dev_context *devc;
35 struct sr_datafeed_packet packet;
36 struct sr_datafeed_analog analog;
37 struct sr_analog_encoding encoding;
38 struct sr_analog_meaning meaning;
39 struct sr_analog_spec spec;
41 float data[MAX_CHANNELS];
44 packet.type = SR_DF_ANALOG;
45 packet.payload = &analog;
47 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
49 analog.meaning->channels = sdi->channels;
50 analog.num_samples = 1;
51 analog.meaning->mq = SR_MQ_VOLTAGE;
52 analog.meaning->unit = SR_UNIT_VOLT;
53 analog.meaning->mqflags = SR_MQFLAG_DC;
54 analog.encoding->digits = 3;
55 analog.spec->spec_digits = 2;
58 for (i = 0; i < devc->model->num_channels; i++)
59 ((float *)analog.data)[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */
60 sr_session_send(sdi, &packet);
62 analog.meaning->mq = SR_MQ_CURRENT;
63 analog.meaning->unit = SR_UNIT_AMPERE;
64 analog.meaning->mqflags = 0;
65 analog.encoding->digits = 4;
66 analog.spec->spec_digits = 3;
68 for (i = 0; i < devc->model->num_channels; i++)
69 ((float *)analog.data)[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */
70 sr_session_send(sdi, &packet);
72 sr_sw_limits_update_samples_read(&devc->limits, 1);
75 /** Process a complete line (without CR/LF) in buf. */
76 static void process_line(struct sr_dev_inst *sdi)
78 struct dev_context *devc;
84 switch (devc->acq_req_pending) {
85 case 0: /* Should not happen... */
87 case 1: /* Waiting for data reply to request */
89 switch (devc->acq_req) {
94 if (sr_atod(devc->buf, &dbl) != SR_OK) {
95 sr_err("Failed to convert '%s' to double, errno=%d %s",
96 devc->buf, errno, g_strerror(errno));
101 if (sr_atoi(devc->buf, &auxint) != SR_OK) {
102 sr_err("Failed to convert '%s' to int, errno=%d %s",
103 devc->buf, errno, g_strerror(errno));
111 switch (devc->acq_req) {
113 devc->channel_status[0].output_voltage_last = dbl;
116 devc->channel_status[0].output_current_last = dbl;
119 devc->channel_status[1].output_voltage_last = dbl;
122 devc->channel_status[1].output_current_last = dbl;
124 case AQ_STATUS: /* Process status and generate data. */
125 if (lps_process_status(sdi, auxint) == SR_OK) {
133 devc->acq_req_pending = 2;
135 case 2: /* Waiting for OK after request */
136 if (strcmp(devc->buf, "OK")) {
137 sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf);
139 devc->acq_req_pending = 0;
147 SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data)
149 struct sr_dev_inst *sdi;
150 struct dev_context *devc;
151 struct sr_serial_dev_inst *serial;
156 if (!(sdi = cb_data))
159 if (!(devc = sdi->priv))
164 if (revents == G_IO_IN) { /* Serial data arrived. */
165 while (LINELEN_MAX - devc->buflen - 2 > 0) {
166 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
170 /* Eliminate whitespace at beginning of line. */
171 if (g_ascii_isspace(devc->buf[0])) {
178 devc->buf[devc->buflen] = '\0';
180 /* If line complete, process msg. */
181 if ((devc->buflen > 0) && ((devc->buf[devc->buflen-1] == '\r') || devc->buf[devc->buflen-1] == '\n')) {
183 devc->buf[devc->buflen] = '\0';
185 sr_spew("Line complete: \"%s\"", devc->buf);
191 if (sr_sw_limits_check(&devc->limits))
192 sdi->driver->dev_acquisition_stop(sdi);
194 /* Only request the next packet if required. */
195 if (!((sdi->status == SR_ST_ACTIVE) && (devc->acq_running)))
198 if (devc->acq_req_pending) {
199 int64_t elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
200 if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) {
201 sr_spew("Request timeout: req=%d t=%" PRIi64 "us",
202 (int)devc->acq_req, elapsed_us);
203 devc->acq_req_pending = 0;
207 if (devc->acq_req_pending == 0) {
208 switch (devc->acq_req) {
209 case AQ_NONE: /* Fall through */
211 devc->acq_req = AQ_U1;
212 lps_send_req(serial, "VOUT1");
215 devc->acq_req = AQ_I1;
216 lps_send_req(serial, "IOUT1");
219 if (devc->model->num_channels == 1) {
220 devc->acq_req = AQ_STATUS;
221 lps_send_req(serial, "STATUS");
223 devc->acq_req = AQ_U2;
224 lps_send_req(serial, "VOUT2");
228 devc->acq_req = AQ_I2;
229 lps_send_req(serial, "IOUT2");
232 devc->acq_req = AQ_STATUS;
233 lps_send_req(serial, "STATUS");
236 sr_err("Illegal devc->acq_req=%d", devc->acq_req);
239 devc->req_sent_at = g_get_real_time();
240 devc->acq_req_pending = 1;