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