]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz> | |
5 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx) | |
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 | ||
21 | /** @file | |
22 | * <em>Motech LPS-30x series</em> power supply driver | |
23 | * @internal | |
24 | */ | |
25 | ||
26 | #include <config.h> | |
27 | #include <errno.h> | |
28 | #include <string.h> | |
29 | #include "protocol.h" | |
30 | ||
31 | /** Send data packets for current measurements. */ | |
32 | static 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_old analog; | |
37 | int i; | |
38 | float data[MAX_CHANNELS]; | |
39 | ||
40 | devc = sdi->priv; | |
41 | packet.type = SR_DF_ANALOG_OLD; | |
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 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
63 | } | |
64 | ||
65 | /** Process a complete line (without CR/LF) in buf. */ | |
66 | static void process_line(struct sr_dev_inst *sdi) | |
67 | { | |
68 | struct dev_context *devc; | |
69 | double dbl; | |
70 | int auxint; | |
71 | ||
72 | devc = sdi->priv; | |
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: | |
81 | case AQ_U2: | |
82 | case AQ_I1: | |
83 | case AQ_I2: | |
84 | if (sr_atod(devc->buf, &dbl) != SR_OK) { | |
85 | sr_err("Failed to convert '%s' to double, errno=%d %s", | |
86 | devc->buf, errno, g_strerror(errno)); | |
87 | dbl = 0.0; | |
88 | } | |
89 | break; | |
90 | case AQ_STATUS: | |
91 | if (sr_atoi(devc->buf, &auxint) != SR_OK) { | |
92 | sr_err("Failed to convert '%s' to int, errno=%d %s", | |
93 | devc->buf, errno, g_strerror(errno)); | |
94 | auxint = 0; | |
95 | } | |
96 | break; | |
97 | default: | |
98 | break; | |
99 | } | |
100 | ||
101 | switch (devc->acq_req) { | |
102 | case AQ_U1: | |
103 | devc->channel_status[0].output_voltage_last = dbl; | |
104 | break; | |
105 | case AQ_I1: | |
106 | devc->channel_status[0].output_current_last = dbl; | |
107 | break; | |
108 | case AQ_U2: | |
109 | devc->channel_status[1].output_voltage_last = dbl; | |
110 | break; | |
111 | case AQ_I2: | |
112 | devc->channel_status[1].output_current_last = dbl; | |
113 | break; | |
114 | case AQ_STATUS: /* Process status and generate data. */ | |
115 | if (lps_process_status(sdi, auxint) == SR_OK) { | |
116 | send_data(sdi); | |
117 | } | |
118 | break; | |
119 | default: | |
120 | break; | |
121 | } | |
122 | ||
123 | devc->acq_req_pending = 2; | |
124 | break; | |
125 | case 2: /* Waiting for OK after request */ | |
126 | if (strcmp(devc->buf, "OK")) { | |
127 | sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf); | |
128 | } | |
129 | devc->acq_req_pending = 0; | |
130 | break; | |
131 | } | |
132 | ||
133 | devc->buf[0] = '\0'; | |
134 | devc->buflen = 0; | |
135 | } | |
136 | ||
137 | SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data) | |
138 | { | |
139 | struct sr_dev_inst *sdi; | |
140 | struct dev_context *devc; | |
141 | struct sr_serial_dev_inst *serial; | |
142 | int len; | |
143 | ||
144 | (void)fd; | |
145 | ||
146 | if (!(sdi = cb_data)) | |
147 | return TRUE; | |
148 | ||
149 | if (!(devc = sdi->priv)) | |
150 | return TRUE; | |
151 | ||
152 | serial = sdi->conn; | |
153 | ||
154 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
155 | while (LINELEN_MAX - devc->buflen - 2 > 0) { | |
156 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); | |
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 | ||
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 (sr_sw_limits_check(&devc->limits)) | |
182 | sdi->driver->dev_acquisition_stop(sdi); | |
183 | ||
184 | /* Only request the next packet if required. */ | |
185 | if (!((sdi->status == SR_ST_ACTIVE) && (devc->acq_running))) | |
186 | return TRUE; | |
187 | ||
188 | if (devc->acq_req_pending) { | |
189 | int64_t elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
190 | if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) { | |
191 | sr_spew("Request timeout: req=%d t=%" PRIi64 "us", | |
192 | (int)devc->acq_req, elapsed_us); | |
193 | devc->acq_req_pending = 0; | |
194 | } | |
195 | } | |
196 | ||
197 | if (devc->acq_req_pending == 0) { | |
198 | switch (devc->acq_req) { | |
199 | case AQ_NONE: /* Fall through */ | |
200 | case AQ_STATUS: | |
201 | devc->acq_req = AQ_U1; | |
202 | lps_send_req(serial, "VOUT1"); | |
203 | break; | |
204 | case AQ_U1: | |
205 | devc->acq_req = AQ_I1; | |
206 | lps_send_req(serial, "IOUT1"); | |
207 | break; | |
208 | case AQ_I1: | |
209 | if (devc->model->num_channels == 1) { | |
210 | devc->acq_req = AQ_STATUS; | |
211 | lps_send_req(serial, "STATUS"); | |
212 | } else { | |
213 | devc->acq_req = AQ_U2; | |
214 | lps_send_req(serial, "VOUT2"); | |
215 | } | |
216 | break; | |
217 | case AQ_U2: | |
218 | devc->acq_req = AQ_I2; | |
219 | lps_send_req(serial, "IOUT2"); | |
220 | break; | |
221 | case AQ_I2: | |
222 | devc->acq_req = AQ_STATUS; | |
223 | lps_send_req(serial, "STATUS"); | |
224 | break; | |
225 | default: | |
226 | sr_err("Illegal devc->acq_req=%d", devc->acq_req); | |
227 | return SR_ERR; | |
228 | } | |
229 | devc->req_sent_at = g_get_real_time(); | |
230 | devc->acq_req_pending = 1; | |
231 | } | |
232 | ||
233 | return TRUE; | |
234 | } |