]> sigrok.org Git - libsigrok.git/blame - hardware/motech-lps-30x/protocol.c
build: Portability fixes.
[libsigrok.git] / 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>
28
41b7bd01
MH
29#include "protocol.h"
30
1c3d002b
MH
31/** Send data packets for current measurements. */
32static void send_data(struct sr_dev_inst *sdi)
33{
34 struct dev_context *devc;
35 struct sr_datafeed_packet packet;
36 struct sr_datafeed_analog analog;
37 int i;
38 float data[MAX_CHANNELS];
39
40 devc = sdi->priv;
41 packet.type = SR_DF_ANALOG;
42 packet.payload = &analog;
43 analog.channels = sdi->channels;
44 analog.num_samples = 1;
45
46 analog.mq = SR_MQ_VOLTAGE;
47 analog.unit = SR_UNIT_VOLT;
48 analog.mqflags = SR_MQFLAG_DC;
49 analog.data = data;
50 for (i = 0; i < devc->model->num_channels; i++)
51 analog.data[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */
52 sr_session_send(sdi, &packet);
53
54 analog.mq = SR_MQ_CURRENT;
55 analog.unit = SR_UNIT_AMPERE;
56 analog.mqflags = 0;
57 analog.data = data;
58 for (i = 0; i < devc->model->num_channels; i++)
59 analog.data[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */
60 sr_session_send(sdi, &packet);
61
62 devc->num_samples++;
63}
64
65/** Process a complete line (without CR/LF) in buf. */
66static void process_line(struct sr_dev_inst *sdi)
67{
68 struct dev_context *devc;
1c3d002b
MH
69 double dbl;
70 int auxint;
71
72 devc = sdi->priv;
1c3d002b
MH
73
74 switch (devc->acq_req_pending) {
75 case 0: /* Should not happen... */
76 break;
77 case 1: /* Waiting for data reply to request */
78 /* Convert numbers */
79 switch (devc->acq_req) {
80 case AQ_U1: case AQ_U2: case AQ_I1: case AQ_I2:
81 if (sr_atod(devc->buf, &dbl) != SR_OK) {
82 sr_err("Failed to convert '%s' to double, errno=%d %s",
83 devc->buf, errno, strerror(errno));
84 dbl = 0.0;
85 }
86 break;
87 case AQ_STATUS:
88 if (sr_atoi(devc->buf, &auxint) != SR_OK) {
89 sr_err("Failed to convert '%s' to int, errno=%d %s",
90 devc->buf, errno, strerror(errno));
91 auxint = 0;
92 }
93 break;
94 default:
95 break;
96 }
97
98 switch (devc->acq_req) {
99 case AQ_U1:
100 devc->channel_status[0].output_voltage_last = dbl;
101 break;
102 case AQ_I1:
103 devc->channel_status[0].output_current_last = dbl;
104 break;
105 case AQ_U2:
106 devc->channel_status[1].output_voltage_last = dbl;
107 break;
108 case AQ_I2:
109 devc->channel_status[1].output_current_last = dbl;
110 break;
111 case AQ_STATUS: /* Process status and generate data. */
112 if (lps_process_status(sdi, auxint) == SR_OK) {
113 send_data(sdi);
114 }
115 break;
116 default:
117 break;
118 }
119
120 devc->acq_req_pending = 2;
121 break;
122 case 2: /* Waiting for OK after request */
123 if (strcmp(devc->buf, "OK")) {
124 sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf);
125 }
126 devc->acq_req_pending = 0;
127 break;
128 }
129
130 devc->buf[0] = '\0';
131 devc->buflen = 0;
132}
133
134
41b7bd01
MH
135SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data)
136{
1c3d002b 137 struct sr_dev_inst *sdi;
41b7bd01 138 struct dev_context *devc;
1c3d002b 139 struct sr_serial_dev_inst *serial;
1c3d002b
MH
140 int len;
141 gdouble elapsed_s;
41b7bd01
MH
142
143 (void)fd;
144
145 if (!(sdi = cb_data))
146 return TRUE;
147
148 if (!(devc = sdi->priv))
149 return TRUE;
150
1c3d002b
MH
151 serial = sdi->conn;
152
153 if (revents == G_IO_IN) { /* Serial data arrived. */
154 while (LINELEN_MAX - devc->buflen - 2 > 0) {
155 len = serial_read(serial, devc->buf + devc->buflen, 1);
156 if (len < 1)
157 break;
158
159 /* Eliminate whitespace at beginning of line. */
160 if (g_ascii_isspace(devc->buf[0])) {
161 devc->buf[0] = '\0';
162 devc->buflen = 0;
163 continue;
164 }
165
1c3d002b
MH
166 devc->buflen += len;
167 devc->buf[devc->buflen] = '\0';
168
169 /* If line complete, process msg. */
170 if ((devc->buflen > 0) && ((devc->buf[devc->buflen-1] == '\r') || devc->buf[devc->buflen-1] == '\n')) {
171 devc->buflen--;
172 devc->buf[devc->buflen] = '\0';
173
174 sr_spew("Line complete: \"%s\"", devc->buf);
175 process_line(sdi);
176 }
177 }
178 }
179
180 /* If number of samples or time limit reached, stop acquisition. */
181 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples))
182 sdi->driver->dev_acquisition_stop(sdi, cb_data);
183
184 if (devc->limit_msec) {
185 elapsed_s = g_timer_elapsed(devc->elapsed_msec, NULL);
186 if ((elapsed_s * 1000) >= devc->limit_msec)
187 sdi->driver->dev_acquisition_stop(sdi, cb_data);
188 }
189
190 /* Request next packet, if required. */
191 if ((sdi->status == SR_ST_ACTIVE) && (devc->acq_running)){
192 if (devc->acq_req_pending) {
193 gint64 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
194 if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) {
195 sr_spew("Request timeout: req=%d t=%lldus", (int)devc->acq_req, elapsed_us);
196 devc->acq_req_pending = 0;
197 }
198 }
199 if (devc->acq_req_pending == 0) {
200 switch(devc->acq_req)
201 {
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}