2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Aurelien Jacobs <aurel@gnuage.org>
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.
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.
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/>.
22 static const uint32_t scanopts[] = {
28 static const uint32_t drvopts[] = {
29 SR_CONF_ELECTRONIC_LOAD,
32 static const uint32_t devopts[] = {
33 SR_CONF_CONTINUOUS | SR_CONF_SET,
34 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
35 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
38 static const uint32_t devopts_cg[] = {
39 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
40 SR_CONF_REGULATION | SR_CONF_GET,
41 SR_CONF_VOLTAGE | SR_CONF_GET,
42 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_CURRENT | SR_CONF_GET,
44 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED | SR_CONF_GET,
46 SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE | SR_CONF_GET,
47 SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
48 SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET,
49 SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE | SR_CONF_GET,
50 SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD | SR_CONF_GET | SR_CONF_SET,
51 SR_CONF_OVER_TEMPERATURE_PROTECTION | SR_CONF_GET,
52 SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE | SR_CONF_GET,
55 /* The IDs in this list are only guessed and needs to be verified
56 against some real hardware. If at least a few of them matches,
57 it will probably be safe to enable the others. */
58 static const struct maynuo_m97_model supported_models[] = {
59 // { 53, "M9711" , 30, 150, 150 },
60 // { 54, "M9712" , 30, 150, 300 },
61 // { 55, "M9712C" , 60, 150, 300 },
62 // { 56, "M9713" , 120, 150, 600 },
63 // { 57, "M9712B" , 15, 500, 300 },
64 // { 58, "M9713B" , 30, 500, 600 },
65 // { 59, "M9714" , 240, 150, 1200 },
66 // { 60, "M9714B" , 60, 500, 1200 },
67 // { 61, "M9715" , 240, 150, 1800 },
68 // { 62, "M9715B" , 120, 500, 1800 },
69 // { 63, "M9716" , 240, 150, 2400 },
70 // { 64, "M9716B" , 120, 500, 2400 },
71 // { 65, "M9717C" , 480, 150, 3600 },
72 // { 66, "M9717" , 240, 150, 3600 },
73 // { 67, "M9717B" , 120, 500, 3600 },
74 // { 68, "M9718" , 240, 150, 6000 },
75 // { 69, "M9718B" , 120, 500, 6000 },
76 // { 70, "M9718D" , 240, 500, 6000 },
77 // { 71, "M9836" , 500, 150, 20000 },
78 // { 72, "M9836B" , 240, 500, 20000 },
79 // { 73, "M9838B" , 240, 500, 50000 },
80 // { 74, "M9839B" , 240, 500, 100000 },
81 // { 75, "M9840B" , 500, 500, 200000 },
82 // { 76, "M9840" , 1500, 150, 200000 },
83 // { 77, "M9712B30" , 30, 500, 300 },
84 // { 78, "M9718E" , 120, 600, 6000 },
85 // { 79, "M9718F" , 480, 150, 6000 },
86 // { 80, "M9716E" , 480, 150, 3000 },
87 // { 81, "M9710" , 30, 150, 150 },
88 // { 82, "M9834" , 500, 150, 10000 },
89 // { 83, "M9835" , 500, 150, 15000 },
90 // { 84, "M9835B" , 240, 500, 15000 },
91 // { 85, "M9837" , 500, 150, 35000 },
92 // { 86, "M9837B" , 240, 500, 35000 },
93 // { 87, "M9838" , 500, 150, 50000 },
94 // { 88, "M9839" , 500, 150, 100000 },
95 // { 89, "M9835C" , 1000, 150, 15000 }, /* ?? */
96 // { 90, "M9836C" , 1000, 150, 20000 }, /* ?? */
97 // { 91, "M9718F-300", 480, 300, 6000 }, /* ?? */
98 // { 92, "M9836F" , 1000, 150, 20000 }, /* ?? */
99 // { 93, "M9836E" , 240, 600, 20000 }, /* ?? */
100 // { 94, "M9717D" , 240, 500, 3600 }, /* ?? */
101 // { 95, "M9836B-720", 240, 720, 20000 }, /* ?? */
102 // { 96, "M9834H" , 500, 150, 10000 }, /* ?? */
103 // { 97, "M9836H" , 500, 150, 20000 }, /* ?? */
104 // { 98, "M9718F-500", 480, 500, 6000 }, /* ?? */
105 // { 99, "M9834B" , 240, 500, 10000 }, /* ?? */
106 // { 100, "M9811" , 30, 150, 200 },
107 { 101, "M9812" , 30, 150, 300 },
108 // { 102, "M9812B" , 15, 500, 300 },
111 SR_PRIV struct sr_dev_driver maynuo_m97_driver_info;
113 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
115 return std_init(sr_ctx, di, LOG_PREFIX);
118 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
120 const struct maynuo_m97_model *model = NULL;
121 struct dev_context *devc;
122 struct sr_dev_inst *sdi;
123 struct sr_channel_group *cg;
124 struct sr_channel *ch;
125 uint16_t id, version;
128 int ret = maynuo_m97_get_model_version(modbus, &id, &version);
131 for (i=0; i<ARRAY_SIZE(supported_models); i++)
132 if (id == supported_models[i].id) {
133 model = &supported_models[i];
137 sr_err("Unkown model: %d.", id);
141 sdi = g_malloc0(sizeof(struct sr_dev_inst));
142 sdi->status = SR_ST_ACTIVE;
143 sdi->vendor = g_strdup("Maynuo");
144 sdi->model = g_strdup(model->name);
145 sdi->version = g_strdup_printf("v%d.%d", version/10, version%10);
147 sdi->driver = &maynuo_m97_driver_info;
148 sdi->inst_type = SR_INST_MODBUS;
150 cg = g_malloc0(sizeof(struct sr_channel_group));
151 cg->name = g_strdup("1");
152 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
154 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V1");
155 cg->channels = g_slist_append(cg->channels, ch);
157 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I1");
158 cg->channels = g_slist_append(cg->channels, ch);
160 devc = g_malloc0(sizeof(struct dev_context));
168 static int config_compare(gconstpointer a, gconstpointer b)
170 const struct sr_config *ac = a, *bc = b;
171 return ac->key != bc->key;
174 static GSList *scan(struct sr_dev_driver *di, GSList *options)
176 struct sr_config default_serialcomm = {
177 .key = SR_CONF_SERIALCOMM,
178 .data = g_variant_new_string("9600/8n1"),
180 struct sr_config default_modbusaddr = {
181 .key = SR_CONF_MODBUSADDR,
182 .data = g_variant_new_uint64(1),
184 GSList *opts = options, *devices;
186 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
187 opts = g_slist_prepend(opts, &default_serialcomm);
188 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
189 opts = g_slist_prepend(opts, &default_modbusaddr);
191 devices = sr_modbus_scan(di->context, opts, probe_device);
193 while (opts != options)
194 opts = g_slist_delete_link(opts, opts);
195 g_variant_unref(default_serialcomm.data);
196 g_variant_unref(default_modbusaddr.data);
201 static GSList *dev_list(const struct sr_dev_driver *di)
203 return ((struct drv_context *)(di->context))->instances;
206 static int dev_clear(const struct sr_dev_driver *di)
208 return std_dev_clear(di, g_free);
211 static int dev_open(struct sr_dev_inst *sdi)
213 struct sr_modbus_dev_inst *modbus = sdi->conn;
215 if (sr_modbus_open(modbus) < 0)
218 sdi->status = SR_ST_ACTIVE;
220 maynuo_m97_set_bit(modbus, PC1, 1);
225 static int dev_close(struct sr_dev_inst *sdi)
227 struct dev_context *devc;
228 struct sr_modbus_dev_inst *modbus;
230 if (sdi->status != SR_ST_ACTIVE)
231 return SR_ERR_DEV_CLOSED;
237 if (devc->expecting_registers) {
238 /* Wait for the last data that was requested from the device. */
239 uint16_t registers[devc->expecting_registers];
240 sr_modbus_read_holding_registers(modbus, -1,
241 devc->expecting_registers,
245 maynuo_m97_set_bit(modbus, PC1, 0);
247 if (sr_modbus_close(modbus) < 0)
249 sdi->status = SR_ST_INACTIVE;
255 static int cleanup(const struct sr_dev_driver *di)
257 return dev_clear(di);
260 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
261 const struct sr_channel_group *cg)
263 struct dev_context *devc;
264 struct sr_modbus_dev_inst *modbus;
265 enum maynuo_m97_mode mode;
276 case SR_CONF_LIMIT_SAMPLES:
277 *data = g_variant_new_uint64(devc->limit_samples);
279 case SR_CONF_LIMIT_MSEC:
280 *data = g_variant_new_uint64(devc->limit_msec);
282 case SR_CONF_ENABLED:
283 if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK)
284 *data = g_variant_new_boolean(ivalue);
286 case SR_CONF_REGULATION:
287 if ((ret = maynuo_m97_get_bit(modbus, UNREG, &ivalue)) != SR_OK)
290 *data = g_variant_new_string("UR");
291 else if ((ret = maynuo_m97_get_mode(modbus, &mode)) == SR_OK)
292 *data = g_variant_new_string(maynuo_m97_mode_to_str(mode));
294 case SR_CONF_VOLTAGE:
295 if ((ret = maynuo_m97_get_float(modbus, U, &fvalue)) == SR_OK)
296 *data = g_variant_new_double(fvalue);
298 case SR_CONF_VOLTAGE_TARGET:
299 if ((ret = maynuo_m97_get_float(modbus, UFIX, &fvalue)) == SR_OK)
300 *data = g_variant_new_double(fvalue);
302 case SR_CONF_CURRENT:
303 if ((ret = maynuo_m97_get_float(modbus, I, &fvalue)) == SR_OK)
304 *data = g_variant_new_double(fvalue);
306 case SR_CONF_CURRENT_LIMIT:
307 if ((ret = maynuo_m97_get_float(modbus, IFIX, &fvalue)) == SR_OK)
308 *data = g_variant_new_double(fvalue);
310 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
311 *data = g_variant_new_boolean(TRUE);
313 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
314 if ((ret = maynuo_m97_get_bit(modbus, UOVER, &ivalue)) == SR_OK)
315 *data = g_variant_new_boolean(ivalue);
317 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
318 if ((ret = maynuo_m97_get_float(modbus, UMAX, &fvalue)) == SR_OK)
319 *data = g_variant_new_double(fvalue);
321 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
322 *data = g_variant_new_boolean(TRUE);
324 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
325 if ((ret = maynuo_m97_get_bit(modbus, IOVER, &ivalue)) == SR_OK)
326 *data = g_variant_new_boolean(ivalue);
328 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
329 if ((ret = maynuo_m97_get_float(modbus, IMAX, &fvalue)) == SR_OK)
330 *data = g_variant_new_double(fvalue);
332 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
333 *data = g_variant_new_boolean(TRUE);
335 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
336 if ((ret = maynuo_m97_get_bit(modbus, HEAT, &ivalue)) == SR_OK)
337 *data = g_variant_new_boolean(ivalue);
346 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
347 const struct sr_channel_group *cg)
349 struct dev_context *devc;
350 struct sr_modbus_dev_inst *modbus;
356 if (sdi->status != SR_ST_ACTIVE)
357 return SR_ERR_DEV_CLOSED;
364 case SR_CONF_LIMIT_SAMPLES:
365 devc->limit_samples = g_variant_get_uint64(data);
367 case SR_CONF_LIMIT_MSEC:
368 devc->limit_msec = g_variant_get_uint64(data);
370 case SR_CONF_ENABLED:
371 ret = maynuo_m97_set_input(modbus, g_variant_get_boolean(data));
373 case SR_CONF_VOLTAGE_TARGET:
374 ret = maynuo_m97_set_float(modbus, UFIX, g_variant_get_double(data));
376 case SR_CONF_CURRENT_LIMIT:
377 ret = maynuo_m97_set_float(modbus, IFIX, g_variant_get_double(data));
379 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
380 ret = maynuo_m97_set_float(modbus, UMAX, g_variant_get_double(data));
382 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
383 ret = maynuo_m97_set_float(modbus, IMAX, g_variant_get_double(data));
392 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
393 const struct sr_channel_group *cg)
395 struct dev_context *devc;
399 /* Always available, even without sdi. */
400 if (key == SR_CONF_SCAN_OPTIONS) {
401 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
402 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
404 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
405 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
406 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
416 /* No channel group: global options. */
418 case SR_CONF_DEVICE_OPTIONS:
419 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
420 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
427 case SR_CONF_DEVICE_OPTIONS:
428 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
429 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
431 case SR_CONF_VOLTAGE_TARGET:
432 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
433 /* Min, max, write resolution. */
434 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
435 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_voltage));
436 g_variant_builder_add_value(&gvb, g_variant_new_double(0.001));
437 *data = g_variant_builder_end(&gvb);
439 case SR_CONF_CURRENT_LIMIT:
440 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
441 /* Min, max, step. */
442 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
443 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_current));
444 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0001));
445 *data = g_variant_builder_end(&gvb);
455 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
458 struct dev_context *devc;
459 struct sr_modbus_dev_inst *modbus;
464 if (sdi->status != SR_ST_ACTIVE)
465 return SR_ERR_DEV_CLOSED;
470 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
471 maynuo_m97_receive_data, (void *)sdi)) != SR_OK)
474 /* Send header packet to the session bus. */
475 std_session_send_df_header(sdi, LOG_PREFIX);
477 devc->num_samples = 0;
478 devc->starttime = g_get_monotonic_time();
480 return maynuo_m97_capture_start(sdi);
483 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
485 struct sr_modbus_dev_inst *modbus;
486 struct sr_datafeed_packet packet;
490 if (sdi->status != SR_ST_ACTIVE)
491 return SR_ERR_DEV_CLOSED;
493 /* End of last frame. */
494 packet.type = SR_DF_END;
495 sr_session_send(sdi, &packet);
498 sr_modbus_source_remove(sdi->session, modbus);
503 SR_PRIV struct sr_dev_driver maynuo_m97_driver_info = {
504 .name = "maynuo-m97",
505 .longname = "maynuo M97/M98 series",
510 .dev_list = dev_list,
511 .dev_clear = dev_clear,
512 .config_get = config_get,
513 .config_set = config_set,
514 .config_list = config_list,
515 .dev_open = dev_open,
516 .dev_close = dev_close,
517 .dev_acquisition_start = dev_acquisition_start,
518 .dev_acquisition_stop = dev_acquisition_stop,