]>
Commit | Line | Data |
---|---|---|
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 | ||
6ec6c43b | 26 | #include <config.h> |
1c3d002b MH |
27 | #include <errno.h> |
28 | #include <string.h> | |
41b7bd01 MH |
29 | #include "protocol.h" |
30 | ||
1c3d002b MH |
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; | |
ddfe99d9 UH |
36 | struct sr_datafeed_analog analog; |
37 | struct sr_analog_encoding encoding; | |
38 | struct sr_analog_meaning meaning; | |
39 | struct sr_analog_spec spec; | |
1c3d002b MH |
40 | int i; |
41 | float data[MAX_CHANNELS]; | |
42 | ||
43 | devc = sdi->priv; | |
ddfe99d9 | 44 | packet.type = SR_DF_ANALOG; |
1c3d002b | 45 | packet.payload = &analog; |
1c3d002b | 46 | |
869c8375 | 47 | /* Note: digits/spec_digits will be overridden later. */ |
ddfe99d9 UH |
48 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
49 | ||
50 | analog.meaning->channels = sdi->channels; | |
51 | analog.num_samples = 1; | |
52 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
53 | analog.meaning->unit = SR_UNIT_VOLT; | |
54 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
bcaadb36 AJ |
55 | analog.encoding->digits = 3; |
56 | analog.spec->spec_digits = 2; | |
1c3d002b | 57 | analog.data = data; |
ddfe99d9 | 58 | |
1c3d002b | 59 | for (i = 0; i < devc->model->num_channels; i++) |
ddfe99d9 | 60 | ((float *)analog.data)[i] = devc->channel_status[i].output_voltage_last; /* Value always 3.3 or 5 for channel 3, if present! */ |
1c3d002b MH |
61 | sr_session_send(sdi, &packet); |
62 | ||
ddfe99d9 UH |
63 | analog.meaning->mq = SR_MQ_CURRENT; |
64 | analog.meaning->unit = SR_UNIT_AMPERE; | |
65 | analog.meaning->mqflags = 0; | |
bcaadb36 AJ |
66 | analog.encoding->digits = 4; |
67 | analog.spec->spec_digits = 3; | |
1c3d002b MH |
68 | analog.data = data; |
69 | for (i = 0; i < devc->model->num_channels; i++) | |
ddfe99d9 | 70 | ((float *)analog.data)[i] = devc->channel_status[i].output_current_last; /* Value always 0 for channel 3, if present! */ |
1c3d002b MH |
71 | sr_session_send(sdi, &packet); |
72 | ||
8aafc5e6 | 73 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
1c3d002b MH |
74 | } |
75 | ||
76 | /** Process a complete line (without CR/LF) in buf. */ | |
77 | static void process_line(struct sr_dev_inst *sdi) | |
78 | { | |
79 | struct dev_context *devc; | |
1c3d002b MH |
80 | double dbl; |
81 | int auxint; | |
82 | ||
83 | devc = sdi->priv; | |
1c3d002b MH |
84 | |
85 | switch (devc->acq_req_pending) { | |
86 | case 0: /* Should not happen... */ | |
87 | break; | |
88 | case 1: /* Waiting for data reply to request */ | |
89 | /* Convert numbers */ | |
90 | switch (devc->acq_req) { | |
93b118da UH |
91 | case AQ_U1: |
92 | case AQ_U2: | |
93 | case AQ_I1: | |
94 | case AQ_I2: | |
1c3d002b MH |
95 | if (sr_atod(devc->buf, &dbl) != SR_OK) { |
96 | sr_err("Failed to convert '%s' to double, errno=%d %s", | |
7237e912 | 97 | devc->buf, errno, g_strerror(errno)); |
1c3d002b MH |
98 | dbl = 0.0; |
99 | } | |
100 | break; | |
101 | case AQ_STATUS: | |
102 | if (sr_atoi(devc->buf, &auxint) != SR_OK) { | |
103 | sr_err("Failed to convert '%s' to int, errno=%d %s", | |
7237e912 | 104 | devc->buf, errno, g_strerror(errno)); |
1c3d002b MH |
105 | auxint = 0; |
106 | } | |
107 | break; | |
108 | default: | |
109 | break; | |
110 | } | |
111 | ||
112 | switch (devc->acq_req) { | |
113 | case AQ_U1: | |
114 | devc->channel_status[0].output_voltage_last = dbl; | |
115 | break; | |
116 | case AQ_I1: | |
117 | devc->channel_status[0].output_current_last = dbl; | |
118 | break; | |
119 | case AQ_U2: | |
120 | devc->channel_status[1].output_voltage_last = dbl; | |
121 | break; | |
122 | case AQ_I2: | |
123 | devc->channel_status[1].output_current_last = dbl; | |
124 | break; | |
125 | case AQ_STATUS: /* Process status and generate data. */ | |
126 | if (lps_process_status(sdi, auxint) == SR_OK) { | |
127 | send_data(sdi); | |
128 | } | |
129 | break; | |
130 | default: | |
131 | break; | |
132 | } | |
133 | ||
134 | devc->acq_req_pending = 2; | |
135 | break; | |
136 | case 2: /* Waiting for OK after request */ | |
137 | if (strcmp(devc->buf, "OK")) { | |
138 | sr_err("Unexpected reply while waiting for OK: '%s'", devc->buf); | |
139 | } | |
140 | devc->acq_req_pending = 0; | |
141 | break; | |
142 | } | |
143 | ||
144 | devc->buf[0] = '\0'; | |
145 | devc->buflen = 0; | |
146 | } | |
147 | ||
41b7bd01 MH |
148 | SR_PRIV int motech_lps_30x_receive_data(int fd, int revents, void *cb_data) |
149 | { | |
1c3d002b | 150 | struct sr_dev_inst *sdi; |
41b7bd01 | 151 | struct dev_context *devc; |
1c3d002b | 152 | struct sr_serial_dev_inst *serial; |
1c3d002b | 153 | int len; |
41b7bd01 MH |
154 | |
155 | (void)fd; | |
156 | ||
157 | if (!(sdi = cb_data)) | |
158 | return TRUE; | |
159 | ||
160 | if (!(devc = sdi->priv)) | |
161 | return TRUE; | |
162 | ||
1c3d002b MH |
163 | serial = sdi->conn; |
164 | ||
165 | if (revents == G_IO_IN) { /* Serial data arrived. */ | |
166 | while (LINELEN_MAX - devc->buflen - 2 > 0) { | |
e0501240 | 167 | len = serial_read_nonblocking(serial, devc->buf + devc->buflen, 1); |
1c3d002b MH |
168 | if (len < 1) |
169 | break; | |
170 | ||
171 | /* Eliminate whitespace at beginning of line. */ | |
172 | if (g_ascii_isspace(devc->buf[0])) { | |
173 | devc->buf[0] = '\0'; | |
174 | devc->buflen = 0; | |
175 | continue; | |
176 | } | |
177 | ||
1c3d002b MH |
178 | devc->buflen += len; |
179 | devc->buf[devc->buflen] = '\0'; | |
180 | ||
181 | /* If line complete, process msg. */ | |
182 | if ((devc->buflen > 0) && ((devc->buf[devc->buflen-1] == '\r') || devc->buf[devc->buflen-1] == '\n')) { | |
183 | devc->buflen--; | |
184 | devc->buf[devc->buflen] = '\0'; | |
185 | ||
186 | sr_spew("Line complete: \"%s\"", devc->buf); | |
187 | process_line(sdi); | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
8aafc5e6 | 192 | if (sr_sw_limits_check(&devc->limits)) |
695dc859 | 193 | sdi->driver->dev_acquisition_stop(sdi); |
1c3d002b | 194 | |
dc89faea UH |
195 | /* Only request the next packet if required. */ |
196 | if (!((sdi->status == SR_ST_ACTIVE) && (devc->acq_running))) | |
197 | return TRUE; | |
198 | ||
199 | if (devc->acq_req_pending) { | |
6433156c | 200 | int64_t elapsed_us = g_get_monotonic_time() - devc->req_sent_at; |
dc89faea | 201 | if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) { |
6433156c DE |
202 | sr_spew("Request timeout: req=%d t=%" PRIi64 "us", |
203 | (int)devc->acq_req, elapsed_us); | |
dc89faea | 204 | devc->acq_req_pending = 0; |
1c3d002b | 205 | } |
dc89faea UH |
206 | } |
207 | ||
208 | if (devc->acq_req_pending == 0) { | |
209 | switch (devc->acq_req) { | |
210 | case AQ_NONE: /* Fall through */ | |
211 | case AQ_STATUS: | |
212 | devc->acq_req = AQ_U1; | |
213 | lps_send_req(serial, "VOUT1"); | |
214 | break; | |
215 | case AQ_U1: | |
216 | devc->acq_req = AQ_I1; | |
217 | lps_send_req(serial, "IOUT1"); | |
218 | break; | |
219 | case AQ_I1: | |
220 | if (devc->model->num_channels == 1) { | |
1c3d002b MH |
221 | devc->acq_req = AQ_STATUS; |
222 | lps_send_req(serial, "STATUS"); | |
dc89faea UH |
223 | } else { |
224 | devc->acq_req = AQ_U2; | |
225 | lps_send_req(serial, "VOUT2"); | |
1c3d002b | 226 | } |
dc89faea UH |
227 | break; |
228 | case AQ_U2: | |
229 | devc->acq_req = AQ_I2; | |
230 | lps_send_req(serial, "IOUT2"); | |
231 | break; | |
232 | case AQ_I2: | |
233 | devc->acq_req = AQ_STATUS; | |
234 | lps_send_req(serial, "STATUS"); | |
235 | break; | |
236 | default: | |
237 | sr_err("Illegal devc->acq_req=%d", devc->acq_req); | |
238 | return SR_ERR; | |
1c3d002b | 239 | } |
dc89faea UH |
240 | devc->req_sent_at = g_get_real_time(); |
241 | devc->acq_req_pending = 1; | |
41b7bd01 MH |
242 | } |
243 | ||
244 | return TRUE; | |
245 | } |