2 * This file is part of the libsigrok project.
4 * Copyright (C) 2019 Vitaliy Vorobyov
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 /* Callback handling data */
26 static int la_prepare_data(int fd, int revents, void *cb_data)
28 struct sr_dev_inst *sdi;
29 struct dev_context *devc;
30 struct sr_usb_dev_inst *usb;
31 int i, j, ret, xfer_len;
32 uint8_t *rle_buf, *samples;
35 int samples_count, rle_samples_count;
37 struct sr_datafeed_packet packet;
38 struct sr_datafeed_logic logic;
43 RLE_SAMPLE_SIZE = sizeof(uint32_t) + sizeof(uint16_t),
44 RLE_SAMPLES_COUNT = 0x100000,
45 RLE_BUF_SIZE = RLE_SAMPLES_COUNT * RLE_SAMPLE_SIZE,
46 RLE_END_MARKER = 0xFFFF,
56 memset(status, 0, sizeof(status));
57 ret = sla5032_get_status(usb, status);
59 sla5032_write_reg14_zero(usb);
60 sr_dev_acquisition_stop(sdi);
61 return G_SOURCE_CONTINUE;
64 /* data not ready (acquision in progress) */
66 return G_SOURCE_CONTINUE;
68 sr_dbg("acquision done, status: %u.", (unsigned int)status[2]);
70 /* data ready (download, decode and send to sigrok) */
71 ret = sla5032_set_read_back(usb);
73 sla5032_write_reg14_zero(usb);
74 sr_dev_acquisition_stop(sdi);
75 return G_SOURCE_CONTINUE;
78 rle_buf = g_try_malloc(RLE_BUF_SIZE);
79 if (rle_buf == NULL) {
80 sla5032_write_reg14_zero(usb);
81 sr_dev_acquisition_stop(sdi);
82 return G_SOURCE_CONTINUE;
87 ret = sla5032_read_data_chunk(usb, rle_buf, RLE_BUF_SIZE, &xfer_len);
89 sla5032_write_reg14_zero(usb);
91 sr_dev_acquisition_stop(sdi);
93 sr_dbg("acquision done, ret: %d.", ret);
94 return G_SOURCE_CONTINUE;
97 sr_dbg("acquision done, xfer_len: %d.", xfer_len);
100 sla5032_write_reg14_zero(usb);
102 sr_dev_acquisition_stop(sdi);
103 return G_SOURCE_CONTINUE;
108 rle_samples_count = xfer_len / RLE_SAMPLE_SIZE;
110 sr_dbg("acquision done, rle_samples_count: %d.", rle_samples_count);
112 for (i = 0; i < rle_samples_count; i++) {
113 p += sizeof(uint32_t); /* skip sample value */
115 rle_count = RL16(p); /* read RLE counter */
116 p += sizeof(uint16_t);
117 if (rle_count == RLE_END_MARKER) {
118 rle_samples_count = i;
121 samples_count += rle_count + 1;
123 sr_dbg("acquision done, samples_count: %d.", samples_count);
125 if (samples_count == 0) {
126 sr_dbg("acquision done, no samples.");
127 sla5032_write_reg14_zero(usb);
129 sr_dev_acquisition_stop(sdi);
130 return G_SOURCE_CONTINUE;
134 samples = g_try_malloc(samples_count * sizeof(uint32_t));
136 sr_dbg("memory allocation error.");
137 sla5032_write_reg14_zero(usb);
139 sr_dev_acquisition_stop(sdi);
140 return G_SOURCE_CONTINUE;
145 for (i = 0; i < rle_samples_count; i++) {
147 p += sizeof(uint32_t); /* read sample value */
149 rle_count = RL16(p); /* read RLE counter */
150 p += sizeof(uint16_t);
152 if (rle_count == RLE_END_MARKER) {
153 sr_dbg("RLE end marker found.");
157 for (j = 0; j <= rle_count; j++) {
159 q += sizeof(uint32_t);
163 if (devc->trigger_fired) {
164 /* Send the incoming transfer to the session bus. */
165 packet.type = SR_DF_LOGIC;
166 packet.payload = &logic;
168 logic.length = samples_count * sizeof(uint32_t);
169 logic.unitsize = sizeof(uint32_t);
170 logic.data = samples;
171 sr_session_send(sdi, &packet);
173 trigger_offset = soft_trigger_logic_check(devc->stl,
174 samples, samples_count * sizeof(uint32_t), NULL);
175 if (trigger_offset > -1) {
176 packet.type = SR_DF_LOGIC;
177 packet.payload = &logic;
178 int num_samples = samples_count - trigger_offset;
180 logic.length = num_samples * sizeof(uint32_t);
181 logic.unitsize = sizeof(uint32_t);
182 logic.data = samples + trigger_offset * sizeof(uint32_t);
183 sr_session_send(sdi, &packet);
185 devc->trigger_fired = TRUE;
190 } while (rle_samples_count == RLE_SAMPLES_COUNT);
192 sr_dbg("acquision stop, rle_samples_count < RLE_SAMPLES_COUNT.");
194 sla5032_write_reg14_zero(usb);
196 sr_dev_acquisition_stop(sdi); /* if all data transfered */
201 soft_trigger_logic_free(devc->stl);
205 return G_SOURCE_CONTINUE;
208 SR_PRIV int la_start_acquisition(const struct sr_dev_inst *sdi)
210 struct dev_context *devc;
211 struct sr_usb_dev_inst *usb;
212 struct sr_trigger* trigger;
214 enum { poll_interval_ms = 100 };
220 if (devc->state != STATE_IDLE) {
221 sr_err("Not in idle state, cannot start acquisition.");
225 pre = (devc->limit_samples * devc->capture_ratio) / 100;
226 post = devc->limit_samples - pre;
228 if ((trigger = sr_session_trigger_get(sdi->session))) {
229 devc->stl = soft_trigger_logic_new(sdi, trigger, pre);
231 sr_err("stl alloc error.");
232 return SR_ERR_MALLOC;
234 devc->trigger_fired = FALSE;
237 devc->trigger_fired = TRUE;
239 sr_dbg("start acquision, smp lim: %" PRIu64 ", cap ratio: %" PRIu64
240 ".", devc->limit_samples, devc->capture_ratio);
242 sr_dbg("start acquision, pre: %" PRIu64 ", post: %" PRIu64 ".", pre, post);
251 sr_dbg("start acquision, pre: %" PRIx64 ", post: %" PRIx64 ".", pre, post);
253 /* (x + 1) * 256 (samples) pre, post */
254 ret = sla5032_set_depth(usb, pre, post);
258 ret = sla5032_set_triggers(usb, devc->trigger_values, devc->trigger_edge_mask, devc->trigger_mask);
262 ret = sla5032_set_samplerate(usb, devc->samplerate);
266 /* TODO: make PWM generator as separate configurable subdevice */
268 pwm1_hi = 20000000 - 1,
269 pwm1_lo = 200000 - 1,
274 ret = sla5032_set_pwm1(usb, pwm1_hi, pwm1_lo);
278 ret = sla5032_set_pwm2(usb, pwm2_hi, pwm2_lo);
282 ret = sla5032_start_sample(usb);
286 sr_session_source_add(sdi->session, -1, 0, poll_interval_ms,
287 la_prepare_data, (struct sr_dev_inst *)sdi);
289 std_session_send_df_header(sdi);