2 * This file is part of the libsigrok project.
4 * Copyright (C) 2023 Gerhard Sittig <gerhard.sittig@gmx.net>
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/>.
26 #define VENDOR_TEXT "Devantech"
28 static const uint32_t scanopts[] = {
32 static const uint32_t drvopts[] = {
36 static const uint32_t devopts[] = {
37 SR_CONF_CONN | SR_CONF_GET,
38 SR_CONF_ENABLED | SR_CONF_SET, /* Enable/disable all relays at once. */
41 static const uint32_t devopts_cg_do[] = {
42 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
45 static const uint32_t devopts_cg_di[] = {
46 SR_CONF_ENABLED | SR_CONF_GET,
49 static const uint32_t devopts_cg_ai[] = {
50 SR_CONF_VOLTAGE | SR_CONF_GET,
53 static const struct devantech_eth008_model models[] = {
54 { 18, "ETH002", 2, 0, 0, 0, 1, 0, },
55 { 19, "ETH008", 8, 0, 0, 0, 1, 0, },
56 { 20, "ETH484", 16, 8, 4, 0, 2, 0x00f0, },
57 { 21, "ETH8020", 20, 8, 8, 0, 3, 0, },
60 static const struct devantech_eth008_model *find_model(uint8_t code)
63 const struct devantech_eth008_model *check;
65 for (idx = 0; idx < ARRAY_SIZE(models); idx++) {
67 if (check->code != code)
75 static struct sr_dev_driver devantech_eth008_driver_info;
77 static struct sr_dev_inst *probe_device_conn(const char *conn)
79 struct sr_dev_inst *sdi;
80 struct dev_context *devc;
81 struct sr_serial_dev_inst *ser;
82 uint8_t code, hwver, fwver;
83 const struct devantech_eth008_model *model;
84 gboolean has_serno_cmd;
86 struct channel_group_context *cgc;
87 size_t ch_idx, nr, do_idx, di_idx, ai_idx;
88 struct sr_channel_group *cg;
92 sdi = g_malloc0(sizeof(*sdi));
93 devc = g_malloc0(sizeof(*devc));
95 ser = sr_serial_dev_inst_new(conn, NULL);
99 ret = serial_open(ser, 0);
103 ret = devantech_eth008_get_model(ser, &code, &hwver, &fwver);
106 model = find_model(code);
108 sr_err("Unknown model ID 0x%02x (HW %u, FW %u).",
112 devc->model_code = code;
113 devc->hardware_version = hwver;
114 devc->firmware_version = fwver;
116 sdi->vendor = g_strdup(VENDOR_TEXT);
117 sdi->model = g_strdup(model->name);
118 sdi->version = g_strdup_printf("HW%u FW%u", hwver, fwver);
119 sdi->connection_id = g_strdup(conn);
120 sdi->driver = &devantech_eth008_driver_info;
121 sdi->inst_type = SR_INST_SERIAL;
123 has_serno_cmd = TRUE;
124 if (model->min_serno_fw && fwver < model->min_serno_fw)
125 has_serno_cmd = FALSE;
128 ret = devantech_eth008_get_serno(ser,
129 snr_txt, sizeof(snr_txt));
132 sdi->serial_num = g_strdup(snr_txt);
136 devc->mask_do = (1UL << devc->model->ch_count_do) - 1;
137 devc->mask_do &= ~devc->model->mask_do_missing;
138 for (do_idx = 0; do_idx < devc->model->ch_count_do; do_idx++) {
140 if (devc->model->mask_do_missing & (1UL << do_idx))
142 snprintf(cg_name, sizeof(cg_name), "DO%zu", nr);
143 cgc = g_malloc0(sizeof(*cgc));
144 cg = sr_channel_group_new(sdi, cg_name, cgc);
147 cgc->ch_type = DV_CH_DIGITAL_OUTPUT;
151 for (di_idx = 0; di_idx < devc->model->ch_count_di; di_idx++) {
153 snprintf(cg_name, sizeof(cg_name), "DI%zu", nr);
154 cgc = g_malloc0(sizeof(*cgc));
155 cg = sr_channel_group_new(sdi, cg_name, cgc);
158 cgc->ch_type = DV_CH_DIGITAL_INPUT;
162 for (ai_idx = 0; ai_idx < devc->model->ch_count_ai; ai_idx++) {
164 snprintf(cg_name, sizeof(cg_name), "AI%zu", nr);
165 cgc = g_malloc0(sizeof(*cgc));
166 cg = sr_channel_group_new(sdi, cg_name, cgc);
169 cgc->ch_type = DV_CH_ANALOG_INPUT;
174 /* Create an analog channel for the supply voltage. */
175 snprintf(cg_name, sizeof(cg_name), "Vsupply");
176 cgc = g_malloc0(sizeof(*cgc));
177 cg = sr_channel_group_new(sdi, cg_name, cgc);
180 cgc->ch_type = DV_CH_SUPPLY_VOLTAGE;
190 sr_serial_dev_inst_free(ser);
197 sr_dev_inst_free(sdi);
203 static GSList *scan(struct sr_dev_driver *di, GSList *options)
205 struct drv_context *drvc;
208 struct sr_dev_inst *sdi;
211 drvc->instances = NULL;
213 /* A conn= spec is required for the TCP attached device. */
215 (void)sr_serial_extract_options(options, &conn, NULL);
220 sdi = probe_device_conn(conn);
222 devices = g_slist_append(devices, sdi);
224 return std_scan_complete(di, devices);
227 static int config_get(uint32_t key, GVariant **data,
228 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
230 struct channel_group_context *cgc;
239 if (!sdi->connection_id)
241 *data = g_variant_new_string(sdi->connection_id);
252 case SR_CONF_ENABLED:
253 if (cgc->ch_type == DV_CH_DIGITAL_OUTPUT) {
254 ret = devantech_eth008_query_do(sdi, cg, &on);
257 *data = g_variant_new_boolean(on);
260 if (cgc->ch_type == DV_CH_DIGITAL_INPUT) {
261 ret = devantech_eth008_query_di(sdi, cg, &on);
264 *data = g_variant_new_boolean(on);
268 case SR_CONF_VOLTAGE:
269 if (cgc->ch_type == DV_CH_ANALOG_INPUT) {
270 ret = devantech_eth008_query_ai(sdi, cg, &vin);
273 *data = g_variant_new_uint32(vin);
276 if (cgc->ch_type == DV_CH_SUPPLY_VOLTAGE) {
277 ret = devantech_eth008_query_supply(sdi, cg, &vin);
282 *data = g_variant_new_double(vsupply);
291 static int config_set(uint32_t key, GVariant *data,
292 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
294 struct channel_group_context *cgc;
299 case SR_CONF_ENABLED:
300 /* Enable/disable all channels at the same time. */
301 on = g_variant_get_boolean(data);
302 return devantech_eth008_setup_do(sdi, cg, on);
312 case SR_CONF_ENABLED:
313 if (cgc->ch_type != DV_CH_DIGITAL_OUTPUT)
315 on = g_variant_get_boolean(data);
316 return devantech_eth008_setup_do(sdi, cg, on);
321 /* XXX Is this actually UNREACH? */
325 static int config_list(uint32_t key, GVariant **data,
326 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
328 struct channel_group_context *cgc;
332 case SR_CONF_SCAN_OPTIONS:
333 case SR_CONF_DEVICE_OPTIONS:
334 return STD_CONFIG_LIST(key, data, sdi, cg,
335 scanopts, drvopts, devopts);
345 case SR_CONF_DEVICE_OPTIONS:
346 if (cgc->ch_type == DV_CH_DIGITAL_OUTPUT) {
347 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_do));
350 if (cgc->ch_type == DV_CH_DIGITAL_INPUT) {
351 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_di));
354 if (cgc->ch_type == DV_CH_ANALOG_INPUT) {
355 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ai));
358 if (cgc->ch_type == DV_CH_SUPPLY_VOLTAGE) {
359 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ai));
368 static struct sr_dev_driver devantech_eth008_driver_info = {
369 .name = "devantech-eth008",
370 .longname = "Devantech ETH008",
373 .cleanup = std_cleanup,
375 .dev_list = std_dev_list,
376 .dev_clear = std_dev_clear,
377 .config_get = config_get,
378 .config_set = config_set,
379 .config_list = config_list,
380 .dev_open = std_serial_dev_open,
381 .dev_close = std_serial_dev_close,
382 .dev_acquisition_start = std_dummy_dev_acquisition_start,
383 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
386 SR_REGISTER_DEV_DRIVER(devantech_eth008_driver_info);