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