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