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 /* List of supported devices. Sorted by model ID. */
54 static const struct devantech_eth008_model models[] = {
55 { 18, "ETH002", 2, 0, 0, 0, 1, 0, 0, },
56 { 19, "ETH008", 8, 0, 0, 0, 1, 0, 0, },
57 { 20, "ETH484", 16, 8, 4, 0, 2, 2, 0x00f0, },
58 { 21, "ETH8020", 20, 8, 8, 0, 3, 4, 0, },
59 { 22, "WIFI484", 16, 8, 4, 0, 2, 2, 0x00f0, },
60 { 24, "WIFI8020", 20, 8, 8, 0, 3, 4, 0, },
61 { 26, "WIFI002", 2, 0, 0, 0, 1, 0, 0, },
62 { 28, "WIFI008", 8, 0, 0, 0, 1, 0, 0, },
63 { 52, "ETH1610", 10, 16, 16, 0, 2, 2, 0, },
66 static const struct devantech_eth008_model *find_model(uint8_t code)
69 const struct devantech_eth008_model *check;
71 for (idx = 0; idx < ARRAY_SIZE(models); idx++) {
73 if (check->code != code)
81 static struct sr_dev_driver devantech_eth008_driver_info;
83 static struct sr_dev_inst *probe_device_conn(const char *conn)
85 struct sr_dev_inst *sdi;
86 struct dev_context *devc;
87 struct sr_serial_dev_inst *ser;
88 uint8_t code, hwver, fwver;
89 const struct devantech_eth008_model *model;
90 gboolean has_serno_cmd;
92 struct channel_group_context *cgc;
93 size_t ch_idx, nr, do_idx, di_idx, ai_idx;
94 struct sr_channel_group *cg;
98 sdi = g_malloc0(sizeof(*sdi));
99 devc = g_malloc0(sizeof(*devc));
101 ser = sr_serial_dev_inst_new(conn, NULL);
105 ret = serial_open(ser, 0);
109 ret = devantech_eth008_get_model(ser, &code, &hwver, &fwver);
112 model = find_model(code);
114 sr_err("Unknown model ID 0x%02x (HW %u, FW %u).",
118 devc->model_code = code;
119 devc->hardware_version = hwver;
120 devc->firmware_version = fwver;
122 sdi->vendor = g_strdup(VENDOR_TEXT);
123 sdi->model = g_strdup(model->name);
124 sdi->version = g_strdup_printf("HW%u FW%u", hwver, fwver);
125 sdi->connection_id = g_strdup(conn);
126 sdi->driver = &devantech_eth008_driver_info;
127 sdi->inst_type = SR_INST_SERIAL;
129 has_serno_cmd = TRUE;
130 if (model->min_serno_fw && fwver < model->min_serno_fw)
131 has_serno_cmd = FALSE;
134 ret = devantech_eth008_get_serno(ser,
135 snr_txt, sizeof(snr_txt));
138 sdi->serial_num = g_strdup(snr_txt);
142 devc->mask_do = (1UL << devc->model->ch_count_do) - 1;
143 devc->mask_do &= ~devc->model->mask_do_missing;
144 for (do_idx = 0; do_idx < devc->model->ch_count_do; do_idx++) {
146 if (devc->model->mask_do_missing & (1UL << do_idx))
148 snprintf(cg_name, sizeof(cg_name), "DO%zu", nr);
149 cgc = g_malloc0(sizeof(*cgc));
150 cg = sr_channel_group_new(sdi, cg_name, cgc);
153 cgc->ch_type = DV_CH_DIGITAL_OUTPUT;
157 for (di_idx = 0; di_idx < devc->model->ch_count_di; di_idx++) {
159 snprintf(cg_name, sizeof(cg_name), "DI%zu", nr);
160 cgc = g_malloc0(sizeof(*cgc));
161 cg = sr_channel_group_new(sdi, cg_name, cgc);
164 cgc->ch_type = DV_CH_DIGITAL_INPUT;
168 for (ai_idx = 0; ai_idx < devc->model->ch_count_ai; ai_idx++) {
170 snprintf(cg_name, sizeof(cg_name), "AI%zu", nr);
171 cgc = g_malloc0(sizeof(*cgc));
172 cg = sr_channel_group_new(sdi, cg_name, cgc);
175 cgc->ch_type = DV_CH_ANALOG_INPUT;
180 /* Create an analog channel for the supply voltage. */
181 snprintf(cg_name, sizeof(cg_name), "Vsupply");
182 cgc = g_malloc0(sizeof(*cgc));
183 cg = sr_channel_group_new(sdi, cg_name, cgc);
186 cgc->ch_type = DV_CH_SUPPLY_VOLTAGE;
196 sr_serial_dev_inst_free(ser);
203 sr_dev_inst_free(sdi);
209 static GSList *scan(struct sr_dev_driver *di, GSList *options)
211 struct drv_context *drvc;
214 struct sr_dev_inst *sdi;
217 drvc->instances = NULL;
219 /* A conn= spec is required for the TCP attached device. */
221 (void)sr_serial_extract_options(options, &conn, NULL);
226 sdi = probe_device_conn(conn);
228 devices = g_slist_append(devices, sdi);
230 return std_scan_complete(di, devices);
233 static int config_get(uint32_t key, GVariant **data,
234 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
236 struct channel_group_context *cgc;
245 if (!sdi->connection_id)
247 *data = g_variant_new_string(sdi->connection_id);
258 case SR_CONF_ENABLED:
259 if (cgc->ch_type == DV_CH_DIGITAL_OUTPUT) {
260 ret = devantech_eth008_query_do(sdi, cg, &on);
263 *data = g_variant_new_boolean(on);
266 if (cgc->ch_type == DV_CH_DIGITAL_INPUT) {
267 ret = devantech_eth008_query_di(sdi, cg, &on);
270 *data = g_variant_new_boolean(on);
274 case SR_CONF_VOLTAGE:
275 if (cgc->ch_type == DV_CH_ANALOG_INPUT) {
276 ret = devantech_eth008_query_ai(sdi, cg, &vin);
279 *data = g_variant_new_uint32(vin);
282 if (cgc->ch_type == DV_CH_SUPPLY_VOLTAGE) {
283 ret = devantech_eth008_query_supply(sdi, cg, &vin);
288 *data = g_variant_new_double(vsupply);
297 static int config_set(uint32_t key, GVariant *data,
298 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
300 struct channel_group_context *cgc;
305 case SR_CONF_ENABLED:
306 /* Enable/disable all channels at the same time. */
307 on = g_variant_get_boolean(data);
308 return devantech_eth008_setup_do(sdi, cg, on);
318 case SR_CONF_ENABLED:
319 if (cgc->ch_type != DV_CH_DIGITAL_OUTPUT)
321 on = g_variant_get_boolean(data);
322 return devantech_eth008_setup_do(sdi, cg, on);
328 static int config_list(uint32_t key, GVariant **data,
329 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
331 struct channel_group_context *cgc;
335 case SR_CONF_SCAN_OPTIONS:
336 case SR_CONF_DEVICE_OPTIONS:
337 return STD_CONFIG_LIST(key, data, sdi, cg,
338 scanopts, drvopts, devopts);
348 case SR_CONF_DEVICE_OPTIONS:
349 if (cgc->ch_type == DV_CH_DIGITAL_OUTPUT) {
350 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_do));
353 if (cgc->ch_type == DV_CH_DIGITAL_INPUT) {
354 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_di));
357 if (cgc->ch_type == DV_CH_ANALOG_INPUT) {
358 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ai));
361 if (cgc->ch_type == DV_CH_SUPPLY_VOLTAGE) {
362 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ai));
371 static struct sr_dev_driver devantech_eth008_driver_info = {
372 .name = "devantech-eth008",
373 .longname = "Devantech ETH008",
376 .cleanup = std_cleanup,
378 .dev_list = std_dev_list,
379 .dev_clear = std_dev_clear,
380 .config_get = config_get,
381 .config_set = config_set,
382 .config_list = config_list,
383 .dev_open = std_serial_dev_open,
384 .dev_close = std_serial_dev_close,
385 .dev_acquisition_start = std_dummy_dev_acquisition_start,
386 .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
389 SR_REGISTER_DEV_DRIVER(devantech_eth008_driver_info);