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_ai[] = {
46 SR_CONF_VOLTAGE | SR_CONF_GET,
49 static const struct devantech_eth008_model models[] = {
50 { 19, "ETH008", 8, 0, 1, },
53 static const struct devantech_eth008_model *find_model(uint8_t code)
56 const struct devantech_eth008_model *check;
58 for (idx = 0; idx < ARRAY_SIZE(models); idx++) {
60 if (check->code != code)
68 static struct sr_dev_driver devantech_eth008_driver_info;
70 static struct sr_dev_inst *probe_device_conn(const char *conn)
72 struct sr_dev_inst *sdi;
73 struct dev_context *devc;
74 struct sr_serial_dev_inst *ser;
75 uint8_t code, hwver, fwver;
76 const struct devantech_eth008_model *model;
77 gboolean has_serno_cmd;
79 struct channel_group_context *cgc;
80 size_t ch_idx, nr, do_idx;
81 struct sr_channel_group *cg;
85 sdi = g_malloc0(sizeof(*sdi));
86 devc = g_malloc0(sizeof(*devc));
88 ser = sr_serial_dev_inst_new(conn, NULL);
92 ret = serial_open(ser, 0);
96 ret = devantech_eth008_get_model(ser, &code, &hwver, &fwver);
99 model = find_model(code);
101 sr_err("Unknown model ID 0x%02x (HW %u, FW %u).",
105 devc->model_code = code;
106 devc->hardware_version = hwver;
107 devc->firmware_version = fwver;
109 sdi->vendor = g_strdup(VENDOR_TEXT);
110 sdi->model = g_strdup(model->name);
111 sdi->version = g_strdup_printf("HW%u FW%u", hwver, fwver);
112 sdi->connection_id = g_strdup(conn);
113 sdi->driver = &devantech_eth008_driver_info;
114 sdi->inst_type = SR_INST_SERIAL;
116 has_serno_cmd = TRUE;
117 if (model->min_serno_fw && fwver < model->min_serno_fw)
118 has_serno_cmd = FALSE;
121 ret = devantech_eth008_get_serno(ser,
122 snr_txt, sizeof(snr_txt));
125 sdi->serial_num = g_strdup(snr_txt);
129 devc->mask_do = (1UL << devc->model->ch_count_do) - 1;
130 for (do_idx = 0; do_idx < devc->model->ch_count_do; do_idx++) {
132 snprintf(cg_name, sizeof(cg_name), "DO%zu", nr);
133 cgc = g_malloc0(sizeof(*cgc));
134 cg = sr_channel_group_new(sdi, cg_name, cgc);
137 cgc->ch_type = DV_CH_DIGITAL_OUTPUT;
142 /* Create an analog channel for the supply voltage. */
143 snprintf(cg_name, sizeof(cg_name), "Vsupply");
144 cgc = g_malloc0(sizeof(*cgc));
145 cg = sr_channel_group_new(sdi, cg_name, cgc);
148 cgc->ch_type = DV_CH_SUPPLY_VOLTAGE;
158 sr_serial_dev_inst_free(ser);
165 sr_dev_inst_free(sdi);
171 static GSList *scan(struct sr_dev_driver *di, GSList *options)
173 struct drv_context *drvc;
176 struct sr_dev_inst *sdi;
179 drvc->instances = NULL;
181 /* A conn= spec is required for the TCP attached device. */
183 (void)sr_serial_extract_options(options, &conn, NULL);
188 sdi = probe_device_conn(conn);
190 devices = g_slist_append(devices, sdi);
192 return std_scan_complete(di, devices);
195 static int config_get(uint32_t key, GVariant **data,
196 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
198 struct channel_group_context *cgc;
207 if (!sdi->connection_id)
209 *data = g_variant_new_string(sdi->connection_id);
220 case SR_CONF_ENABLED:
221 if (cgc->ch_type == DV_CH_DIGITAL_OUTPUT) {
222 ret = devantech_eth008_query_do(sdi, cg, &on);
225 *data = g_variant_new_boolean(on);
229 case SR_CONF_VOLTAGE:
230 if (cgc->ch_type == DV_CH_SUPPLY_VOLTAGE) {
231 ret = devantech_eth008_query_supply(sdi, cg, &vin);
236 *data = g_variant_new_double(vsupply);
245 static int config_set(uint32_t key, GVariant *data,
246 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
248 struct channel_group_context *cgc;
253 case SR_CONF_ENABLED:
254 /* Enable/disable all channels at the same time. */
255 on = g_variant_get_boolean(data);
256 return devantech_eth008_setup_do(sdi, cg, on);
266 case SR_CONF_ENABLED:
267 if (cgc->ch_type != DV_CH_DIGITAL_OUTPUT)
269 on = g_variant_get_boolean(data);
270 return devantech_eth008_setup_do(sdi, cg, on);
275 /* XXX Is this actually UNREACH? */
279 static int config_list(uint32_t key, GVariant **data,
280 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
282 struct channel_group_context *cgc;
286 case SR_CONF_SCAN_OPTIONS:
287 case SR_CONF_DEVICE_OPTIONS:
288 return STD_CONFIG_LIST(key, data, sdi, cg,
289 scanopts, drvopts, devopts);
299 case SR_CONF_DEVICE_OPTIONS:
300 if (cgc->ch_type == DV_CH_DIGITAL_OUTPUT) {
301 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_do));
304 if (cgc->ch_type == DV_CH_SUPPLY_VOLTAGE) {
305 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ai));
314 static struct sr_dev_driver devantech_eth008_driver_info = {
315 .name = "devantech-eth008",
316 .longname = "Devantech ETH008",
319 .cleanup = std_cleanup,
321 .dev_list = std_dev_list,
322 .dev_clear = std_dev_clear,
323 .config_get = config_get,
324 .config_set = config_set,
325 .config_list = config_list,
326 .dev_open = std_serial_dev_open,
327 .dev_close = std_serial_dev_close,
328 .dev_acquisition_start = std_dummy_dev_acquisition_start,
329 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
332 SR_REGISTER_DEV_DRIVER(devantech_eth008_driver_info);