scpi-pps: Add a missing "break" in config_get().
[libsigrok.git] / src / hardware / atten-pps3xxx / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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 <string.h>
22 #include "protocol.h"
23
24 /*
25  * The default serial communication settings on the device are 9600
26  * baud, 9 data bits. The 9th bit isn't actually used, and the vendor
27  * software uses Mark parity to absorb the extra bit.
28  *
29  * Since 9 data bits is not a standard available in POSIX, we use two
30  * stop bits to skip over the extra bit instead.
31  */
32 #define SERIALCOMM "9600/8n2"
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_POWER_SUPPLY,
41 };
42
43 static const uint32_t devopts[] = {
44         SR_CONF_CONTINUOUS,
45         SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46         SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_GET | SR_CONF_SET,
47 };
48
49 static const uint32_t devopts_cg[] = {
50         SR_CONF_VOLTAGE | SR_CONF_GET,
51         SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
52         SR_CONF_CURRENT | SR_CONF_GET,
53         SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
54         SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
55 };
56
57 static const char *channel_modes[] = {
58         "Independent",
59         "Series",
60         "Parallel",
61 };
62
63 static const struct pps_model models[] = {
64         { PPS_3203T_3S, "PPS3203T-3S",
65                 CHANMODE_INDEPENDENT | CHANMODE_SERIES | CHANMODE_PARALLEL,
66                 3,
67                 {
68                         /* Channel 1 */
69                         { { 0, 32, 0.01 }, { 0, 3, 0.001 } },
70                         /* Channel 2 */
71                         { { 0, 32, 0.01 }, { 0, 3, 0.001 } },
72                         /* Channel 3 */
73                         { { 0, 6, 0.01 }, { 0, 3, 0.001 } },
74                 },
75         },
76 };
77
78 static GSList *scan(struct sr_dev_driver *di, GSList *options, int modelid)
79 {
80         struct sr_dev_inst *sdi;
81         struct dev_context *devc;
82         struct sr_config *src;
83         struct sr_channel *ch;
84         struct sr_channel_group *cg;
85         struct sr_serial_dev_inst *serial;
86         GSList *l;
87         const struct pps_model *model;
88         uint8_t packet[PACKET_SIZE];
89         unsigned int i;
90         int delay_ms, ret;
91         const char *conn, *serialcomm;
92         char channel[10];
93
94         conn = serialcomm = NULL;
95         for (l = options; l; l = l->next) {
96                 src = l->data;
97                 switch (src->key) {
98                 case SR_CONF_CONN:
99                         conn = g_variant_get_string(src->data, NULL);
100                         break;
101                 case SR_CONF_SERIALCOMM:
102                         serialcomm = g_variant_get_string(src->data, NULL);
103                         break;
104                 }
105         }
106         if (!conn)
107                 return NULL;
108         if (!serialcomm)
109                 serialcomm = SERIALCOMM;
110
111         serial = sr_serial_dev_inst_new(conn, serialcomm);
112
113         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
114                 return NULL;
115
116         serial_flush(serial);
117
118         /* This is how the vendor software scans for hardware. */
119         memset(packet, 0, PACKET_SIZE);
120         packet[0] = 0xaa;
121         packet[1] = 0xaa;
122         delay_ms = serial_timeout(serial, PACKET_SIZE);
123         if (serial_write_blocking(serial, packet, PACKET_SIZE, delay_ms) < PACKET_SIZE) {
124                 sr_err("Unable to write while probing for hardware.");
125                 return NULL;
126         }
127         /* The device responds with a 24-byte packet when it receives a packet.
128          * At 9600 baud, 300ms is long enough for it to have arrived. */
129         g_usleep(300 * 1000);
130         memset(packet, 0, PACKET_SIZE);
131         if ((ret = serial_read_nonblocking(serial, packet, PACKET_SIZE)) < 0) {
132                 sr_err("Unable to read while probing for hardware: %s",
133                                 sr_strerror(ret));
134                 return NULL;
135         }
136         if (ret != PACKET_SIZE || packet[0] != 0xaa || packet[1] != 0xaa) {
137                 /* Doesn't look like an Atten PPS. */
138                 return NULL;
139         }
140
141         model = NULL;
142         for (i = 0; i < ARRAY_SIZE(models); i++) {
143                 if (models[i].modelid == modelid) {
144                         model = &models[i];
145                         break;
146                 }
147         }
148         if (!model) {
149                 sr_err("Unknown modelid %d", modelid);
150                 return NULL;
151         }
152
153         sdi = g_malloc0(sizeof(struct sr_dev_inst));
154         sdi->status = SR_ST_INACTIVE;
155         sdi->vendor = g_strdup("Atten");
156         sdi->model = g_strdup(model->name);
157         sdi->inst_type = SR_INST_SERIAL;
158         sdi->conn = serial;
159         for (i = 0; i < MAX_CHANNELS; i++) {
160                 snprintf(channel, 10, "CH%d", i + 1);
161                 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel);
162                 cg = g_malloc(sizeof(struct sr_channel_group));
163                 cg->name = g_strdup(channel);
164                 cg->channels = g_slist_append(NULL, ch);
165                 cg->priv = NULL;
166                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
167         }
168
169         devc = g_malloc0(sizeof(struct dev_context));
170         devc->model = model;
171         devc->config = g_malloc0(sizeof(struct per_channel_config) * model->num_channels);
172         devc->delay_ms = delay_ms;
173         sdi->priv = devc;
174
175         serial_close(serial);
176
177         return std_scan_complete(di, g_slist_append(NULL, sdi));
178 }
179
180 static GSList *scan_3203(struct sr_dev_driver *di, GSList *options)
181 {
182         return scan(di, options, PPS_3203T_3S);
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         struct dev_context *devc;
189         struct sr_channel *ch;
190         int channel;
191
192         if (!sdi)
193                 return SR_ERR_ARG;
194
195         devc = sdi->priv;
196
197         if (!cg) {
198                 switch (key) {
199                 case SR_CONF_CHANNEL_CONFIG:
200                         *data = g_variant_new_string(channel_modes[devc->channel_mode]);
201                         break;
202                 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
203                         *data = g_variant_new_boolean(devc->over_current_protection);
204                         break;
205                 default:
206                         return SR_ERR_NA;
207                 }
208         } else {
209                 /* We only ever have one channel per channel group in this driver. */
210                 ch = cg->channels->data;
211                 channel = ch->index;
212
213                 switch (key) {
214                 case SR_CONF_VOLTAGE:
215                         *data = g_variant_new_double(devc->config[channel].output_voltage_last);
216                         break;
217                 case SR_CONF_VOLTAGE_TARGET:
218                         *data = g_variant_new_double(devc->config[channel].output_voltage_max);
219                         break;
220                 case SR_CONF_CURRENT:
221                         *data = g_variant_new_double(devc->config[channel].output_current_last);
222                         break;
223                 case SR_CONF_CURRENT_LIMIT:
224                         *data = g_variant_new_double(devc->config[channel].output_current_max);
225                         break;
226                 case SR_CONF_ENABLED:
227                         *data = g_variant_new_boolean(devc->config[channel].output_enabled);
228                         break;
229                 default:
230                         return SR_ERR_NA;
231                 }
232         }
233
234         return SR_OK;
235 }
236
237 static int config_set(uint32_t key, GVariant *data,
238         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
239 {
240         struct dev_context *devc;
241         struct sr_channel *ch;
242         gdouble dval;
243         int channel, ival;
244         gboolean bval;
245
246         devc = sdi->priv;
247
248         if (!cg) {
249                 switch (key) {
250                 case SR_CONF_CHANNEL_CONFIG:
251                         if ((ival = std_str_idx(data, ARRAY_AND_SIZE(channel_modes))) < 0)
252                                 return SR_ERR_ARG;
253                         if (devc->model->channel_modes && (1 << ival) == 0)
254                                 return SR_ERR_ARG; /* Not supported on this model. */
255                         if (ival == devc->channel_mode_set)
256                                 break; /* Nothing to do. */
257                         devc->channel_mode_set = ival;
258                         devc->config_dirty = TRUE;
259                         break;
260                 case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
261                         bval = g_variant_get_boolean(data);
262                         if (bval == devc->over_current_protection_set)
263                                 break; /* Nothing to do. */
264                         devc->over_current_protection_set = bval;
265                         devc->config_dirty = TRUE;
266                         break;
267                 default:
268                         return SR_ERR_NA;
269                 }
270         } else {
271                 /* We only ever have one channel per channel group in this driver. */
272                 ch = cg->channels->data;
273                 channel = ch->index;
274
275                 switch (key) {
276                 case SR_CONF_VOLTAGE_TARGET:
277                         dval = g_variant_get_double(data);
278                         if (dval < 0 || dval > devc->model->channels[channel].voltage[1])
279                                 return SR_ERR_ARG;
280                         devc->config[channel].output_voltage_max = dval;
281                         devc->config_dirty = TRUE;
282                         break;
283                 case SR_CONF_CURRENT_LIMIT:
284                         dval = g_variant_get_double(data);
285                         if (dval < 0 || dval > devc->model->channels[channel].current[1])
286                                 return SR_ERR_ARG;
287                         devc->config[channel].output_current_max = dval;
288                         devc->config_dirty = TRUE;
289                         break;
290                 case SR_CONF_ENABLED:
291                         bval = g_variant_get_boolean(data);
292                         if (bval == devc->config[channel].output_enabled_set)
293                                 break; /* Nothing to do. */
294                         devc->config[channel].output_enabled_set = bval;
295                         devc->config_dirty = TRUE;
296                         break;
297                 default:
298                         return SR_ERR_NA;
299                 }
300         }
301
302         return SR_OK;
303 }
304
305 static int config_list(uint32_t key, GVariant **data,
306         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
307 {
308         struct dev_context *devc;
309         struct sr_channel *ch;
310         int channel;
311
312         devc = (sdi) ? sdi->priv : NULL;
313
314         if (!cg) {
315                 switch (key) {
316                 case SR_CONF_SCAN_OPTIONS:
317                 case SR_CONF_DEVICE_OPTIONS:
318                         return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
319                 case SR_CONF_CHANNEL_CONFIG:
320                         if (!devc || !devc->model)
321                                 return SR_ERR_ARG;
322                         if (devc->model->channel_modes == CHANMODE_INDEPENDENT) {
323                                 /* The 1-channel models. */
324                                 *data = g_variant_new_strv(channel_modes, 1);
325                         } else {
326                                 /* The other models support all modes. */
327                                 *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes));
328                         }
329                         break;
330                 default:
331                         return SR_ERR_NA;
332                 }
333         } else {
334                 /* We only ever have one channel per channel group in this driver. */
335                 ch = cg->channels->data;
336                 channel = ch->index;
337
338                 switch (key) {
339                 case SR_CONF_DEVICE_OPTIONS:
340                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
341                         break;
342                 case SR_CONF_VOLTAGE_TARGET:
343                         if (!devc || !devc->model)
344                                 return SR_ERR_ARG;
345                         *data = std_gvar_min_max_step_array(devc->model->channels[channel].voltage);
346                         break;
347                 case SR_CONF_CURRENT_LIMIT:
348                         if (!devc || !devc->model)
349                                 return SR_ERR_ARG;
350                         *data = std_gvar_min_max_step_array(devc->model->channels[channel].current);
351                         break;
352                 default:
353                         return SR_ERR_NA;
354                 }
355         }
356
357         return SR_OK;
358 }
359
360 static int dev_close(struct sr_dev_inst *sdi)
361 {
362         struct dev_context *devc;
363
364         devc = sdi->priv;
365
366         if (devc->config_dirty)
367                 /* Some configuration changes were queued up but didn't
368                  * get sent to the device, likely because we were never
369                  * in acquisition mode. Send them out now. */
370                 send_config(sdi);
371
372         return std_serial_dev_close(sdi);
373 }
374
375 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
376 {
377         struct dev_context *devc;
378         struct sr_serial_dev_inst *serial;
379         uint8_t packet[PACKET_SIZE];
380
381         devc = sdi->priv;
382         memset(devc->packet, 0x44, PACKET_SIZE);
383         devc->packet_size = 0;
384
385         devc->acquisition_running = TRUE;
386
387         serial = sdi->conn;
388         serial_source_add(sdi->session, serial, G_IO_IN, 50,
389                         atten_pps3xxx_receive_data, (void *)sdi);
390         std_session_send_df_header(sdi);
391
392         /* Send a "channel" configuration packet now. */
393         memset(packet, 0, PACKET_SIZE);
394         packet[0] = 0xaa;
395         packet[1] = 0xaa;
396         send_packet(sdi, packet);
397
398         return SR_OK;
399 }
400
401 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
402 {
403         struct dev_context *devc;
404
405         devc = sdi->priv;
406         devc->acquisition_running = FALSE;
407
408         return SR_OK;
409 }
410
411 static struct sr_dev_driver atten_pps3203_driver_info = {
412         .name = "atten-pps3203",
413         .longname = "Atten PPS3203T-3S",
414         .api_version = 1,
415         .init = std_init,
416         .cleanup = std_cleanup,
417         .scan = scan_3203,
418         .dev_list = std_dev_list,
419         .dev_clear = std_dev_clear,
420         .config_get = config_get,
421         .config_set = config_set,
422         .config_list = config_list,
423         .dev_open = std_serial_dev_open,
424         .dev_close = dev_close,
425         .dev_acquisition_start = dev_acquisition_start,
426         .dev_acquisition_stop = dev_acquisition_stop,
427         .context = NULL,
428 };
429 SR_REGISTER_DEV_DRIVER(atten_pps3203_driver_info);