]>
Commit | Line | Data |
---|---|---|
8da8c826 VV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2019 Vitaliy Vorobyov | |
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 3 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 | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <string.h> | |
22 | #include "protocol.h" | |
23 | #include "sla5032.h" | |
24 | ||
25 | /* Callback handling data */ | |
26 | static int la_prepare_data(int fd, int revents, void *cb_data) | |
27 | { | |
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; | |
33 | const uint8_t *p, *q; | |
34 | uint16_t rle_count; | |
35 | int samples_count, rle_samples_count; | |
36 | uint32_t status[3]; | |
37 | struct sr_datafeed_packet packet; | |
38 | struct sr_datafeed_logic logic; | |
39 | uint32_t value; | |
40 | int trigger_offset; | |
41 | ||
42 | enum { | |
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, | |
47 | }; | |
48 | ||
49 | (void)fd; | |
50 | (void)revents; | |
51 | ||
52 | sdi = cb_data; | |
53 | devc = sdi->priv; | |
54 | usb = sdi->conn; | |
55 | ||
56 | memset(status, 0, sizeof(status)); | |
57 | ret = sla5032_get_status(usb, status); | |
58 | if (ret != SR_OK) { | |
59 | sla5032_write_reg14_zero(usb); | |
60 | sr_dev_acquisition_stop(sdi); | |
61 | return G_SOURCE_CONTINUE; | |
62 | } | |
63 | ||
64 | /* data not ready (acquision in progress) */ | |
65 | if (status[1] != 3) | |
66 | return G_SOURCE_CONTINUE; | |
67 | ||
68 | sr_dbg("acquision done, status: %u.", (unsigned int)status[2]); | |
69 | ||
70 | /* data ready (download, decode and send to sigrok) */ | |
71 | ret = sla5032_set_read_back(usb); | |
72 | if (ret != SR_OK) { | |
73 | sla5032_write_reg14_zero(usb); | |
74 | sr_dev_acquisition_stop(sdi); | |
75 | return G_SOURCE_CONTINUE; | |
76 | } | |
77 | ||
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; | |
83 | } | |
84 | ||
85 | do { | |
86 | xfer_len = 0; | |
87 | ret = sla5032_read_data_chunk(usb, rle_buf, RLE_BUF_SIZE, &xfer_len); | |
88 | if (ret != SR_OK) { | |
89 | sla5032_write_reg14_zero(usb); | |
90 | g_free(rle_buf); | |
91 | sr_dev_acquisition_stop(sdi); | |
92 | ||
93 | sr_dbg("acquision done, ret: %d.", ret); | |
94 | return G_SOURCE_CONTINUE; | |
95 | } | |
96 | ||
97 | sr_dbg("acquision done, xfer_len: %d.", xfer_len); | |
98 | ||
99 | if (xfer_len == 0) { | |
100 | sla5032_write_reg14_zero(usb); | |
101 | g_free(rle_buf); | |
102 | sr_dev_acquisition_stop(sdi); | |
103 | return G_SOURCE_CONTINUE; | |
104 | } | |
105 | ||
106 | p = rle_buf; | |
107 | samples_count = 0; | |
108 | rle_samples_count = xfer_len / RLE_SAMPLE_SIZE; | |
109 | ||
110 | sr_dbg("acquision done, rle_samples_count: %d.", rle_samples_count); | |
111 | ||
112 | for (i = 0; i < rle_samples_count; i++) { | |
113 | p += sizeof(uint32_t); /* skip sample value */ | |
114 | ||
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; | |
119 | break; | |
120 | } | |
121 | samples_count += rle_count + 1; | |
122 | } | |
123 | sr_dbg("acquision done, samples_count: %d.", samples_count); | |
124 | ||
125 | if (samples_count == 0) { | |
126 | sr_dbg("acquision done, no samples."); | |
127 | sla5032_write_reg14_zero(usb); | |
128 | g_free(rle_buf); | |
129 | sr_dev_acquisition_stop(sdi); | |
130 | return G_SOURCE_CONTINUE; | |
131 | } | |
132 | ||
133 | /* Decode RLE */ | |
134 | samples = g_try_malloc(samples_count * sizeof(uint32_t)); | |
135 | if (!samples) { | |
136 | sr_dbg("memory allocation error."); | |
137 | sla5032_write_reg14_zero(usb); | |
138 | g_free(rle_buf); | |
139 | sr_dev_acquisition_stop(sdi); | |
140 | return G_SOURCE_CONTINUE; | |
141 | } | |
142 | ||
143 | p = rle_buf; | |
144 | q = samples; | |
145 | for (i = 0; i < rle_samples_count; i++) { | |
146 | value = RL32(p); | |
147 | p += sizeof(uint32_t); /* read sample value */ | |
148 | ||
149 | rle_count = RL16(p); /* read RLE counter */ | |
150 | p += sizeof(uint16_t); | |
151 | ||
152 | if (rle_count == RLE_END_MARKER) { | |
153 | sr_dbg("RLE end marker found."); | |
154 | break; | |
155 | } | |
156 | ||
157 | for (j = 0; j <= rle_count; j++) { | |
158 | WL32(q, value); | |
159 | q += sizeof(uint32_t); | |
160 | } | |
161 | } | |
162 | ||
163 | if (devc->trigger_fired) { | |
164 | /* Send the incoming transfer to the session bus. */ | |
165 | packet.type = SR_DF_LOGIC; | |
166 | packet.payload = &logic; | |
167 | ||
168 | logic.length = samples_count * sizeof(uint32_t); | |
169 | logic.unitsize = sizeof(uint32_t); | |
170 | logic.data = samples; | |
171 | sr_session_send(sdi, &packet); | |
172 | } else { | |
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; | |
179 | ||
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); | |
184 | ||
185 | devc->trigger_fired = TRUE; | |
186 | } | |
187 | } | |
188 | ||
189 | g_free(samples); | |
190 | } while (rle_samples_count == RLE_SAMPLES_COUNT); | |
191 | ||
192 | sr_dbg("acquision stop, rle_samples_count < RLE_SAMPLES_COUNT."); | |
193 | ||
194 | sla5032_write_reg14_zero(usb); | |
195 | ||
196 | sr_dev_acquisition_stop(sdi); /* if all data transfered */ | |
197 | ||
198 | g_free(rle_buf); | |
199 | ||
200 | if (devc->stl) { | |
201 | soft_trigger_logic_free(devc->stl); | |
202 | devc->stl = NULL; | |
203 | } | |
204 | ||
205 | return G_SOURCE_CONTINUE; | |
206 | } | |
207 | ||
208 | SR_PRIV int la_start_acquisition(const struct sr_dev_inst *sdi) | |
209 | { | |
210 | struct dev_context *devc; | |
211 | struct sr_usb_dev_inst *usb; | |
212 | struct sr_trigger* trigger; | |
213 | int ret; | |
214 | enum { poll_interval_ms = 100 }; | |
215 | uint64_t pre, post; | |
216 | ||
217 | devc = sdi->priv; | |
218 | usb = sdi->conn; | |
219 | ||
220 | if (devc->state != STATE_IDLE) { | |
221 | sr_err("Not in idle state, cannot start acquisition."); | |
222 | return SR_ERR; | |
223 | } | |
224 | ||
225 | pre = (devc->limit_samples * devc->capture_ratio) / 100; | |
226 | post = devc->limit_samples - pre; | |
227 | ||
228 | if ((trigger = sr_session_trigger_get(sdi->session))) { | |
229 | devc->stl = soft_trigger_logic_new(sdi, trigger, pre); | |
230 | if (!devc->stl) { | |
231 | sr_err("stl alloc error."); | |
232 | return SR_ERR_MALLOC; | |
233 | } | |
234 | devc->trigger_fired = FALSE; | |
235 | } | |
236 | else | |
237 | devc->trigger_fired = TRUE; | |
238 | ||
239 | sr_dbg("start acquision, smp lim: %" PRIu64 ", cap ratio: %" PRIu64 | |
240 | ".", devc->limit_samples, devc->capture_ratio); | |
241 | ||
242 | sr_dbg("start acquision, pre: %" PRIu64 ", post: %" PRIu64 ".", pre, post); | |
243 | pre /= 256; | |
262cb996 | 244 | pre = MAX(pre, 2); |
8da8c826 VV |
245 | pre--; |
246 | ||
247 | post /= 256; | |
262cb996 | 248 | post = MAX(post, 2); |
8da8c826 VV |
249 | post--; |
250 | ||
251 | sr_dbg("start acquision, pre: %" PRIx64 ", post: %" PRIx64 ".", pre, post); | |
252 | ||
253 | /* (x + 1) * 256 (samples) pre, post */ | |
254 | ret = sla5032_set_depth(usb, pre, post); | |
255 | if (ret != SR_OK) | |
256 | return ret; | |
257 | ||
258 | ret = sla5032_set_triggers(usb, devc->trigger_values, devc->trigger_edge_mask, devc->trigger_mask); | |
259 | if (ret != SR_OK) | |
260 | return ret; | |
261 | ||
262 | ret = sla5032_set_samplerate(usb, devc->samplerate); | |
263 | if (ret != SR_OK) | |
264 | return ret; | |
265 | ||
266 | /* TODO: make PWM generator as separate configurable subdevice */ | |
267 | enum { | |
268 | pwm1_hi = 20000000 - 1, | |
269 | pwm1_lo = 200000 - 1, | |
270 | pwm2_hi = 15 - 1, | |
271 | pwm2_lo = 5 - 1, | |
272 | }; | |
273 | ||
274 | ret = sla5032_set_pwm1(usb, pwm1_hi, pwm1_lo); | |
275 | if (ret != SR_OK) | |
276 | return ret; | |
277 | ||
278 | ret = sla5032_set_pwm2(usb, pwm2_hi, pwm2_lo); | |
279 | if (ret != SR_OK) | |
280 | return ret; | |
281 | ||
282 | ret = sla5032_start_sample(usb); | |
283 | if (ret != SR_OK) | |
284 | return ret; | |
285 | ||
286 | sr_session_source_add(sdi->session, -1, 0, poll_interval_ms, | |
287 | la_prepare_data, (struct sr_dev_inst *)sdi); | |
288 | ||
289 | std_session_send_df_header(sdi); | |
290 | ||
291 | return ret; | |
292 | } |