]> sigrok.org Git - libsigrok.git/blob - src/hardware/manson-hcs-3xxx/protocol.c
manson-hcs-3xxx: Convert to SR_DF_ANALOG.
[libsigrok.git] / src / hardware / manson-hcs-3xxx / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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
22 /** @file
23   *  <em>Manson HCS-3xxx Series</em> power supply driver
24   *  @internal
25   */
26
27 #include <config.h>
28 #include "protocol.h"
29
30 #define REQ_TIMEOUT_MS 500
31
32 SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
33 {
34         int ret;
35         char cmdbuf[50];
36         char *cmd_esc;
37         va_list args;
38
39         va_start(args, cmd);
40         vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
41         va_end(args);
42
43         cmd_esc = g_strescape(cmdbuf, NULL);
44         sr_dbg("Sending '%s'.", cmd_esc);
45         g_free(cmd_esc);
46
47         if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf),
48                         serial_timeout(serial, strlen(cmdbuf)))) < 0) {
49                 sr_err("Error sending command: %d.", ret);
50                 return ret;
51         }
52
53         return ret;
54 }
55
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  */
67 SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char *buf, int buflen)
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))) {
77                 retc = serial_read_blocking(serial, &buf[bufpos], 1, 0);
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)
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
116         /* Output enabled? Works because voltage cannot be set to 0.0 directly. */
117         devc->output_enabled = devc->voltage != 0.0;
118
119         return SR_OK;
120 }
121
122 static void send_sample(struct sr_dev_inst *sdi)
123 {
124         struct dev_context *devc;
125         struct sr_datafeed_packet packet;
126         struct sr_datafeed_analog analog;
127         struct sr_analog_encoding encoding;
128         struct sr_analog_meaning meaning;
129         struct sr_analog_spec spec;
130
131         devc = sdi->priv;
132
133         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
134
135         packet.type = SR_DF_ANALOG;
136         packet.payload = &analog;
137         analog.meaning->channels = sdi->channels;
138         analog.num_samples = 1;
139
140         analog.meaning->mq = SR_MQ_VOLTAGE;
141         analog.meaning->unit = SR_UNIT_VOLT;
142         analog.meaning->mqflags = SR_MQFLAG_DC;
143         analog.data = &devc->voltage;
144         sr_session_send(sdi, &packet);
145
146         analog.meaning->mq = SR_MQ_CURRENT;
147         analog.meaning->unit = SR_UNIT_AMPERE;
148         analog.meaning->mqflags = 0;
149         analog.data = &devc->current;
150         sr_session_send(sdi, &packet);
151
152
153         sr_sw_limits_update_samples_read(&devc->limits, 1);
154 }
155
156 static int parse_reply(struct sr_dev_inst *sdi)
157 {
158         struct dev_context *devc;
159         char *reply_esc, **tokens;
160         int retc;
161
162         devc = sdi->priv;
163
164         reply_esc = g_strescape(devc->buf, NULL);
165         sr_dbg("Received '%s'.", reply_esc);
166         g_free(reply_esc);
167
168         tokens = g_strsplit(devc->buf, "\r", 0);
169         retc = hcs_parse_volt_curr_mode(sdi, tokens);
170         g_strfreev(tokens);
171         if (retc < 0)
172                 return SR_ERR;
173
174         send_sample(sdi);
175
176         return SR_OK;
177 }
178
179 static int handle_new_data(struct sr_dev_inst *sdi)
180 {
181         int len;
182         struct dev_context *devc;
183         struct sr_serial_dev_inst *serial;
184
185         devc = sdi->priv;
186         serial = sdi->conn;
187
188         len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0);
189         if (len < 1)
190                 return SR_ERR;
191
192         devc->buflen += len;
193         devc->buf[devc->buflen] = '\0';
194
195         /* Wait until we received an "OK\r" (among other bytes). */
196         if (!g_str_has_suffix(devc->buf, "OK\r"))
197                 return SR_OK;
198
199         parse_reply(sdi);
200
201         devc->buf[0] = '\0';
202         devc->buflen = 0;
203
204         devc->reply_pending = FALSE;
205
206         return SR_OK;
207 }
208
209 /** Driver/serial data reception function. */
210 SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
211 {
212         struct sr_dev_inst *sdi;
213         struct dev_context *devc;
214         struct sr_serial_dev_inst *serial;
215         uint64_t elapsed_us;
216
217         (void)fd;
218
219         if (!(sdi = cb_data))
220                 return TRUE;
221
222         if (!(devc = sdi->priv))
223                 return TRUE;
224
225         serial = sdi->conn;
226
227         if (revents == G_IO_IN) {
228                 /* New data arrived. */
229                 handle_new_data(sdi);
230         } else {
231                 /* Timeout. */
232         }
233
234         if (sr_sw_limits_check(&devc->limits)) {
235                 sdi->driver->dev_acquisition_stop(sdi);
236                 return TRUE;
237         }
238
239         /* Request next packet, if required. */
240         if (sdi->status == SR_ST_ACTIVE) {
241                 if (devc->reply_pending) {
242                         elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
243                         if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
244                                 devc->reply_pending = FALSE;
245                         return TRUE;
246                 }
247
248                 /* Send command to get voltage, current, and mode (CC or CV). */
249                 if (hcs_send_cmd(serial, "GETD\r") < 0)
250                         return TRUE;
251
252                 devc->req_sent_at = g_get_monotonic_time();
253                 devc->reply_pending = TRUE;
254         }
255
256         return TRUE;
257 }