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