]>
Commit | Line | Data |
---|---|---|
cae33a58 AS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2020 Andreas Sandberg <andreas@sandberg.pp.se> | |
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> | |
ea5fc667 GS |
21 | |
22 | #include <fcntl.h> | |
cae33a58 | 23 | #include <glib.h> |
ea5fc667 GS |
24 | #include <libsigrok/libsigrok.h> |
25 | #include <string.h> | |
cae33a58 AS |
26 | #include <sys/types.h> |
27 | #include <sys/stat.h> | |
ea5fc667 | 28 | |
cae33a58 AS |
29 | #include "libsigrok-internal.h" |
30 | #include "protocol.h" | |
31 | ||
32 | #define RDTECH_TC_SERIALCOMM "115200/8n1" | |
33 | ||
34 | static const uint32_t scanopts[] = { | |
35 | SR_CONF_CONN, | |
36 | SR_CONF_SERIALCOMM, | |
37 | }; | |
38 | ||
39 | static const uint32_t drvopts[] = { | |
40 | SR_CONF_ENERGYMETER, | |
41 | }; | |
42 | ||
43 | static const uint32_t devopts[] = { | |
44 | SR_CONF_CONTINUOUS, | |
15ceaf9d GS |
45 | SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET, |
46 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
cae33a58 AS |
47 | }; |
48 | ||
5955b58c GS |
49 | static GSList *rdtech_tc_scan(struct sr_dev_driver *di, |
50 | const char *conn, const char *serialcomm) | |
cae33a58 AS |
51 | { |
52 | struct sr_serial_dev_inst *serial; | |
c1f9428a GS |
53 | GSList *devices; |
54 | struct dev_context *devc; | |
55 | struct sr_dev_inst *sdi; | |
56 | size_t i; | |
b3df7668 GS |
57 | const struct rdtech_tc_channel_desc *pch; |
58 | struct sr_channel *ch; | |
59 | struct feed_queue_analog *feed; | |
cae33a58 AS |
60 | |
61 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
62 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
63 | goto err_out; | |
64 | ||
5df2033d | 65 | devc = g_malloc0(sizeof(*devc)); |
cae33a58 AS |
66 | sr_sw_limits_init(&devc->limits); |
67 | ||
68 | if (rdtech_tc_probe(serial, devc) != SR_OK) { | |
69 | sr_err("Failed to find a supported RDTech TC device."); | |
70 | goto err_out_serial; | |
71 | } | |
72 | ||
5df2033d GS |
73 | sdi = g_malloc0(sizeof(*sdi)); |
74 | sdi->priv = devc; | |
cae33a58 AS |
75 | sdi->status = SR_ST_INACTIVE; |
76 | sdi->vendor = g_strdup("RDTech"); | |
77 | sdi->model = g_strdup(devc->dev_info.model_name); | |
78 | sdi->version = g_strdup(devc->dev_info.fw_ver); | |
79 | sdi->serial_num = g_strdup_printf("%08" PRIu32, devc->dev_info.serial_num); | |
80 | sdi->inst_type = SR_INST_SERIAL; | |
81 | sdi->conn = serial; | |
cae33a58 | 82 | |
b3df7668 | 83 | devc->feeds = g_malloc0(devc->channel_count * sizeof(devc->feeds[0])); |
fd2a8a50 | 84 | for (i = 0; i < devc->channel_count; i++) { |
b3df7668 GS |
85 | pch = &devc->channels[i]; |
86 | ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, pch->name); | |
87 | feed = feed_queue_analog_alloc(sdi, 1, pch->digits, ch); | |
88 | feed_queue_analog_mq_unit(feed, pch->mq, 0, pch->unit); | |
89 | feed_queue_analog_scale_offset(feed, &pch->scale, NULL); | |
90 | devc->feeds[i] = feed; | |
c1f9428a | 91 | } |
cae33a58 | 92 | |
c1f9428a | 93 | devices = g_slist_append(NULL, sdi); |
cae33a58 AS |
94 | serial_close(serial); |
95 | if (!devices) | |
96 | sr_serial_dev_inst_free(serial); | |
97 | ||
98 | return std_scan_complete(di, devices); | |
99 | ||
100 | err_out_serial: | |
101 | g_free(devc); | |
102 | serial_close(serial); | |
103 | err_out: | |
104 | sr_serial_dev_inst_free(serial); | |
105 | ||
106 | return NULL; | |
107 | } | |
108 | ||
b3df7668 GS |
109 | static void clear_helper(struct dev_context *devc) |
110 | { | |
111 | size_t idx; | |
112 | ||
113 | if (!devc) | |
114 | return; | |
115 | ||
116 | if (devc->feeds) { | |
117 | for (idx = 0; idx < devc->channel_count; idx++) | |
118 | feed_queue_analog_free(devc->feeds[idx]); | |
119 | g_free(devc->feeds); | |
120 | } | |
121 | } | |
122 | ||
123 | static int dev_clear(const struct sr_dev_driver *driver) | |
124 | { | |
125 | return std_dev_clear_with_callback(driver, (std_dev_clear_callback)clear_helper); | |
126 | } | |
127 | ||
cae33a58 AS |
128 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
129 | { | |
c1f9428a GS |
130 | const char *conn; |
131 | const char *serialcomm; | |
cae33a58 | 132 | |
c1f9428a GS |
133 | conn = NULL; |
134 | serialcomm = RDTECH_TC_SERIALCOMM; | |
fa801c4f | 135 | (void)sr_serial_extract_options(options, &conn, &serialcomm); |
cae33a58 AS |
136 | if (!conn) |
137 | return NULL; | |
138 | ||
139 | return rdtech_tc_scan(di, conn, serialcomm); | |
140 | } | |
141 | ||
15ceaf9d GS |
142 | static int config_get(uint32_t key, GVariant **data, |
143 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
144 | { | |
145 | struct dev_context *devc; | |
146 | ||
147 | (void)cg; | |
148 | ||
149 | devc = sdi->priv; | |
150 | ||
151 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
152 | } | |
153 | ||
cae33a58 | 154 | static int config_set(uint32_t key, GVariant *data, |
5955b58c | 155 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
cae33a58 AS |
156 | { |
157 | struct dev_context *devc; | |
158 | ||
159 | (void)cg; | |
160 | ||
161 | devc = sdi->priv; | |
162 | ||
163 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
164 | } | |
165 | ||
166 | static int config_list(uint32_t key, GVariant **data, | |
5955b58c | 167 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) |
cae33a58 AS |
168 | { |
169 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
170 | } | |
171 | ||
172 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
173 | { | |
c1f9428a GS |
174 | struct dev_context *devc; |
175 | struct sr_serial_dev_inst *serial; | |
cae33a58 | 176 | |
c1f9428a | 177 | devc = sdi->priv; |
cae33a58 AS |
178 | sr_sw_limits_acquisition_start(&devc->limits); |
179 | std_session_send_df_header(sdi); | |
180 | ||
c1f9428a | 181 | serial = sdi->conn; |
cae33a58 | 182 | serial_source_add(sdi->session, serial, G_IO_IN, 50, |
5955b58c | 183 | rdtech_tc_receive_data, (void *)sdi); |
cae33a58 | 184 | |
a82490b6 | 185 | return rdtech_tc_poll(sdi, TRUE); |
cae33a58 AS |
186 | } |
187 | ||
188 | static struct sr_dev_driver rdtech_tc_driver_info = { | |
189 | .name = "rdtech-tc", | |
190 | .longname = "RDTech TC66C USB power meter", | |
191 | .api_version = 1, | |
192 | .init = std_init, | |
193 | .cleanup = std_cleanup, | |
194 | .scan = scan, | |
195 | .dev_list = std_dev_list, | |
b3df7668 | 196 | .dev_clear = dev_clear, |
15ceaf9d | 197 | .config_get = config_get, |
cae33a58 AS |
198 | .config_set = config_set, |
199 | .config_list = config_list, | |
200 | .dev_open = std_serial_dev_open, | |
201 | .dev_close = std_serial_dev_close, | |
202 | .dev_acquisition_start = dev_acquisition_start, | |
203 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
204 | .context = NULL, | |
205 | }; | |
206 | SR_REGISTER_DEV_DRIVER(rdtech_tc_driver_info); |