]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | static const uint32_t scanopts[] = { | |
24 | SR_CONF_CONN, | |
25 | SR_CONF_SERIALCOMM, | |
26 | }; | |
27 | ||
28 | static const uint32_t drvopts[] = { | |
29 | SR_CONF_POWER_SUPPLY, | |
30 | }; | |
31 | ||
32 | static const uint32_t devopts[] = { | |
33 | SR_CONF_CONTINUOUS, | |
34 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
35 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
36 | SR_CONF_VOLTAGE | SR_CONF_GET, | |
37 | SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
38 | SR_CONF_CURRENT | SR_CONF_GET, | |
39 | SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
40 | SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
41 | SR_CONF_REGULATION | SR_CONF_GET, | |
42 | SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
43 | SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
44 | }; | |
45 | ||
46 | static const struct korad_kaxxxxp_model models[] = { | |
47 | /* Device enum, vendor, model, ID reply, channels, voltage, current */ | |
48 | {VELLEMAN_PS3005D, "Velleman", "PS3005D", | |
49 | "VELLEMANPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
50 | {VELLEMAN_LABPS3005D, "Velleman", "LABPS3005D", | |
51 | "VELLEMANLABPS3005DV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
52 | {KORAD_KA3005P, "Korad", "KA3005P", | |
53 | "KORADKA3005PV2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
54 | /* Sometimes the KA3005P has an extra 0x01 after the ID. */ | |
55 | {KORAD_KA3005P_0X01, "Korad", "KA3005P", | |
56 | "KORADKA3005PV2.0\x01", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
57 | {KORAD_KD3005P, "Korad", "KD3005P", | |
58 | "KORAD KD3005P V2.0", 1, {0, 31, 0.01}, {0, 5, 0.001}}, | |
59 | ALL_ZERO | |
60 | }; | |
61 | ||
62 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
63 | { | |
64 | struct dev_context *devc; | |
65 | GSList *l; | |
66 | struct sr_dev_inst *sdi; | |
67 | struct sr_config *src; | |
68 | const char *conn, *serialcomm; | |
69 | struct sr_serial_dev_inst *serial; | |
70 | char reply[50]; | |
71 | int i, model_id; | |
72 | unsigned int len; | |
73 | ||
74 | conn = NULL; | |
75 | serialcomm = NULL; | |
76 | ||
77 | for (l = options; l; l = l->next) { | |
78 | src = l->data; | |
79 | switch (src->key) { | |
80 | case SR_CONF_CONN: | |
81 | conn = g_variant_get_string(src->data, NULL); | |
82 | break; | |
83 | case SR_CONF_SERIALCOMM: | |
84 | serialcomm = g_variant_get_string(src->data, NULL); | |
85 | break; | |
86 | default: | |
87 | sr_err("Unknown option %d, skipping.", src->key); | |
88 | break; | |
89 | } | |
90 | } | |
91 | ||
92 | if (!conn) | |
93 | return NULL; | |
94 | if (!serialcomm) | |
95 | serialcomm = "9600/8n1"; | |
96 | ||
97 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
98 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
99 | return NULL; | |
100 | ||
101 | serial_flush(serial); | |
102 | ||
103 | /* Get the device model. */ | |
104 | len = 0; | |
105 | for (i = 0; models[i].id; i++) { | |
106 | if (strlen(models[i].id) > len) | |
107 | len = strlen(models[i].id); | |
108 | } | |
109 | memset(&reply, 0, sizeof(reply)); | |
110 | sr_dbg("Want max %d bytes.", len); | |
111 | if ((korad_kaxxxxp_send_cmd(serial, "*IDN?") < 0)) | |
112 | return NULL; | |
113 | ||
114 | /* i is used here for debug purposes only. */ | |
115 | if ((i = korad_kaxxxxp_read_chars(serial, len, reply)) < 0) | |
116 | return NULL; | |
117 | sr_dbg("Received: %d, %s", i, reply); | |
118 | model_id = -1; | |
119 | for (i = 0; models[i].id; i++) { | |
120 | if (!strcmp(models[i].id, reply)) | |
121 | model_id = i; | |
122 | } | |
123 | if (model_id < 0) { | |
124 | sr_err("Unknown model ID '%s' detected, aborting.", reply); | |
125 | return NULL; | |
126 | } | |
127 | sr_dbg("Found: %s %s (idx %d, ID '%s').", models[model_id].vendor, | |
128 | models[model_id].name, model_id, models[model_id].id); | |
129 | ||
130 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
131 | sdi->status = SR_ST_INACTIVE; | |
132 | sdi->vendor = g_strdup(models[model_id].vendor); | |
133 | sdi->model = g_strdup(models[model_id].name); | |
134 | sdi->inst_type = SR_INST_SERIAL; | |
135 | sdi->conn = serial; | |
136 | ||
137 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V"); | |
138 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I"); | |
139 | ||
140 | devc = g_malloc0(sizeof(struct dev_context)); | |
141 | sr_sw_limits_init(&devc->limits); | |
142 | devc->model = &models[model_id]; | |
143 | devc->reply[5] = 0; | |
144 | devc->req_sent_at = 0; | |
145 | sdi->priv = devc; | |
146 | ||
147 | /* Get current status of device. */ | |
148 | if (korad_kaxxxxp_get_all_values(serial, devc) < 0) | |
149 | goto exit_err; | |
150 | ||
151 | serial_close(serial); | |
152 | ||
153 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
154 | ||
155 | exit_err: | |
156 | sr_dev_inst_free(sdi); | |
157 | g_free(devc); | |
158 | sr_dbg("Scan failed."); | |
159 | ||
160 | return NULL; | |
161 | } | |
162 | ||
163 | static int config_get(uint32_t key, GVariant **data, | |
164 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
165 | { | |
166 | struct dev_context *devc; | |
167 | ||
168 | (void)cg; | |
169 | ||
170 | if (!sdi || !data) | |
171 | return SR_ERR_ARG; | |
172 | ||
173 | devc = sdi->priv; | |
174 | ||
175 | switch (key) { | |
176 | case SR_CONF_LIMIT_SAMPLES: | |
177 | case SR_CONF_LIMIT_MSEC: | |
178 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
179 | case SR_CONF_VOLTAGE: | |
180 | *data = g_variant_new_double(devc->voltage); | |
181 | break; | |
182 | case SR_CONF_VOLTAGE_TARGET: | |
183 | *data = g_variant_new_double(devc->voltage_max); | |
184 | break; | |
185 | case SR_CONF_CURRENT: | |
186 | *data = g_variant_new_double(devc->current); | |
187 | break; | |
188 | case SR_CONF_CURRENT_LIMIT: | |
189 | *data = g_variant_new_double(devc->current_max); | |
190 | break; | |
191 | case SR_CONF_ENABLED: | |
192 | *data = g_variant_new_boolean(devc->output_enabled); | |
193 | break; | |
194 | case SR_CONF_REGULATION: | |
195 | /* Dual channel not supported. */ | |
196 | *data = g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV"); | |
197 | break; | |
198 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
199 | *data = g_variant_new_boolean(devc->ocp_enabled); | |
200 | break; | |
201 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
202 | *data = g_variant_new_boolean(devc->ovp_enabled); | |
203 | break; | |
204 | default: | |
205 | return SR_ERR_NA; | |
206 | } | |
207 | ||
208 | return SR_OK; | |
209 | } | |
210 | ||
211 | static int config_set(uint32_t key, GVariant *data, | |
212 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
213 | { | |
214 | struct dev_context *devc; | |
215 | double dval; | |
216 | gboolean bval; | |
217 | ||
218 | (void)cg; | |
219 | ||
220 | devc = sdi->priv; | |
221 | ||
222 | switch (key) { | |
223 | case SR_CONF_LIMIT_MSEC: | |
224 | case SR_CONF_LIMIT_SAMPLES: | |
225 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
226 | case SR_CONF_VOLTAGE_TARGET: | |
227 | dval = g_variant_get_double(data); | |
228 | if (dval < devc->model->voltage[0] || dval > devc->model->voltage[1]) | |
229 | return SR_ERR_ARG; | |
230 | devc->voltage_max = dval; | |
231 | devc->target = KAXXXXP_VOLTAGE_MAX; | |
232 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
233 | return SR_ERR; | |
234 | break; | |
235 | case SR_CONF_CURRENT_LIMIT: | |
236 | dval = g_variant_get_double(data); | |
237 | if (dval < devc->model->current[0] || dval > devc->model->current[1]) | |
238 | return SR_ERR_ARG; | |
239 | devc->current_max = dval; | |
240 | devc->target = KAXXXXP_CURRENT_MAX; | |
241 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
242 | return SR_ERR; | |
243 | break; | |
244 | case SR_CONF_ENABLED: | |
245 | bval = g_variant_get_boolean(data); | |
246 | /* Set always so it is possible turn off with sigrok-cli. */ | |
247 | devc->output_enabled = bval; | |
248 | devc->target = KAXXXXP_OUTPUT; | |
249 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
250 | return SR_ERR; | |
251 | break; | |
252 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
253 | bval = g_variant_get_boolean(data); | |
254 | devc->ocp_enabled = bval; | |
255 | devc->target = KAXXXXP_OCP; | |
256 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
257 | return SR_ERR; | |
258 | break; | |
259 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
260 | bval = g_variant_get_boolean(data); | |
261 | devc->ovp_enabled = bval; | |
262 | devc->target = KAXXXXP_OVP; | |
263 | if (korad_kaxxxxp_set_value(sdi->conn, devc) < 0) | |
264 | return SR_ERR; | |
265 | break; | |
266 | default: | |
267 | return SR_ERR_NA; | |
268 | } | |
269 | ||
270 | return SR_OK; | |
271 | } | |
272 | ||
273 | static int config_list(uint32_t key, GVariant **data, | |
274 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
275 | { | |
276 | struct dev_context *devc; | |
277 | ||
278 | devc = (sdi) ? sdi->priv : NULL; | |
279 | ||
280 | switch (key) { | |
281 | case SR_CONF_SCAN_OPTIONS: | |
282 | case SR_CONF_DEVICE_OPTIONS: | |
283 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
284 | case SR_CONF_VOLTAGE_TARGET: | |
285 | if (!devc || !devc->model) | |
286 | return SR_ERR_ARG; | |
287 | *data = std_gvar_min_max_step_array(devc->model->voltage); | |
288 | break; | |
289 | case SR_CONF_CURRENT_LIMIT: | |
290 | if (!devc || !devc->model) | |
291 | return SR_ERR_ARG; | |
292 | *data = std_gvar_min_max_step_array(devc->model->current); | |
293 | break; | |
294 | default: | |
295 | return SR_ERR_NA; | |
296 | } | |
297 | ||
298 | return SR_OK; | |
299 | } | |
300 | ||
301 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
302 | { | |
303 | struct dev_context *devc; | |
304 | struct sr_serial_dev_inst *serial; | |
305 | ||
306 | devc = sdi->priv; | |
307 | ||
308 | sr_sw_limits_acquisition_start(&devc->limits); | |
309 | std_session_send_df_header(sdi); | |
310 | ||
311 | devc->reply_pending = FALSE; | |
312 | devc->req_sent_at = 0; | |
313 | serial = sdi->conn; | |
314 | serial_source_add(sdi->session, serial, G_IO_IN, | |
315 | KAXXXXP_POLL_INTERVAL_MS, | |
316 | korad_kaxxxxp_receive_data, (void *)sdi); | |
317 | ||
318 | return SR_OK; | |
319 | } | |
320 | ||
321 | static struct sr_dev_driver korad_kaxxxxp_driver_info = { | |
322 | .name = "korad-kaxxxxp", | |
323 | .longname = "Korad KAxxxxP", | |
324 | .api_version = 1, | |
325 | .init = std_init, | |
326 | .cleanup = std_cleanup, | |
327 | .scan = scan, | |
328 | .dev_list = std_dev_list, | |
329 | .dev_clear = std_dev_clear, | |
330 | .config_get = config_get, | |
331 | .config_set = config_set, | |
332 | .config_list = config_list, | |
333 | .dev_open = std_serial_dev_open, | |
334 | .dev_close = std_serial_dev_close, | |
335 | .dev_acquisition_start = dev_acquisition_start, | |
336 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
337 | .context = NULL, | |
338 | }; | |
339 | SR_REGISTER_DEV_DRIVER(korad_kaxxxxp_driver_info); |