]> sigrok.org Git - libsigrok.git/blame - src/hardware/mastech-ms6514/protocol.c
uni-t-ut181a: silence compiler warning, use of uninitialized variable
[libsigrok.git] / src / hardware / mastech-ms6514 / protocol.c
CommitLineData
23669c3d
DB
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2019 Dave Buechi <db@pflutsch.ch>
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>
c810ad60 21#include <string.h>
23669c3d
DB
22#include <math.h>
23#include "protocol.h"
24
25static const uint8_t channel_assignment[16][2] = {
26 /* MAIN AUX */
27 {0, 1}, /* T1 T2 */
28 {1, 0}, /* T2 T1 */
29 {2, 0}, /* T1-T2 T1 */
30 {2, 1}, /* T1-T2 T2 */
31 {0, 0}, /* T1 T1 MAX */
32 {1, 1}, /* T2 T2 MAX */
33 {2, 2}, /* T1-T2 T1-T2 MAX */
34 {2, 2}, /* T1-T2 T1-T2 MAX */
35 {0, 0}, /* T1 T1 MIN */
36 {1, 1}, /* T2 T2 MIN */
37 {2, 2}, /* T1-T2 T1-T2 MIN */
38 {2, 2}, /* T1-T2 T1-T2 MIN */
39 {0, 0}, /* T1 T1 AVG */
40 {1, 1}, /* T2 T2 AVG */
41 {2, 2}, /* T1-T2 T1-T2 AVG */
42 {2, 2}, /* T1-T2 T1-T2 AVG */
43};
44
45SR_PRIV gboolean mastech_ms6514_packet_valid(const uint8_t *buf)
46{
47 if ((buf[0] == 0x65) && (buf[1] == 0x14) &&
48 (buf[16] == 0x0D) && (buf[17] == 0x0A))
49 return TRUE;
50
51 return FALSE;
52}
53
54static uint64_t mastech_ms6514_flags(const uint8_t *buf, const uint8_t channel_index)
55{
56 uint64_t flags;
57
58 flags = 0;
59 if ((buf[10] & 0x40) == 0x40)
60 flags |= SR_MQFLAG_HOLD;
61
62 if (channel_index == 0) {
63 if ((buf[11] & 0x03) > 0x01)
64 flags |= SR_MQFLAG_RELATIVE;
65 }
66
67 if (channel_index == 1) {
68 switch (buf[12] & 0x03) {
69 case 0x01:
70 flags |= SR_MQFLAG_MAX;
71 break;
72 case 0x02:
73 flags |= SR_MQFLAG_MIN;
74 break;
75 case 0x03:
76 flags |= SR_MQFLAG_AVG;
77 break;
78 }
79 }
80
81 return flags;
82}
83
84static enum sr_unit mastech_ms6514_unit(const uint8_t *buf)
85{
86 enum sr_unit unit;
87
88 switch (buf[10] & 0x03) {
89 case 0x01:
90 unit = SR_UNIT_CELSIUS;
91 break;
92 case 0x02:
93 unit = SR_UNIT_FAHRENHEIT;
94 break;
95 case 0x03:
96 unit = SR_UNIT_KELVIN;
97 break;
98 default:
99 unit = SR_UNIT_UNITLESS;
100 break;
101 }
102
103 return unit;
104}
105
106static uint8_t mastech_ms6514_channel_assignment(const uint8_t *buf, const uint8_t index)
107{
108 return channel_assignment[((buf[12] & 0x03) << 2) + (buf[11] & 0x03)][index];
109}
110
111static uint8_t mastech_ms6514_data_source(const uint8_t *buf)
112{
113 if ((buf[2] & 0x01) == 0x01)
114 return DATA_SOURCE_MEMORY;
115 else
116 return DATA_SOURCE_LIVE;
117}
118
119static float mastech_ms6514_temperature(const uint8_t *buf, const uint8_t channel_index, int *digits)
120{
121 float value;
122 uint8_t modifiers;
123
124 *digits = 0;
125 value = (buf[5 + channel_index * 2] << 8) + buf[6 + channel_index * 2];
126 modifiers = buf[11 + channel_index];
127
128 if ((modifiers & 0x80) == 0x80)
129 value = -value;
130
131 if ((modifiers & 0x08) == 0x08) {
132 value /= 10.0;
133 *digits = 1;
134 }
135
136 if ((modifiers & 0x40) == 0x40)
137 value = INFINITY;
138
139 return value;
140}
141
142static void mastech_ms6514_data(struct sr_dev_inst *sdi, const uint8_t *buf)
143{
144 struct dev_context *devc;
145 struct sr_datafeed_packet packet;
146 struct sr_datafeed_analog analog;
147 struct sr_analog_encoding encoding;
148 struct sr_analog_meaning meaning;
149 struct sr_analog_spec spec;
150 struct sr_channel *ch;
151 float value;
152 int i, digits;
153
154 devc = sdi->priv;
155
156 if ((devc->data_source == DATA_SOURCE_MEMORY) && \
157 (mastech_ms6514_data_source(buf) == DATA_SOURCE_LIVE)) {
158 sr_dev_acquisition_stop(sdi);
159 return;
160 }
161
162 for (i = 0; i < MASTECH_MS6514_NUM_CHANNELS; i++) {
163 ch = g_slist_nth_data(sdi->channels, i);
164 if (!ch->enabled)
165 continue;
166
167 value = mastech_ms6514_temperature(buf, i, &digits);
168 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
169 analog.num_samples = 1;
170 analog.data = &value;
171 analog.meaning->mq = SR_MQ_TEMPERATURE;
172 analog.meaning->unit = mastech_ms6514_unit(buf);
173 analog.meaning->mqflags = mastech_ms6514_flags(buf, i);
174
175 analog.meaning->channels = g_slist_append(NULL,
176 g_slist_nth_data(sdi->channels,
177 mastech_ms6514_channel_assignment(buf, i)));
178
179 packet.type = SR_DF_ANALOG;
180 packet.payload = &analog;
181 sr_session_send(sdi, &packet);
182 g_slist_free(analog.meaning->channels);
183 }
184
185 sr_sw_limits_update_samples_read(&devc->limits, 1);
186}
187
188static const uint8_t *mastech_ms6514_parse_data(struct sr_dev_inst *sdi,
189 const uint8_t *buf, int len)
190{
191 if (len < MASTECH_MS6514_FRAME_SIZE)
192 return NULL; /* Not enough data for a full packet. */
193
194 if (buf[0] != 0x65 || buf[1] != 0x14)
195 return buf + 1; /* Try to re-synchronize on a packet start. */
196
197 if (buf[16] != 0x0D || buf[17] != 0x0A)
198 return buf + MASTECH_MS6514_FRAME_SIZE; /* Valid start but no valid end -> skip. */
199
200 mastech_ms6514_data(sdi, buf);
201
202 return buf + MASTECH_MS6514_FRAME_SIZE;
203}
204
205SR_PRIV int mastech_ms6514_receive_data(int fd, int revents, void *cb_data)
206{
207 struct sr_dev_inst *sdi;
208 struct dev_context *devc;
209 struct sr_serial_dev_inst *serial;
210 const uint8_t *ptr, *next_ptr, *end_ptr;
211 int len;
212
213 (void)fd;
214
215 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
216 return TRUE;
217 serial = sdi->conn;
218
219 /* Try to get as much data as the buffer can hold. */
220 len = sizeof(devc->buf) - devc->buf_len;
221 len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len);
222 if (len < 1) {
223 sr_err("Serial port read error: %d.", len);
224 return FALSE;
225 }
226 devc->buf_len += len;
227
228 /* Now look for packets in that data. */
229 ptr = devc->buf;
230 end_ptr = ptr + devc->buf_len;
231 while ((next_ptr = mastech_ms6514_parse_data(sdi, ptr, end_ptr - ptr)))
232 ptr = next_ptr;
233
234 /* If we have any data left, move it to the beginning of our buffer. */
235 memmove(devc->buf, ptr, end_ptr - ptr);
236 devc->buf_len -= ptr - devc->buf;
237
238 /* If buffer is full and no valid packet was found, wipe buffer. */
239 if (devc->buf_len >= sizeof(devc->buf)) {
240 devc->buf_len = 0;
241 return FALSE;
242 }
243
244 if (sr_sw_limits_check(&devc->limits)) {
245 sr_dev_acquisition_stop(sdi);
246 return TRUE;
247 }
248
249 return TRUE;
250}