2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
24 static void dump_packet(const char *msg, uint8_t *packet)
30 for (i = 0; i < PACKET_SIZE; i++)
31 sprintf(str + strlen(str), "%.2x ", packet[i]);
32 sr_dbg("%s: %s", msg, str);
36 static void handle_packet(const struct sr_dev_inst *sdi)
38 struct dev_context *devc;
39 struct sr_datafeed_packet packet;
40 struct sr_datafeed_analog_old analog;
41 float value, data[MAX_CHANNELS];
45 dump_packet("received", devc->packet);
46 packet.type = SR_DF_ANALOG_OLD;
47 packet.payload = &analog;
48 analog.channels = sdi->channels;
49 analog.num_samples = 1;
51 analog.mq = SR_MQ_VOLTAGE;
52 analog.unit = SR_UNIT_VOLT;
53 analog.mqflags = SR_MQFLAG_DC;
55 for (i = 0; i < devc->model->num_channels; i++) {
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;
61 sr_session_send(sdi, &packet);
63 analog.mq = SR_MQ_CURRENT;
64 analog.unit = SR_UNIT_AMPERE;
67 for (i = 0; i < devc->model->num_channels; i++) {
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;
73 sr_session_send(sdi, &packet);
75 for (i = 0; i < devc->model->num_channels; i++)
76 devc->config[i].output_enabled = (devc->packet[15] & (1 << i)) ? TRUE : FALSE;
78 devc->over_current_protection = devc->packet[18] ? TRUE : FALSE;
79 if (devc->packet[19] < 3)
80 devc->channel_mode = devc->packet[19];
84 SR_PRIV void send_packet(const struct sr_dev_inst *sdi, uint8_t *packet)
86 struct dev_context *devc;
87 struct sr_serial_dev_inst *serial;
91 if (serial_write_blocking(serial, packet, PACKET_SIZE, devc->delay_ms) < PACKET_SIZE)
92 sr_dbg("Failed to send packet.");
93 dump_packet("sent", packet);
96 SR_PRIV void send_config(const struct sr_dev_inst *sdi)
98 struct dev_context *devc;
99 uint8_t packet[PACKET_SIZE];
100 int value, offset, i;
103 memset(packet, 0, PACKET_SIZE);
108 for (i = 0; i < devc->model->num_channels; i++) {
110 value = devc->config[i].output_voltage_max * 100;
111 packet[offset] = (value >> 8) & 0xff;
112 packet[offset + 1] = value & 0xff;
113 value = devc->config[i].output_current_max * 1000;
114 packet[offset + 2] = (value >> 8) & 0xff;
115 packet[offset + 3] = value & 0xff;
116 if (devc->config[i].output_enabled_set)
117 packet[15] |= 1 << i;
119 packet[18] = devc->over_current_protection_set ? 1 : 0;
120 packet[19] = devc->channel_mode_set;
123 for (i = 0; i < PACKET_SIZE - 1; i++)
125 packet[i] = value & 0xff;
126 send_packet(sdi, packet);
127 devc->config_dirty = FALSE;
131 SR_PRIV int atten_pps3xxx_receive_data(int fd, int revents, void *cb_data)
133 struct dev_context *devc;
134 const struct sr_dev_inst *sdi;
135 struct sr_serial_dev_inst *serial;
136 struct sr_datafeed_packet packet;
141 if (!(sdi = cb_data))
144 if (!(devc = sdi->priv))
148 if (revents == G_IO_IN) {
149 if (serial_read_nonblocking(serial, &c, 1) < 0)
151 devc->packet[devc->packet_size++] = c;
152 if (devc->packet_size == PACKET_SIZE) {
154 devc->packet_size = 0;
155 if (devc->acquisition_running)
158 serial_source_remove(sdi->session, serial);
159 packet.type = SR_DF_END;
160 sr_session_send(sdi, &packet);