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/>.
23 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
30 SR_CONF_ELECTRONIC_LOAD,
33 static 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,
39 static 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,
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. */
59 static 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 },
112 SR_PRIV struct sr_dev_driver maynuo_m97_driver_info;
114 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
116 return std_init(sr_ctx, di, LOG_PREFIX);
119 static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
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;
129 int ret = maynuo_m97_get_model_version(modbus, &id, &version);
132 for (i = 0; i < ARRAY_SIZE(supported_models); i++)
133 if (id == supported_models[i].id) {
134 model = &supported_models[i];
138 sr_err("Unkown model: %d.", id);
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);
148 sdi->driver = &maynuo_m97_driver_info;
149 sdi->inst_type = SR_INST_MODBUS;
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);
155 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V1");
156 cg->channels = g_slist_append(cg->channels, ch);
158 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I1");
159 cg->channels = g_slist_append(cg->channels, ch);
161 devc = g_malloc0(sizeof(struct dev_context));
169 static int config_compare(gconstpointer a, gconstpointer b)
171 const struct sr_config *ac = a, *bc = b;
172 return ac->key != bc->key;
175 static GSList *scan(struct sr_dev_driver *di, GSList *options)
177 struct sr_config default_serialcomm = {
178 .key = SR_CONF_SERIALCOMM,
179 .data = g_variant_new_string("9600/8n1"),
181 struct sr_config default_modbusaddr = {
182 .key = SR_CONF_MODBUSADDR,
183 .data = g_variant_new_uint64(1),
185 GSList *opts = options, *devices;
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);
192 devices = sr_modbus_scan(di->context, opts, probe_device);
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);
202 static GSList *dev_list(const struct sr_dev_driver *di)
204 return ((struct drv_context *)(di->context))->instances;
207 static int dev_clear(const struct sr_dev_driver *di)
209 return std_dev_clear(di, g_free);
212 static int dev_open(struct sr_dev_inst *sdi)
214 struct sr_modbus_dev_inst *modbus = sdi->conn;
216 if (sr_modbus_open(modbus) < 0)
219 sdi->status = SR_ST_ACTIVE;
221 maynuo_m97_set_bit(modbus, PC1, 1);
226 static int dev_close(struct sr_dev_inst *sdi)
228 struct dev_context *devc;
229 struct sr_modbus_dev_inst *modbus;
231 if (sdi->status != SR_ST_ACTIVE)
232 return SR_ERR_DEV_CLOSED;
238 if (devc->expecting_registers) {
239 /* Wait for the last data that was requested from the device. */
240 uint16_t registers[devc->expecting_registers];
241 sr_modbus_read_holding_registers(modbus, -1,
242 devc->expecting_registers,
246 maynuo_m97_set_bit(modbus, PC1, 0);
248 if (sr_modbus_close(modbus) < 0)
250 sdi->status = SR_ST_INACTIVE;
256 static int cleanup(const struct sr_dev_driver *di)
258 return dev_clear(di);
261 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
262 const struct sr_channel_group *cg)
264 struct dev_context *devc;
265 struct sr_modbus_dev_inst *modbus;
266 enum maynuo_m97_mode mode;
277 case SR_CONF_LIMIT_SAMPLES:
278 *data = g_variant_new_uint64(devc->limit_samples);
280 case SR_CONF_LIMIT_MSEC:
281 *data = g_variant_new_uint64(devc->limit_msec);
283 case SR_CONF_ENABLED:
284 if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK)
285 *data = g_variant_new_boolean(ivalue);
287 case SR_CONF_REGULATION:
288 if ((ret = maynuo_m97_get_bit(modbus, UNREG, &ivalue)) != SR_OK)
291 *data = g_variant_new_string("UR");
292 else if ((ret = maynuo_m97_get_mode(modbus, &mode)) == SR_OK)
293 *data = g_variant_new_string(maynuo_m97_mode_to_str(mode));
295 case SR_CONF_VOLTAGE:
296 if ((ret = maynuo_m97_get_float(modbus, U, &fvalue)) == SR_OK)
297 *data = g_variant_new_double(fvalue);
299 case SR_CONF_VOLTAGE_TARGET:
300 if ((ret = maynuo_m97_get_float(modbus, UFIX, &fvalue)) == SR_OK)
301 *data = g_variant_new_double(fvalue);
303 case SR_CONF_CURRENT:
304 if ((ret = maynuo_m97_get_float(modbus, I, &fvalue)) == SR_OK)
305 *data = g_variant_new_double(fvalue);
307 case SR_CONF_CURRENT_LIMIT:
308 if ((ret = maynuo_m97_get_float(modbus, IFIX, &fvalue)) == SR_OK)
309 *data = g_variant_new_double(fvalue);
311 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
312 *data = g_variant_new_boolean(TRUE);
314 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
315 if ((ret = maynuo_m97_get_bit(modbus, UOVER, &ivalue)) == SR_OK)
316 *data = g_variant_new_boolean(ivalue);
318 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
319 if ((ret = maynuo_m97_get_float(modbus, UMAX, &fvalue)) == SR_OK)
320 *data = g_variant_new_double(fvalue);
322 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
323 *data = g_variant_new_boolean(TRUE);
325 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
326 if ((ret = maynuo_m97_get_bit(modbus, IOVER, &ivalue)) == SR_OK)
327 *data = g_variant_new_boolean(ivalue);
329 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
330 if ((ret = maynuo_m97_get_float(modbus, IMAX, &fvalue)) == SR_OK)
331 *data = g_variant_new_double(fvalue);
333 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
334 *data = g_variant_new_boolean(TRUE);
336 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
337 if ((ret = maynuo_m97_get_bit(modbus, HEAT, &ivalue)) == SR_OK)
338 *data = g_variant_new_boolean(ivalue);
347 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
348 const struct sr_channel_group *cg)
350 struct dev_context *devc;
351 struct sr_modbus_dev_inst *modbus;
357 if (sdi->status != SR_ST_ACTIVE)
358 return SR_ERR_DEV_CLOSED;
365 case SR_CONF_LIMIT_SAMPLES:
366 devc->limit_samples = g_variant_get_uint64(data);
368 case SR_CONF_LIMIT_MSEC:
369 devc->limit_msec = g_variant_get_uint64(data);
371 case SR_CONF_ENABLED:
372 ret = maynuo_m97_set_input(modbus, g_variant_get_boolean(data));
374 case SR_CONF_VOLTAGE_TARGET:
375 ret = maynuo_m97_set_float(modbus, UFIX, g_variant_get_double(data));
377 case SR_CONF_CURRENT_LIMIT:
378 ret = maynuo_m97_set_float(modbus, IFIX, g_variant_get_double(data));
380 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
381 ret = maynuo_m97_set_float(modbus, UMAX, g_variant_get_double(data));
383 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
384 ret = maynuo_m97_set_float(modbus, IMAX, g_variant_get_double(data));
393 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
394 const struct sr_channel_group *cg)
396 struct dev_context *devc;
400 /* Always available, even without sdi. */
401 if (key == SR_CONF_SCAN_OPTIONS) {
402 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
403 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
405 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
406 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
407 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
417 /* No channel group: global options. */
419 case SR_CONF_DEVICE_OPTIONS:
420 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
421 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
428 case SR_CONF_DEVICE_OPTIONS:
429 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
430 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
432 case SR_CONF_VOLTAGE_TARGET:
433 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
434 /* Min, max, write resolution. */
435 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
436 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_voltage));
437 g_variant_builder_add_value(&gvb, g_variant_new_double(0.001));
438 *data = g_variant_builder_end(&gvb);
440 case SR_CONF_CURRENT_LIMIT:
441 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
442 /* Min, max, step. */
443 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
444 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_current));
445 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0001));
446 *data = g_variant_builder_end(&gvb);
456 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
459 struct dev_context *devc;
460 struct sr_modbus_dev_inst *modbus;
465 if (sdi->status != SR_ST_ACTIVE)
466 return SR_ERR_DEV_CLOSED;
471 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
472 maynuo_m97_receive_data, (void *)sdi)) != SR_OK)
475 /* Send header packet to the session bus. */
476 std_session_send_df_header(sdi, LOG_PREFIX);
478 devc->num_samples = 0;
479 devc->starttime = g_get_monotonic_time();
481 return maynuo_m97_capture_start(sdi);
484 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
486 struct sr_modbus_dev_inst *modbus;
487 struct sr_datafeed_packet packet;
491 if (sdi->status != SR_ST_ACTIVE)
492 return SR_ERR_DEV_CLOSED;
494 /* End of last frame. */
495 packet.type = SR_DF_END;
496 sr_session_send(sdi, &packet);
499 sr_modbus_source_remove(sdi->session, modbus);
504 SR_PRIV struct sr_dev_driver maynuo_m97_driver_info = {
505 .name = "maynuo-m97",
506 .longname = "maynuo M97/M98 series",
511 .dev_list = dev_list,
512 .dev_clear = dev_clear,
513 .config_get = config_get,
514 .config_set = config_set,
515 .config_list = config_list,
516 .dev_open = dev_open,
517 .dev_close = dev_close,
518 .dev_acquisition_start = dev_acquisition_start,
519 .dev_acquisition_stop = dev_acquisition_stop,