]> sigrok.org Git - libsigrok.git/blame - src/hardware/motech-lps-30x/protocol.c
Various #include file cosmetic fixes.
[libsigrok.git] / src / hardware / motech-lps-30x / protocol.c
CommitLineData
41b7bd01
MH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
1c3d002b 5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx)
41b7bd01
MH
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
1c3d002b
MH
21/** @file
22 * <em>Motech LPS-30x series</em> power supply driver
23 * @internal
24 */
25
26#include <errno.h>
27#include <string.h>
41b7bd01
MH
28#include "protocol.h"
29
1c3d002b
MH
30/** Send data packets for current measurements. */
31static void send_data(struct sr_dev_inst *sdi)
32{
33 struct dev_context *devc;
34 struct sr_datafeed_packet packet;
35 struct sr_datafeed_analog analog;
36 int i;
37 float data[MAX_CHANNELS];
38
39 devc = sdi->priv;
40 packet.type = SR_DF_ANALOG;
41 packet.payload = &analog;
42 analog.channels = sdi->channels;
43 analog.num_samples = 1;
44
45 analog.mq = SR_MQ_VOLTAGE;
46 analog.unit = SR_UNIT_VOLT;
47 analog.mqflags = SR_MQFLAG_DC;
48 analog.data = data;
49 for (i = 0; i < devc->model->num_channels; i++)
50 analog.data[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */
51 sr_session_send(sdi, &packet);
52
53 analog.mq = SR_MQ_CURRENT;
54 analog.unit = SR_UNIT_AMPERE;
55 analog.mqflags = 0;
56 analog.data = data;
57 for (i = 0; i < devc->model->num_channels; i++)
58 analog.data[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */
59 sr_session_send(sdi, &packet);
60
61 devc->num_samples++;
62}
63
64/** Process a complete line (without CR/LF) in buf. */
65static void process_line(struct sr_dev_inst *sdi)
66{
67 struct dev_context *devc;
1c3d002b
MH
68 double dbl;
69 int auxint;
70
71 devc = sdi->priv;
1c3d002b
MH
72
73 switch (devc->acq_req_pending) {
74 case 0: /* Should not happen... */
75 break;
76 case 1: /* Waiting for data reply to request */
77 /* Convert numbers */
78 switch (devc->acq_req) {
93b118da
UH
79 case AQ_U1:
80 case AQ_U2:
81 case AQ_I1:
82 case AQ_I2:
1c3d002b
MH
83 if (sr_atod(devc->buf, &dbl) != SR_OK) {
84 sr_err("Failed to convert '%s' to double, errno=%d %s",
85 devc->buf, errno, strerror(errno));
86 dbl = 0.0;
87 }
88 break;
89 case AQ_STATUS:
90 if (sr_atoi(devc->buf, &auxint) != SR_OK) {
91 sr_err("Failed to convert '%s' to int, errno=%d %s",
92 devc->buf, errno, strerror(errno));
93 auxint = 0;
94 }
95 break;
96 default:
97 break;
98 }
99
100 switch (devc->acq_req) {
101 case AQ_U1:
102 devc->channel_status[0].output_voltage_last = dbl;
103 break;
104 case AQ_I1:
105 devc->channel_status[0].output_current_last = dbl;
106 break;
107 case AQ_U2:
108 devc->channel_status[1].output_voltage_last = dbl;
109 break;
110 case AQ_I2:
111 devc->channel_status[1].output_current_last = dbl;
112 break;
113 case AQ_STATUS: /* Process status and generate data. */
114 if (lps_process_status(sdi, auxint) == SR_OK) {
115 send_data(sdi);
116 }
117 break;
118 default:
119 break;
120 }
121
122 devc->acq_req_pending = 2;
123 break;
124 case 2: /* Waiting for OK after request */
125 if (strcmp(devc->buf, "OK")) {
126 sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf);
127 }
128 devc->acq_req_pending = 0;
129 break;
130 }
131
132 devc->buf[0] = '\0';
133 devc->buflen = 0;
134}
135
41b7bd01
MH
136SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data)
137{
1c3d002b 138 struct sr_dev_inst *sdi;
41b7bd01 139 struct dev_context *devc;
1c3d002b 140 struct sr_serial_dev_inst *serial;
1c3d002b
MH
141 int len;
142 gdouble elapsed_s;
41b7bd01
MH
143
144 (void)fd;
145
146 if (!(sdi = cb_data))
147 return TRUE;
148
149 if (!(devc = sdi->priv))
150 return TRUE;
151
1c3d002b
MH
152 serial = sdi->conn;
153
154 if (revents == G_IO_IN) { /* Serial data arrived. */
155 while (LINELEN_MAX - devc->buflen - 2 > 0) {
e0501240 156 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1);
1c3d002b
MH
157 if (len < 1)
158 break;
159
160 /* Eliminate whitespace at beginning of line. */
161 if (g_ascii_isspace(devc->buf[0])) {
162 devc->buf[0] = '\0';
163 devc->buflen = 0;
164 continue;
165 }
166
1c3d002b
MH
167 devc->buflen += len;
168 devc->buf[devc->buflen] = '\0';
169
170 /* If line complete, process msg. */
171 if ((devc->buflen > 0) && ((devc->buf[devc->buflen-1] == '\r') || devc->buf[devc->buflen-1] == '\n')) {
172 devc->buflen--;
173 devc->buf[devc->buflen] = '\0';
174
175 sr_spew("Line complete: \"%s\"", devc->buf);
176 process_line(sdi);
177 }
178 }
179 }
180
181 /* If number of samples or time limit reached, stop acquisition. */
182 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples))
183 sdi->driver->dev_acquisition_stop(sdi, cb_data);
184
185 if (devc->limit_msec) {
186 elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
187 if ((elapsed_s * 1000) >= devc->limit_msec)
188 sdi->driver->dev_acquisition_stop(sdi, cb_data);
189 }
190
191 /* Request next packet, if required. */
192 if ((sdi->status == SR_ST_ACTIVE) && (devc->acq_running)){
193 if (devc->acq_req_pending) {
194 gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
195 if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) {
196 sr_spew("Request timeout: req=%d t=%lldus", (int)devc->acq_req, elapsed_us);
197 devc->acq_req_pending = 0;
198 }
199 }
200 if (devc->acq_req_pending == 0) {
0c5f2abc 201 switch (devc->acq_req) {
1c3d002b
MH
202 case AQ_NONE: /* Fall through */
203 case AQ_STATUS:
204 devc->acq_req = AQ_U1;
205 lps_send_req(serial, "VOUT1");
206 break;
207 case AQ_U1:
208 devc->acq_req = AQ_I1;
209 lps_send_req(serial, "IOUT1");
210 break;
211 case AQ_I1:
212 if (devc->model->num_channels == 1) {
213 devc->acq_req = AQ_STATUS;
214 lps_send_req(serial, "STATUS");
215 } else {
216 devc->acq_req = AQ_U2;
217 lps_send_req(serial, "VOUT2");
218 }
219 break;
220 case AQ_U2:
221 devc->acq_req = AQ_I2;
222 lps_send_req(serial, "IOUT2");
223 break;
224 case AQ_I2:
225 devc->acq_req = AQ_STATUS;
226 lps_send_req(serial, "STATUS");
227 break;
228 default:
229 sr_err("Illegal devc->acq_req=%d", devc->acq_req);
230 return SR_ERR;
231 }
232 devc->req_sent_at = g_get_real_time();
233 devc->acq_req_pending = 1;
234 }
41b7bd01
MH
235 }
236
237 return TRUE;
238}