]> sigrok.org Git - libsigrok.git/blame - src/hardware/pce-322a/api.c
saleae-logic-pro: Use sr_dev_acquisition_stop() wrapper.
[libsigrok.git] / src / hardware / pce-322a / api.c
CommitLineData
5a2c71cc
GH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2016 George Hopkins <george-hopkins@null.net>
ae87e02f 5 * Copyright (C) 2016 Matthieu Guillaumin <matthieu@guillaum.in>
5a2c71cc
GH
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 <string.h>
22#include <config.h>
23#include "protocol.h"
24
25#define SERIALCOMM "115200/8n1"
26
27static const uint32_t scanopts[] = {
28 SR_CONF_CONN,
29};
30
31static const uint32_t drvopts[] = {
32 SR_CONF_SOUNDLEVELMETER,
33};
34
35static const uint32_t devopts[] = {
36 SR_CONF_CONTINUOUS | SR_CONF_SET,
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_SPL_WEIGHT_FREQ | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_SPL_WEIGHT_TIME | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_SPL_MEASUREMENT_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
ae87e02f 42 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
5a2c71cc
GH
43};
44
45static const char *weight_freq[] = {
46 "A",
47 "C",
48};
49
50static const char *weight_time[] = {
51 "F",
52 "S",
53};
54
55static const uint64_t meas_ranges[][2] = {
56 { 30, 130 },
57 { 30, 80 },
58 { 50, 100 },
59 { 80, 130 },
60};
61
ae87e02f
MG
62static const char *data_sources[] = {
63 "Live",
64 "Memory",
65};
66
5a2c71cc
GH
67static GSList *scan(struct sr_dev_driver *di, GSList *options)
68{
5a2c71cc
GH
69 struct dev_context *devc;
70 struct sr_config *src;
71 struct sr_serial_dev_inst *serial;
72 struct sr_dev_inst *sdi;
43376f33 73 GSList *l;
5a2c71cc
GH
74 const char *conn;
75
76 conn = NULL;
77 for (l = options; l; l = l->next) {
78 src = l->data;
79 if (src->key == SR_CONF_CONN)
80 conn = g_variant_get_string(src->data, NULL);
81 }
82 if (!conn)
83 return NULL;
84
85 serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
86
87 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
88 return NULL;
89
5a2c71cc
GH
90 sdi = g_malloc0(sizeof(struct sr_dev_inst));
91 sdi->status = SR_ST_INACTIVE;
92 sdi->vendor = g_strdup("PCE");
93 sdi->model = g_strdup("PCE-322A");
94 devc = g_malloc0(sizeof(struct dev_context));
95 devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A;
96 sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
97 sdi->inst_type = SR_INST_SERIAL;
98 sdi->priv = devc;
5a2c71cc 99 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
5a2c71cc
GH
100
101 serial_close(serial);
102
43376f33 103 return std_scan_complete(di, g_slist_append(NULL, sdi));
5a2c71cc
GH
104}
105
5a2c71cc
GH
106static int config_get(uint32_t key, GVariant **data,
107 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
108{
109 struct dev_context *devc;
110 GVariant *range[2];
111 uint64_t low, high;
112 uint64_t tmp;
113 int ret;
114
115 (void)cg;
116
117 if (!sdi)
118 return SR_ERR_ARG;
119
120 devc = sdi->priv;
121 ret = SR_OK;
122 switch (key) {
123 case SR_CONF_LIMIT_SAMPLES:
124 *data = g_variant_new_uint64(devc->limit_samples);
125 break;
126 case SR_CONF_SPL_WEIGHT_FREQ:
127 tmp = pce_322a_weight_freq_get(sdi);
128 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
129 *data = g_variant_new_string("A");
130 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
131 *data = g_variant_new_string("C");
132 else
133 return SR_ERR;
134 break;
135 case SR_CONF_SPL_WEIGHT_TIME:
136 tmp = pce_322a_weight_time_get(sdi);
137 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
138 *data = g_variant_new_string("F");
139 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
140 *data = g_variant_new_string("S");
141 else
142 return SR_ERR;
143 break;
144 case SR_CONF_SPL_MEASUREMENT_RANGE:
145 if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK) {
146 range[0] = g_variant_new_uint64(low);
147 range[1] = g_variant_new_uint64(high);
148 *data = g_variant_new_tuple(range, 2);
149 }
150 break;
151 case SR_CONF_POWER_OFF:
152 *data = g_variant_new_boolean(FALSE);
153 break;
ae87e02f
MG
154 case SR_CONF_DATA_SOURCE:
155 if (devc->cur_data_source == DATA_SOURCE_LIVE)
156 *data = g_variant_new_string("Live");
157 else
158 *data = g_variant_new_string("Memory");
159 break;
5a2c71cc
GH
160 default:
161 return SR_ERR_NA;
162 }
163
164 return ret;
165}
166
167static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
168 const struct sr_channel_group *cg)
169{
170 struct dev_context *devc;
171 uint64_t tmp_u64, low, high;
172 unsigned int i;
173 int ret;
174 const char *tmp_str;
175
176 (void)cg;
177
5a2c71cc
GH
178 devc = sdi->priv;
179
180 ret = SR_OK;
181 switch (key) {
182 case SR_CONF_LIMIT_SAMPLES:
183 tmp_u64 = g_variant_get_uint64(data);
184 devc->limit_samples = tmp_u64;
185 ret = SR_OK;
186 break;
187 case SR_CONF_SPL_WEIGHT_FREQ:
188 tmp_str = g_variant_get_string(data, NULL);
189 if (!strcmp(tmp_str, "A"))
190 ret = pce_322a_weight_freq_set(sdi,
191 SR_MQFLAG_SPL_FREQ_WEIGHT_A);
192 else if (!strcmp(tmp_str, "C"))
193 ret = pce_322a_weight_freq_set(sdi,
194 SR_MQFLAG_SPL_FREQ_WEIGHT_C);
195 else
196 return SR_ERR_ARG;
197 break;
198 case SR_CONF_SPL_WEIGHT_TIME:
199 tmp_str = g_variant_get_string(data, NULL);
200 if (!strcmp(tmp_str, "F"))
201 ret = pce_322a_weight_time_set(sdi,
202 SR_MQFLAG_SPL_TIME_WEIGHT_F);
203 else if (!strcmp(tmp_str, "S"))
204 ret = pce_322a_weight_time_set(sdi,
205 SR_MQFLAG_SPL_TIME_WEIGHT_S);
206 else
207 return SR_ERR_ARG;
208 break;
209 case SR_CONF_SPL_MEASUREMENT_RANGE:
210 g_variant_get(data, "(tt)", &low, &high);
211 ret = SR_ERR_ARG;
212 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
213 if (meas_ranges[i][0] == low && meas_ranges[i][1] == high) {
214 ret = pce_322a_meas_range_set(sdi, low, high);
215 break;
216 }
217 }
218 break;
219 case SR_CONF_POWER_OFF:
220 if (g_variant_get_boolean(data))
221 ret = pce_322a_power_off(sdi);
222 break;
ae87e02f
MG
223 case SR_CONF_DATA_SOURCE:
224 tmp_str = g_variant_get_string(data, NULL);
225 if (!strcmp(tmp_str, "Live"))
226 devc->cur_data_source = DATA_SOURCE_LIVE;
227 else if (!strcmp(tmp_str, "Memory"))
228 devc->cur_data_source = DATA_SOURCE_MEMORY;
229 else
230 return SR_ERR;
231 break;
5a2c71cc
GH
232 default:
233 ret = SR_ERR_NA;
234 }
235
236 return ret;
237}
238
239static int config_list(uint32_t key, GVariant **data,
240 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
241{
242 GVariant *tuple, *range[2];
243 GVariantBuilder gvb;
244 unsigned int i;
245 int ret;
246
247 (void)cg;
248
249 ret = SR_OK;
250 if (!sdi) {
251 switch (key) {
252 case SR_CONF_SCAN_OPTIONS:
253 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
254 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
255 break;
256 case SR_CONF_DEVICE_OPTIONS:
257 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
258 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
259 break;
260 default:
261 return SR_ERR_NA;
262 }
263 } else {
264 switch (key) {
265 case SR_CONF_DEVICE_OPTIONS:
266 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
267 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
268 break;
269 case SR_CONF_SPL_WEIGHT_FREQ:
270 *data = g_variant_new_strv(weight_freq, ARRAY_SIZE(weight_freq));
271 break;
272 case SR_CONF_SPL_WEIGHT_TIME:
273 *data = g_variant_new_strv(weight_time, ARRAY_SIZE(weight_time));
274 break;
275 case SR_CONF_SPL_MEASUREMENT_RANGE:
276 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
277 for (i = 0; i < ARRAY_SIZE(meas_ranges); i++) {
278 range[0] = g_variant_new_uint64(meas_ranges[i][0]);
279 range[1] = g_variant_new_uint64(meas_ranges[i][1]);
280 tuple = g_variant_new_tuple(range, 2);
281 g_variant_builder_add_value(&gvb, tuple);
282 }
283 *data = g_variant_builder_end(&gvb);
284 break;
ae87e02f
MG
285 case SR_CONF_DATA_SOURCE:
286 *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources));
287 break;
5a2c71cc
GH
288 default:
289 return SR_ERR_NA;
290 }
291 }
292
293 return ret;
294}
295
296static int dev_open(struct sr_dev_inst *sdi)
297{
298 int ret;
299
300 ret = std_serial_dev_open(sdi);
301 if (ret != SR_OK)
302 return ret;
303
304 return pce_322a_connect(sdi);
305}
306
307static int dev_close(struct sr_dev_inst *sdi)
308{
309 /*
310 * Ensure device gets properly disconnected even when there was
311 * no acquisition.
312 */
313 pce_322a_disconnect(sdi);
314
315 return std_serial_dev_close(sdi);
316}
317
318static int dev_acquisition_start(const struct sr_dev_inst *sdi)
319{
320 struct dev_context *devc;
321 struct sr_serial_dev_inst *serial;
322
5a2c71cc
GH
323 devc = sdi->priv;
324 devc->buffer_len = 0;
ae87e02f 325 devc->memory_state = MEM_STATE_REQUEST_MEMORY_USAGE;
5a2c71cc 326
bee2b016 327 std_session_send_df_header(sdi);
5a2c71cc
GH
328
329 /* Poll every 150ms, or whenever some data comes in. */
330 serial = sdi->conn;
331 serial_source_add(sdi->session, serial, G_IO_IN, 150,
332 pce_322a_receive_data, (void *)sdi);
333
334 return SR_OK;
335}
336
5a2c71cc
GH
337static struct sr_dev_driver pce_322a_driver_info = {
338 .name = "pce-322a",
339 .longname = "PCE PCE-322A",
340 .api_version = 1,
341 .init = std_init,
342 .cleanup = std_cleanup,
343 .scan = scan,
344 .dev_list = std_dev_list,
f778bf02 345 .dev_clear = std_dev_clear,
5a2c71cc
GH
346 .config_get = config_get,
347 .config_set = config_set,
348 .config_list = config_list,
349 .dev_open = dev_open,
350 .dev_close = dev_close,
351 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 352 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
5a2c71cc
GH
353 .context = NULL,
354};
355SR_REGISTER_DEV_DRIVER(pce_322a_driver_info);