]>
Commit | Line | Data |
---|---|---|
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 <config.h> | |
21 | #include "protocol.h" | |
22 | ||
23 | static const uint32_t scanopts[] = { | |
24 | SR_CONF_CONN, | |
25 | SR_CONF_SERIALCOMM, | |
26 | SR_CONF_MODBUSADDR, | |
27 | }; | |
28 | ||
29 | static const uint32_t drvopts[] = { | |
30 | SR_CONF_ELECTRONIC_LOAD, | |
31 | }; | |
32 | ||
33 | static const uint32_t devopts[] = { | |
34 | SR_CONF_CONTINUOUS, | |
35 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
36 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
37 | }; | |
38 | ||
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, | |
54 | }; | |
55 | ||
56 | /* | |
57 | * The IDs in this list are only guessed and needs to be verified | |
58 | * against some real hardware. If at least a few of them matches, | |
59 | * it will probably be safe to enable the others. | |
60 | */ | |
61 | static const struct maynuo_m97_model supported_models[] = { | |
62 | // { 53, "M9711" , 30, 150, 150 }, | |
63 | // { 54, "M9712" , 30, 150, 300 }, | |
64 | // { 55, "M9712C" , 60, 150, 300 }, | |
65 | // { 56, "M9713" , 120, 150, 600 }, | |
66 | // { 57, "M9712B" , 15, 500, 300 }, | |
67 | // { 58, "M9713B" , 30, 500, 600 }, | |
68 | // { 59, "M9714" , 240, 150, 1200 }, | |
69 | // { 60, "M9714B" , 60, 500, 1200 }, | |
70 | // { 61, "M9715" , 240, 150, 1800 }, | |
71 | // { 62, "M9715B" , 120, 500, 1800 }, | |
72 | // { 63, "M9716" , 240, 150, 2400 }, | |
73 | // { 64, "M9716B" , 120, 500, 2400 }, | |
74 | // { 65, "M9717C" , 480, 150, 3600 }, | |
75 | // { 66, "M9717" , 240, 150, 3600 }, | |
76 | // { 67, "M9717B" , 120, 500, 3600 }, | |
77 | // { 68, "M9718" , 240, 150, 6000 }, | |
78 | // { 69, "M9718B" , 120, 500, 6000 }, | |
79 | // { 70, "M9718D" , 240, 500, 6000 }, | |
80 | // { 71, "M9836" , 500, 150, 20000 }, | |
81 | // { 72, "M9836B" , 240, 500, 20000 }, | |
82 | // { 73, "M9838B" , 240, 500, 50000 }, | |
83 | // { 74, "M9839B" , 240, 500, 100000 }, | |
84 | // { 75, "M9840B" , 500, 500, 200000 }, | |
85 | // { 76, "M9840" , 1500, 150, 200000 }, | |
86 | // { 77, "M9712B30" , 30, 500, 300 }, | |
87 | // { 78, "M9718E" , 120, 600, 6000 }, | |
88 | // { 79, "M9718F" , 480, 150, 6000 }, | |
89 | // { 80, "M9716E" , 480, 150, 3000 }, | |
90 | // { 81, "M9710" , 30, 150, 150 }, | |
91 | // { 82, "M9834" , 500, 150, 10000 }, | |
92 | // { 83, "M9835" , 500, 150, 15000 }, | |
93 | // { 84, "M9835B" , 240, 500, 15000 }, | |
94 | // { 85, "M9837" , 500, 150, 35000 }, | |
95 | // { 86, "M9837B" , 240, 500, 35000 }, | |
96 | // { 87, "M9838" , 500, 150, 50000 }, | |
97 | // { 88, "M9839" , 500, 150, 100000 }, | |
98 | // { 89, "M9835C" , 1000, 150, 15000 }, /* ?? */ | |
99 | // { 90, "M9836C" , 1000, 150, 20000 }, /* ?? */ | |
100 | // { 91, "M9718F-300", 480, 300, 6000 }, /* ?? */ | |
101 | // { 92, "M9836F" , 1000, 150, 20000 }, /* ?? */ | |
102 | // { 93, "M9836E" , 240, 600, 20000 }, /* ?? */ | |
103 | // { 94, "M9717D" , 240, 500, 3600 }, /* ?? */ | |
104 | // { 95, "M9836B-720", 240, 720, 20000 }, /* ?? */ | |
105 | // { 96, "M9834H" , 500, 150, 10000 }, /* ?? */ | |
106 | // { 97, "M9836H" , 500, 150, 20000 }, /* ?? */ | |
107 | // { 98, "M9718F-500", 480, 500, 6000 }, /* ?? */ | |
108 | // { 99, "M9834B" , 240, 500, 10000 }, /* ?? */ | |
109 | // { 100, "M9811" , 30, 150, 200 }, | |
110 | { 101, "M9812" , 30, 150, 300 }, | |
111 | // { 102, "M9812B" , 15, 500, 300 }, | |
112 | }; | |
113 | ||
114 | static struct sr_dev_driver maynuo_m97_driver_info; | |
115 | ||
116 | static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus) | |
117 | { | |
118 | const struct maynuo_m97_model *model = NULL; | |
119 | struct dev_context *devc; | |
120 | struct sr_dev_inst *sdi; | |
121 | struct sr_channel_group *cg; | |
122 | struct sr_channel *ch; | |
123 | uint16_t id, version; | |
124 | unsigned int i; | |
125 | ||
126 | int ret = maynuo_m97_get_model_version(modbus, &id, &version); | |
127 | if (ret != SR_OK) | |
128 | return NULL; | |
129 | for (i = 0; i < ARRAY_SIZE(supported_models); i++) | |
130 | if (id == supported_models[i].id) { | |
131 | model = &supported_models[i]; | |
132 | break; | |
133 | } | |
134 | if (model == NULL) { | |
135 | sr_err("Unknown model: %d.", id); | |
136 | return NULL; | |
137 | } | |
138 | ||
139 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
140 | sdi->status = SR_ST_INACTIVE; | |
141 | sdi->vendor = g_strdup("Maynuo"); | |
142 | sdi->model = g_strdup(model->name); | |
143 | sdi->version = g_strdup_printf("v%d.%d", version / 10, version % 10); | |
144 | sdi->conn = modbus; | |
145 | sdi->driver = &maynuo_m97_driver_info; | |
146 | sdi->inst_type = SR_INST_MODBUS; | |
147 | ||
148 | cg = sr_channel_group_new(sdi, "1", NULL); | |
149 | ||
150 | ch = sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V1"); | |
151 | cg->channels = g_slist_append(cg->channels, ch); | |
152 | ||
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 | ||
164 | static 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 | ||
170 | static 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 | ||
187 | devices = sr_modbus_scan(di->context, opts, probe_device); | |
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); | |
193 | ||
194 | return devices; | |
195 | } | |
196 | ||
197 | static int dev_open(struct sr_dev_inst *sdi) | |
198 | { | |
199 | struct sr_modbus_dev_inst *modbus = sdi->conn; | |
200 | ||
201 | if (sr_modbus_open(modbus) < 0) | |
202 | return SR_ERR; | |
203 | ||
204 | maynuo_m97_set_bit(modbus, PC1, 1); | |
205 | ||
206 | return SR_OK; | |
207 | } | |
208 | ||
209 | static int dev_close(struct sr_dev_inst *sdi) | |
210 | { | |
211 | struct dev_context *devc; | |
212 | struct sr_modbus_dev_inst *modbus; | |
213 | ||
214 | modbus = sdi->conn; | |
215 | ||
216 | if (!modbus) | |
217 | return SR_ERR_BUG; | |
218 | ||
219 | devc = sdi->priv; | |
220 | ||
221 | if (devc->expecting_registers) { | |
222 | /* Wait for the last data that was requested from the device. */ | |
223 | uint16_t registers[devc->expecting_registers]; | |
224 | sr_modbus_read_holding_registers(modbus, -1, | |
225 | devc->expecting_registers, registers); | |
226 | } | |
227 | ||
228 | maynuo_m97_set_bit(modbus, PC1, 0); | |
229 | ||
230 | return sr_modbus_close(modbus); | |
231 | } | |
232 | ||
233 | static int config_get(uint32_t key, GVariant **data, | |
234 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
235 | { | |
236 | struct dev_context *devc; | |
237 | struct sr_modbus_dev_inst *modbus; | |
238 | enum maynuo_m97_mode mode; | |
239 | int ret, ivalue; | |
240 | float fvalue; | |
241 | ||
242 | (void)cg; | |
243 | ||
244 | modbus = sdi->conn; | |
245 | devc = sdi->priv; | |
246 | ||
247 | ret = SR_OK; | |
248 | switch (key) { | |
249 | case SR_CONF_LIMIT_SAMPLES: | |
250 | case SR_CONF_LIMIT_MSEC: | |
251 | ret = sr_sw_limits_config_get(&devc->limits, key, data); | |
252 | break; | |
253 | case SR_CONF_ENABLED: | |
254 | if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK) | |
255 | *data = g_variant_new_boolean(ivalue); | |
256 | break; | |
257 | case SR_CONF_REGULATION: | |
258 | if ((ret = maynuo_m97_get_bit(modbus, UNREG, &ivalue)) != SR_OK) | |
259 | break; | |
260 | if (ivalue) | |
261 | *data = g_variant_new_string("UR"); | |
262 | else if ((ret = maynuo_m97_get_mode(modbus, &mode)) == SR_OK) | |
263 | *data = g_variant_new_string(maynuo_m97_mode_to_str(mode)); | |
264 | break; | |
265 | case SR_CONF_VOLTAGE: | |
266 | if ((ret = maynuo_m97_get_float(modbus, U, &fvalue)) == SR_OK) | |
267 | *data = g_variant_new_double(fvalue); | |
268 | break; | |
269 | case SR_CONF_VOLTAGE_TARGET: | |
270 | if ((ret = maynuo_m97_get_float(modbus, UFIX, &fvalue)) == SR_OK) | |
271 | *data = g_variant_new_double(fvalue); | |
272 | break; | |
273 | case SR_CONF_CURRENT: | |
274 | if ((ret = maynuo_m97_get_float(modbus, I, &fvalue)) == SR_OK) | |
275 | *data = g_variant_new_double(fvalue); | |
276 | break; | |
277 | case SR_CONF_CURRENT_LIMIT: | |
278 | if ((ret = maynuo_m97_get_float(modbus, IFIX, &fvalue)) == SR_OK) | |
279 | *data = g_variant_new_double(fvalue); | |
280 | break; | |
281 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED: | |
282 | *data = g_variant_new_boolean(TRUE); | |
283 | break; | |
284 | case SR_CONF_OVER_VOLTAGE_PROTECTION_ACTIVE: | |
285 | if ((ret = maynuo_m97_get_bit(modbus, UOVER, &ivalue)) == SR_OK) | |
286 | *data = g_variant_new_boolean(ivalue); | |
287 | break; | |
288 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
289 | if ((ret = maynuo_m97_get_float(modbus, UMAX, &fvalue)) == SR_OK) | |
290 | *data = g_variant_new_double(fvalue); | |
291 | break; | |
292 | case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED: | |
293 | *data = g_variant_new_boolean(TRUE); | |
294 | break; | |
295 | case SR_CONF_OVER_CURRENT_PROTECTION_ACTIVE: | |
296 | if ((ret = maynuo_m97_get_bit(modbus, IOVER, &ivalue)) == SR_OK) | |
297 | *data = g_variant_new_boolean(ivalue); | |
298 | break; | |
299 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
300 | if ((ret = maynuo_m97_get_float(modbus, IMAX, &fvalue)) == SR_OK) | |
301 | *data = g_variant_new_double(fvalue); | |
302 | break; | |
303 | case SR_CONF_OVER_TEMPERATURE_PROTECTION: | |
304 | *data = g_variant_new_boolean(TRUE); | |
305 | break; | |
306 | case SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE: | |
307 | if ((ret = maynuo_m97_get_bit(modbus, HEAT, &ivalue)) == SR_OK) | |
308 | *data = g_variant_new_boolean(ivalue); | |
309 | break; | |
310 | default: | |
311 | return SR_ERR_NA; | |
312 | } | |
313 | ||
314 | return ret; | |
315 | } | |
316 | ||
317 | static int config_set(uint32_t key, GVariant *data, | |
318 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
319 | { | |
320 | struct dev_context *devc; | |
321 | struct sr_modbus_dev_inst *modbus; | |
322 | ||
323 | (void)cg; | |
324 | ||
325 | modbus = sdi->conn; | |
326 | devc = sdi->priv; | |
327 | ||
328 | switch (key) { | |
329 | case SR_CONF_LIMIT_SAMPLES: | |
330 | case SR_CONF_LIMIT_MSEC: | |
331 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
332 | case SR_CONF_ENABLED: | |
333 | return maynuo_m97_set_input(modbus, g_variant_get_boolean(data)); | |
334 | case SR_CONF_VOLTAGE_TARGET: | |
335 | return maynuo_m97_set_float(modbus, UFIX, g_variant_get_double(data)); | |
336 | case SR_CONF_CURRENT_LIMIT: | |
337 | return maynuo_m97_set_float(modbus, IFIX, g_variant_get_double(data)); | |
338 | case SR_CONF_OVER_VOLTAGE_PROTECTION_THRESHOLD: | |
339 | return maynuo_m97_set_float(modbus, UMAX, g_variant_get_double(data)); | |
340 | case SR_CONF_OVER_CURRENT_PROTECTION_THRESHOLD: | |
341 | return maynuo_m97_set_float(modbus, IMAX, g_variant_get_double(data)); | |
342 | default: | |
343 | return SR_ERR_NA; | |
344 | } | |
345 | ||
346 | return SR_OK; | |
347 | } | |
348 | ||
349 | static int config_list(uint32_t key, GVariant **data, | |
350 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
351 | { | |
352 | struct dev_context *devc; | |
353 | ||
354 | devc = (sdi) ? sdi->priv : NULL; | |
355 | ||
356 | if (!cg) { | |
357 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
358 | } else { | |
359 | switch (key) { | |
360 | case SR_CONF_DEVICE_OPTIONS: | |
361 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg)); | |
362 | break; | |
363 | case SR_CONF_VOLTAGE_TARGET: | |
364 | if (!devc || !devc->model) | |
365 | return SR_ERR_ARG; | |
366 | *data = std_gvar_min_max_step(0.0, devc->model->max_voltage, 0.001); | |
367 | break; | |
368 | case SR_CONF_CURRENT_LIMIT: | |
369 | if (!devc || !devc->model) | |
370 | return SR_ERR_ARG; | |
371 | *data = std_gvar_min_max_step(0.0, devc->model->max_current, 0.0001); | |
372 | break; | |
373 | default: | |
374 | return SR_ERR_NA; | |
375 | } | |
376 | } | |
377 | ||
378 | return SR_OK; | |
379 | } | |
380 | ||
381 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
382 | { | |
383 | struct dev_context *devc; | |
384 | struct sr_modbus_dev_inst *modbus; | |
385 | int ret; | |
386 | ||
387 | modbus = sdi->conn; | |
388 | devc = sdi->priv; | |
389 | ||
390 | if ((ret = sr_modbus_source_add(sdi->session, modbus, G_IO_IN, 10, | |
391 | maynuo_m97_receive_data, (void *)sdi)) != SR_OK) | |
392 | return ret; | |
393 | ||
394 | sr_sw_limits_acquisition_start(&devc->limits); | |
395 | std_session_send_df_header(sdi); | |
396 | ||
397 | return maynuo_m97_capture_start(sdi); | |
398 | } | |
399 | ||
400 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
401 | { | |
402 | struct sr_modbus_dev_inst *modbus; | |
403 | ||
404 | std_session_send_df_end(sdi); | |
405 | ||
406 | modbus = sdi->conn; | |
407 | sr_modbus_source_remove(sdi->session, modbus); | |
408 | ||
409 | return SR_OK; | |
410 | } | |
411 | ||
412 | static struct sr_dev_driver maynuo_m97_driver_info = { | |
413 | .name = "maynuo-m97", | |
414 | .longname = "maynuo M97/M98 series", | |
415 | .api_version = 1, | |
416 | .init = std_init, | |
417 | .cleanup = std_cleanup, | |
418 | .scan = scan, | |
419 | .dev_list = std_dev_list, | |
420 | .dev_clear = std_dev_clear, | |
421 | .config_get = config_get, | |
422 | .config_set = config_set, | |
423 | .config_list = config_list, | |
424 | .dev_open = dev_open, | |
425 | .dev_close = dev_close, | |
426 | .dev_acquisition_start = dev_acquisition_start, | |
427 | .dev_acquisition_stop = dev_acquisition_stop, | |
428 | .context = NULL, | |
429 | }; | |
430 | SR_REGISTER_DEV_DRIVER(maynuo_m97_driver_info); |