]> sigrok.org Git - libsigrok.git/blob - hardware/atten-pps3xxx/protocol.c
build: Portability fixes.
[libsigrok.git] / hardware / atten-pps3xxx / protocol.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 <string.h>
21 #include <errno.h>
22 #include "protocol.h"
23
24 static void dump_packet(char *msg, uint8_t *packet)
25 {
26         int i;
27         char str[128];
28
29         str[0] = 0;
30         for (i = 0; i < PACKET_SIZE; i++)
31                 sprintf(str + strlen(str), "%.2x ", packet[i]);
32         sr_dbg("%s: %s", msg, str);
33
34 }
35
36 static void handle_packet(const struct sr_dev_inst *sdi)
37 {
38         struct dev_context *devc;
39         struct sr_datafeed_packet packet;
40         struct sr_datafeed_analog analog;
41         float value, data[MAX_CHANNELS];
42         int offset, i;
43
44         devc = sdi->priv;
45         dump_packet("received", devc->packet);
46         packet.type = SR_DF_ANALOG;
47         packet.payload = &analog;
48         analog.channels = sdi->channels;
49         analog.num_samples = 1;
50
51         analog.mq = SR_MQ_VOLTAGE;
52         analog.unit = SR_UNIT_VOLT;
53         analog.mqflags = SR_MQFLAG_DC;
54         analog.data = data;
55         for (i = 0; i < devc->model->num_channels; i++) {
56                 offset = 2 + i * 4;
57                 value = ((devc->packet[offset] << 8) + devc->packet[offset + 1]) / 100.0;
58                 analog.data[i] = value;
59                 devc->config[i].output_voltage_last = value;
60         }
61         sr_session_send(sdi, &packet);
62
63         analog.mq = SR_MQ_CURRENT;
64         analog.unit = SR_UNIT_AMPERE;
65         analog.mqflags = 0;
66         analog.data = data;
67         for (i = 0; i < devc->model->num_channels; i++) {
68                 offset = 4 + i * 4;
69                 value = ((devc->packet[offset] << 8) + devc->packet[offset + 1]) / 1000.0;
70                 analog.data[i] = value;
71                 devc->config[i].output_current_last = value;
72         }
73         sr_session_send(sdi, &packet);
74
75         for (i = 0; i < devc->model->num_channels; i++)
76                 devc->config[i].output_enabled = (devc->packet[15] & (1 << i)) ? TRUE : FALSE;
77
78         devc->over_current_protection = devc->packet[18] ? TRUE : FALSE;
79         if (devc->packet[19] < 3)
80                 devc->channel_mode = devc->packet[19];
81
82 }
83
84 SR_PRIV void send_packet(const struct sr_dev_inst *sdi, uint8_t *packet)
85 {
86         struct sr_serial_dev_inst *serial;
87
88         serial = sdi->conn;
89         if (serial_write(serial, packet, PACKET_SIZE) == -1)
90                 sr_dbg("Failed to send packet: %s", strerror(errno));
91         dump_packet("sent", packet);
92 }
93
94 SR_PRIV void send_config(const struct sr_dev_inst *sdi)
95 {
96         struct dev_context *devc;
97         uint8_t packet[PACKET_SIZE];
98         int value, offset, i;
99
100         devc = sdi->priv;
101         memset(packet, 0, PACKET_SIZE);
102         packet[0] = 0xaa;
103         packet[1] = 0x20;
104         packet[14] = 0x01;
105         packet[16] = 0x01;
106         for (i = 0; i < devc->model->num_channels; i++) {
107                 offset = 2 + i * 4;
108                 value = devc->config[i].output_voltage_max * 100;
109                 packet[offset] = (value >> 8) & 0xff;
110                 packet[offset + 1] = value & 0xff;
111                 value = devc->config[i].output_current_max * 1000;
112                 packet[offset + 2] = (value >> 8) & 0xff;
113                 packet[offset + 3] = value & 0xff;
114                 if (devc->config[i].output_enabled_set)
115                         packet[15] |= 1 << i;
116         }
117         packet[18] = devc->over_current_protection_set ? 1 : 0;
118         packet[19] = devc->channel_mode_set;
119         /* Checksum. */
120         value = 0;
121         for (i = 0; i < PACKET_SIZE - 1; i++)
122                 value += packet[i];
123         packet[i] = value & 0xff;
124         send_packet(sdi, packet);
125         devc->config_dirty = FALSE;
126
127 }
128
129 SR_PRIV int atten_pps3xxx_receive_data(int fd, int revents, void *cb_data)
130 {
131         struct dev_context *devc;
132         const struct sr_dev_inst *sdi;
133         struct sr_serial_dev_inst *serial;
134         struct sr_datafeed_packet packet;
135         unsigned char c;
136
137         (void)fd;
138
139         if (!(sdi = cb_data))
140                 return TRUE;
141
142         if (!(devc = sdi->priv))
143                 return TRUE;
144
145         serial = sdi->conn;
146         if (revents == G_IO_IN) {
147                 if (serial_read_nonblocking(serial, &c, 1) < 0)
148                         return TRUE;
149                 devc->packet[devc->packet_size++] = c;
150                 if (devc->packet_size == PACKET_SIZE) {
151                         handle_packet(sdi);
152                         devc->packet_size = 0;
153                         if (devc->acquisition_running)
154                                 send_config(sdi);
155                         else {
156                                 serial_source_remove(sdi->session, serial);
157                                 packet.type = SR_DF_END;
158                                 sr_session_send(sdi, &packet);
159                         }
160                 }
161         }
162
163         return TRUE;
164 }
165