]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/atten-pps3xxx/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / hardware / atten-pps3xxx / protocol.c
... / ...
CommitLineData
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
24static void dump_packet(const 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
36static 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 struct sr_analog_encoding encoding;
42 struct sr_analog_meaning meaning;
43 struct sr_analog_spec spec;
44 float value, data[MAX_CHANNELS];
45 int offset, i;
46
47 devc = sdi->priv;
48 dump_packet("received", devc->packet);
49
50 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
51
52 packet.type = SR_DF_ANALOG;
53 packet.payload = &analog;
54 analog.meaning->channels = sdi->channels;
55 analog.num_samples = 1;
56
57 analog.meaning->mq = SR_MQ_VOLTAGE;
58 analog.meaning->unit = SR_UNIT_VOLT;
59 analog.meaning->mqflags = SR_MQFLAG_DC;
60 analog.data = data;
61 for (i = 0; i < devc->model->num_channels; i++) {
62 offset = 2 + i * 4;
63 value = ((devc->packet[offset] << 8) + devc->packet[offset + 1]) / 100.0;
64 ((float *)analog.data)[i] = value;
65 devc->config[i].output_voltage_last = value;
66 }
67 sr_session_send(sdi, &packet);
68
69 analog.meaning->mq = SR_MQ_CURRENT;
70 analog.meaning->unit = SR_UNIT_AMPERE;
71 analog.meaning->mqflags = 0;
72 analog.data = data;
73 for (i = 0; i < devc->model->num_channels; i++) {
74 offset = 4 + i * 4;
75 value = ((devc->packet[offset] << 8) + devc->packet[offset + 1]) / 1000.0;
76 ((float *)analog.data)[i] = value;
77 devc->config[i].output_current_last = value;
78 }
79 sr_session_send(sdi, &packet);
80
81 for (i = 0; i < devc->model->num_channels; i++)
82 devc->config[i].output_enabled = (devc->packet[15] & (1 << i)) ? TRUE : FALSE;
83
84 devc->over_current_protection = devc->packet[18] ? TRUE : FALSE;
85 if (devc->packet[19] < 3)
86 devc->channel_mode = devc->packet[19];
87
88}
89
90SR_PRIV void send_packet(const struct sr_dev_inst *sdi, uint8_t *packet)
91{
92 struct dev_context *devc;
93 struct sr_serial_dev_inst *serial;
94
95 devc = sdi->priv;
96 serial = sdi->conn;
97 if (serial_write_blocking(serial, packet, PACKET_SIZE, devc->delay_ms) < PACKET_SIZE)
98 sr_dbg("Failed to send packet.");
99 dump_packet("sent", packet);
100}
101
102SR_PRIV void send_config(const struct sr_dev_inst *sdi)
103{
104 struct dev_context *devc;
105 uint8_t packet[PACKET_SIZE];
106 int value, offset, i;
107
108 devc = sdi->priv;
109 memset(packet, 0, PACKET_SIZE);
110 packet[0] = 0xaa;
111 packet[1] = 0x20;
112 packet[14] = 0x01;
113 packet[16] = 0x01;
114 for (i = 0; i < devc->model->num_channels; i++) {
115 offset = 2 + i * 4;
116 value = devc->config[i].output_voltage_max * 100;
117 packet[offset] = (value >> 8) & 0xff;
118 packet[offset + 1] = value & 0xff;
119 value = devc->config[i].output_current_max * 1000;
120 packet[offset + 2] = (value >> 8) & 0xff;
121 packet[offset + 3] = value & 0xff;
122 if (devc->config[i].output_enabled_set)
123 packet[15] |= 1 << i;
124 }
125 packet[18] = devc->over_current_protection_set ? 1 : 0;
126 packet[19] = devc->channel_mode_set;
127 /* Checksum. */
128 value = 0;
129 for (i = 0; i < PACKET_SIZE - 1; i++)
130 value += packet[i];
131 packet[i] = value & 0xff;
132 send_packet(sdi, packet);
133 devc->config_dirty = FALSE;
134
135}
136
137SR_PRIV int atten_pps3xxx_receive_data(int fd, int revents, void *cb_data)
138{
139 struct dev_context *devc;
140 const struct sr_dev_inst *sdi;
141 struct sr_serial_dev_inst *serial;
142 unsigned char c;
143
144 (void)fd;
145
146 if (!(sdi = cb_data))
147 return TRUE;
148
149 if (!(devc = sdi->priv))
150 return TRUE;
151
152 serial = sdi->conn;
153 if (revents == G_IO_IN) {
154 if (serial_read_nonblocking(serial, &c, 1) < 0)
155 return TRUE;
156 devc->packet[devc->packet_size++] = c;
157 if (devc->packet_size == PACKET_SIZE) {
158 handle_packet(sdi);
159 devc->packet_size = 0;
160 if (devc->acquisition_running)
161 send_config(sdi);
162 else {
163 serial_source_remove(sdi->session, serial);
164 std_session_send_df_end(sdi);
165 }
166 }
167 }
168
169 return TRUE;
170}