]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2018 Sven Bursch-Osewold <sb_git@bursch.com> | |
5 | * Copyright (C) 2019 King Kévin <kingkevin@cuvoodoo.info> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | static const uint32_t scanopts[] = { | |
25 | SR_CONF_CONN, | |
26 | SR_CONF_SERIALCOMM, | |
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_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
36 | SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
37 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
38 | }; | |
39 | ||
40 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
41 | { | |
42 | struct dev_context *devc; | |
43 | GSList *l; | |
44 | struct sr_dev_inst *sdi; | |
45 | const char *conn, *serialcomm; | |
46 | struct sr_config *src; | |
47 | struct sr_serial_dev_inst *serial; | |
48 | uint8_t reply[MSG_MAX_LEN]; | |
49 | ||
50 | conn = NULL; | |
51 | serialcomm = NULL; | |
52 | ||
53 | for (l = options; l; l = l->next) { | |
54 | src = l->data; | |
55 | switch (src->key) { | |
56 | case SR_CONF_CONN: | |
57 | conn = g_variant_get_string(src->data, NULL); | |
58 | break; | |
59 | case SR_CONF_SERIALCOMM: | |
60 | serialcomm = g_variant_get_string(src->data, NULL); | |
61 | break; | |
62 | } | |
63 | } | |
64 | ||
65 | if (!conn) | |
66 | return NULL; | |
67 | if (!serialcomm) | |
68 | serialcomm = "9600/8e1"; | |
69 | ||
70 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
71 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
72 | return NULL; | |
73 | ||
74 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
75 | sdi->status = SR_ST_INACTIVE; | |
76 | sdi->vendor = g_strdup("ZKETECH"); | |
77 | sdi->model = g_strdup("EBD-USB"); | |
78 | sdi->inst_type = SR_INST_SERIAL; | |
79 | sdi->conn = serial; | |
80 | ||
81 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "V.VBUS"); | |
82 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "I.VBUS"); | |
83 | sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "V.D+"); | |
84 | sr_channel_new(sdi, 3, SR_CHANNEL_ANALOG, TRUE, "V.D-"); | |
85 | ||
86 | devc = g_malloc0(sizeof(struct dev_context)); | |
87 | g_mutex_init(&devc->rw_mutex); | |
88 | devc->current_limit = 0; | |
89 | devc->uvc_threshold = 0; | |
90 | devc->running = FALSE; | |
91 | devc->load_activated = FALSE; | |
92 | sr_sw_limits_init(&devc->limits); | |
93 | sdi->priv = devc; | |
94 | ||
95 | /* Starting device. */ | |
96 | ebd_init(serial, devc); | |
97 | int ret = ebd_read_message(serial, MSG_MAX_LEN, reply); | |
98 | if (ret < 0) { | |
99 | sr_warn("Could not receive message!"); | |
100 | ret = SR_ERR; | |
101 | } else if (ret == 0) { | |
102 | sr_warn("No message received!"); | |
103 | ret = SR_ERR; | |
104 | } | |
105 | ebd_stop(serial, devc); | |
106 | ||
107 | serial_close(serial); | |
108 | ||
109 | if (ret < 0) | |
110 | return NULL; | |
111 | ||
112 | return std_scan_complete(di, g_slist_append(NULL, sdi)); | |
113 | } | |
114 | ||
115 | static int dev_close(struct sr_dev_inst *sdi) | |
116 | { | |
117 | struct dev_context *devc; | |
118 | ||
119 | devc = (sdi) ? sdi->priv : NULL; | |
120 | if (devc) | |
121 | g_mutex_clear(&devc->rw_mutex); | |
122 | ||
123 | return std_serial_dev_close(sdi); | |
124 | } | |
125 | ||
126 | static int config_get(uint32_t key, GVariant **data, | |
127 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
128 | { | |
129 | int ret; | |
130 | struct dev_context *devc; | |
131 | float fvalue; | |
132 | ||
133 | (void)cg; | |
134 | ||
135 | if (!sdi || !data) | |
136 | return SR_ERR_ARG; | |
137 | ||
138 | devc = sdi->priv; | |
139 | ||
140 | switch (key) { | |
141 | case SR_CONF_LIMIT_SAMPLES: | |
142 | case SR_CONF_LIMIT_MSEC: | |
143 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
144 | case SR_CONF_CURRENT_LIMIT: | |
145 | ret = ebd_get_current_limit(sdi, &fvalue); | |
146 | if (ret == SR_OK) | |
147 | *data = g_variant_new_double(fvalue); | |
148 | return ret; | |
149 | case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: | |
150 | ret = ebd_get_uvc_threshold(sdi, &fvalue); | |
151 | if (ret == SR_OK) | |
152 | *data = g_variant_new_double(fvalue); | |
153 | return ret; | |
154 | default: | |
155 | return SR_ERR_NA; | |
156 | } | |
157 | } | |
158 | ||
159 | static int config_set(uint32_t key, GVariant *data, | |
160 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
161 | { | |
162 | double value; | |
163 | struct dev_context *devc; | |
164 | ||
165 | (void)data; | |
166 | (void)cg; | |
167 | ||
168 | devc = sdi->priv; | |
169 | ||
170 | switch (key) { | |
171 | case SR_CONF_LIMIT_MSEC: | |
172 | case SR_CONF_LIMIT_SAMPLES: | |
173 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
174 | case SR_CONF_CURRENT_LIMIT: | |
175 | value = g_variant_get_double(data); | |
176 | if (value < 0.0 || value > 4.0) | |
177 | return SR_ERR_ARG; | |
178 | return ebd_set_current_limit(sdi, value); | |
179 | case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: | |
180 | value = g_variant_get_double(data); | |
181 | if (value < 0.0 || value > 21.0) | |
182 | return SR_ERR_ARG; | |
183 | return ebd_set_uvc_threshold(sdi, value); | |
184 | default: | |
185 | return SR_ERR_NA; | |
186 | } | |
187 | } | |
188 | ||
189 | static int config_list(uint32_t key, GVariant **data, | |
190 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
191 | { | |
192 | switch (key) { | |
193 | case SR_CONF_SCAN_OPTIONS: | |
194 | case SR_CONF_DEVICE_OPTIONS: | |
195 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, | |
196 | devopts); | |
197 | case SR_CONF_CURRENT_LIMIT: | |
198 | *data = std_gvar_min_max_step(0.0, 4.0, 0.001); | |
199 | break; | |
200 | case SR_CONF_UNDER_VOLTAGE_CONDITION_THRESHOLD: | |
201 | *data = std_gvar_min_max_step(0.0, 21.0, 0.01); | |
202 | break; | |
203 | default: | |
204 | return SR_ERR_NA; | |
205 | } | |
206 | ||
207 | return SR_OK; | |
208 | } | |
209 | ||
210 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
211 | { | |
212 | struct dev_context *devc; | |
213 | struct sr_serial_dev_inst *serial; | |
214 | ||
215 | devc = sdi->priv; | |
216 | serial = sdi->conn; | |
217 | ||
218 | sr_sw_limits_acquisition_start(&devc->limits); | |
219 | std_session_send_df_header(sdi); | |
220 | ||
221 | ebd_init(serial, devc); | |
222 | ||
223 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
224 | ebd_receive_data, (void *)sdi); | |
225 | ||
226 | return SR_OK; | |
227 | } | |
228 | ||
229 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
230 | { | |
231 | struct dev_context *devc; | |
232 | ||
233 | if (sdi) { | |
234 | devc = sdi->priv; | |
235 | if (devc->load_activated) | |
236 | ebd_loadtoggle(sdi->conn, devc); | |
237 | ebd_stop(sdi->conn, devc); | |
238 | } | |
239 | ||
240 | return std_serial_dev_acquisition_stop(sdi); | |
241 | } | |
242 | ||
243 | static struct sr_dev_driver zketech_ebd_usb_driver_info = { | |
244 | .name = "zketech-ebd-usb", | |
245 | .longname = "ZKETECH EBD-USB", | |
246 | .api_version = 1, | |
247 | .init = std_init, | |
248 | .cleanup = std_cleanup, | |
249 | .scan = scan, | |
250 | .dev_list = std_dev_list, | |
251 | .dev_clear = std_dev_clear, | |
252 | .config_get = config_get, | |
253 | .config_set = config_set, | |
254 | .config_list = config_list, | |
255 | .dev_open = std_serial_dev_open, | |
256 | .dev_close = dev_close, | |
257 | .dev_acquisition_start = dev_acquisition_start, | |
258 | .dev_acquisition_stop = dev_acquisition_stop, | |
259 | .context = NULL, | |
260 | }; | |
261 | SR_REGISTER_DEV_DRIVER(zketech_ebd_usb_driver_info); |