]>
Commit | Line | Data |
---|---|---|
a28b3df1 VI |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 Vlad Ivanov <vlad.ivanov@lab-systems.ru> | |
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> | |
21 | #include <libserialport.h> | |
22 | #include <scpi.h> | |
23 | #include <string.h> | |
24 | ||
25 | #include "protocol.h" | |
26 | ||
27 | SR_PRIV struct sr_dev_driver rohde_schwarz_sme_0x_driver_info; | |
28 | ||
9e506594 SA |
29 | static const char *manufacturer = "Rohde&Schwarz"; |
30 | ||
31 | static const struct rs_device_model device_models[] = { | |
32 | { | |
33 | .model_str = "SME02", | |
34 | .freq_max = SR_GHZ(1.5), | |
35 | .freq_min = SR_KHZ(5), | |
36 | .power_max = 16, | |
ed1fae0b | 37 | .power_min = -144, |
9e506594 SA |
38 | }, |
39 | { | |
40 | .model_str = "SME03E", | |
41 | .freq_max = SR_GHZ(2.2), | |
42 | .freq_min = SR_KHZ(5), | |
43 | .power_max = 16, | |
ed1fae0b | 44 | .power_min = -144, |
9e506594 SA |
45 | }, |
46 | { | |
47 | .model_str = "SME03A", | |
48 | .freq_max = SR_GHZ(3), | |
49 | .freq_min = SR_KHZ(5), | |
50 | .power_max = 16, | |
ed1fae0b | 51 | .power_min = -144, |
9e506594 SA |
52 | }, |
53 | { | |
54 | .model_str = "SME03", | |
55 | .freq_max = SR_GHZ(3), | |
56 | .freq_min = SR_KHZ(5), | |
57 | .power_max = 16, | |
ed1fae0b | 58 | .power_min = -144, |
9e506594 SA |
59 | }, |
60 | { | |
61 | .model_str = "SME06", | |
62 | .freq_max = SR_GHZ(1.5), | |
63 | .freq_min = SR_KHZ(5), | |
64 | .power_max = 16, | |
ed1fae0b | 65 | .power_min = -144, |
9e506594 SA |
66 | } |
67 | }; | |
a28b3df1 VI |
68 | |
69 | static const uint32_t scanopts[] = { | |
70 | SR_CONF_CONN, | |
71 | SR_CONF_SERIALCOMM, | |
72 | }; | |
73 | ||
74 | static const uint32_t devopts[] = { | |
75 | SR_CONF_OUTPUT_FREQUENCY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
76 | SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
77 | }; | |
78 | ||
a28b3df1 VI |
79 | static int rs_init_device(struct sr_dev_inst *sdi) |
80 | { | |
81 | struct dev_context *devc; | |
82 | uint8_t model_found; | |
83 | ||
84 | devc = sdi->priv; | |
85 | model_found = 0; | |
86 | ||
87 | for (size_t i = 0; i < ARRAY_SIZE(device_models); i++) { | |
88 | if (!strcmp(device_models[i].model_str, sdi->model)) { | |
89 | model_found = 1; | |
90 | devc->model_config = &device_models[i]; | |
91 | break; | |
92 | } | |
93 | } | |
94 | ||
95 | if (!model_found) { | |
ed1fae0b UH |
96 | sr_dbg("Device %s %s is not supported by this driver.", |
97 | manufacturer, sdi->model); | |
a28b3df1 VI |
98 | return SR_ERR_NA; |
99 | } | |
100 | ||
101 | return SR_OK; | |
102 | } | |
103 | ||
104 | static struct sr_dev_inst *rs_probe_serial_device(struct sr_scpi_dev_inst *scpi) | |
105 | { | |
106 | struct sr_dev_inst *sdi; | |
107 | struct dev_context *devc; | |
108 | struct sr_scpi_hw_info *hw_info; | |
109 | ||
110 | sdi = NULL; | |
111 | devc = NULL; | |
112 | hw_info = NULL; | |
113 | ||
114 | rs_sme0x_mode_remote(scpi); | |
115 | ||
ed1fae0b | 116 | if (sr_scpi_get_hw_id(scpi, &hw_info) != SR_OK) |
a28b3df1 | 117 | goto fail; |
a28b3df1 | 118 | |
ed1fae0b | 119 | if (strcmp(hw_info->manufacturer, manufacturer) != 0) |
a28b3df1 | 120 | goto fail; |
a28b3df1 VI |
121 | |
122 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
123 | sdi->vendor = g_strdup(hw_info->manufacturer); | |
124 | sdi->model = g_strdup(hw_info->model); | |
125 | sdi->version = g_strdup(hw_info->firmware_version); | |
126 | sdi->serial_num = g_strdup(hw_info->serial_number); | |
127 | sdi->driver = &rohde_schwarz_sme_0x_driver_info; | |
128 | sdi->inst_type = SR_INST_SCPI; | |
129 | sdi->conn = scpi; | |
130 | ||
131 | sr_scpi_hw_info_free(hw_info); | |
132 | hw_info = NULL; | |
133 | ||
134 | devc = g_malloc0(sizeof(struct dev_context)); | |
135 | sdi->priv = devc; | |
136 | ||
ed1fae0b | 137 | if (rs_init_device(sdi) != SR_OK) |
a28b3df1 | 138 | goto fail; |
a28b3df1 VI |
139 | |
140 | return sdi; | |
141 | ||
142 | fail: | |
ed1fae0b | 143 | if (hw_info) |
a28b3df1 | 144 | sr_scpi_hw_info_free(hw_info); |
a28b3df1 | 145 | |
ed1fae0b | 146 | if (sdi) |
a28b3df1 | 147 | sr_dev_inst_free(sdi); |
a28b3df1 VI |
148 | |
149 | g_free(devc); | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
154 | { | |
155 | return sr_scpi_scan(di->context, options, rs_probe_serial_device); | |
156 | } | |
157 | ||
158 | static int dev_clear(const struct sr_dev_driver *di) | |
159 | { | |
160 | return std_dev_clear(di, NULL); | |
161 | } | |
162 | ||
163 | static int dev_open(struct sr_dev_inst *sdi) | |
164 | { | |
ed1fae0b | 165 | if ((sdi->status != SR_ST_ACTIVE) && (sr_scpi_open(sdi->conn) != SR_OK)) |
a28b3df1 | 166 | return SR_ERR; |
a28b3df1 VI |
167 | |
168 | sdi->status = SR_ST_ACTIVE; | |
169 | ||
170 | return SR_OK; | |
171 | } | |
172 | ||
173 | static int dev_close(struct sr_dev_inst *sdi) | |
174 | { | |
ed1fae0b | 175 | if (sdi->status == SR_ST_INACTIVE) |
a28b3df1 | 176 | return SR_OK; |
a28b3df1 VI |
177 | |
178 | sr_scpi_close(sdi->conn); | |
179 | ||
180 | sdi->status = SR_ST_INACTIVE; | |
181 | ||
182 | return SR_OK; | |
183 | } | |
184 | ||
185 | static int config_get(uint32_t key, GVariant **data, | |
186 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
187 | { | |
188 | double value_f; | |
189 | ||
190 | (void) cg; | |
191 | ||
192 | switch (key) { | |
193 | case SR_CONF_OUTPUT_FREQUENCY: | |
194 | rs_sme0x_get_freq(sdi, &value_f); | |
195 | *data = g_variant_new_double(value_f); | |
196 | break; | |
197 | case SR_CONF_AMPLITUDE: | |
198 | rs_sme0x_get_power(sdi, &value_f); | |
199 | *data = g_variant_new_double(value_f); | |
200 | break; | |
201 | default: | |
202 | return SR_ERR_NA; | |
203 | } | |
204 | ||
205 | return SR_OK; | |
206 | } | |
207 | ||
208 | static int config_set(uint32_t key, GVariant *data, | |
209 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
210 | { | |
211 | double value_f; | |
212 | ||
213 | (void)cg; | |
214 | ||
ed1fae0b | 215 | if (!sdi) |
a28b3df1 | 216 | return SR_ERR_ARG; |
a28b3df1 | 217 | |
ed1fae0b | 218 | if (sdi->status != SR_ST_ACTIVE) |
a28b3df1 | 219 | return SR_ERR_DEV_CLOSED; |
a28b3df1 VI |
220 | |
221 | switch (key) { | |
222 | case SR_CONF_OUTPUT_FREQUENCY: | |
223 | value_f = g_variant_get_double(data); | |
224 | rs_sme0x_set_freq(sdi, value_f); | |
225 | break; | |
226 | case SR_CONF_AMPLITUDE: | |
227 | value_f = g_variant_get_double(data); | |
228 | rs_sme0x_set_power(sdi, value_f); | |
229 | break; | |
230 | default: | |
231 | return SR_ERR_NA; | |
232 | } | |
233 | ||
234 | return SR_OK; | |
235 | } | |
236 | ||
237 | static int config_list(uint32_t key, GVariant **data, | |
238 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
239 | { | |
240 | (void)sdi; | |
241 | (void)cg; | |
242 | ||
243 | switch (key) { | |
244 | case SR_CONF_SCAN_OPTIONS: | |
ed1fae0b UH |
245 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
246 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
a28b3df1 VI |
247 | break; |
248 | case SR_CONF_DEVICE_OPTIONS: | |
ed1fae0b UH |
249 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
250 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
a28b3df1 VI |
251 | break; |
252 | default: | |
253 | return SR_ERR_NA; | |
254 | } | |
255 | ||
256 | return SR_OK; | |
257 | } | |
258 | ||
259 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
260 | { | |
ed1fae0b | 261 | if (sdi->status != SR_ST_ACTIVE) |
a28b3df1 | 262 | return SR_ERR_DEV_CLOSED; |
a28b3df1 VI |
263 | |
264 | return SR_OK; | |
265 | } | |
266 | ||
267 | SR_PRIV struct sr_dev_driver rohde_schwarz_sme_0x_driver_info = { | |
268 | .name = "rohde-schwarz-sme-0x", | |
269 | .longname = "Rohde&Schwarz SME-0x", | |
270 | .api_version = 1, | |
271 | .init = std_init, | |
272 | .cleanup = std_cleanup, | |
273 | .scan = scan, | |
274 | .dev_list = std_dev_list, | |
275 | .dev_clear = dev_clear, | |
276 | .config_get = config_get, | |
277 | .config_set = config_set, | |
278 | .config_list = config_list, | |
279 | .dev_open = dev_open, | |
280 | .dev_close = dev_close, | |
281 | .dev_acquisition_start = dev_acquisition_start, | |
282 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
283 | .context = NULL, | |
284 | }; | |
285 | ||
286 | SR_REGISTER_DEV_DRIVER(rohde_schwarz_sme_0x_driver_info); |