]>
Commit | Line | Data |
---|---|---|
8f4e922f UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de> | |
9740d9bf | 5 | * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz> |
8f4e922f UH |
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 2 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, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
d9251a2c UH |
22 | /** |
23 | * @file | |
24 | * | |
25 | * <em>Manson HCS-3xxx Series</em> power supply driver | |
26 | * | |
27 | * @internal | |
28 | */ | |
9740d9bf | 29 | |
6ec6c43b | 30 | #include <config.h> |
8f4e922f UH |
31 | #include "protocol.h" |
32 | ||
b5e92647 UH |
33 | #define REQ_TIMEOUT_MS 500 |
34 | ||
9740d9bf | 35 | SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...) |
b5e92647 UH |
36 | { |
37 | int ret; | |
9740d9bf | 38 | char cmdbuf[50]; |
b5e92647 | 39 | char *cmd_esc; |
9740d9bf MH |
40 | va_list args; |
41 | ||
42 | va_start(args, cmd); | |
43 | vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args); | |
44 | va_end(args); | |
b5e92647 | 45 | |
9740d9bf | 46 | cmd_esc = g_strescape(cmdbuf, NULL); |
b5e92647 | 47 | sr_dbg("Sending '%s'.", cmd_esc); |
9740d9bf | 48 | g_free(cmd_esc); |
b5e92647 | 49 | |
896e1a45 UH |
50 | if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf), |
51 | serial_timeout(serial, strlen(cmdbuf)))) < 0) { | |
b5e92647 | 52 | sr_err("Error sending command: %d.", ret); |
b5e92647 UH |
53 | return ret; |
54 | } | |
55 | ||
b5e92647 UH |
56 | return ret; |
57 | } | |
58 | ||
9740d9bf MH |
59 | /** |
60 | * Read data from interface into buffer blocking until @a lines number of \\r chars | |
61 | * received. | |
d9251a2c | 62 | * |
9740d9bf MH |
63 | * @param serial Previously initialized serial port structure. |
64 | * @param[in] lines Number of \\r-terminated lines to read (1-n). | |
d9251a2c | 65 | * @param buf Buffer for result. Contents is NUL-terminated on success. |
9740d9bf | 66 | * @param[in] buflen Buffer length (>0). |
d9251a2c | 67 | * |
9740d9bf MH |
68 | * @retval SR_OK Lines received and ending with "OK\r" (success). |
69 | * @retval SR_ERR Error. | |
70 | * @retval SR_ERR_ARG Invalid argument. | |
71 | */ | |
c442ffda | 72 | SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char *buf, int buflen) |
9740d9bf MH |
73 | { |
74 | int l_recv = 0; | |
75 | int bufpos = 0; | |
76 | int retc; | |
77 | ||
78 | if (!serial || (lines <= 0) || !buf || (buflen <= 0)) | |
79 | return SR_ERR_ARG; | |
80 | ||
81 | while ((l_recv < lines) && (bufpos < (buflen + 1))) { | |
eead2782 | 82 | retc = serial_read_blocking(serial, &buf[bufpos], 1, 0); |
9740d9bf MH |
83 | if (retc != 1) |
84 | return SR_ERR; | |
85 | if (buf[bufpos] == '\r') | |
86 | l_recv++; | |
87 | bufpos++; | |
88 | } | |
89 | buf[bufpos] = '\0'; | |
90 | ||
91 | if ((l_recv == lines) && (g_str_has_suffix(buf, "OK\r"))) | |
92 | return SR_OK; | |
93 | else | |
94 | return SR_ERR; | |
95 | } | |
96 | ||
97 | /** Interpret result of GETD command. */ | |
98 | SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens) | |
b5e92647 UH |
99 | { |
100 | char *str; | |
101 | double val; | |
102 | struct dev_context *devc; | |
103 | ||
104 | devc = sdi->priv; | |
105 | ||
106 | /* Bytes 0-3: Voltage. */ | |
107 | str = g_strndup(tokens[0], 4); | |
108 | val = g_ascii_strtod(str, NULL) / 100; | |
109 | devc->voltage = val; | |
110 | g_free(str); | |
111 | ||
112 | /* Bytes 4-7: Current. */ | |
113 | str = g_strndup((tokens[0] + 4), 4); | |
114 | val = g_ascii_strtod(str, NULL) / 100; | |
115 | devc->current = val; | |
116 | g_free(str); | |
117 | ||
118 | /* Byte 8: Mode ('0' means CV, '1' means CC). */ | |
119 | devc->cc_mode = (tokens[0][8] == '1'); | |
120 | ||
9740d9bf MH |
121 | /* Output enabled? Works because voltage cannot be set to 0.0 directly. */ |
122 | devc->output_enabled = devc->voltage != 0.0; | |
123 | ||
b5e92647 UH |
124 | return SR_OK; |
125 | } | |
126 | ||
127 | static void send_sample(struct sr_dev_inst *sdi) | |
8f4e922f | 128 | { |
8f4e922f | 129 | struct dev_context *devc; |
b5e92647 | 130 | struct sr_datafeed_packet packet; |
2463ec77 UH |
131 | struct sr_datafeed_analog analog; |
132 | struct sr_analog_encoding encoding; | |
133 | struct sr_analog_meaning meaning; | |
134 | struct sr_analog_spec spec; | |
b5e92647 UH |
135 | |
136 | devc = sdi->priv; | |
137 | ||
1af428d0 | 138 | sr_analog_init(&analog, &encoding, &meaning, &spec, 2); |
2463ec77 UH |
139 | |
140 | packet.type = SR_DF_ANALOG; | |
b5e92647 | 141 | packet.payload = &analog; |
2463ec77 | 142 | analog.meaning->channels = sdi->channels; |
b5e92647 UH |
143 | analog.num_samples = 1; |
144 | ||
2463ec77 UH |
145 | analog.meaning->mq = SR_MQ_VOLTAGE; |
146 | analog.meaning->unit = SR_UNIT_VOLT; | |
147 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
b5e92647 UH |
148 | analog.data = &devc->voltage; |
149 | sr_session_send(sdi, &packet); | |
150 | ||
2463ec77 UH |
151 | analog.meaning->mq = SR_MQ_CURRENT; |
152 | analog.meaning->unit = SR_UNIT_AMPERE; | |
153 | analog.meaning->mqflags = 0; | |
b5e92647 UH |
154 | analog.data = &devc->current; |
155 | sr_session_send(sdi, &packet); | |
156 | ||
a655b3fd LPC |
157 | |
158 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
b5e92647 UH |
159 | } |
160 | ||
161 | static int parse_reply(struct sr_dev_inst *sdi) | |
162 | { | |
163 | struct dev_context *devc; | |
164 | char *reply_esc, **tokens; | |
9740d9bf | 165 | int retc; |
b5e92647 UH |
166 | |
167 | devc = sdi->priv; | |
168 | ||
169 | reply_esc = g_strescape(devc->buf, NULL); | |
170 | sr_dbg("Received '%s'.", reply_esc); | |
9740d9bf | 171 | g_free(reply_esc); |
b5e92647 UH |
172 | |
173 | tokens = g_strsplit(devc->buf, "\r", 0); | |
9740d9bf MH |
174 | retc = hcs_parse_volt_curr_mode(sdi, tokens); |
175 | g_strfreev(tokens); | |
176 | if (retc < 0) | |
b5e92647 | 177 | return SR_ERR; |
b5e92647 | 178 | |
9740d9bf | 179 | send_sample(sdi); |
b5e92647 UH |
180 | |
181 | return SR_OK; | |
182 | } | |
183 | ||
184 | static int handle_new_data(struct sr_dev_inst *sdi) | |
185 | { | |
186 | int len; | |
187 | struct dev_context *devc; | |
188 | struct sr_serial_dev_inst *serial; | |
189 | ||
190 | devc = sdi->priv; | |
191 | serial = sdi->conn; | |
192 | ||
eead2782 | 193 | len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0); |
b5e92647 UH |
194 | if (len < 1) |
195 | return SR_ERR; | |
196 | ||
197 | devc->buflen += len; | |
198 | devc->buf[devc->buflen] = '\0'; | |
199 | ||
200 | /* Wait until we received an "OK\r" (among other bytes). */ | |
201 | if (!g_str_has_suffix(devc->buf, "OK\r")) | |
202 | return SR_OK; | |
203 | ||
204 | parse_reply(sdi); | |
205 | ||
206 | devc->buf[0] = '\0'; | |
207 | devc->buflen = 0; | |
208 | ||
209 | devc->reply_pending = FALSE; | |
210 | ||
211 | return SR_OK; | |
212 | } | |
213 | ||
9740d9bf | 214 | /** Driver/serial data reception function. */ |
b5e92647 UH |
215 | SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data) |
216 | { | |
217 | struct sr_dev_inst *sdi; | |
218 | struct dev_context *devc; | |
219 | struct sr_serial_dev_inst *serial; | |
a655b3fd | 220 | uint64_t elapsed_us; |
8f4e922f UH |
221 | |
222 | (void)fd; | |
223 | ||
224 | if (!(sdi = cb_data)) | |
225 | return TRUE; | |
226 | ||
227 | if (!(devc = sdi->priv)) | |
228 | return TRUE; | |
229 | ||
b5e92647 UH |
230 | serial = sdi->conn; |
231 | ||
8f4e922f | 232 | if (revents == G_IO_IN) { |
b5e92647 UH |
233 | /* New data arrived. */ |
234 | handle_new_data(sdi); | |
235 | } else { | |
236 | /* Timeout. */ | |
237 | } | |
238 | ||
a655b3fd | 239 | if (sr_sw_limits_check(&devc->limits)) { |
695dc859 | 240 | sdi->driver->dev_acquisition_stop(sdi); |
b5e92647 UH |
241 | return TRUE; |
242 | } | |
243 | ||
b5e92647 UH |
244 | /* Request next packet, if required. */ |
245 | if (sdi->status == SR_ST_ACTIVE) { | |
246 | if (devc->reply_pending) { | |
247 | elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
248 | if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) | |
249 | devc->reply_pending = FALSE; | |
250 | return TRUE; | |
251 | } | |
252 | ||
253 | /* Send command to get voltage, current, and mode (CC or CV). */ | |
9740d9bf | 254 | if (hcs_send_cmd(serial, "GETD\r") < 0) |
b5e92647 UH |
255 | return TRUE; |
256 | ||
257 | devc->req_sent_at = g_get_monotonic_time(); | |
258 | devc->reply_pending = TRUE; | |
8f4e922f UH |
259 | } |
260 | ||
261 | return TRUE; | |
262 | } |