]> sigrok.org Git - libsigrok.git/blame - src/hardware/sysclk-lwla/lwla.h
sysclk-lwla: Implement support for LWLA1016
[libsigrok.git] / src / hardware / sysclk-lwla / lwla.h
CommitLineData
5874e88d
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 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#ifndef LIBSIGROK_HARDWARE_SYSCLK_LWLA_LWLA_H
21#define LIBSIGROK_HARDWARE_SYSCLK_LWLA_LWLA_H
22
5874e88d
DE
23#include <stdint.h>
24#include <libusb.h>
25#include <glib.h>
c1aae900 26#include <libsigrok/libsigrok.h>
5874e88d
DE
27
28struct sr_usb_dev_inst;
29
e0df15d4
DE
30/* Rotate argument n bits to the left.
31 * This construct is an idiom recognized by GCC as bit rotation.
32 */
33#define LROTATE(a, n) (((a) << (n)) | ((a) >> (CHAR_BIT * sizeof(a) - (n))))
34
35/* Convert 16-bit little endian LWLA protocol word to machine word order. */
36#define LWLA_TO_UINT16(val) GUINT16_FROM_LE(val)
37
38/* Convert 32-bit mixed endian LWLA protocol word to machine word order. */
39#define LWLA_TO_UINT32(val) LROTATE(GUINT32_FROM_LE(val), 16)
40
41/* Convert 16-bit argument to LWLA protocol word. */
5874e88d
DE
42#define LWLA_WORD(val) GUINT16_TO_LE(val)
43
e0df15d4
DE
44/* Extract 16-bit units in mixed endian order from 32/64-bit value. */
45#define LWLA_WORD_0(val) GUINT16_TO_LE(((val) >> 16) & 0xFFFF)
46#define LWLA_WORD_1(val) GUINT16_TO_LE((val) & 0xFFFF)
47#define LWLA_WORD_2(val) GUINT16_TO_LE(((val) >> 48) & 0xFFFF)
48#define LWLA_WORD_3(val) GUINT16_TO_LE(((val) >> 32) & 0xFFFF)
5874e88d 49
be64f90b
DE
50/* Maximum number of 16-bit words sent at a time during acquisition.
51 * Used for allocating the libusb transfer buffer.
52 */
53#define MAX_ACQ_SEND_LEN16 64 /* 43 for capture setup plus stuffing */
54
55/* Maximum number of 32-bit words received at a time during acquisition.
56 * This is a multiple of the endpoint buffer size to avoid transfer overflow
57 * conditions.
58 */
59#define MAX_ACQ_RECV_LEN32 (2 * 512 / 4)
60
61/* Maximum length of a register read/write sequence.
62 */
63#define MAX_REG_SEQ_LEN 16
64
65/* Logic datafeed packet size in bytes.
66 * This is a multiple of both 4 and 5 to match any model's unit size
67 * and memory granularity.
68 */
69#define PACKET_SIZE (5000 * 4 * 5)
70
5874e88d
DE
71/** USB device end points.
72 */
be64f90b
DE
73enum usb_endpoint {
74 EP_COMMAND = 2,
75 EP_CONFIG = 4,
76 EP_REPLY = 6 | LIBUSB_ENDPOINT_IN
5874e88d
DE
77};
78
79/** LWLA protocol command ID codes.
80 */
be64f90b 81enum command_id {
5874e88d
DE
82 CMD_READ_REG = 1,
83 CMD_WRITE_REG = 2,
be64f90b
DE
84 CMD_READ_MEM32 = 3,
85 CMD_READ_MEM36 = 6,
86 CMD_WRITE_LREGS = 7,
87 CMD_READ_LREGS = 8,
5874e88d
DE
88};
89
90/** LWLA capture state flags.
be64f90b 91 * The bit positions are the same as in the LWLA1016 control register.
5874e88d
DE
92 */
93enum {
be64f90b
DE
94 STATUS_CAPTURING = 1 << 2,
95 STATUS_TRIGGERED = 1 << 5,
96 STATUS_MEM_AVAIL = 1 << 6,
5874e88d
DE
97};
98
be64f90b 99/** LWLA1034 run-length encoding states.
5874e88d 100 */
be64f90b
DE
101enum rle_state {
102 RLE_STATE_DATA,
103 RLE_STATE_LEN
586ff70a
DE
104};
105
be64f90b 106/** Register address/value pair.
30f34dbd 107 */
be64f90b
DE
108struct regval {
109 unsigned int reg;
110 uint32_t val;
30f34dbd
DE
111};
112
be64f90b 113/** LWLA sample acquisition and decompression state.
30f34dbd 114 */
be64f90b
DE
115struct acquisition_state {
116 uint64_t samples_max; /* maximum number of samples to process */
117 uint64_t samples_done; /* number of samples sent to the session bus */
118 uint64_t duration_max; /* maximum capture duration in milliseconds */
119 uint64_t duration_now; /* running capture duration since trigger */
120
121 uint64_t sample; /* last sample read from capture memory */
122 uint64_t run_len; /* remaining run length of current sample */
123
124 struct libusb_transfer *xfer_in; /* USB in transfer record */
125 struct libusb_transfer *xfer_out; /* USB out transfer record */
126
127 size_t mem_addr_fill; /* capture memory fill level */
128 size_t mem_addr_done; /* position up to which data was received */
129 size_t mem_addr_next; /* start address for next async read */
130 size_t mem_addr_stop; /* end of memory range to be read */
131 size_t in_index; /* position in read transfer buffer */
132 size_t out_index; /* position in logic packet buffer */
133 enum rle_state rle; /* RLE decoding state */
134
135 gboolean rle_enabled; /* capturing in timing-state mode */
136 gboolean clock_boost; /* switch to faster clock during capture */
137 unsigned int status; /* last received device status */
138
139 unsigned int reg_seq_pos; /* index of next register/value pair */
140 unsigned int reg_seq_len; /* length of register/value sequence */
141
142 struct regval reg_sequence[MAX_REG_SEQ_LEN]; /* register buffer */
143 uint32_t xfer_buf_in[MAX_ACQ_RECV_LEN32]; /* USB in buffer */
144 uint16_t xfer_buf_out[MAX_ACQ_SEND_LEN16]; /* USB out buffer */
145 uint8_t out_packet[PACKET_SIZE]; /* logic payload */
30f34dbd
DE
146};
147
be64f90b
DE
148static inline void lwla_queue_regval(struct acquisition_state *acq,
149 unsigned int reg, uint32_t value)
150{
151 acq->reg_sequence[acq->reg_seq_len].reg = reg;
152 acq->reg_sequence[acq->reg_seq_len].val = value;
153 acq->reg_seq_len++;
154}
5874e88d 155
8e2d6c9d
DE
156SR_PRIV int lwla_send_bitstream(struct sr_context *ctx,
157 const struct sr_usb_dev_inst *usb,
158 const char *name);
5874e88d
DE
159
160SR_PRIV int lwla_send_command(const struct sr_usb_dev_inst *usb,
161 const uint16_t *command, int cmd_len);
162
163SR_PRIV int lwla_receive_reply(const struct sr_usb_dev_inst *usb,
e0df15d4 164 uint32_t *reply, int reply_len, int expect_len);
5874e88d
DE
165
166SR_PRIV int lwla_read_reg(const struct sr_usb_dev_inst *usb,
167 uint16_t reg, uint32_t *value);
168
169SR_PRIV int lwla_write_reg(const struct sr_usb_dev_inst *usb,
170 uint16_t reg, uint32_t value);
171
172SR_PRIV int lwla_write_regs(const struct sr_usb_dev_inst *usb,
be64f90b 173 const struct regval *regvals, int count);
5874e88d 174
db24496a 175#endif