]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2016 George Hopkins <george-hopkins@null.net> | |
5 | * Copyright (C) 2016 Matthieu Guillaumin <matthieu@guillaum.in> | |
6 | * | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "protocol.h" | |
23 | ||
24 | static int send_command(const struct sr_dev_inst *sdi, uint16_t command) | |
25 | { | |
26 | struct sr_serial_dev_inst *serial; | |
27 | uint8_t buffer[2]; | |
28 | ||
29 | buffer[0] = command >> 8; | |
30 | buffer[1] = command; | |
31 | ||
32 | if (!(serial = sdi->conn)) | |
33 | return SR_ERR; | |
34 | ||
35 | return serial_write_blocking(serial, (const void *)buffer, 2, 0); | |
36 | } | |
37 | ||
38 | static int send_long_command(const struct sr_dev_inst *sdi, uint32_t command) | |
39 | { | |
40 | struct sr_serial_dev_inst *serial; | |
41 | uint8_t buffer[4]; | |
42 | ||
43 | buffer[0] = command >> 24; | |
44 | buffer[1] = command >> 16; | |
45 | buffer[2] = command >> 8; | |
46 | buffer[3] = command; | |
47 | ||
48 | if (!(serial = sdi->conn)) | |
49 | return SR_ERR; | |
50 | ||
51 | return serial_write_blocking(serial, (const void *)buffer, 4, 0); | |
52 | } | |
53 | ||
54 | static void send_data(const struct sr_dev_inst *sdi, float sample) | |
55 | { | |
56 | struct dev_context *devc; | |
57 | struct sr_datafeed_packet packet; | |
58 | struct sr_datafeed_analog analog; | |
59 | struct sr_analog_encoding encoding; | |
60 | struct sr_analog_meaning meaning; | |
61 | struct sr_analog_spec spec; | |
62 | ||
63 | devc = sdi->priv; | |
64 | ||
65 | sr_analog_init(&analog, &encoding, &meaning, &spec, 1); | |
66 | meaning.mq = SR_MQ_SOUND_PRESSURE_LEVEL; | |
67 | meaning.mqflags = devc->cur_mqflags; | |
68 | meaning.unit = SR_UNIT_DECIBEL_SPL; | |
69 | meaning.channels = sdi->channels; | |
70 | analog.num_samples = 1; | |
71 | analog.data = &sample; | |
72 | packet.type = SR_DF_ANALOG; | |
73 | packet.payload = &analog; | |
74 | sr_session_send(sdi, &packet); | |
75 | ||
76 | devc->num_samples++; | |
77 | /* Limiting number of samples is only supported for live data. */ | |
78 | if (devc->cur_data_source == DATA_SOURCE_LIVE && devc->limit_samples && devc->num_samples >= devc->limit_samples) | |
79 | sr_dev_acquisition_stop((struct sr_dev_inst *)sdi); | |
80 | } | |
81 | ||
82 | static void process_measurement(const struct sr_dev_inst *sdi) | |
83 | { | |
84 | struct dev_context *devc; | |
85 | unsigned short value; | |
86 | ||
87 | devc = sdi->priv; | |
88 | ||
89 | if (devc->buffer[3] & (1 << 0)) { | |
90 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
91 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
92 | } else { | |
93 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
94 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
95 | } | |
96 | ||
97 | if (devc->buffer[3] & (1 << 1)) { | |
98 | devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
99 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
100 | } else { | |
101 | devc->cur_mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F; | |
102 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_TIME_WEIGHT_S; | |
103 | } | |
104 | ||
105 | devc->cur_meas_range = devc->buffer[4] & 3; | |
106 | ||
107 | if (devc->buffer[4] & (1 << 2)) { | |
108 | devc->cur_mqflags |= SR_MQFLAG_MAX; | |
109 | devc->cur_mqflags &= ~SR_MQFLAG_MIN; | |
110 | } else if (devc->buffer[4] & (1 << 3)) { | |
111 | devc->cur_mqflags |= SR_MQFLAG_MIN; | |
112 | devc->cur_mqflags &= ~SR_MQFLAG_MAX; | |
113 | } else { | |
114 | devc->cur_mqflags &= ~SR_MQFLAG_MIN; | |
115 | devc->cur_mqflags &= ~SR_MQFLAG_MAX; | |
116 | } | |
117 | ||
118 | value = devc->buffer[1] << 8 | devc->buffer[2]; | |
119 | send_data(sdi, value / 10.0); | |
120 | } | |
121 | ||
122 | static void process_memory_measurement(const struct sr_dev_inst *sdi) | |
123 | { | |
124 | struct dev_context *devc; | |
125 | uint16_t value; | |
126 | ||
127 | devc = sdi->priv; | |
128 | value = devc->buffer[devc->buffer_len - 1] << 8; | |
129 | value |= devc->buffer[devc->buffer_len - 2]; | |
130 | ||
131 | send_data(sdi, value / 10.0); | |
132 | } | |
133 | ||
134 | static void process_byte(const struct sr_dev_inst *sdi, const unsigned char c) | |
135 | { | |
136 | struct dev_context *devc; | |
137 | unsigned int i; | |
138 | ||
139 | devc = sdi->priv; | |
140 | ||
141 | if (devc->buffer_len < BUFFER_SIZE) { | |
142 | devc->buffer[devc->buffer_len++] = c; | |
143 | } else { | |
144 | for (i = 1; i < BUFFER_SIZE; i++) | |
145 | devc->buffer[i - 1] = devc->buffer[i]; | |
146 | devc->buffer[BUFFER_SIZE - 1] = c; | |
147 | } | |
148 | ||
149 | if (devc->buffer_len == BUFFER_SIZE && devc->buffer[0] == 0x7f | |
150 | && devc->buffer[BUFFER_SIZE - 1] == 0x00) { | |
151 | process_measurement(sdi); | |
152 | devc->buffer_len = 0; | |
153 | } | |
154 | } | |
155 | ||
156 | static void process_usage_byte(const struct sr_dev_inst *sdi, uint8_t c) | |
157 | { | |
158 | struct dev_context *devc; | |
159 | unsigned int i; | |
160 | ||
161 | devc = sdi->priv; | |
162 | ||
163 | if (devc->buffer_len < MEM_USAGE_BUFFER_SIZE) { | |
164 | devc->buffer[devc->buffer_len++] = c; | |
165 | } else { | |
166 | for (i = 1; i < MEM_USAGE_BUFFER_SIZE; i++) | |
167 | devc->buffer[i - 1] = devc->buffer[i]; | |
168 | devc->buffer[MEM_USAGE_BUFFER_SIZE - 1] = c; | |
169 | } | |
170 | ||
171 | if (devc->buffer_len == MEM_USAGE_BUFFER_SIZE && devc->buffer[0] == 0xd1 | |
172 | && devc->buffer[1] == 0x05 && devc->buffer[2] == 0x00 | |
173 | && devc->buffer[3] == 0x01 && devc->buffer[4] == 0xd2 | |
174 | && devc->buffer[MEM_USAGE_BUFFER_SIZE - 1] == 0x20) { | |
175 | devc->memory_block_usage = devc->buffer[5] << 8 | devc->buffer[6]; | |
176 | devc->memory_last_block_usage = devc->buffer[7]; | |
177 | sr_warn("Memory usage: %d blocks of 256 bytes, 1 block of %d bytes", | |
178 | devc->memory_block_usage - 1, devc->memory_last_block_usage); | |
179 | devc->buffer_len = 0; | |
180 | devc->buffer_skip = 1; | |
181 | devc->memory_state = MEM_STATE_REQUEST_MEMORY_BLOCK; | |
182 | devc->memory_block_cursor = 0; | |
183 | devc->memory_block_counter = 0; | |
184 | } | |
185 | } | |
186 | ||
187 | static void process_memory_byte(const struct sr_dev_inst *sdi, uint8_t c) | |
188 | { | |
189 | struct dev_context *devc; | |
190 | unsigned int i; | |
191 | ||
192 | devc = sdi->priv; | |
193 | ||
194 | if (devc->buffer_len < MEM_DATA_BUFFER_SIZE) { | |
195 | devc->buffer[devc->buffer_len++] = c; | |
196 | } else { | |
197 | for (i = 1; i < MEM_DATA_BUFFER_SIZE; i++) | |
198 | devc->buffer[i - 1] = devc->buffer[i]; | |
199 | devc->buffer[MEM_DATA_BUFFER_SIZE - 1] = c; | |
200 | } | |
201 | ||
202 | if (devc->buffer_skip == 0 \ | |
203 | && (devc->buffer[devc->buffer_len-2] & 0x7f) == 0x7f | |
204 | && (devc->buffer[devc->buffer_len-1] & 0xf7) == 0xf7) { | |
205 | /* Recording session header bytes found, load next 7 bytes. */ | |
206 | devc->buffer_skip = MEM_DATA_BUFFER_SIZE - 2; | |
207 | } | |
208 | ||
209 | if (devc->buffer_skip == 0 && devc->buffer_len == MEM_DATA_BUFFER_SIZE | |
210 | && (devc->buffer[0] & 0x7f) == 0x7f && (devc->buffer[1] & 0xf7) == 0xf7 | |
211 | && devc->buffer[2] == 0x01 && devc->buffer[3] == 0x00) { | |
212 | /* Print information about recording. */ | |
213 | sr_err("Recording dB(%X) %02x/%02x/%02x %02x:%02x:%02x ", | |
214 | devc->buffer[4], devc->buffer[5], devc->buffer[6], devc->buffer[7], | |
215 | devc->buffer[8] & 0x3f, devc->buffer[9], devc->buffer[10]); | |
216 | /* Set dBA/dBC flag for recording. */ | |
217 | if (devc->buffer[4] == 0x0c) { | |
218 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
219 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
220 | } else { | |
221 | devc->cur_mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A; | |
222 | devc->cur_mqflags &= ~SR_MQFLAG_SPL_FREQ_WEIGHT_C; | |
223 | } | |
224 | send_data(sdi, -1.0); /* Signal switch of recording. */ | |
225 | devc->buffer_skip = 2; | |
226 | } | |
227 | ||
228 | if (devc->buffer_skip == 0) { | |
229 | process_memory_measurement(sdi); | |
230 | devc->buffer_skip = 1; | |
231 | } else { | |
232 | devc->buffer_skip -= 1; | |
233 | } | |
234 | ||
235 | devc->memory_block_cursor++; /* uint8_t goes back to 0 after 255. */ | |
236 | if (devc->memory_block_cursor == 0) { | |
237 | /* Current block is completed. */ | |
238 | devc->memory_block_counter++; | |
239 | devc->memory_state = MEM_STATE_REQUEST_MEMORY_BLOCK; | |
240 | } | |
241 | } | |
242 | ||
243 | SR_PRIV int pce_322a_receive_data(int fd, int revents, void *cb_data) | |
244 | { | |
245 | const struct sr_dev_inst *sdi; | |
246 | struct dev_context *devc; | |
247 | struct sr_serial_dev_inst *serial; | |
248 | unsigned char c; | |
249 | ||
250 | (void)fd; | |
251 | ||
252 | if (!(sdi = cb_data)) | |
253 | return TRUE; | |
254 | ||
255 | if (!(devc = sdi->priv)) | |
256 | return TRUE; | |
257 | ||
258 | if (!(serial = sdi->conn)) | |
259 | return TRUE; | |
260 | ||
261 | if (devc->cur_data_source == DATA_SOURCE_MEMORY) { | |
262 | switch (devc->memory_state) { | |
263 | case MEM_STATE_REQUEST_MEMORY_USAGE: | |
264 | /* At init, disconnect and request the memory status. */ | |
265 | sr_warn("Requesting memory usage."); | |
266 | pce_322a_disconnect(sdi); | |
267 | devc->memory_state = MEM_STATE_GET_MEMORY_USAGE; | |
268 | devc->memory_block_usage = 0; | |
269 | devc->memory_last_block_usage = 0; | |
270 | devc->memory_block_counter = 0; | |
271 | devc->memory_block_cursor = 0; | |
272 | pce_322a_memory_status(sdi); | |
273 | break; | |
274 | case MEM_STATE_GET_MEMORY_USAGE: | |
275 | /* Listen for memory usage answer. */ | |
276 | if (revents == G_IO_IN) { | |
277 | if (serial_read_nonblocking(serial, &c, 1) != 1) | |
278 | return TRUE; | |
279 | process_usage_byte(sdi, c); | |
280 | } | |
281 | break; | |
282 | case MEM_STATE_REQUEST_MEMORY_BLOCK: | |
283 | /* When cursor is 0, request next memory block. */ | |
284 | if (devc->memory_block_counter <= devc->memory_block_usage) { | |
285 | sr_warn("Requesting memory block %d.", devc->memory_block_counter); | |
286 | pce_322a_memory_block(sdi, devc->memory_block_counter); | |
287 | devc->memory_state = MEM_STATE_GET_MEMORY_BLOCK; | |
288 | } else { | |
289 | sr_warn("Exhausted memory blocks."); | |
290 | return FALSE; | |
291 | } | |
292 | break; | |
293 | case MEM_STATE_GET_MEMORY_BLOCK: | |
294 | /* Stop after reading last byte of last block. */ | |
295 | if (devc->memory_block_counter >= devc->memory_block_usage | |
296 | && devc->memory_block_cursor >= devc->memory_last_block_usage) { | |
297 | sr_warn("Done reading memory (%d bytes).", | |
298 | 256 * (devc->memory_block_counter - 1) | |
299 | + devc->memory_block_cursor); | |
300 | return FALSE; | |
301 | } | |
302 | /* Listen for memory data. */ | |
303 | if (revents == G_IO_IN) { | |
304 | if (serial_read_nonblocking(serial, &c, 1) != 1) | |
305 | return TRUE; | |
306 | process_memory_byte(sdi, c); | |
307 | } | |
308 | break; | |
309 | } | |
310 | } else { | |
311 | /* Listen for live data. */ | |
312 | if (revents == G_IO_IN) { | |
313 | if (serial_read_nonblocking(serial, &c, 1) != 1) | |
314 | return TRUE; | |
315 | process_byte(sdi, c); | |
316 | } | |
317 | } | |
318 | ||
319 | return TRUE; | |
320 | } | |
321 | ||
322 | SR_PRIV int pce_322a_connect(const struct sr_dev_inst *sdi) | |
323 | { | |
324 | return send_command(sdi, CMD_CONNECT); | |
325 | } | |
326 | ||
327 | SR_PRIV int pce_322a_disconnect(const struct sr_dev_inst *sdi) | |
328 | { | |
329 | return send_command(sdi, CMD_DISCONNECT); | |
330 | } | |
331 | ||
332 | SR_PRIV int pce_322a_memory_status(const struct sr_dev_inst *sdi) | |
333 | { | |
334 | return send_command(sdi, CMD_MEMORY_STATUS); | |
335 | } | |
336 | ||
337 | SR_PRIV int pce_322a_memory_clear(const struct sr_dev_inst *sdi) | |
338 | { | |
339 | return send_command(sdi, CMD_MEMORY_CLEAR); | |
340 | } | |
341 | ||
342 | SR_PRIV int pce_322a_memory_block(const struct sr_dev_inst *sdi, uint16_t memblk) | |
343 | { | |
344 | uint8_t buf0 = memblk; | |
345 | uint8_t buf1 = memblk >> 8; | |
346 | uint32_t command = CMD_MEMORY_TRANSFER << 16 | buf0 << 8 | buf1; | |
347 | return send_long_command(sdi, command); | |
348 | } | |
349 | ||
350 | SR_PRIV uint64_t pce_322a_weight_freq_get(const struct sr_dev_inst *sdi) | |
351 | { | |
352 | struct dev_context *devc; | |
353 | ||
354 | devc = sdi->priv; | |
355 | ||
356 | return devc->cur_mqflags & (SR_MQFLAG_SPL_FREQ_WEIGHT_A | SR_MQFLAG_SPL_FREQ_WEIGHT_C); | |
357 | } | |
358 | ||
359 | SR_PRIV int pce_322a_weight_freq_set(const struct sr_dev_inst *sdi, uint64_t freqw) | |
360 | { | |
361 | struct dev_context *devc; | |
362 | ||
363 | devc = sdi->priv; | |
364 | ||
365 | if (devc->cur_mqflags & freqw) | |
366 | return SR_OK; | |
367 | ||
368 | return send_command(sdi, CMD_TOGGLE_WEIGHT_FREQ); | |
369 | } | |
370 | ||
371 | SR_PRIV uint64_t pce_322a_weight_time_get(const struct sr_dev_inst *sdi) | |
372 | { | |
373 | struct dev_context *devc; | |
374 | ||
375 | devc = sdi->priv; | |
376 | ||
377 | return devc->cur_mqflags & (SR_MQFLAG_SPL_TIME_WEIGHT_F | SR_MQFLAG_SPL_TIME_WEIGHT_S); | |
378 | } | |
379 | ||
380 | SR_PRIV int pce_322a_weight_time_set(const struct sr_dev_inst *sdi, uint64_t timew) | |
381 | { | |
382 | struct dev_context *devc; | |
383 | ||
384 | devc = sdi->priv; | |
385 | ||
386 | if (devc->cur_mqflags & timew) | |
387 | return SR_OK; | |
388 | ||
389 | return send_command(sdi, CMD_TOGGLE_WEIGHT_TIME); | |
390 | } | |
391 | ||
392 | SR_PRIV int pce_322a_meas_range_get(const struct sr_dev_inst *sdi, | |
393 | uint64_t *low, uint64_t *high) | |
394 | { | |
395 | struct dev_context *devc; | |
396 | ||
397 | devc = sdi->priv; | |
398 | ||
399 | switch (devc->cur_meas_range) { | |
400 | case MEAS_RANGE_30_130: | |
401 | *low = 30; | |
402 | *high = 130; | |
403 | break; | |
404 | case MEAS_RANGE_30_80: | |
405 | *low = 30; | |
406 | *high = 80; | |
407 | break; | |
408 | case MEAS_RANGE_50_100: | |
409 | *low = 50; | |
410 | *high = 100; | |
411 | break; | |
412 | case MEAS_RANGE_80_130: | |
413 | *low = 80; | |
414 | *high = 130; | |
415 | break; | |
416 | default: | |
417 | return SR_ERR; | |
418 | } | |
419 | ||
420 | return SR_OK; | |
421 | } | |
422 | ||
423 | SR_PRIV int pce_322a_meas_range_set(const struct sr_dev_inst *sdi, | |
424 | uint64_t low, uint64_t high) | |
425 | { | |
426 | struct dev_context *devc; | |
427 | uint8_t range; | |
428 | int ret = SR_OK; | |
429 | ||
430 | devc = sdi->priv; | |
431 | ||
432 | if (low == 30 && high == 130) | |
433 | range = MEAS_RANGE_30_130; | |
434 | else if (low == 30 && high == 80) | |
435 | range = MEAS_RANGE_30_80; | |
436 | else if (low == 50 && high == 100) | |
437 | range = MEAS_RANGE_50_100; | |
438 | else if (low == 80 && high == 130) | |
439 | range = MEAS_RANGE_80_130; | |
440 | else | |
441 | return SR_ERR; | |
442 | ||
443 | while (range != devc->cur_meas_range) { | |
444 | ret = send_command(sdi, CMD_TOGGLE_MEAS_RANGE); | |
445 | if (ret != SR_OK) | |
446 | break; | |
447 | range = (range - 1) & 3; | |
448 | } | |
449 | ||
450 | return ret; | |
451 | } | |
452 | ||
453 | SR_PRIV int pce_322a_power_off(const struct sr_dev_inst *sdi) | |
454 | { | |
455 | return send_command(sdi, CMD_POWER_OFF); | |
456 | } |