]>
Commit | Line | Data |
---|---|---|
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 | ||
6ec6c43b | 20 | #include <config.h> |
33c40990 | 21 | #include <string.h> |
fa0d6afe BV |
22 | #include "protocol.h" |
23 | ||
2c240774 | 24 | static void dump_packet(const char *msg, uint8_t *packet) |
33c40990 BV |
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; | |
67eb7c6b UH |
40 | struct sr_datafeed_analog analog; |
41 | struct sr_analog_encoding encoding; | |
42 | struct sr_analog_meaning meaning; | |
43 | struct sr_analog_spec spec; | |
33c40990 BV |
44 | float value, data[MAX_CHANNELS]; |
45 | int offset, i; | |
46 | ||
47 | devc = sdi->priv; | |
48 | dump_packet("received", devc->packet); | |
67eb7c6b | 49 | |
869c8375 | 50 | /* Note: digits/spec_digits will be overridden later. */ |
67eb7c6b UH |
51 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
52 | ||
53 | packet.type = SR_DF_ANALOG; | |
33c40990 | 54 | packet.payload = &analog; |
67eb7c6b | 55 | analog.meaning->channels = sdi->channels; |
33c40990 BV |
56 | analog.num_samples = 1; |
57 | ||
67eb7c6b UH |
58 | analog.meaning->mq = SR_MQ_VOLTAGE; |
59 | analog.meaning->unit = SR_UNIT_VOLT; | |
60 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
fad810b5 AJ |
61 | analog.encoding->digits = 2; |
62 | analog.spec->spec_digits = 2; | |
33c40990 BV |
63 | analog.data = data; |
64 | for (i = 0; i < devc->model->num_channels; i++) { | |
65 | offset = 2 + i * 4; | |
66 | value = ((devc->packet[offset] << 8) + devc->packet[offset + 1]) / 100.0; | |
67eb7c6b | 67 | ((float *)analog.data)[i] = value; |
33c40990 BV |
68 | devc->config[i].output_voltage_last = value; |
69 | } | |
70 | sr_session_send(sdi, &packet); | |
71 | ||
67eb7c6b UH |
72 | analog.meaning->mq = SR_MQ_CURRENT; |
73 | analog.meaning->unit = SR_UNIT_AMPERE; | |
74 | analog.meaning->mqflags = 0; | |
fad810b5 AJ |
75 | analog.encoding->digits = 3; |
76 | analog.spec->spec_digits = 3; | |
33c40990 BV |
77 | analog.data = data; |
78 | for (i = 0; i < devc->model->num_channels; i++) { | |
79 | offset = 4 + i * 4; | |
80 | value = ((devc->packet[offset] << 8) + devc->packet[offset + 1]) / 1000.0; | |
67eb7c6b | 81 | ((float *)analog.data)[i] = value; |
33c40990 BV |
82 | devc->config[i].output_current_last = value; |
83 | } | |
84 | sr_session_send(sdi, &packet); | |
85 | ||
86 | for (i = 0; i < devc->model->num_channels; i++) | |
87 | devc->config[i].output_enabled = (devc->packet[15] & (1 << i)) ? TRUE : FALSE; | |
88 | ||
89 | devc->over_current_protection = devc->packet[18] ? TRUE : FALSE; | |
90 | if (devc->packet[19] < 3) | |
91 | devc->channel_mode = devc->packet[19]; | |
92 | ||
93 | } | |
94 | ||
95 | SR_PRIV void send_packet(const struct sr_dev_inst *sdi, uint8_t *packet) | |
96 | { | |
945cfd4f | 97 | struct dev_context *devc; |
33c40990 BV |
98 | struct sr_serial_dev_inst *serial; |
99 | ||
945cfd4f | 100 | devc = sdi->priv; |
33c40990 | 101 | serial = sdi->conn; |
2eb1612d | 102 | if (serial_write_blocking(serial, packet, PACKET_SIZE, devc->delay_ms) < PACKET_SIZE) |
081c214e | 103 | sr_dbg("Failed to send packet."); |
33c40990 BV |
104 | dump_packet("sent", packet); |
105 | } | |
106 | ||
107 | SR_PRIV void send_config(const struct sr_dev_inst *sdi) | |
108 | { | |
109 | struct dev_context *devc; | |
110 | uint8_t packet[PACKET_SIZE]; | |
111 | int value, offset, i; | |
112 | ||
113 | devc = sdi->priv; | |
114 | memset(packet, 0, PACKET_SIZE); | |
115 | packet[0] = 0xaa; | |
116 | packet[1] = 0x20; | |
117 | packet[14] = 0x01; | |
118 | packet[16] = 0x01; | |
119 | for (i = 0; i < devc->model->num_channels; i++) { | |
120 | offset = 2 + i * 4; | |
121 | value = devc->config[i].output_voltage_max * 100; | |
122 | packet[offset] = (value >> 8) & 0xff; | |
123 | packet[offset + 1] = value & 0xff; | |
124 | value = devc->config[i].output_current_max * 1000; | |
125 | packet[offset + 2] = (value >> 8) & 0xff; | |
126 | packet[offset + 3] = value & 0xff; | |
81c9e1a0 | 127 | if (devc->config[i].output_enabled_set) |
33c40990 BV |
128 | packet[15] |= 1 << i; |
129 | } | |
81c9e1a0 BV |
130 | packet[18] = devc->over_current_protection_set ? 1 : 0; |
131 | packet[19] = devc->channel_mode_set; | |
33c40990 BV |
132 | /* Checksum. */ |
133 | value = 0; | |
134 | for (i = 0; i < PACKET_SIZE - 1; i++) | |
135 | value += packet[i]; | |
136 | packet[i] = value & 0xff; | |
137 | send_packet(sdi, packet); | |
ab988ecb | 138 | devc->config_dirty = FALSE; |
33c40990 BV |
139 | |
140 | } | |
141 | ||
fa0d6afe BV |
142 | SR_PRIV int atten_pps3xxx_receive_data(int fd, int revents, void *cb_data) |
143 | { | |
fa0d6afe | 144 | struct dev_context *devc; |
33c40990 BV |
145 | const struct sr_dev_inst *sdi; |
146 | struct sr_serial_dev_inst *serial; | |
33c40990 | 147 | unsigned char c; |
fa0d6afe BV |
148 | |
149 | (void)fd; | |
150 | ||
151 | if (!(sdi = cb_data)) | |
152 | return TRUE; | |
153 | ||
154 | if (!(devc = sdi->priv)) | |
155 | return TRUE; | |
156 | ||
33c40990 | 157 | serial = sdi->conn; |
fa0d6afe | 158 | if (revents == G_IO_IN) { |
33c40990 BV |
159 | if (serial_read_nonblocking(serial, &c, 1) < 0) |
160 | return TRUE; | |
161 | devc->packet[devc->packet_size++] = c; | |
162 | if (devc->packet_size == PACKET_SIZE) { | |
163 | handle_packet(sdi); | |
164 | devc->packet_size = 0; | |
165 | if (devc->acquisition_running) | |
166 | send_config(sdi); | |
167 | else { | |
102f1239 | 168 | serial_source_remove(sdi->session, serial); |
bee2b016 | 169 | std_session_send_df_end(sdi); |
33c40990 BV |
170 | } |
171 | } | |
fa0d6afe BV |
172 | } |
173 | ||
174 | return TRUE; | |
175 | } |