2 * This file is part of the libsigrok project.
4 * Copyright (C) 2019 Dave Buechi <db@pflutsch.ch>
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/>.
25 static const uint8_t channel_assignment[16][2] = {
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 */
45 SR_PRIV gboolean mastech_ms6514_packet_valid(const uint8_t *buf)
47 if ((buf[0] == 0x65) && (buf[1] == 0x14) &&
48 (buf[16] == 0x0D) && (buf[17] == 0x0A))
54 static uint64_t mastech_ms6514_flags(const uint8_t *buf, const uint8_t channel_index)
59 if ((buf[10] & 0x40) == 0x40)
60 flags |= SR_MQFLAG_HOLD;
62 if (channel_index == 0) {
63 if ((buf[11] & 0x03) > 0x01)
64 flags |= SR_MQFLAG_RELATIVE;
67 if (channel_index == 1) {
68 switch (buf[12] & 0x03) {
70 flags |= SR_MQFLAG_MAX;
73 flags |= SR_MQFLAG_MIN;
76 flags |= SR_MQFLAG_AVG;
84 static enum sr_unit mastech_ms6514_unit(const uint8_t *buf)
88 switch (buf[10] & 0x03) {
90 unit = SR_UNIT_CELSIUS;
93 unit = SR_UNIT_FAHRENHEIT;
96 unit = SR_UNIT_KELVIN;
99 unit = SR_UNIT_UNITLESS;
106 static uint8_t mastech_ms6514_channel_assignment(const uint8_t *buf, const uint8_t index)
108 return channel_assignment[((buf[12] & 0x03) << 2) + (buf[11] & 0x03)][index];
111 static uint8_t mastech_ms6514_data_source(const uint8_t *buf)
113 if ((buf[2] & 0x01) == 0x01)
114 return DATA_SOURCE_MEMORY;
116 return DATA_SOURCE_LIVE;
119 static float mastech_ms6514_temperature(const uint8_t *buf, const uint8_t channel_index, int *digits)
125 value = (buf[5 + channel_index * 2] << 8) + buf[6 + channel_index * 2];
126 modifiers = buf[11 + channel_index];
128 if ((modifiers & 0x80) == 0x80)
131 if ((modifiers & 0x08) == 0x08) {
136 if ((modifiers & 0x40) == 0x40)
142 static void mastech_ms6514_data(struct sr_dev_inst *sdi, const uint8_t *buf)
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;
156 if ((devc->data_source == DATA_SOURCE_MEMORY) && \
157 (mastech_ms6514_data_source(buf) == DATA_SOURCE_LIVE)) {
158 sr_dev_acquisition_stop(sdi);
162 for (i = 0; i < MASTECH_MS6514_NUM_CHANNELS; i++) {
163 ch = g_slist_nth_data(sdi->channels, i);
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);
175 analog.meaning->channels = g_slist_append(NULL,
176 g_slist_nth_data(sdi->channels,
177 mastech_ms6514_channel_assignment(buf, i)));
179 packet.type = SR_DF_ANALOG;
180 packet.payload = &analog;
181 sr_session_send(sdi, &packet);
182 g_slist_free(analog.meaning->channels);
185 sr_sw_limits_update_samples_read(&devc->limits, 1);
188 static const uint8_t *mastech_ms6514_parse_data(struct sr_dev_inst *sdi,
189 const uint8_t *buf, int len)
191 if (len < MASTECH_MS6514_FRAME_SIZE)
192 return NULL; /* Not enough data for a full packet. */
194 if (buf[0] != 0x65 || buf[1] != 0x14)
195 return buf + 1; /* Try to re-synchronize on a packet start. */
197 if (buf[16] != 0x0D || buf[17] != 0x0A)
198 return buf + MASTECH_MS6514_FRAME_SIZE; /* Valid start but no valid end -> skip. */
200 mastech_ms6514_data(sdi, buf);
202 return buf + MASTECH_MS6514_FRAME_SIZE;
205 SR_PRIV int mastech_ms6514_receive_data(int fd, int revents, void *cb_data)
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;
215 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
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);
223 sr_err("Serial port read error: %d.", len);
226 devc->buf_len += len;
228 /* Now look for packets in that data. */
230 end_ptr = ptr + devc->buf_len;
231 while ((next_ptr = mastech_ms6514_parse_data(sdi, ptr, end_ptr - ptr)))
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;
238 /* If buffer is full and no valid packet was found, wipe buffer. */
239 if (devc->buf_len >= sizeof(devc->buf)) {
244 if (sr_sw_limits_check(&devc->limits)) {
245 sr_dev_acquisition_stop(sdi);