]> sigrok.org Git - libsigrok.git/blame - src/hardware/tondaj-sl-814/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / tondaj-sl-814 / protocol.c
CommitLineData
aa2af324 1/*
50985c20 2 * This file is part of the libsigrok project.
aa2af324
UH
3 *
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
aa2af324
UH
18 */
19
6ec6c43b 20#include <config.h>
c073af80 21#include <string.h>
aa2af324 22#include <glib.h>
c1aae900 23#include <libsigrok/libsigrok.h>
aa2af324
UH
24#include "libsigrok-internal.h"
25#include "protocol.h"
26
c073af80
UH
27/* States */
28enum {
29 SEND_INIT,
30 GET_INIT_REPLY,
31 SEND_PACKET_REQUEST,
32 GET_PACKET,
33};
34
35static void parse_packet(const uint8_t *buf, float *floatval,
c68ade3c 36 struct sr_datafeed_analog *analog)
c073af80
UH
37{
38 gboolean is_a, is_fast;
39 uint16_t intval;
40 uint8_t level = 0, level_bits;
41
42 /* Byte 0 [7:7]: 0 = A, 1 = C */
43 is_a = ((buf[0] & (1 << 7)) == 0);
44
45 /* Byte 0 [6:6]: Unknown/unused? */
46
47 /* Byte 0 [5:4]: Level (00 = 40, 01 = 60, 10 = 80, 11 = 100) */
48 level_bits = (buf[0] >> 4) & 0x03;
49 if (level_bits == 0)
50 level = 40;
51 else if (level_bits == 1)
52 level = 60;
53 else if (level_bits == 2)
54 level = 80;
55 else if (level_bits == 3)
56 level = 100;
57
58 /* Byte 0 [3:3]: 0 = fast, 1 = slow */
59 is_fast = ((buf[0] & (1 << 3)) == 0);
60
61 /* Byte 0 [2:0]: value[10..8] */
62 /* Byte 1 [7:0]: value[7..0] */
63 intval = (buf[0] & 0x7) << 8;
64 intval |= buf[1];
65
66 *floatval = (float)intval;
67
68 /* The value on the display always has one digit after the comma. */
69 *floatval /= 10;
70
c68ade3c
UH
71 analog->meaning->mq = SR_MQ_SOUND_PRESSURE_LEVEL;
72 analog->meaning->unit = SR_UNIT_DECIBEL_SPL;
c073af80
UH
73
74 if (is_a)
c68ade3c 75 analog->meaning->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A;
c073af80 76 else
c68ade3c 77 analog->meaning->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C;
c073af80
UH
78
79 if (is_fast)
c68ade3c 80 analog->meaning->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F;
c073af80 81 else
c68ade3c 82 analog->meaning->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S;
c073af80
UH
83
84 /* TODO: How to handle level? */
85 (void)level;
86}
87
69e19dd7 88static void decode_packet(struct sr_dev_inst *sdi)
aa2af324 89{
c073af80 90 struct sr_datafeed_packet packet;
c68ade3c
UH
91 struct sr_datafeed_analog analog;
92 struct sr_analog_encoding encoding;
93 struct sr_analog_meaning meaning;
94 struct sr_analog_spec spec;
69e19dd7 95 struct dev_context *devc;
c073af80
UH
96 float floatval;
97
69e19dd7 98 devc = sdi->priv;
c68ade3c 99
d043b29d 100 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
c073af80
UH
101
102 parse_packet(devc->buf, &floatval, &analog);
103
104 /* Send a sample packet with one analog value. */
c68ade3c 105 analog.meaning->channels = sdi->channels;
c073af80
UH
106 analog.num_samples = 1;
107 analog.data = &floatval;
c68ade3c 108 packet.type = SR_DF_ANALOG;
c073af80 109 packet.payload = &analog;
695dc859 110 sr_session_send(sdi, &packet);
c073af80 111
2630f97e 112 sr_sw_limits_update_samples_read(&devc->limits, 1);
c073af80
UH
113}
114
e8be6169 115SR_PRIV int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data)
c073af80
UH
116{
117 struct sr_dev_inst *sdi;
aa2af324 118 struct dev_context *devc;
a5e44c32 119 struct sr_serial_dev_inst *serial;
c073af80
UH
120 uint8_t buf[3];
121 int ret;
122
f4284934 123 (void)fd;
c073af80
UH
124 (void)revents;
125
126 sdi = cb_data;
a5e44c32 127 serial = sdi->conn;
c073af80 128 devc = sdi->priv;
aa2af324 129
c073af80 130 /* TODO: Parts of this code need to be improved later. */
aa2af324 131
c073af80
UH
132 /* State machine. */
133 if (devc->state == SEND_INIT) {
134 /* On the first run, send the "init" command. */
135 buf[0] = 0x10;
136 buf[1] = 0x04;
137 buf[2] = 0x0d;
138 sr_spew("Sending init command: %02x %02x %02x.",
139 buf[0], buf[1], buf[2]);
32c45845
UH
140 if ((ret = serial_write_blocking(serial, buf, 3,
141 serial_timeout(serial, 3))) < 0) {
c073af80
UH
142 sr_err("Error sending init command: %d.", ret);
143 return FALSE;
144 }
145 devc->state = GET_INIT_REPLY;
146 } else if (devc->state == GET_INIT_REPLY) {
147 /* If we just sent the "init" command, get its reply. */
eead2782 148 if ((ret = serial_read_blocking(serial, buf, 2, 0)) < 0) {
c073af80
UH
149 sr_err("Error reading init reply: %d.", ret);
150 return FALSE;
151 }
152 sr_spew("Received init reply: %02x %02x.", buf[0], buf[1]);
153 /* Expected reply: 0x05 0x0d */
154 if (buf[0] != 0x05 || buf[1] != 0x0d) {
155 sr_err("Received incorrect init reply, retrying.");
156 devc->state = SEND_INIT;
157 return TRUE;
158 }
159 devc->state = SEND_PACKET_REQUEST;
160 } else if (devc->state == SEND_PACKET_REQUEST) {
161 /* Request a packet (send 0x30 ZZ 0x0d). */
162 buf[0] = 0x30;
163 buf[1] = 0x00; /* ZZ */
164 buf[2] = 0x0d;
165 sr_spew("Sending data request command: %02x %02x %02x.",
166 buf[0], buf[1], buf[2]);
32c45845
UH
167 if ((ret = serial_write_blocking(serial, buf, 3,
168 serial_timeout(serial, 3))) < 0) {
c073af80
UH
169 sr_err("Error sending request command: %d.", ret);
170 return FALSE;
171 }
172 devc->buflen = 0;
173 devc->state = GET_PACKET;
174 } else if (devc->state == GET_PACKET) {
175 /* Read a packet from the device. */
47d98603 176 ret = serial_read_nonblocking(serial, devc->buf + devc->buflen,
d9251a2c 177 4 - devc->buflen);
c073af80
UH
178 if (ret < 0) {
179 sr_err("Error reading packet: %d.", ret);
180 return TRUE;
181 }
182
183 devc->buflen += ret;
184
185 /* Didn't receive all 4 bytes, yet. */
186 if (devc->buflen != 4)
187 return TRUE;
188
189 sr_spew("Received packet: %02x %02x %02x %02x.", devc->buf[0],
190 devc->buf[1], devc->buf[2], devc->buf[3]);
191
192 /* Expected reply: AA BB ZZ+1 0x0d */
193 if (devc->buf[2] != 0x01 || devc->buf[3] != 0x0d) {
194 sr_err("Received incorrect request reply, retrying.");
195 devc->state = SEND_PACKET_REQUEST;
196 return TRUE;
197 }
198
69e19dd7 199 decode_packet(sdi);
c073af80
UH
200
201 devc->state = SEND_PACKET_REQUEST;
202 } else {
203 sr_err("Invalid state: %d.", devc->state);
204 return FALSE;
205 }
aa2af324 206
2630f97e 207 if (sr_sw_limits_check(&devc->limits))
d2f7c417 208 sr_dev_acquisition_stop(sdi);
aa2af324
UH
209
210 return TRUE;
211}