]> sigrok.org Git - libsigrok.git/blame - hardware/manson-hcs-3xxx/protocol.c
manson-hcs-3xxx: Added missing models of series, fixed current resolution.
[libsigrok.git] / hardware / manson-hcs-3xxx / protocol.c
CommitLineData
8f4e922f
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "protocol.h"
22
b5e92647
UH
23#define REQ_TIMEOUT_MS 500
24
25static int send_cmd(struct sr_serial_dev_inst *serial, const char *cmd)
26{
27 int ret;
28 char *cmd_esc;
29
30 cmd_esc = g_strescape(cmd, NULL);
31 sr_dbg("Sending '%s'.", cmd_esc);
32
33 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd))) < 0) {
34 sr_err("Error sending command: %d.", ret);
35 g_free(cmd_esc);
36 return ret;
37 }
38
39 g_free(cmd_esc);
40
41 return ret;
42}
43
44static int parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
45{
46 char *str;
47 double val;
48 struct dev_context *devc;
49
50 devc = sdi->priv;
51
52 /* Bytes 0-3: Voltage. */
53 str = g_strndup(tokens[0], 4);
54 val = g_ascii_strtod(str, NULL) / 100;
55 devc->voltage = val;
56 g_free(str);
57
58 /* Bytes 4-7: Current. */
59 str = g_strndup((tokens[0] + 4), 4);
60 val = g_ascii_strtod(str, NULL) / 100;
61 devc->current = val;
62 g_free(str);
63
64 /* Byte 8: Mode ('0' means CV, '1' means CC). */
65 devc->cc_mode = (tokens[0][8] == '1');
66
67 return SR_OK;
68}
69
70static void send_sample(struct sr_dev_inst *sdi)
8f4e922f 71{
8f4e922f 72 struct dev_context *devc;
b5e92647
UH
73 struct sr_datafeed_packet packet;
74 struct sr_datafeed_analog analog;
75
76 devc = sdi->priv;
77
78 packet.type = SR_DF_ANALOG;
79 packet.payload = &analog;
80 analog.channels = sdi->channels;
81 analog.num_samples = 1;
82
83 analog.mq = SR_MQ_VOLTAGE;
84 analog.unit = SR_UNIT_VOLT;
85 analog.mqflags = SR_MQFLAG_DC;
86 analog.data = &devc->voltage;
87 sr_session_send(sdi, &packet);
88
89 analog.mq = SR_MQ_CURRENT;
90 analog.unit = SR_UNIT_AMPERE;
91 analog.mqflags = 0;
92 analog.data = &devc->current;
93 sr_session_send(sdi, &packet);
94
95 devc->num_samples++;
96}
97
98static int parse_reply(struct sr_dev_inst *sdi)
99{
100 struct dev_context *devc;
101 char *reply_esc, **tokens;
102
103 devc = sdi->priv;
104
105 reply_esc = g_strescape(devc->buf, NULL);
106 sr_dbg("Received '%s'.", reply_esc);
107
108 tokens = g_strsplit(devc->buf, "\r", 0);
109
110 if (parse_volt_curr_mode(sdi, tokens) < 0)
111 return SR_ERR;
112 send_sample(sdi);
113
114 g_free(reply_esc);
115 g_strfreev(tokens);
116
117 return SR_OK;
118}
119
120static int handle_new_data(struct sr_dev_inst *sdi)
121{
122 int len;
123 struct dev_context *devc;
124 struct sr_serial_dev_inst *serial;
125
126 devc = sdi->priv;
127 serial = sdi->conn;
128
129 len = serial_read(serial, devc->buf + devc->buflen, 1);
130 if (len < 1)
131 return SR_ERR;
132
133 devc->buflen += len;
134 devc->buf[devc->buflen] = '\0';
135
136 /* Wait until we received an "OK\r" (among other bytes). */
137 if (!g_str_has_suffix(devc->buf, "OK\r"))
138 return SR_OK;
139
140 parse_reply(sdi);
141
142 devc->buf[0] = '\0';
143 devc->buflen = 0;
144
145 devc->reply_pending = FALSE;
146
147 return SR_OK;
148}
149
150SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
151{
152 struct sr_dev_inst *sdi;
153 struct dev_context *devc;
154 struct sr_serial_dev_inst *serial;
155 int64_t t, elapsed_us;
8f4e922f
UH
156
157 (void)fd;
158
159 if (!(sdi = cb_data))
160 return TRUE;
161
162 if (!(devc = sdi->priv))
163 return TRUE;
164
b5e92647
UH
165 serial = sdi->conn;
166
8f4e922f 167 if (revents == G_IO_IN) {
b5e92647
UH
168 /* New data arrived. */
169 handle_new_data(sdi);
170 } else {
171 /* Timeout. */
172 }
173
174 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
175 sr_info("Requested number of samples reached.");
176 sdi->driver->dev_acquisition_stop(sdi, cb_data);
177 return TRUE;
178 }
179
180 if (devc->limit_msec) {
181 t = (g_get_monotonic_time() - devc->starttime) / 1000;
182 if (t > (int64_t)devc->limit_msec) {
183 sr_info("Requested time limit reached.");
184 sdi->driver->dev_acquisition_stop(sdi, cb_data);
185 return TRUE;
186 }
187 }
188
189 /* Request next packet, if required. */
190 if (sdi->status == SR_ST_ACTIVE) {
191 if (devc->reply_pending) {
192 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
193 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
194 devc->reply_pending = FALSE;
195 return TRUE;
196 }
197
198 /* Send command to get voltage, current, and mode (CC or CV). */
199 if (send_cmd(serial, "GETD\r") < 0)
200 return TRUE;
201
202 devc->req_sent_at = g_get_monotonic_time();
203 devc->reply_pending = TRUE;
8f4e922f
UH
204 }
205
206 return TRUE;
207}