]> sigrok.org Git - libsigrok.git/blame - src/hardware/maynuo-m97/api.c
sr_dev_close(): Set status to SR_ST_INACTIVE.
[libsigrok.git] / src / hardware / maynuo-m97 / api.c
CommitLineData
e1ccfb19
AJ
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Aurelien Jacobs <aurel@gnuage.org>
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
6ec6c43b 20#include <config.h>
e1ccfb19
AJ
21#include "protocol.h"
22
ffb580cf
AJ
23static const uint32_t scanopts[] = {
24 SR_CONF_CONN,
25 SR_CONF_SERIALCOMM,
26 SR_CONF_MODBUSADDR
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_ELECTRONIC_LOAD,
31};
32
33static const uint32_t devopts[] = {
e91bb0a6 34 SR_CONF_CONTINUOUS,
ffb580cf
AJ
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
37};
38
39static const uint32_t devopts_cg[] = {
40 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
41 SR_CONF_REGULATION | SR_CONF_GET,
42 SR_CONF_VOLTAGE | SR_CONF_GET,
43 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_CURRENT | SR_CONF_GET,
45 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET,
47 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
48 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
49 SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET,
50 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
51 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
52 SR_CONF_OVER_TEMPERATURE_PROTECTION | SR_CONF_GET,
53 SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE | SR_CONF_GET,
54};
55
d9251a2c
UH
56/*
57 * The IDs in this list are only guessed and needs to be verified
58 * against some real hardware. If at least a few of them matches,
59 * it will probably be safe to enable the others.
60 */
ffb580cf
AJ
61static const struct maynuo_m97_model supported_models[] = {
62// { 53, "M9711" , 30, 150, 150 },
63// { 54, "M9712" , 30, 150, 300 },
64// { 55, "M9712C" , 60, 150, 300 },
65// { 56, "M9713" , 120, 150, 600 },
66// { 57, "M9712B" , 15, 500, 300 },
67// { 58, "M9713B" , 30, 500, 600 },
68// { 59, "M9714" , 240, 150, 1200 },
69// { 60, "M9714B" , 60, 500, 1200 },
70// { 61, "M9715" , 240, 150, 1800 },
71// { 62, "M9715B" , 120, 500, 1800 },
72// { 63, "M9716" , 240, 150, 2400 },
73// { 64, "M9716B" , 120, 500, 2400 },
74// { 65, "M9717C" , 480, 150, 3600 },
75// { 66, "M9717" , 240, 150, 3600 },
76// { 67, "M9717B" , 120, 500, 3600 },
77// { 68, "M9718" , 240, 150, 6000 },
78// { 69, "M9718B" , 120, 500, 6000 },
79// { 70, "M9718D" , 240, 500, 6000 },
80// { 71, "M9836" , 500, 150, 20000 },
81// { 72, "M9836B" , 240, 500, 20000 },
82// { 73, "M9838B" , 240, 500, 50000 },
83// { 74, "M9839B" , 240, 500, 100000 },
84// { 75, "M9840B" , 500, 500, 200000 },
85// { 76, "M9840" , 1500, 150, 200000 },
86// { 77, "M9712B30" , 30, 500, 300 },
87// { 78, "M9718E" , 120, 600, 6000 },
88// { 79, "M9718F" , 480, 150, 6000 },
89// { 80, "M9716E" , 480, 150, 3000 },
90// { 81, "M9710" , 30, 150, 150 },
91// { 82, "M9834" , 500, 150, 10000 },
92// { 83, "M9835" , 500, 150, 15000 },
93// { 84, "M9835B" , 240, 500, 15000 },
94// { 85, "M9837" , 500, 150, 35000 },
95// { 86, "M9837B" , 240, 500, 35000 },
96// { 87, "M9838" , 500, 150, 50000 },
97// { 88, "M9839" , 500, 150, 100000 },
98// { 89, "M9835C" , 1000, 150, 15000 }, /* ?? */
99// { 90, "M9836C" , 1000, 150, 20000 }, /* ?? */
100// { 91, "M9718F-300", 480, 300, 6000 }, /* ?? */
101// { 92, "M9836F" , 1000, 150, 20000 }, /* ?? */
102// { 93, "M9836E" , 240, 600, 20000 }, /* ?? */
103// { 94, "M9717D" , 240, 500, 3600 }, /* ?? */
104// { 95, "M9836B-720", 240, 720, 20000 }, /* ?? */
105// { 96, "M9834H" , 500, 150, 10000 }, /* ?? */
106// { 97, "M9836H" , 500, 150, 20000 }, /* ?? */
107// { 98, "M9718F-500", 480, 500, 6000 }, /* ?? */
108// { 99, "M9834B" , 240, 500, 10000 }, /* ?? */
109// { 100, "M9811" , 30, 150, 200 },
110 { 101, "M9812" , 30, 150, 300 },
111// { 102, "M9812B" , 15, 500, 300 },
112};
113
dd5c48a6 114static struct sr_dev_driver maynuo_m97_driver_info;
e1ccfb19 115
ffb580cf 116static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
e1ccfb19 117{
ffb580cf
AJ
118 const struct maynuo_m97_model *model = NULL;
119 struct dev_context *devc;
120 struct sr_dev_inst *sdi;
121 struct sr_channel_group *cg;
122 struct sr_channel *ch;
123 uint16_t id, version;
124 unsigned int i;
125
126 int ret = maynuo_m97_get_model_version(modbus, &id, &version);
127 if (ret != SR_OK)
128 return NULL;
c442ffda 129 for (i = 0; i < ARRAY_SIZE(supported_models); i++)
ffb580cf
AJ
130 if (id == supported_models[i].id) {
131 model = &supported_models[i];
132 break;
133 }
134 if (model == NULL) {
5af975b5 135 sr_err("Unknown model: %d.", id);
ffb580cf
AJ
136 return NULL;
137 }
138
139 sdi = g_malloc0(sizeof(struct sr_dev_inst));
45884333 140 sdi->status = SR_ST_INACTIVE;
ffb580cf
AJ
141 sdi->vendor = g_strdup("Maynuo");
142 sdi->model = g_strdup(model->name);
143 sdi->version = g_strdup_printf("v%d.%d", version/10, version%10);
144 sdi->conn = modbus;
145 sdi->driver = &maynuo_m97_driver_info;
146 sdi->inst_type = SR_INST_MODBUS;
e1ccfb19 147
ffb580cf
AJ
148 cg = g_malloc0(sizeof(struct sr_channel_group));
149 cg->name = g_strdup("1");
150 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
e1ccfb19 151
ffb580cf
AJ
152 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V1");
153 cg->channels = g_slist_append(cg->channels, ch);
e1ccfb19 154
ffb580cf
AJ
155 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I1");
156 cg->channels = g_slist_append(cg->channels, ch);
157
158 devc = g_malloc0(sizeof(struct dev_context));
159 devc->model = model;
160
161 sdi->priv = devc;
162
163 return sdi;
164}
165
166static int config_compare(gconstpointer a, gconstpointer b)
167{
168 const struct sr_config *ac = a, *bc = b;
169 return ac->key != bc->key;
170}
171
172static GSList *scan(struct sr_dev_driver *di, GSList *options)
173{
174 struct sr_config default_serialcomm = {
d9251a2c
UH
175 .key = SR_CONF_SERIALCOMM,
176 .data = g_variant_new_string("9600/8n1"),
ffb580cf
AJ
177 };
178 struct sr_config default_modbusaddr = {
d9251a2c
UH
179 .key = SR_CONF_MODBUSADDR,
180 .data = g_variant_new_uint64(1),
ffb580cf
AJ
181 };
182 GSList *opts = options, *devices;
183
184 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
185 opts = g_slist_prepend(opts, &default_serialcomm);
186 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
187 opts = g_slist_prepend(opts, &default_modbusaddr);
188
41812aca 189 devices = sr_modbus_scan(di->context, opts, probe_device);
ffb580cf
AJ
190
191 while (opts != options)
192 opts = g_slist_delete_link(opts, opts);
193 g_variant_unref(default_serialcomm.data);
194 g_variant_unref(default_modbusaddr.data);
e1ccfb19
AJ
195
196 return devices;
197}
198
e1ccfb19
AJ
199static int dev_open(struct sr_dev_inst *sdi)
200{
ffb580cf 201 struct sr_modbus_dev_inst *modbus = sdi->conn;
e1ccfb19 202
ffb580cf
AJ
203 if (sr_modbus_open(modbus) < 0)
204 return SR_ERR;
e1ccfb19 205
ffb580cf
AJ
206 maynuo_m97_set_bit(modbus, PC1, 1);
207
e1ccfb19
AJ
208 return SR_OK;
209}
210
211static int dev_close(struct sr_dev_inst *sdi)
212{
ffb580cf
AJ
213 struct dev_context *devc;
214 struct sr_modbus_dev_inst *modbus;
e1ccfb19 215
ffb580cf
AJ
216 modbus = sdi->conn;
217
f1ba6b4b
UH
218 if (!modbus)
219 return SR_ERR_BUG;
ffb580cf 220
f1ba6b4b 221 devc = sdi->priv;
ffb580cf 222
f1ba6b4b
UH
223 if (devc->expecting_registers) {
224 /* Wait for the last data that was requested from the device. */
225 uint16_t registers[devc->expecting_registers];
226 sr_modbus_read_holding_registers(modbus, -1,
227 devc->expecting_registers, registers);
ffb580cf 228 }
e1ccfb19 229
f1ba6b4b
UH
230 maynuo_m97_set_bit(modbus, PC1, 0);
231
232 return sr_modbus_close(modbus);
e1ccfb19
AJ
233}
234
e1ccfb19
AJ
235static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
236 const struct sr_channel_group *cg)
237{
ffb580cf
AJ
238 struct dev_context *devc;
239 struct sr_modbus_dev_inst *modbus;
240 enum maynuo_m97_mode mode;
241 int ret, ivalue;
242 float fvalue;
e1ccfb19 243
e1ccfb19
AJ
244 (void)cg;
245
ffb580cf
AJ
246 modbus = sdi->conn;
247 devc = sdi->priv;
248
e1ccfb19
AJ
249 ret = SR_OK;
250 switch (key) {
ffb580cf 251 case SR_CONF_LIMIT_SAMPLES:
ffb580cf 252 case SR_CONF_LIMIT_MSEC:
45b75c36 253 ret = sr_sw_limits_config_get(&devc->limits, key, data);
ffb580cf
AJ
254 break;
255 case SR_CONF_ENABLED:
256 if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK)
257 *data = g_variant_new_boolean(ivalue);
258 break;
259 case SR_CONF_REGULATION:
260 if ((ret = maynuo_m97_get_bit(modbus, UNREG, &ivalue)) != SR_OK)
261 break;
262 if (ivalue)
263 *data = g_variant_new_string("UR");
264 else if ((ret = maynuo_m97_get_mode(modbus, &mode)) == SR_OK)
265 *data = g_variant_new_string(maynuo_m97_mode_to_str(mode));
266 break;
267 case SR_CONF_VOLTAGE:
268 if ((ret = maynuo_m97_get_float(modbus, U, &fvalue)) == SR_OK)
269 *data = g_variant_new_double(fvalue);
270 break;
271 case SR_CONF_VOLTAGE_TARGET:
272 if ((ret = maynuo_m97_get_float(modbus, UFIX, &fvalue)) == SR_OK)
273 *data = g_variant_new_double(fvalue);
274 break;
275 case SR_CONF_CURRENT:
276 if ((ret = maynuo_m97_get_float(modbus, I, &fvalue)) == SR_OK)
277 *data = g_variant_new_double(fvalue);
278 break;
279 case SR_CONF_CURRENT_LIMIT:
280 if ((ret = maynuo_m97_get_float(modbus, IFIX, &fvalue)) == SR_OK)
281 *data = g_variant_new_double(fvalue);
282 break;
283 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
284 *data = g_variant_new_boolean(TRUE);
285 break;
286 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
287 if ((ret = maynuo_m97_get_bit(modbus, UOVER, &ivalue)) == SR_OK)
288 *data = g_variant_new_boolean(ivalue);
289 break;
290 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
291 if ((ret = maynuo_m97_get_float(modbus, UMAX, &fvalue)) == SR_OK)
292 *data = g_variant_new_double(fvalue);
293 break;
294 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
295 *data = g_variant_new_boolean(TRUE);
296 break;
297 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
298 if ((ret = maynuo_m97_get_bit(modbus, IOVER, &ivalue)) == SR_OK)
299 *data = g_variant_new_boolean(ivalue);
300 break;
301 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
302 if ((ret = maynuo_m97_get_float(modbus, IMAX, &fvalue)) == SR_OK)
303 *data = g_variant_new_double(fvalue);
304 break;
305 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
306 *data = g_variant_new_boolean(TRUE);
307 break;
308 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
309 if ((ret = maynuo_m97_get_bit(modbus, HEAT, &ivalue)) == SR_OK)
310 *data = g_variant_new_boolean(ivalue);
311 break;
e1ccfb19
AJ
312 default:
313 return SR_ERR_NA;
314 }
315
316 return ret;
317}
318
319static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
320 const struct sr_channel_group *cg)
321{
ffb580cf
AJ
322 struct dev_context *devc;
323 struct sr_modbus_dev_inst *modbus;
e1ccfb19
AJ
324 int ret;
325
e1ccfb19
AJ
326 (void)cg;
327
ffb580cf
AJ
328 modbus = sdi->conn;
329 devc = sdi->priv;
330
e1ccfb19
AJ
331 ret = SR_OK;
332 switch (key) {
ffb580cf 333 case SR_CONF_LIMIT_SAMPLES:
ffb580cf 334 case SR_CONF_LIMIT_MSEC:
45b75c36 335 ret = sr_sw_limits_config_set(&devc->limits, key, data);
ffb580cf
AJ
336 break;
337 case SR_CONF_ENABLED:
338 ret = maynuo_m97_set_input(modbus, g_variant_get_boolean(data));
339 break;
340 case SR_CONF_VOLTAGE_TARGET:
341 ret = maynuo_m97_set_float(modbus, UFIX, g_variant_get_double(data));
342 break;
343 case SR_CONF_CURRENT_LIMIT:
344 ret = maynuo_m97_set_float(modbus, IFIX, g_variant_get_double(data));
345 break;
346 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
347 ret = maynuo_m97_set_float(modbus, UMAX, g_variant_get_double(data));
348 break;
349 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
350 ret = maynuo_m97_set_float(modbus, IMAX, g_variant_get_double(data));
351 break;
e1ccfb19
AJ
352 default:
353 ret = SR_ERR_NA;
354 }
355
356 return ret;
357}
358
359static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
360 const struct sr_channel_group *cg)
361{
ffb580cf
AJ
362 struct dev_context *devc;
363 GVariantBuilder gvb;
e1ccfb19
AJ
364 int ret;
365
ffb580cf
AJ
366 /* Always available, even without sdi. */
367 if (key == SR_CONF_SCAN_OPTIONS) {
368 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
369 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
370 return SR_OK;
371 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
372 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
373 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
374 return SR_OK;
375 }
376
377 if (!sdi)
378 return SR_ERR_ARG;
379 devc = sdi->priv;
e1ccfb19
AJ
380
381 ret = SR_OK;
ffb580cf
AJ
382 if (!cg) {
383 /* No channel group: global options. */
384 switch (key) {
385 case SR_CONF_DEVICE_OPTIONS:
386 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
387 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
388 break;
389 default:
390 return SR_ERR_NA;
391 }
392 } else {
393 switch (key) {
394 case SR_CONF_DEVICE_OPTIONS:
395 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
396 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
397 break;
398 case SR_CONF_VOLTAGE_TARGET:
399 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
400 /* Min, max, write resolution. */
401 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
402 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_voltage));
403 g_variant_builder_add_value(&gvb, g_variant_new_double(0.001));
404 *data = g_variant_builder_end(&gvb);
405 break;
406 case SR_CONF_CURRENT_LIMIT:
407 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
408 /* Min, max, step. */
409 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
410 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_current));
411 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0001));
412 *data = g_variant_builder_end(&gvb);
413 break;
414 default:
415 return SR_ERR_NA;
416 }
e1ccfb19
AJ
417 }
418
419 return ret;
420}
421
695dc859 422static int dev_acquisition_start(const struct sr_dev_inst *sdi)
e1ccfb19 423{
ffb580cf
AJ
424 struct dev_context *devc;
425 struct sr_modbus_dev_inst *modbus;
426 int ret;
427
ffb580cf
AJ
428 modbus = sdi->conn;
429 devc = sdi->priv;
e1ccfb19 430
ffb580cf
AJ
431 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
432 maynuo_m97_receive_data, (void *)sdi)) != SR_OK)
433 return ret;
434
45b75c36 435 sr_sw_limits_acquisition_start(&devc->limits);
bee2b016 436 std_session_send_df_header(sdi);
ffb580cf 437
ffb580cf 438 return maynuo_m97_capture_start(sdi);
e1ccfb19
AJ
439}
440
695dc859 441static int dev_acquisition_stop(struct sr_dev_inst *sdi)
e1ccfb19 442{
ffb580cf 443 struct sr_modbus_dev_inst *modbus;
ffb580cf 444
bee2b016 445 std_session_send_df_end(sdi);
ffb580cf
AJ
446
447 modbus = sdi->conn;
448 sr_modbus_source_remove(sdi->session, modbus);
e1ccfb19
AJ
449
450 return SR_OK;
451}
452
dd5c48a6 453static struct sr_dev_driver maynuo_m97_driver_info = {
e1ccfb19 454 .name = "maynuo-m97",
ffb580cf 455 .longname = "maynuo M97/M98 series",
e1ccfb19 456 .api_version = 1,
c2fdcc25 457 .init = std_init,
700d6b64 458 .cleanup = std_cleanup,
e1ccfb19 459 .scan = scan,
c01bf34c 460 .dev_list = std_dev_list,
e1ccfb19
AJ
461 .config_get = config_get,
462 .config_set = config_set,
463 .config_list = config_list,
464 .dev_open = dev_open,
465 .dev_close = dev_close,
466 .dev_acquisition_start = dev_acquisition_start,
467 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 468 .context = NULL,
e1ccfb19 469};
dd5c48a6 470SR_REGISTER_DEV_DRIVER(maynuo_m97_driver_info);