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/>.
24 static const uint8_t channel_assignment[16][2] = {
28 {2, 0}, /* T1-T2 T1 */
29 {2, 1}, /* T1-T2 T2 */
30 {0, 0}, /* T1 T1 MAX */
31 {1, 1}, /* T2 T2 MAX */
32 {2, 2}, /* T1-T2 T1-T2 MAX */
33 {2, 2}, /* T1-T2 T1-T2 MAX */
34 {0, 0}, /* T1 T1 MIN */
35 {1, 1}, /* T2 T2 MIN */
36 {2, 2}, /* T1-T2 T1-T2 MIN */
37 {2, 2}, /* T1-T2 T1-T2 MIN */
38 {0, 0}, /* T1 T1 AVG */
39 {1, 1}, /* T2 T2 AVG */
40 {2, 2}, /* T1-T2 T1-T2 AVG */
41 {2, 2}, /* T1-T2 T1-T2 AVG */
44 SR_PRIV gboolean mastech_ms6514_packet_valid(const uint8_t *buf)
46 if ((buf[0] == 0x65) && (buf[1] == 0x14) &&
47 (buf[16] == 0x0D) && (buf[17] == 0x0A))
53 static uint64_t mastech_ms6514_flags(const uint8_t *buf, const uint8_t channel_index)
58 if ((buf[10] & 0x40) == 0x40)
59 flags |= SR_MQFLAG_HOLD;
61 if (channel_index == 0) {
62 if ((buf[11] & 0x03) > 0x01)
63 flags |= SR_MQFLAG_RELATIVE;
66 if (channel_index == 1) {
67 switch (buf[12] & 0x03) {
69 flags |= SR_MQFLAG_MAX;
72 flags |= SR_MQFLAG_MIN;
75 flags |= SR_MQFLAG_AVG;
83 static enum sr_unit mastech_ms6514_unit(const uint8_t *buf)
87 switch (buf[10] & 0x03) {
89 unit = SR_UNIT_CELSIUS;
92 unit = SR_UNIT_FAHRENHEIT;
95 unit = SR_UNIT_KELVIN;
98 unit = SR_UNIT_UNITLESS;
105 static uint8_t mastech_ms6514_channel_assignment(const uint8_t *buf, const uint8_t index)
107 return channel_assignment[((buf[12] & 0x03) << 2) + (buf[11] & 0x03)][index];
110 static uint8_t mastech_ms6514_data_source(const uint8_t *buf)
112 if ((buf[2] & 0x01) == 0x01)
113 return DATA_SOURCE_MEMORY;
115 return DATA_SOURCE_LIVE;
118 static float mastech_ms6514_temperature(const uint8_t *buf, const uint8_t channel_index, int *digits)
124 value = (buf[5 + channel_index * 2] << 8) + buf[6 + channel_index * 2];
125 modifiers = buf[11 + channel_index];
127 if ((modifiers & 0x80) == 0x80)
130 if ((modifiers & 0x08) == 0x08) {
135 if ((modifiers & 0x40) == 0x40)
141 static void mastech_ms6514_data(struct sr_dev_inst *sdi, const uint8_t *buf)
143 struct dev_context *devc;
144 struct sr_datafeed_packet packet;
145 struct sr_datafeed_analog analog;
146 struct sr_analog_encoding encoding;
147 struct sr_analog_meaning meaning;
148 struct sr_analog_spec spec;
149 struct sr_channel *ch;
155 if ((devc->data_source == DATA_SOURCE_MEMORY) && \
156 (mastech_ms6514_data_source(buf) == DATA_SOURCE_LIVE)) {
157 sr_dev_acquisition_stop(sdi);
161 for (i = 0; i < MASTECH_MS6514_NUM_CHANNELS; i++) {
162 ch = g_slist_nth_data(sdi->channels, i);
166 value = mastech_ms6514_temperature(buf, i, &digits);
167 sr_analog_init(&analog, &encoding, &meaning, &spec, digits);
168 analog.num_samples = 1;
169 analog.data = &value;
170 analog.meaning->mq = SR_MQ_TEMPERATURE;
171 analog.meaning->unit = mastech_ms6514_unit(buf);
172 analog.meaning->mqflags = mastech_ms6514_flags(buf, i);
174 analog.meaning->channels = g_slist_append(NULL,
175 g_slist_nth_data(sdi->channels,
176 mastech_ms6514_channel_assignment(buf, i)));
178 packet.type = SR_DF_ANALOG;
179 packet.payload = &analog;
180 sr_session_send(sdi, &packet);
181 g_slist_free(analog.meaning->channels);
184 sr_sw_limits_update_samples_read(&devc->limits, 1);
187 static const uint8_t *mastech_ms6514_parse_data(struct sr_dev_inst *sdi,
188 const uint8_t *buf, int len)
190 if (len < MASTECH_MS6514_FRAME_SIZE)
191 return NULL; /* Not enough data for a full packet. */
193 if (buf[0] != 0x65 || buf[1] != 0x14)
194 return buf + 1; /* Try to re-synchronize on a packet start. */
196 if (buf[16] != 0x0D || buf[17] != 0x0A)
197 return buf + MASTECH_MS6514_FRAME_SIZE; /* Valid start but no valid end -> skip. */
199 mastech_ms6514_data(sdi, buf);
201 return buf + MASTECH_MS6514_FRAME_SIZE;
204 SR_PRIV int mastech_ms6514_receive_data(int fd, int revents, void *cb_data)
206 struct sr_dev_inst *sdi;
207 struct dev_context *devc;
208 struct sr_serial_dev_inst *serial;
209 const uint8_t *ptr, *next_ptr, *end_ptr;
214 if (!(sdi = cb_data) || !(devc = sdi->priv) || revents != G_IO_IN)
218 /* Try to get as much data as the buffer can hold. */
219 len = sizeof(devc->buf) - devc->buf_len;
220 len = serial_read_nonblocking(serial, devc->buf + devc->buf_len, len);
222 sr_err("Serial port read error: %d.", len);
225 devc->buf_len += len;
227 /* Now look for packets in that data. */
229 end_ptr = ptr + devc->buf_len;
230 while ((next_ptr = mastech_ms6514_parse_data(sdi, ptr, end_ptr - ptr)))
233 /* If we have any data left, move it to the beginning of our buffer. */
234 memmove(devc->buf, ptr, end_ptr - ptr);
235 devc->buf_len -= ptr - devc->buf;
237 /* If buffer is full and no valid packet was found, wipe buffer. */
238 if (devc->buf_len >= sizeof(devc->buf)) {
243 if (sr_sw_limits_check(&devc->limits)) {
244 sr_dev_acquisition_stop(sdi);