]> sigrok.org Git - libsigrok.git/blame - src/hardware/maynuo-m97/api.c
Fix #442 by renaming sr_dev_driver.priv to .context
[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
20#include "protocol.h"
21
ffb580cf
AJ
22static const uint32_t scanopts[] = {
23 SR_CONF_CONN,
24 SR_CONF_SERIALCOMM,
25 SR_CONF_MODBUSADDR
26};
27
28static const uint32_t drvopts[] = {
29 SR_CONF_ELECTRONIC_LOAD,
30};
31
32static 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,
36};
37
38static 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,
53};
54
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. */
58static 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 },
109};
110
e1ccfb19
AJ
111SR_PRIV struct sr_dev_driver maynuo_m97_driver_info;
112
113static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
114{
115 return std_init(sr_ctx, di, LOG_PREFIX);
116}
117
ffb580cf 118static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
e1ccfb19 119{
ffb580cf
AJ
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;
126 unsigned int i;
127
128 int ret = maynuo_m97_get_model_version(modbus, &id, &version);
129 if (ret != SR_OK)
130 return NULL;
131 for (i=0; i<ARRAY_SIZE(supported_models); i++)
132 if (id == supported_models[i].id) {
133 model = &supported_models[i];
134 break;
135 }
136 if (model == NULL) {
137 sr_err("Unkown model: %d.", id);
138 return NULL;
139 }
140
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);
146 sdi->conn = modbus;
147 sdi->driver = &maynuo_m97_driver_info;
148 sdi->inst_type = SR_INST_MODBUS;
e1ccfb19 149
ffb580cf
AJ
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);
e1ccfb19 153
ffb580cf
AJ
154 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V1");
155 cg->channels = g_slist_append(cg->channels, ch);
e1ccfb19 156
ffb580cf
AJ
157 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I1");
158 cg->channels = g_slist_append(cg->channels, ch);
159
160 devc = g_malloc0(sizeof(struct dev_context));
161 devc->model = model;
162
163 sdi->priv = devc;
164
165 return sdi;
166}
167
168static int config_compare(gconstpointer a, gconstpointer b)
169{
170 const struct sr_config *ac = a, *bc = b;
171 return ac->key != bc->key;
172}
173
174static GSList *scan(struct sr_dev_driver *di, GSList *options)
175{
176 struct sr_config default_serialcomm = {
177 .key = SR_CONF_SERIALCOMM,
178 .data = g_variant_new_string("9600/8n1"),
179 };
180 struct sr_config default_modbusaddr = {
181 .key = SR_CONF_MODBUSADDR,
182 .data = g_variant_new_uint64(1),
183 };
184 GSList *opts = options, *devices;
185
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);
190
41812aca 191 devices = sr_modbus_scan(di->context, opts, probe_device);
ffb580cf
AJ
192
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);
e1ccfb19
AJ
197
198 return devices;
199}
200
201static GSList *dev_list(const struct sr_dev_driver *di)
202{
41812aca 203 return ((struct drv_context *)(di->context))->instances;
e1ccfb19
AJ
204}
205
206static int dev_clear(const struct sr_dev_driver *di)
207{
ffb580cf 208 return std_dev_clear(di, g_free);
e1ccfb19
AJ
209}
210
211static int dev_open(struct sr_dev_inst *sdi)
212{
ffb580cf 213 struct sr_modbus_dev_inst *modbus = sdi->conn;
e1ccfb19 214
ffb580cf
AJ
215 if (sr_modbus_open(modbus) < 0)
216 return SR_ERR;
e1ccfb19
AJ
217
218 sdi->status = SR_ST_ACTIVE;
219
ffb580cf
AJ
220 maynuo_m97_set_bit(modbus, PC1, 1);
221
e1ccfb19
AJ
222 return SR_OK;
223}
224
225static int dev_close(struct sr_dev_inst *sdi)
226{
ffb580cf
AJ
227 struct dev_context *devc;
228 struct sr_modbus_dev_inst *modbus;
e1ccfb19 229
ffb580cf
AJ
230 if (sdi->status != SR_ST_ACTIVE)
231 return SR_ERR_DEV_CLOSED;
e1ccfb19 232
ffb580cf
AJ
233 modbus = sdi->conn;
234
235 if (modbus) {
236 devc = sdi->priv;
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,
242 registers);
243 }
244
245 maynuo_m97_set_bit(modbus, PC1, 0);
246
247 if (sr_modbus_close(modbus) < 0)
248 return SR_ERR;
249 sdi->status = SR_ST_INACTIVE;
250 }
e1ccfb19
AJ
251
252 return SR_OK;
253}
254
255static int cleanup(const struct sr_dev_driver *di)
256{
ffb580cf 257 return dev_clear(di);
e1ccfb19
AJ
258}
259
260static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
261 const struct sr_channel_group *cg)
262{
ffb580cf
AJ
263 struct dev_context *devc;
264 struct sr_modbus_dev_inst *modbus;
265 enum maynuo_m97_mode mode;
266 int ret, ivalue;
267 float fvalue;
e1ccfb19 268
e1ccfb19
AJ
269 (void)cg;
270
ffb580cf
AJ
271 modbus = sdi->conn;
272 devc = sdi->priv;
273
e1ccfb19
AJ
274 ret = SR_OK;
275 switch (key) {
ffb580cf
AJ
276 case SR_CONF_LIMIT_SAMPLES:
277 *data = g_variant_new_uint64(devc->limit_samples);
278 break;
279 case SR_CONF_LIMIT_MSEC:
280 *data = g_variant_new_uint64(devc->limit_msec);
281 break;
282 case SR_CONF_ENABLED:
283 if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK)
284 *data = g_variant_new_boolean(ivalue);
285 break;
286 case SR_CONF_REGULATION:
287 if ((ret = maynuo_m97_get_bit(modbus, UNREG, &ivalue)) != SR_OK)
288 break;
289 if (ivalue)
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));
293 break;
294 case SR_CONF_VOLTAGE:
295 if ((ret = maynuo_m97_get_float(modbus, U, &fvalue)) == SR_OK)
296 *data = g_variant_new_double(fvalue);
297 break;
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);
301 break;
302 case SR_CONF_CURRENT:
303 if ((ret = maynuo_m97_get_float(modbus, I, &fvalue)) == SR_OK)
304 *data = g_variant_new_double(fvalue);
305 break;
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);
309 break;
310 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
311 *data = g_variant_new_boolean(TRUE);
312 break;
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);
316 break;
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);
320 break;
321 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
322 *data = g_variant_new_boolean(TRUE);
323 break;
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);
327 break;
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);
331 break;
332 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
333 *data = g_variant_new_boolean(TRUE);
334 break;
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);
338 break;
e1ccfb19
AJ
339 default:
340 return SR_ERR_NA;
341 }
342
343 return ret;
344}
345
346static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
347 const struct sr_channel_group *cg)
348{
ffb580cf
AJ
349 struct dev_context *devc;
350 struct sr_modbus_dev_inst *modbus;
e1ccfb19
AJ
351 int ret;
352
353 (void)data;
354 (void)cg;
355
356 if (sdi->status != SR_ST_ACTIVE)
357 return SR_ERR_DEV_CLOSED;
358
ffb580cf
AJ
359 modbus = sdi->conn;
360 devc = sdi->priv;
361
e1ccfb19
AJ
362 ret = SR_OK;
363 switch (key) {
ffb580cf
AJ
364 case SR_CONF_LIMIT_SAMPLES:
365 devc->limit_samples = g_variant_get_uint64(data);
366 break;
367 case SR_CONF_LIMIT_MSEC:
368 devc->limit_msec = g_variant_get_uint64(data);
369 break;
370 case SR_CONF_ENABLED:
371 ret = maynuo_m97_set_input(modbus, g_variant_get_boolean(data));
372 break;
373 case SR_CONF_VOLTAGE_TARGET:
374 ret = maynuo_m97_set_float(modbus, UFIX, g_variant_get_double(data));
375 break;
376 case SR_CONF_CURRENT_LIMIT:
377 ret = maynuo_m97_set_float(modbus, IFIX, g_variant_get_double(data));
378 break;
379 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
380 ret = maynuo_m97_set_float(modbus, UMAX, g_variant_get_double(data));
381 break;
382 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
383 ret = maynuo_m97_set_float(modbus, IMAX, g_variant_get_double(data));
384 break;
e1ccfb19
AJ
385 default:
386 ret = SR_ERR_NA;
387 }
388
389 return ret;
390}
391
392static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
393 const struct sr_channel_group *cg)
394{
ffb580cf
AJ
395 struct dev_context *devc;
396 GVariantBuilder gvb;
e1ccfb19
AJ
397 int ret;
398
ffb580cf
AJ
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));
403 return SR_OK;
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));
407 return SR_OK;
408 }
409
410 if (!sdi)
411 return SR_ERR_ARG;
412 devc = sdi->priv;
e1ccfb19
AJ
413
414 ret = SR_OK;
ffb580cf
AJ
415 if (!cg) {
416 /* No channel group: global options. */
417 switch (key) {
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));
421 break;
422 default:
423 return SR_ERR_NA;
424 }
425 } else {
426 switch (key) {
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));
430 break;
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);
438 break;
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);
446 break;
447 default:
448 return SR_ERR_NA;
449 }
e1ccfb19
AJ
450 }
451
452 return ret;
453}
454
455static int dev_acquisition_start(const struct sr_dev_inst *sdi,
456 void *cb_data)
457{
ffb580cf
AJ
458 struct dev_context *devc;
459 struct sr_modbus_dev_inst *modbus;
460 int ret;
461
e1ccfb19
AJ
462 (void)cb_data;
463
464 if (sdi->status != SR_ST_ACTIVE)
465 return SR_ERR_DEV_CLOSED;
466
ffb580cf
AJ
467 modbus = sdi->conn;
468 devc = sdi->priv;
e1ccfb19 469
ffb580cf
AJ
470 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
471 maynuo_m97_receive_data, (void *)sdi)) != SR_OK)
472 return ret;
473
474 /* Send header packet to the session bus. */
475 std_session_send_df_header(sdi, LOG_PREFIX);
476
477 devc->num_samples = 0;
478 devc->starttime = g_get_monotonic_time();
479
480 return maynuo_m97_capture_start(sdi);
e1ccfb19
AJ
481}
482
483static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
484{
ffb580cf
AJ
485 struct sr_modbus_dev_inst *modbus;
486 struct sr_datafeed_packet packet;
487
e1ccfb19
AJ
488 (void)cb_data;
489
490 if (sdi->status != SR_ST_ACTIVE)
491 return SR_ERR_DEV_CLOSED;
492
ffb580cf
AJ
493 /* End of last frame. */
494 packet.type = SR_DF_END;
495 sr_session_send(sdi, &packet);
496
497 modbus = sdi->conn;
498 sr_modbus_source_remove(sdi->session, modbus);
e1ccfb19
AJ
499
500 return SR_OK;
501}
502
503SR_PRIV struct sr_dev_driver maynuo_m97_driver_info = {
504 .name = "maynuo-m97",
ffb580cf 505 .longname = "maynuo M97/M98 series",
e1ccfb19
AJ
506 .api_version = 1,
507 .init = init,
508 .cleanup = cleanup,
509 .scan = scan,
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,
41812aca 519 .context = NULL,
e1ccfb19 520};