]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com> | |
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 "lwla.h" | |
22 | #include "protocol.h" | |
23 | ||
24 | /* Number of logic channels. | |
25 | */ | |
26 | #define NUM_CHANNELS 34 | |
27 | ||
28 | /* Bit mask covering all logic channels. | |
29 | */ | |
30 | #define ALL_CHANNELS_MASK ((UINT64_C(1) << NUM_CHANNELS) - 1) | |
31 | ||
32 | /* Unit size for the sigrok logic datafeed. | |
33 | */ | |
34 | #define UNIT_SIZE ((NUM_CHANNELS + 7) / 8) | |
35 | ||
36 | /* Size of the acquisition buffer in device memory units. | |
37 | */ | |
38 | #define MEMORY_DEPTH (256 * 1024) /* 256k x 36 bit */ | |
39 | ||
40 | /* Capture memory read start address. | |
41 | */ | |
42 | #define READ_START_ADDR 4 | |
43 | ||
44 | /* Number of device memory units (36 bit) to read at a time. Slices of 8 | |
45 | * consecutive 36-bit words are mapped to 9 32-bit words each, so the chunk | |
46 | * length should be a multiple of 8 to ensure alignment to slice boundaries. | |
47 | * | |
48 | * Experimentation has shown that reading chunks larger than about 1024 bytes | |
49 | * is unreliable. The threshold seems to relate to the buffer size on the FX2 | |
50 | * USB chip: The configured endpoint buffer size is 512, and with double or | |
51 | * triple buffering enabled a multiple of 512 bytes can be kept in fly. | |
52 | * | |
53 | * The vendor software limits reads to 120 words (15 slices, 540 bytes) at | |
54 | * a time. So far, it appears safe to increase this to 224 words (28 slices, | |
55 | * 1008 bytes), thus making the most of two 512 byte buffers. | |
56 | */ | |
57 | #define READ_CHUNK_LEN (28 * 8) | |
58 | ||
59 | /* Bit mask for the RLE repeat-count-follows flag. | |
60 | */ | |
61 | #define RLE_FLAG_LEN_FOLLOWS (UINT64_C(1) << 35) | |
62 | ||
63 | /* Start index and count for bulk long register reads. | |
64 | * The first five long registers do not return useful values when read, | |
65 | * so skip over them to reduce the transfer size of status poll responses. | |
66 | */ | |
67 | #define READ_LREGS_START LREG_MEM_FILL | |
68 | #define READ_LREGS_COUNT (LREG_STATUS + 1 - READ_LREGS_START) | |
69 | ||
70 | /** LWLA1034 register addresses. | |
71 | */ | |
72 | enum reg_addr { | |
73 | REG_MEM_CTRL = 0x1074, /* capture buffer control */ | |
74 | REG_MEM_FILL = 0x1078, /* capture buffer fill level */ | |
75 | REG_MEM_START = 0x107C, /* capture buffer start address */ | |
76 | ||
77 | REG_CLK_BOOST = 0x1094, /* logic clock boost flag */ | |
78 | ||
79 | REG_LONG_STROBE = 0x10B0, /* long register read/write strobe */ | |
80 | REG_LONG_ADDR = 0x10B4, /* long register address */ | |
81 | REG_LONG_LOW = 0x10B8, /* long register low word */ | |
82 | REG_LONG_HIGH = 0x10BC, /* long register high word */ | |
83 | }; | |
84 | ||
85 | /** Flag bits for REG_MEM_CTRL. | |
86 | */ | |
87 | enum mem_ctrl_flag { | |
88 | MEM_CTRL_WRITE = 1 << 0, /* "wr1rd0" bit */ | |
89 | MEM_CTRL_CLR_IDX = 1 << 1, /* "clr_idx" bit */ | |
90 | }; | |
91 | ||
92 | /* LWLA1034 long register addresses. | |
93 | */ | |
94 | enum long_reg_addr { | |
95 | LREG_CHAN_MASK = 0, /* channel enable mask */ | |
96 | LREG_DIV_COUNT = 1, /* clock divider max count */ | |
97 | LREG_TRG_VALUE = 2, /* trigger level/slope bits */ | |
98 | LREG_TRG_TYPE = 3, /* trigger type bits (level or edge) */ | |
99 | LREG_TRG_ENABLE = 4, /* trigger enable mask */ | |
100 | LREG_MEM_FILL = 5, /* capture memory fill level or limit */ | |
101 | ||
102 | LREG_DURATION = 7, /* elapsed time in ms (0.8 ms at 125 MS/s) */ | |
103 | LREG_CHAN_STATE = 8, /* current logic levels at the inputs */ | |
104 | LREG_STATUS = 9, /* capture status flags */ | |
105 | ||
106 | LREG_CAP_CTRL = 10, /* capture control bits */ | |
107 | LREG_TEST_ID = 100, /* constant test ID */ | |
108 | }; | |
109 | ||
110 | /** Flag bits for LREG_CAP_CTRL. | |
111 | */ | |
112 | enum cap_ctrl_flag { | |
113 | CAP_CTRL_TRG_EN = 1 << 0, /* "trg_en" bit */ | |
114 | CAP_CTRL_CLR_TIMEBASE = 1 << 2, /* "do_clr_timebase" bit */ | |
115 | CAP_CTRL_FLUSH_FIFO = 1 << 4, /* "flush_fifo" bit */ | |
116 | CAP_CTRL_CLR_FIFOFULL = 1 << 5, /* "clr_fifo32_ful" bit */ | |
117 | CAP_CTRL_CLR_COUNTER = 1 << 6, /* "clr_cntr0" bit */ | |
118 | }; | |
119 | ||
120 | /* Available FPGA configurations. | |
121 | */ | |
122 | enum fpga_config { | |
123 | FPGA_OFF = 0, /* FPGA shutdown config */ | |
124 | FPGA_INT, /* internal clock config */ | |
125 | FPGA_EXTPOS, /* external clock, rising edge config */ | |
126 | FPGA_EXTNEG, /* external clock, falling edge config */ | |
127 | }; | |
128 | ||
129 | /* FPGA bitstream resource filenames. | |
130 | */ | |
131 | static const char bitstream_map[][32] = { | |
132 | [FPGA_OFF] = "sysclk-lwla1034-off.rbf", | |
133 | [FPGA_INT] = "sysclk-lwla1034-int.rbf", | |
134 | [FPGA_EXTPOS] = "sysclk-lwla1034-extpos.rbf", | |
135 | [FPGA_EXTNEG] = "sysclk-lwla1034-extneg.rbf", | |
136 | }; | |
137 | ||
138 | /* Read 64-bit long register. | |
139 | */ | |
140 | static int read_long_reg(const struct sr_usb_dev_inst *usb, | |
141 | uint32_t addr, uint64_t *value) | |
142 | { | |
143 | uint32_t low, high, dummy; | |
144 | int ret; | |
145 | ||
146 | ret = lwla_write_reg(usb, REG_LONG_ADDR, addr); | |
147 | if (ret != SR_OK) | |
148 | return ret; | |
149 | ||
150 | ret = lwla_read_reg(usb, REG_LONG_STROBE, &dummy); | |
151 | if (ret != SR_OK) | |
152 | return ret; | |
153 | ||
154 | ret = lwla_read_reg(usb, REG_LONG_HIGH, &high); | |
155 | if (ret != SR_OK) | |
156 | return ret; | |
157 | ||
158 | ret = lwla_read_reg(usb, REG_LONG_LOW, &low); | |
159 | if (ret != SR_OK) | |
160 | return ret; | |
161 | ||
162 | *value = ((uint64_t)high << 32) | low; | |
163 | ||
164 | return SR_OK; | |
165 | } | |
166 | ||
167 | /* Queue access sequence for a long register write. | |
168 | */ | |
169 | static void queue_long_regval(struct acquisition_state *acq, | |
170 | uint32_t addr, uint64_t value) | |
171 | { | |
172 | lwla_queue_regval(acq, REG_LONG_ADDR, addr); | |
173 | lwla_queue_regval(acq, REG_LONG_LOW, value & 0xFFFFFFFF); | |
174 | lwla_queue_regval(acq, REG_LONG_HIGH, value >> 32); | |
175 | lwla_queue_regval(acq, REG_LONG_STROBE, 0); | |
176 | } | |
177 | ||
178 | /* Helper to fill in the long register bulk write command. | |
179 | */ | |
180 | static inline void bulk_long_set(struct acquisition_state *acq, | |
181 | unsigned int idx, uint64_t value) | |
182 | { | |
183 | acq->xfer_buf_out[4 * idx + 3] = LWLA_WORD_0(value); | |
184 | acq->xfer_buf_out[4 * idx + 4] = LWLA_WORD_1(value); | |
185 | acq->xfer_buf_out[4 * idx + 5] = LWLA_WORD_2(value); | |
186 | acq->xfer_buf_out[4 * idx + 6] = LWLA_WORD_3(value); | |
187 | } | |
188 | ||
189 | /* Helper for dissecting the response to a long register bulk read. | |
190 | */ | |
191 | static inline uint64_t bulk_long_get(const struct acquisition_state *acq, | |
192 | unsigned int idx) | |
193 | { | |
194 | uint64_t low, high; | |
195 | ||
196 | low = LWLA_TO_UINT32(acq->xfer_buf_in[2 * (idx - READ_LREGS_START)]); | |
197 | high = LWLA_TO_UINT32(acq->xfer_buf_in[2 * (idx - READ_LREGS_START) + 1]); | |
198 | ||
199 | return (high << 32) | low; | |
200 | } | |
201 | ||
202 | /* Demangle and decompress incoming sample data from the transfer buffer. | |
203 | * The data chunk is taken from the acquisition state, and is expected to | |
204 | * contain a multiple of 8 packed 36-bit words. | |
205 | */ | |
206 | static void read_response(struct acquisition_state *acq) | |
207 | { | |
208 | uint64_t sample, high_nibbles, word; | |
209 | uint32_t *slice; | |
210 | uint8_t *out_p; | |
211 | unsigned int words_left, max_samples, run_samples, wi, ri, si; | |
212 | ||
213 | /* Number of 36-bit words remaining in the transfer buffer. */ | |
214 | words_left = MIN(acq->mem_addr_next, acq->mem_addr_stop) | |
215 | - acq->mem_addr_done; | |
216 | ||
217 | for (wi = 0;; wi++) { | |
218 | /* Calculate number of samples to write into packet. */ | |
219 | max_samples = MIN(acq->samples_max - acq->samples_done, | |
220 | PACKET_SIZE / UNIT_SIZE - acq->out_index); | |
221 | run_samples = MIN(max_samples, acq->run_len); | |
222 | ||
223 | /* Expand run-length samples into session packet. */ | |
224 | sample = acq->sample; | |
225 | out_p = &acq->out_packet[acq->out_index * UNIT_SIZE]; | |
226 | ||
227 | for (ri = 0; ri < run_samples; ri++) { | |
228 | out_p[0] = sample & 0xFF; | |
229 | out_p[1] = (sample >> 8) & 0xFF; | |
230 | out_p[2] = (sample >> 16) & 0xFF; | |
231 | out_p[3] = (sample >> 24) & 0xFF; | |
232 | out_p[4] = (sample >> 32) & 0xFF; | |
233 | out_p += UNIT_SIZE; | |
234 | } | |
235 | acq->run_len -= run_samples; | |
236 | acq->out_index += run_samples; | |
237 | acq->samples_done += run_samples; | |
238 | ||
239 | if (run_samples == max_samples) | |
240 | break; /* Packet full or sample limit reached. */ | |
241 | if (wi >= words_left) | |
242 | break; /* Done with current transfer. */ | |
243 | ||
244 | /* Get the current slice of 8 packed 36-bit words. */ | |
245 | slice = &acq->xfer_buf_in[(acq->in_index + wi) / 8 * 9]; | |
246 | si = (acq->in_index + wi) % 8; /* Word index within slice. */ | |
247 | ||
248 | /* Extract the next 36-bit word. */ | |
249 | high_nibbles = LWLA_TO_UINT32(slice[8]); | |
250 | word = LWLA_TO_UINT32(slice[si]); | |
251 | word |= (high_nibbles << (4 * si + 4)) & (UINT64_C(0xF) << 32); | |
252 | ||
253 | if (acq->rle == RLE_STATE_DATA) { | |
254 | acq->sample = word & ALL_CHANNELS_MASK; | |
255 | acq->run_len = ((word >> NUM_CHANNELS) & 1) + 1; | |
256 | acq->rle = ((word & RLE_FLAG_LEN_FOLLOWS) != 0) | |
257 | ? RLE_STATE_LEN : RLE_STATE_DATA; | |
258 | } else { | |
259 | acq->run_len += word << 1; | |
260 | acq->rle = RLE_STATE_DATA; | |
261 | } | |
262 | } | |
263 | ||
264 | acq->in_index += wi; | |
265 | acq->mem_addr_done += wi; | |
266 | } | |
267 | ||
268 | /* Check whether we can receive responses of more than 64 bytes. | |
269 | * The FX2 firmware of the LWLA1034 has a bug in the reset logic which | |
270 | * sometimes causes the response endpoint to be limited to transfers of | |
271 | * 64 bytes at a time, instead of the expected 2*512 bytes. The problem | |
272 | * can be worked around by never requesting more than 64 bytes. | |
273 | * This quirk manifests itself only under certain conditions, and some | |
274 | * users seem to see it more frequently than others. Detect it here in | |
275 | * order to avoid paying the penalty unnecessarily. | |
276 | */ | |
277 | static int detect_short_transfer_quirk(const struct sr_dev_inst *sdi) | |
278 | { | |
279 | struct dev_context *devc; | |
280 | struct sr_usb_dev_inst *usb; | |
281 | int xfer_len, ret; | |
282 | uint16_t command[3]; | |
283 | unsigned char buf[512]; | |
284 | const int lreg_count = 10; | |
285 | ||
286 | devc = sdi->priv; | |
287 | usb = sdi->conn; | |
288 | ||
289 | command[0] = LWLA_WORD(CMD_READ_LREGS); | |
290 | command[1] = LWLA_WORD(0); | |
291 | command[2] = LWLA_WORD(lreg_count); | |
292 | ||
293 | ret = lwla_send_command(usb, command, ARRAY_SIZE(command)); | |
294 | if (ret != SR_OK) | |
295 | return ret; | |
296 | ||
297 | ret = lwla_receive_reply(usb, buf, sizeof(buf), &xfer_len); | |
298 | if (ret != SR_OK) | |
299 | return ret; | |
300 | ||
301 | devc->short_transfer_quirk = (xfer_len == 64); | |
302 | ||
303 | if (xfer_len == 8 * lreg_count) | |
304 | return SR_OK; | |
305 | ||
306 | if (xfer_len == 64) { | |
307 | /* Drain the tailing portion of the split transfer. */ | |
308 | ret = lwla_receive_reply(usb, buf, sizeof(buf), &xfer_len); | |
309 | if (ret != SR_OK) | |
310 | return ret; | |
311 | ||
312 | if (xfer_len == 8 * lreg_count - 64) | |
313 | return SR_OK; | |
314 | } | |
315 | sr_err("Received response of unexpected length %d.", xfer_len); | |
316 | ||
317 | return SR_ERR; | |
318 | } | |
319 | ||
320 | /* Select and transfer FPGA bitstream for the current configuration. | |
321 | */ | |
322 | static int apply_fpga_config(const struct sr_dev_inst *sdi) | |
323 | { | |
324 | struct dev_context *devc; | |
325 | struct drv_context *drvc; | |
326 | int config, ret; | |
327 | ||
328 | devc = sdi->priv; | |
329 | drvc = sdi->driver->context; | |
330 | ||
331 | if (sdi->status == SR_ST_INACTIVE) | |
332 | config = FPGA_OFF; | |
333 | else if (devc->cfg_clock_source == CLOCK_INTERNAL) | |
334 | config = FPGA_INT; | |
335 | else if (devc->cfg_clock_edge == EDGE_POSITIVE) | |
336 | config = FPGA_EXTPOS; | |
337 | else | |
338 | config = FPGA_EXTNEG; | |
339 | ||
340 | if (config == devc->active_fpga_config) | |
341 | return SR_OK; /* No change. */ | |
342 | ||
343 | ret = lwla_send_bitstream(drvc->sr_ctx, sdi->conn, | |
344 | bitstream_map[config]); | |
345 | devc->active_fpga_config = (ret == SR_OK) ? config : FPGA_NOCONF; | |
346 | ||
347 | return ret; | |
348 | } | |
349 | ||
350 | /* Perform initialization self test. | |
351 | */ | |
352 | static int device_init_check(const struct sr_dev_inst *sdi) | |
353 | { | |
354 | uint64_t value; | |
355 | int ret; | |
356 | ||
357 | read_long_reg(sdi->conn, LREG_TEST_ID, &value); | |
358 | ||
359 | /* Ignore the value returned by the first read. */ | |
360 | ret = read_long_reg(sdi->conn, LREG_TEST_ID, &value); | |
361 | if (ret != SR_OK) | |
362 | return ret; | |
363 | ||
364 | if (value != UINT64_C(0x1234567887654321)) { | |
365 | sr_err("Received invalid test word 0x%016" PRIX64 ".", value); | |
366 | return SR_ERR; | |
367 | } | |
368 | ||
369 | return detect_short_transfer_quirk(sdi); | |
370 | } | |
371 | ||
372 | /* Set up the device in preparation for an acquisition session. | |
373 | */ | |
374 | static int setup_acquisition(const struct sr_dev_inst *sdi) | |
375 | { | |
376 | static const struct regval capture_init[] = { | |
377 | {REG_MEM_CTRL, MEM_CTRL_CLR_IDX}, | |
378 | {REG_MEM_CTRL, MEM_CTRL_WRITE}, | |
379 | {REG_LONG_ADDR, LREG_CAP_CTRL}, | |
380 | {REG_LONG_LOW, CAP_CTRL_CLR_TIMEBASE | CAP_CTRL_FLUSH_FIFO | | |
381 | CAP_CTRL_CLR_FIFOFULL | CAP_CTRL_CLR_COUNTER}, | |
382 | {REG_LONG_HIGH, 0}, | |
383 | {REG_LONG_STROBE, 0}, | |
384 | }; | |
385 | uint64_t divider_count, trigger_mask; | |
386 | struct dev_context *devc; | |
387 | struct sr_usb_dev_inst *usb; | |
388 | struct acquisition_state *acq; | |
389 | int ret; | |
390 | ||
391 | devc = sdi->priv; | |
392 | usb = sdi->conn; | |
393 | acq = devc->acquisition; | |
394 | ||
395 | ret = lwla_write_regs(usb, capture_init, ARRAY_SIZE(capture_init)); | |
396 | if (ret != SR_OK) | |
397 | return ret; | |
398 | ||
399 | ret = lwla_write_reg(usb, REG_CLK_BOOST, acq->clock_boost); | |
400 | if (ret != SR_OK) | |
401 | return ret; | |
402 | ||
403 | acq->xfer_buf_out[0] = LWLA_WORD(CMD_WRITE_LREGS); | |
404 | acq->xfer_buf_out[1] = LWLA_WORD(0); | |
405 | acq->xfer_buf_out[2] = LWLA_WORD(LREG_STATUS + 1); | |
406 | ||
407 | bulk_long_set(acq, LREG_CHAN_MASK, devc->channel_mask); | |
408 | ||
409 | if (devc->samplerate > 0 && devc->samplerate <= SR_MHZ(100) | |
410 | && !acq->clock_boost) | |
411 | divider_count = SR_MHZ(100) / devc->samplerate - 1; | |
412 | else | |
413 | divider_count = 0; | |
414 | ||
415 | bulk_long_set(acq, LREG_DIV_COUNT, divider_count); | |
416 | bulk_long_set(acq, LREG_TRG_VALUE, devc->trigger_values); | |
417 | bulk_long_set(acq, LREG_TRG_TYPE, devc->trigger_edge_mask); | |
418 | ||
419 | trigger_mask = devc->trigger_mask; | |
420 | ||
421 | /* Set bits to select external TRG input edge. */ | |
422 | if (devc->cfg_trigger_source == TRIGGER_EXT_TRG) | |
423 | switch (devc->cfg_trigger_slope) { | |
424 | case EDGE_POSITIVE: | |
425 | trigger_mask |= UINT64_C(1) << 35; | |
426 | break; | |
427 | case EDGE_NEGATIVE: | |
428 | trigger_mask |= UINT64_C(1) << 34; | |
429 | break; | |
430 | } | |
431 | ||
432 | bulk_long_set(acq, LREG_TRG_ENABLE, trigger_mask); | |
433 | ||
434 | /* Set the capture memory full threshold. This is slightly less | |
435 | * than the actual maximum, most likely in order to compensate for | |
436 | * pipeline latency. | |
437 | */ | |
438 | bulk_long_set(acq, LREG_MEM_FILL, MEMORY_DEPTH - 16); | |
439 | ||
440 | /* Fill remaining words with zeroes. */ | |
441 | bulk_long_set(acq, 6, 0); | |
442 | bulk_long_set(acq, LREG_DURATION, 0); | |
443 | bulk_long_set(acq, LREG_CHAN_STATE, 0); | |
444 | bulk_long_set(acq, LREG_STATUS, 0); | |
445 | ||
446 | return lwla_send_command(sdi->conn, acq->xfer_buf_out, | |
447 | 3 + (LREG_STATUS + 1) * 4); | |
448 | } | |
449 | ||
450 | static int prepare_request(const struct sr_dev_inst *sdi) | |
451 | { | |
452 | struct dev_context *devc; | |
453 | struct acquisition_state *acq; | |
454 | unsigned int chunk_len, remaining, count; | |
455 | ||
456 | devc = sdi->priv; | |
457 | acq = devc->acquisition; | |
458 | ||
459 | acq->xfer_out->length = 0; | |
460 | acq->reg_seq_pos = 0; | |
461 | acq->reg_seq_len = 0; | |
462 | ||
463 | switch (devc->state) { | |
464 | case STATE_START_CAPTURE: | |
465 | queue_long_regval(acq, LREG_CAP_CTRL, CAP_CTRL_TRG_EN); | |
466 | break; | |
467 | case STATE_STOP_CAPTURE: | |
468 | queue_long_regval(acq, LREG_CAP_CTRL, 0); | |
469 | lwla_queue_regval(acq, REG_CLK_BOOST, 0); | |
470 | break; | |
471 | case STATE_READ_PREPARE: | |
472 | lwla_queue_regval(acq, REG_CLK_BOOST, 1); | |
473 | lwla_queue_regval(acq, REG_MEM_CTRL, MEM_CTRL_CLR_IDX); | |
474 | lwla_queue_regval(acq, REG_MEM_START, READ_START_ADDR); | |
475 | break; | |
476 | case STATE_READ_FINISH: | |
477 | lwla_queue_regval(acq, REG_CLK_BOOST, 0); | |
478 | break; | |
479 | case STATE_STATUS_REQUEST: | |
480 | acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_LREGS); | |
481 | acq->xfer_buf_out[1] = LWLA_WORD(READ_LREGS_START); | |
482 | acq->xfer_buf_out[2] = LWLA_WORD(READ_LREGS_COUNT); | |
483 | acq->xfer_out->length = 3 * sizeof(acq->xfer_buf_out[0]); | |
484 | break; | |
485 | case STATE_LENGTH_REQUEST: | |
486 | lwla_queue_regval(acq, REG_MEM_FILL, 0); | |
487 | break; | |
488 | case STATE_READ_REQUEST: | |
489 | /* Limit reads to 8 device words (36 bytes) at a time if the | |
490 | * device firmware has the short transfer quirk. */ | |
491 | chunk_len = (devc->short_transfer_quirk) ? 8 : READ_CHUNK_LEN; | |
492 | /* Always read a multiple of 8 device words. */ | |
493 | remaining = (acq->mem_addr_stop - acq->mem_addr_next + 7) / 8 * 8; | |
494 | count = MIN(chunk_len, remaining); | |
495 | ||
496 | acq->xfer_buf_out[0] = LWLA_WORD(CMD_READ_MEM36); | |
497 | acq->xfer_buf_out[1] = LWLA_WORD_0(acq->mem_addr_next); | |
498 | acq->xfer_buf_out[2] = LWLA_WORD_1(acq->mem_addr_next); | |
499 | acq->xfer_buf_out[3] = LWLA_WORD_0(count); | |
500 | acq->xfer_buf_out[4] = LWLA_WORD_1(count); | |
501 | acq->xfer_out->length = 5 * sizeof(acq->xfer_buf_out[0]); | |
502 | ||
503 | acq->mem_addr_next += count; | |
504 | break; | |
505 | default: | |
506 | sr_err("BUG: unhandled request state %d.", devc->state); | |
507 | return SR_ERR_BUG; | |
508 | } | |
509 | ||
510 | return SR_OK; | |
511 | } | |
512 | ||
513 | static int handle_response(const struct sr_dev_inst *sdi) | |
514 | { | |
515 | struct dev_context *devc; | |
516 | struct acquisition_state *acq; | |
517 | int expect_len; | |
518 | ||
519 | devc = sdi->priv; | |
520 | acq = devc->acquisition; | |
521 | ||
522 | switch (devc->state) { | |
523 | case STATE_STATUS_REQUEST: | |
524 | if (acq->xfer_in->actual_length != READ_LREGS_COUNT * 8) { | |
525 | sr_err("Received size %d doesn't match expected size %d.", | |
526 | acq->xfer_in->actual_length, READ_LREGS_COUNT * 8); | |
527 | return SR_ERR; | |
528 | } | |
529 | acq->mem_addr_fill = bulk_long_get(acq, LREG_MEM_FILL) & 0xFFFFFFFF; | |
530 | acq->duration_now = bulk_long_get(acq, LREG_DURATION); | |
531 | /* Shift left by one so the bit positions match the LWLA1016. */ | |
532 | acq->status = (bulk_long_get(acq, LREG_STATUS) & 0x3F) << 1; | |
533 | /* | |
534 | * It seems that the 125 MS/s mode is implemented simply by | |
535 | * running the FPGA logic at a 25% higher clock rate. As a | |
536 | * result, the millisecond counter for the capture duration | |
537 | * is also off by 25%, and thus needs to be corrected here. | |
538 | */ | |
539 | if (acq->clock_boost) | |
540 | acq->duration_now = acq->duration_now * 4 / 5; | |
541 | break; | |
542 | case STATE_LENGTH_REQUEST: | |
543 | acq->mem_addr_next = READ_START_ADDR; | |
544 | acq->mem_addr_stop = acq->reg_sequence[0].val; | |
545 | break; | |
546 | case STATE_READ_REQUEST: | |
547 | /* Expect a multiple of 8 36-bit words packed into 9 32-bit | |
548 | * words. */ | |
549 | expect_len = (acq->mem_addr_next - acq->mem_addr_done | |
550 | + acq->in_index + 7) / 8 * 9 * sizeof(acq->xfer_buf_in[0]); | |
551 | ||
552 | if (acq->xfer_in->actual_length != expect_len) { | |
553 | sr_err("Received size %d does not match expected size %d.", | |
554 | acq->xfer_in->actual_length, expect_len); | |
555 | devc->transfer_error = TRUE; | |
556 | return SR_ERR; | |
557 | } | |
558 | read_response(acq); | |
559 | break; | |
560 | default: | |
561 | sr_err("BUG: unhandled response state %d.", devc->state); | |
562 | return SR_ERR_BUG; | |
563 | } | |
564 | ||
565 | return SR_OK; | |
566 | } | |
567 | ||
568 | /** Model descriptor for the LWLA1034. | |
569 | */ | |
570 | SR_PRIV const struct model_info lwla1034_info = { | |
571 | .name = "LWLA1034", | |
572 | .num_channels = NUM_CHANNELS, | |
573 | ||
574 | .num_devopts = 8, | |
575 | .devopts = { | |
576 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
577 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
578 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
579 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, | |
580 | SR_CONF_EXTERNAL_CLOCK | SR_CONF_GET | SR_CONF_SET, | |
581 | SR_CONF_CLOCK_EDGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
582 | SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
583 | SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
584 | }, | |
585 | .num_samplerates = 20, | |
586 | .samplerates = { | |
587 | SR_MHZ(125), SR_MHZ(100), | |
588 | SR_MHZ(50), SR_MHZ(20), SR_MHZ(10), | |
589 | SR_MHZ(5), SR_MHZ(2), SR_MHZ(1), | |
590 | SR_KHZ(500), SR_KHZ(200), SR_KHZ(100), | |
591 | SR_KHZ(50), SR_KHZ(20), SR_KHZ(10), | |
592 | SR_KHZ(5), SR_KHZ(2), SR_KHZ(1), | |
593 | SR_HZ(500), SR_HZ(200), SR_HZ(100), | |
594 | }, | |
595 | ||
596 | .apply_fpga_config = &apply_fpga_config, | |
597 | .device_init_check = &device_init_check, | |
598 | .setup_acquisition = &setup_acquisition, | |
599 | ||
600 | .prepare_request = &prepare_request, | |
601 | .handle_response = &handle_response, | |
602 | }; |