]> sigrok.org Git - libsigrok.git/blame - src/hardware/pce-322a/api.c
Consistently use the "Sysclk" spelling everywhere.
[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
5a2c71cc 21#include <config.h>
dea7f634 22#include <string.h>
5a2c71cc
GH
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[] = {
f8195cb2 46 "A", "C",
5a2c71cc
GH
47};
48
49static const char *weight_time[] = {
f8195cb2 50 "F", "S",
5a2c71cc
GH
51};
52
53static const uint64_t meas_ranges[][2] = {
54 { 30, 130 },
55 { 30, 80 },
56 { 50, 100 },
57 { 80, 130 },
58};
59
ae87e02f 60static const char *data_sources[] = {
f8195cb2 61 "Live", "Memory",
ae87e02f
MG
62};
63
5a2c71cc
GH
64static GSList *scan(struct sr_dev_driver *di, GSList *options)
65{
5a2c71cc
GH
66 struct dev_context *devc;
67 struct sr_config *src;
68 struct sr_serial_dev_inst *serial;
69 struct sr_dev_inst *sdi;
43376f33 70 GSList *l;
5a2c71cc
GH
71 const char *conn;
72
73 conn = NULL;
74 for (l = options; l; l = l->next) {
75 src = l->data;
76 if (src->key == SR_CONF_CONN)
77 conn = g_variant_get_string(src->data, NULL);
78 }
79 if (!conn)
80 return NULL;
81
82 serial = sr_serial_dev_inst_new(conn, SERIALCOMM);
83
84 if (serial_open(serial, SERIAL_RDONLY) != SR_OK)
85 return NULL;
86
5a2c71cc
GH
87 sdi = g_malloc0(sizeof(struct sr_dev_inst));
88 sdi->status = SR_ST_INACTIVE;
89 sdi->vendor = g_strdup("PCE");
90 sdi->model = g_strdup("PCE-322A");
91 devc = g_malloc0(sizeof(struct dev_context));
92 devc->cur_mqflags = SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_FREQ_WEIGHT_A;
93 sdi->conn = sr_serial_dev_inst_new(conn, SERIALCOMM);
94 sdi->inst_type = SR_INST_SERIAL;
95 sdi->priv = devc;
5a2c71cc 96 sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "SPL");
5a2c71cc
GH
97
98 serial_close(serial);
99
43376f33 100 return std_scan_complete(di, g_slist_append(NULL, sdi));
5a2c71cc
GH
101}
102
5a2c71cc
GH
103static int config_get(uint32_t key, GVariant **data,
104 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
105{
106 struct dev_context *devc;
5a2c71cc
GH
107 uint64_t low, high;
108 uint64_t tmp;
109 int ret;
110
111 (void)cg;
112
113 if (!sdi)
114 return SR_ERR_ARG;
115
116 devc = sdi->priv;
758906aa 117
5a2c71cc
GH
118 switch (key) {
119 case SR_CONF_LIMIT_SAMPLES:
120 *data = g_variant_new_uint64(devc->limit_samples);
121 break;
122 case SR_CONF_SPL_WEIGHT_FREQ:
123 tmp = pce_322a_weight_freq_get(sdi);
124 if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_A)
125 *data = g_variant_new_string("A");
126 else if (tmp == SR_MQFLAG_SPL_FREQ_WEIGHT_C)
127 *data = g_variant_new_string("C");
128 else
129 return SR_ERR;
130 break;
131 case SR_CONF_SPL_WEIGHT_TIME:
132 tmp = pce_322a_weight_time_get(sdi);
133 if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_F)
134 *data = g_variant_new_string("F");
135 else if (tmp == SR_MQFLAG_SPL_TIME_WEIGHT_S)
136 *data = g_variant_new_string("S");
137 else
138 return SR_ERR;
139 break;
140 case SR_CONF_SPL_MEASUREMENT_RANGE:
a162eeb2
UH
141 if ((ret = pce_322a_meas_range_get(sdi, &low, &high)) == SR_OK)
142 *data = std_gvar_tuple_u64(low, high);
758906aa
UH
143 else
144 return ret;
5a2c71cc
GH
145 break;
146 case SR_CONF_POWER_OFF:
147 *data = g_variant_new_boolean(FALSE);
148 break;
ae87e02f
MG
149 case SR_CONF_DATA_SOURCE:
150 if (devc->cur_data_source == DATA_SOURCE_LIVE)
151 *data = g_variant_new_string("Live");
152 else
153 *data = g_variant_new_string("Memory");
154 break;
5a2c71cc
GH
155 default:
156 return SR_ERR_NA;
157 }
158
758906aa 159 return SR_OK;
5a2c71cc
GH
160}
161
162static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
163 const struct sr_channel_group *cg)
164{
165 struct dev_context *devc;
758906aa 166 int idx;
5a2c71cc
GH
167
168 (void)cg;
169
5a2c71cc
GH
170 devc = sdi->priv;
171
5a2c71cc
GH
172 switch (key) {
173 case SR_CONF_LIMIT_SAMPLES:
50ccb36f 174 devc->limit_samples = g_variant_get_uint64(data);
5a2c71cc
GH
175 break;
176 case SR_CONF_SPL_WEIGHT_FREQ:
697fb6dd 177 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(weight_freq))) < 0)
5a2c71cc 178 return SR_ERR_ARG;
758906aa 179 return pce_322a_weight_freq_set(sdi, (weight_freq[idx][0] == 'A') ?
697fb6dd 180 SR_MQFLAG_SPL_FREQ_WEIGHT_A : SR_MQFLAG_SPL_FREQ_WEIGHT_C);
5a2c71cc 181 case SR_CONF_SPL_WEIGHT_TIME:
697fb6dd 182 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(weight_time))) < 0)
5a2c71cc 183 return SR_ERR_ARG;
758906aa 184 return pce_322a_weight_time_set(sdi, (weight_time[idx][0] == 'F') ?
697fb6dd 185 SR_MQFLAG_SPL_TIME_WEIGHT_F : SR_MQFLAG_SPL_TIME_WEIGHT_S);
5a2c71cc 186 case SR_CONF_SPL_MEASUREMENT_RANGE:
697fb6dd
UH
187 if ((idx = std_u64_tuple_idx(data, ARRAY_AND_SIZE(meas_ranges))) < 0)
188 return SR_ERR_ARG;
758906aa 189 return pce_322a_meas_range_set(sdi, meas_ranges[idx][0], meas_ranges[idx][1]);
5a2c71cc
GH
190 case SR_CONF_POWER_OFF:
191 if (g_variant_get_boolean(data))
758906aa 192 return pce_322a_power_off(sdi);
5a2c71cc 193 break;
ae87e02f 194 case SR_CONF_DATA_SOURCE:
697fb6dd
UH
195 if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0)
196 return SR_ERR_ARG;
197 devc->cur_data_source = idx;
ae87e02f 198 break;
5a2c71cc 199 default:
758906aa 200 return SR_ERR_NA;
5a2c71cc
GH
201 }
202
758906aa 203 return SR_OK;
5a2c71cc
GH
204}
205
206static int config_list(uint32_t key, GVariant **data,
207 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
208{
e66d1892
UH
209 switch (key) {
210 case SR_CONF_SCAN_OPTIONS:
211 case SR_CONF_DEVICE_OPTIONS:
212 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
213 case SR_CONF_SPL_WEIGHT_FREQ:
53012da6 214 *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_freq));
e66d1892
UH
215 break;
216 case SR_CONF_SPL_WEIGHT_TIME:
53012da6 217 *data = g_variant_new_strv(ARRAY_AND_SIZE(weight_time));
e66d1892
UH
218 break;
219 case SR_CONF_SPL_MEASUREMENT_RANGE:
58ffcf97 220 *data = std_gvar_tuple_array(ARRAY_AND_SIZE(meas_ranges));
e66d1892
UH
221 break;
222 case SR_CONF_DATA_SOURCE:
53012da6 223 *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources));
e66d1892
UH
224 break;
225 default:
226 return SR_ERR_NA;
5a2c71cc
GH
227 }
228
e66d1892 229 return SR_OK;
5a2c71cc
GH
230}
231
232static int dev_open(struct sr_dev_inst *sdi)
233{
234 int ret;
235
236 ret = std_serial_dev_open(sdi);
237 if (ret != SR_OK)
238 return ret;
239
240 return pce_322a_connect(sdi);
241}
242
243static int dev_close(struct sr_dev_inst *sdi)
244{
245 /*
246 * Ensure device gets properly disconnected even when there was
247 * no acquisition.
248 */
249 pce_322a_disconnect(sdi);
250
251 return std_serial_dev_close(sdi);
252}
253
254static int dev_acquisition_start(const struct sr_dev_inst *sdi)
255{
256 struct dev_context *devc;
257 struct sr_serial_dev_inst *serial;
258
5a2c71cc
GH
259 devc = sdi->priv;
260 devc->buffer_len = 0;
ae87e02f 261 devc->memory_state = MEM_STATE_REQUEST_MEMORY_USAGE;
5a2c71cc 262
bee2b016 263 std_session_send_df_header(sdi);
5a2c71cc 264
5a2c71cc
GH
265 serial = sdi->conn;
266 serial_source_add(sdi->session, serial, G_IO_IN, 150,
267 pce_322a_receive_data, (void *)sdi);
268
269 return SR_OK;
270}
271
5a2c71cc
GH
272static struct sr_dev_driver pce_322a_driver_info = {
273 .name = "pce-322a",
274 .longname = "PCE PCE-322A",
275 .api_version = 1,
276 .init = std_init,
277 .cleanup = std_cleanup,
278 .scan = scan,
279 .dev_list = std_dev_list,
f778bf02 280 .dev_clear = std_dev_clear,
5a2c71cc
GH
281 .config_get = config_get,
282 .config_set = config_set,
283 .config_list = config_list,
284 .dev_open = dev_open,
285 .dev_close = dev_close,
286 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 287 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
5a2c71cc
GH
288 .context = NULL,
289};
290SR_REGISTER_DEV_DRIVER(pce_322a_driver_info);