]> sigrok.org Git - libsigrok.git/blame - src/hardware/maynuo-m97/api.c
Put driver pointers into special section
[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
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
dd5c48a6 112static struct sr_dev_driver maynuo_m97_driver_info;
e1ccfb19 113
ffb580cf 114static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus)
e1ccfb19 115{
ffb580cf
AJ
116 const struct maynuo_m97_model *model = NULL;
117 struct dev_context *devc;
118 struct sr_dev_inst *sdi;
119 struct sr_channel_group *cg;
120 struct sr_channel *ch;
121 uint16_t id, version;
122 unsigned int i;
123
124 int ret = maynuo_m97_get_model_version(modbus, &id, &version);
125 if (ret != SR_OK)
126 return NULL;
c442ffda 127 for (i = 0; i < ARRAY_SIZE(supported_models); i++)
ffb580cf
AJ
128 if (id == supported_models[i].id) {
129 model = &supported_models[i];
130 break;
131 }
132 if (model == NULL) {
5af975b5 133 sr_err("Unknown model: %d.", id);
ffb580cf
AJ
134 return NULL;
135 }
136
137 sdi = g_malloc0(sizeof(struct sr_dev_inst));
45884333 138 sdi->status = SR_ST_INACTIVE;
ffb580cf
AJ
139 sdi->vendor = g_strdup("Maynuo");
140 sdi->model = g_strdup(model->name);
141 sdi->version = g_strdup_printf("v%d.%d", version/10, version%10);
142 sdi->conn = modbus;
143 sdi->driver = &maynuo_m97_driver_info;
144 sdi->inst_type = SR_INST_MODBUS;
e1ccfb19 145
ffb580cf
AJ
146 cg = g_malloc0(sizeof(struct sr_channel_group));
147 cg->name = g_strdup("1");
148 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
e1ccfb19 149
ffb580cf
AJ
150 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V1");
151 cg->channels = g_slist_append(cg->channels, ch);
e1ccfb19 152
ffb580cf
AJ
153 ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "I1");
154 cg->channels = g_slist_append(cg->channels, ch);
155
156 devc = g_malloc0(sizeof(struct dev_context));
157 devc->model = model;
158
159 sdi->priv = devc;
160
161 return sdi;
162}
163
164static int config_compare(gconstpointer a, gconstpointer b)
165{
166 const struct sr_config *ac = a, *bc = b;
167 return ac->key != bc->key;
168}
169
170static GSList *scan(struct sr_dev_driver *di, GSList *options)
171{
172 struct sr_config default_serialcomm = {
173 .key = SR_CONF_SERIALCOMM,
174 .data = g_variant_new_string("9600/8n1"),
175 };
176 struct sr_config default_modbusaddr = {
177 .key = SR_CONF_MODBUSADDR,
178 .data = g_variant_new_uint64(1),
179 };
180 GSList *opts = options, *devices;
181
182 if (!g_slist_find_custom(options, &default_serialcomm, config_compare))
183 opts = g_slist_prepend(opts, &default_serialcomm);
184 if (!g_slist_find_custom(options, &default_modbusaddr, config_compare))
185 opts = g_slist_prepend(opts, &default_modbusaddr);
186
41812aca 187 devices = sr_modbus_scan(di->context, opts, probe_device);
ffb580cf
AJ
188
189 while (opts != options)
190 opts = g_slist_delete_link(opts, opts);
191 g_variant_unref(default_serialcomm.data);
192 g_variant_unref(default_modbusaddr.data);
e1ccfb19
AJ
193
194 return devices;
195}
196
e1ccfb19
AJ
197static int dev_open(struct sr_dev_inst *sdi)
198{
ffb580cf 199 struct sr_modbus_dev_inst *modbus = sdi->conn;
e1ccfb19 200
ffb580cf
AJ
201 if (sr_modbus_open(modbus) < 0)
202 return SR_ERR;
e1ccfb19
AJ
203
204 sdi->status = SR_ST_ACTIVE;
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 if (sdi->status != SR_ST_ACTIVE)
217 return SR_ERR_DEV_CLOSED;
e1ccfb19 218
ffb580cf
AJ
219 modbus = sdi->conn;
220
221 if (modbus) {
222 devc = sdi->priv;
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,
228 registers);
229 }
230
231 maynuo_m97_set_bit(modbus, PC1, 0);
232
233 if (sr_modbus_close(modbus) < 0)
234 return SR_ERR;
235 sdi->status = SR_ST_INACTIVE;
236 }
e1ccfb19
AJ
237
238 return SR_OK;
239}
240
e1ccfb19
AJ
241static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
242 const struct sr_channel_group *cg)
243{
ffb580cf
AJ
244 struct dev_context *devc;
245 struct sr_modbus_dev_inst *modbus;
246 enum maynuo_m97_mode mode;
247 int ret, ivalue;
248 float fvalue;
e1ccfb19 249
e1ccfb19
AJ
250 (void)cg;
251
ffb580cf
AJ
252 modbus = sdi->conn;
253 devc = sdi->priv;
254
e1ccfb19
AJ
255 ret = SR_OK;
256 switch (key) {
ffb580cf 257 case SR_CONF_LIMIT_SAMPLES:
ffb580cf 258 case SR_CONF_LIMIT_MSEC:
45b75c36 259 ret = sr_sw_limits_config_get(&devc->limits, key, data);
ffb580cf
AJ
260 break;
261 case SR_CONF_ENABLED:
262 if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK)
263 *data = g_variant_new_boolean(ivalue);
264 break;
265 case SR_CONF_REGULATION:
266 if ((ret = maynuo_m97_get_bit(modbus, UNREG, &ivalue)) != SR_OK)
267 break;
268 if (ivalue)
269 *data = g_variant_new_string("UR");
270 else if ((ret = maynuo_m97_get_mode(modbus, &mode)) == SR_OK)
271 *data = g_variant_new_string(maynuo_m97_mode_to_str(mode));
272 break;
273 case SR_CONF_VOLTAGE:
274 if ((ret = maynuo_m97_get_float(modbus, U, &fvalue)) == SR_OK)
275 *data = g_variant_new_double(fvalue);
276 break;
277 case SR_CONF_VOLTAGE_TARGET:
278 if ((ret = maynuo_m97_get_float(modbus, UFIX, &fvalue)) == SR_OK)
279 *data = g_variant_new_double(fvalue);
280 break;
281 case SR_CONF_CURRENT:
282 if ((ret = maynuo_m97_get_float(modbus, I, &fvalue)) == SR_OK)
283 *data = g_variant_new_double(fvalue);
284 break;
285 case SR_CONF_CURRENT_LIMIT:
286 if ((ret = maynuo_m97_get_float(modbus, IFIX, &fvalue)) == SR_OK)
287 *data = g_variant_new_double(fvalue);
288 break;
289 case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED:
290 *data = g_variant_new_boolean(TRUE);
291 break;
292 case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE:
293 if ((ret = maynuo_m97_get_bit(modbus, UOVER, &ivalue)) == SR_OK)
294 *data = g_variant_new_boolean(ivalue);
295 break;
296 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
297 if ((ret = maynuo_m97_get_float(modbus, UMAX, &fvalue)) == SR_OK)
298 *data = g_variant_new_double(fvalue);
299 break;
300 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
301 *data = g_variant_new_boolean(TRUE);
302 break;
303 case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE:
304 if ((ret = maynuo_m97_get_bit(modbus, IOVER, &ivalue)) == SR_OK)
305 *data = g_variant_new_boolean(ivalue);
306 break;
307 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
308 if ((ret = maynuo_m97_get_float(modbus, IMAX, &fvalue)) == SR_OK)
309 *data = g_variant_new_double(fvalue);
310 break;
311 case SR_CONF_OVER_TEMPERATURE_PROTECTION:
312 *data = g_variant_new_boolean(TRUE);
313 break;
314 case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE:
315 if ((ret = maynuo_m97_get_bit(modbus, HEAT, &ivalue)) == SR_OK)
316 *data = g_variant_new_boolean(ivalue);
317 break;
e1ccfb19
AJ
318 default:
319 return SR_ERR_NA;
320 }
321
322 return ret;
323}
324
325static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
326 const struct sr_channel_group *cg)
327{
ffb580cf
AJ
328 struct dev_context *devc;
329 struct sr_modbus_dev_inst *modbus;
e1ccfb19
AJ
330 int ret;
331
e1ccfb19
AJ
332 (void)cg;
333
334 if (sdi->status != SR_ST_ACTIVE)
335 return SR_ERR_DEV_CLOSED;
336
ffb580cf
AJ
337 modbus = sdi->conn;
338 devc = sdi->priv;
339
e1ccfb19
AJ
340 ret = SR_OK;
341 switch (key) {
ffb580cf 342 case SR_CONF_LIMIT_SAMPLES:
ffb580cf 343 case SR_CONF_LIMIT_MSEC:
45b75c36 344 ret = sr_sw_limits_config_set(&devc->limits, key, data);
ffb580cf
AJ
345 break;
346 case SR_CONF_ENABLED:
347 ret = maynuo_m97_set_input(modbus, g_variant_get_boolean(data));
348 break;
349 case SR_CONF_VOLTAGE_TARGET:
350 ret = maynuo_m97_set_float(modbus, UFIX, g_variant_get_double(data));
351 break;
352 case SR_CONF_CURRENT_LIMIT:
353 ret = maynuo_m97_set_float(modbus, IFIX, g_variant_get_double(data));
354 break;
355 case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD:
356 ret = maynuo_m97_set_float(modbus, UMAX, g_variant_get_double(data));
357 break;
358 case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD:
359 ret = maynuo_m97_set_float(modbus, IMAX, g_variant_get_double(data));
360 break;
e1ccfb19
AJ
361 default:
362 ret = SR_ERR_NA;
363 }
364
365 return ret;
366}
367
368static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
369 const struct sr_channel_group *cg)
370{
ffb580cf
AJ
371 struct dev_context *devc;
372 GVariantBuilder gvb;
e1ccfb19
AJ
373 int ret;
374
ffb580cf
AJ
375 /* Always available, even without sdi. */
376 if (key == SR_CONF_SCAN_OPTIONS) {
377 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
378 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
379 return SR_OK;
380 } else if (key == SR_CONF_DEVICE_OPTIONS && !sdi) {
381 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
382 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
383 return SR_OK;
384 }
385
386 if (!sdi)
387 return SR_ERR_ARG;
388 devc = sdi->priv;
e1ccfb19
AJ
389
390 ret = SR_OK;
ffb580cf
AJ
391 if (!cg) {
392 /* No channel group: global options. */
393 switch (key) {
394 case SR_CONF_DEVICE_OPTIONS:
395 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
396 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
397 break;
398 default:
399 return SR_ERR_NA;
400 }
401 } else {
402 switch (key) {
403 case SR_CONF_DEVICE_OPTIONS:
404 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
405 devopts_cg, ARRAY_SIZE(devopts_cg), sizeof(uint32_t));
406 break;
407 case SR_CONF_VOLTAGE_TARGET:
408 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
409 /* Min, max, write resolution. */
410 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
411 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_voltage));
412 g_variant_builder_add_value(&gvb, g_variant_new_double(0.001));
413 *data = g_variant_builder_end(&gvb);
414 break;
415 case SR_CONF_CURRENT_LIMIT:
416 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
417 /* Min, max, step. */
418 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0));
419 g_variant_builder_add_value(&gvb, g_variant_new_double(devc->model->max_current));
420 g_variant_builder_add_value(&gvb, g_variant_new_double(0.0001));
421 *data = g_variant_builder_end(&gvb);
422 break;
423 default:
424 return SR_ERR_NA;
425 }
e1ccfb19
AJ
426 }
427
428 return ret;
429}
430
695dc859 431static int dev_acquisition_start(const struct sr_dev_inst *sdi)
e1ccfb19 432{
ffb580cf
AJ
433 struct dev_context *devc;
434 struct sr_modbus_dev_inst *modbus;
435 int ret;
436
e1ccfb19
AJ
437 if (sdi->status != SR_ST_ACTIVE)
438 return SR_ERR_DEV_CLOSED;
439
ffb580cf
AJ
440 modbus = sdi->conn;
441 devc = sdi->priv;
e1ccfb19 442
ffb580cf
AJ
443 if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10,
444 maynuo_m97_receive_data, (void *)sdi)) != SR_OK)
445 return ret;
446
45b75c36 447 sr_sw_limits_acquisition_start(&devc->limits);
ffb580cf
AJ
448 std_session_send_df_header(sdi, LOG_PREFIX);
449
ffb580cf 450 return maynuo_m97_capture_start(sdi);
e1ccfb19
AJ
451}
452
695dc859 453static int dev_acquisition_stop(struct sr_dev_inst *sdi)
e1ccfb19 454{
ffb580cf 455 struct sr_modbus_dev_inst *modbus;
ffb580cf 456
e1ccfb19
AJ
457 if (sdi->status != SR_ST_ACTIVE)
458 return SR_ERR_DEV_CLOSED;
459
3be42bc2 460 std_session_send_df_end(sdi, LOG_PREFIX);
ffb580cf
AJ
461
462 modbus = sdi->conn;
463 sr_modbus_source_remove(sdi->session, modbus);
e1ccfb19
AJ
464
465 return SR_OK;
466}
467
dd5c48a6 468static struct sr_dev_driver maynuo_m97_driver_info = {
e1ccfb19 469 .name = "maynuo-m97",
ffb580cf 470 .longname = "maynuo M97/M98 series",
e1ccfb19 471 .api_version = 1,
c2fdcc25 472 .init = std_init,
700d6b64 473 .cleanup = std_cleanup,
e1ccfb19 474 .scan = scan,
c01bf34c 475 .dev_list = std_dev_list,
e1ccfb19
AJ
476 .config_get = config_get,
477 .config_set = config_set,
478 .config_list = config_list,
479 .dev_open = dev_open,
480 .dev_close = dev_close,
481 .dev_acquisition_start = dev_acquisition_start,
482 .dev_acquisition_stop = dev_acquisition_stop,
41812aca 483 .context = NULL,
e1ccfb19 484};
dd5c48a6 485SR_REGISTER_DEV_DRIVER(maynuo_m97_driver_info);