summaryrefslogtreecommitdiff
path: root/Radio/HW/BladeRF/fpga_common/include/nios_pkt_32x32.h
blob: e77cdee900d836eaaa98e3116b4e3cc96d721cfa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Copyright (c) 2015 Nuand LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef BLADERF_NIOS_PKT_32x32_H_
#define BLADERF_NIOS_PKT_32x32_H_

#include <stdint.h>
#include <stdbool.h>
#include <string.h>

/*
 * This file defines the Host <-> FPGA (NIOS II) packet formats for accesses
 * to devices/blocks with 32-bit addresses and 32-bit data
 *
 *
 *                              Request
 *                      ----------------------
 *
 * +================+=========================================================+
 * |  Byte offset   |                       Description                       |
 * +================+=========================================================+
 * |        0       | Magic Value                                             |
 * +----------------+---------------------------------------------------------+
 * |        1       | Target ID (Note 1)                                      |
 * +----------------+---------------------------------------------------------+
 * |        2       | Flags (Note 2)                                          |
 * +----------------+---------------------------------------------------------+
 * |        3       | Reserved. Set to 0x00.                                  |
 * +----------------+---------------------------------------------------------+
 * |       7:4      | 32-bit address                                           |
 * +----------------+---------------------------------------------------------+
 * |      11:8      | 32-bit data, little-endian                              |
 * +----------------+---------------------------------------------------------+
 * |      15:12     | Reserved. Set to 0.                                     |
 * +----------------+---------------------------------------------------------+
 *
 *
 *                              Response
 *                      ----------------------
 *
 * The response packet contains the same information as the request.
 * A status flag will be set if the operation completed successfully.
 *
 * In the case of a read request, the data field will contain the read data, if
 * the read succeeded.
 *
 * (Note 1)
 *  The "Target ID" refers to the peripheral, device, or block to access.
 *  See the NIOS_PKT_32x32_TARGET_* values.
 *
 * (Note 2)
 *  The flags are defined as follows:
 *
 *    +================+========================+
 *    |      Bit(s)    |         Value          |
 *    +================+========================+
 *    |       7:2      | Reserved. Set to 0.    |
 *    +----------------+------------------------+
 *    |                | Status. Only used in   |
 *    |                | response packet.       |
 *    |                | Ignored in request.    |
 *    |        1       |                        |
 *    |                |   1 = Success          |
 *    |                |   0 = Failure          |
 *    +----------------+------------------------+
 *    |        0       |   0 = Read operation   |
 *    |                |   1 = Write operation  |
 *    +----------------+------------------------+
 *
 */

#define NIOS_PKT_32x32_MAGIC         ((uint8_t) 'K')

/* Request packet indices */
#define NIOS_PKT_32x32_IDX_MAGIC      0
#define NIOS_PKT_32x32_IDX_TARGET_ID  1
#define NIOS_PKT_32x32_IDX_FLAGS      2
#define NIOS_PKT_32x32_IDX_RESV1      3
#define NIOS_PKT_32x32_IDX_ADDR       4
#define NIOS_PKT_32x32_IDX_DATA       8
#define NIOS_PKT_32x32_IDX_RESV2      12

/* Target IDs */

/* For the EXP and EXP_DIR targets, the address is a bitmask of values
 * to read/write */
#define NIOS_PKT_32x32_TARGET_EXP     0x00 /* Expansion I/O */
#define NIOS_PKT_32x32_TARGET_EXP_DIR 0x01 /* Expansion I/O Direction reg */
#define NIOS_PKT_32x32_TARGET_ADI_AXI 0x02 /* ADI AXI Interface */
#define NIOS_PKT_32x32_TARGET_WB_MSTR 0x03 /* Wishbone Master */

/* IDs 0x80 through 0xff will not be assigned by Nuand. These are reserved
 * for user customizations */
#define NIOS_PKT_32x32_TARGET_USR1    0x80
#define NIOS_PKT_32x32_TARGET_USR128  0xff

/* Flag bits */
#define NIOS_PKT_32x32_FLAG_WRITE     (1 << 0)
#define NIOS_PKT_32x32_FLAG_SUCCESS   (1 << 1)


/* Pack the request buffer */
static inline void nios_pkt_32x32_pack(uint8_t *buf, uint8_t target, bool write,
                                       uint32_t addr, uint32_t data)
{
    buf[NIOS_PKT_32x32_IDX_MAGIC]     = NIOS_PKT_32x32_MAGIC;
    buf[NIOS_PKT_32x32_IDX_TARGET_ID] = target;

    if (write) {
        buf[NIOS_PKT_32x32_IDX_FLAGS] = NIOS_PKT_32x32_FLAG_WRITE;
    } else {
        buf[NIOS_PKT_32x32_IDX_FLAGS] = 0x00;
    }

    buf[NIOS_PKT_32x32_IDX_RESV1] = 0x00;

    buf[NIOS_PKT_32x32_IDX_ADDR + 0] = (addr >> 0);
    buf[NIOS_PKT_32x32_IDX_ADDR + 1] = (addr >> 8);
    buf[NIOS_PKT_32x32_IDX_ADDR + 2] = (addr >> 16);
    buf[NIOS_PKT_32x32_IDX_ADDR + 3] = (addr >> 24);

    buf[NIOS_PKT_32x32_IDX_DATA + 0] = (data >> 0);
    buf[NIOS_PKT_32x32_IDX_DATA + 1] = (data >> 8);
    buf[NIOS_PKT_32x32_IDX_DATA + 2] = (data >> 16);
    buf[NIOS_PKT_32x32_IDX_DATA + 3] = (data >> 24);

    buf[NIOS_PKT_32x32_IDX_RESV2 + 0] = 0x00;
    buf[NIOS_PKT_32x32_IDX_RESV2 + 1] = 0x00;
    buf[NIOS_PKT_32x32_IDX_RESV2 + 2] = 0x00;
    buf[NIOS_PKT_32x32_IDX_RESV2 + 3] = 0x00;
}

/* Unpack the request buffer */
static inline void nios_pkt_32x32_unpack(const uint8_t *buf, uint8_t *target,
                                        bool *write, uint32_t *addr,
                                        uint32_t *data)
{
    if (target != NULL) {
        *target = buf[NIOS_PKT_32x32_IDX_TARGET_ID];
    }

    if (write != NULL) {
        *write  = (buf[NIOS_PKT_32x32_IDX_FLAGS] & NIOS_PKT_32x32_FLAG_WRITE) != 0;
    }

    if (addr != NULL) {
        *addr   = (buf[NIOS_PKT_32x32_IDX_ADDR + 0] << 0)  |
                  (buf[NIOS_PKT_32x32_IDX_ADDR + 1] << 8)  |
                  (buf[NIOS_PKT_32x32_IDX_ADDR + 2] << 16) |
                  (buf[NIOS_PKT_32x32_IDX_ADDR + 3] << 24);
    }


    if (data != NULL) {
        *data   = (buf[NIOS_PKT_32x32_IDX_DATA + 0] << 0)  |
                  (buf[NIOS_PKT_32x32_IDX_DATA + 1] << 8)  |
                  (buf[NIOS_PKT_32x32_IDX_DATA + 2] << 16) |
                  (buf[NIOS_PKT_32x32_IDX_DATA + 3] << 24);
    }
}

/* Pack the response buffer */
static inline void nios_pkt_32x32_resp_pack(uint8_t *buf, uint8_t target,
                                            bool write, uint32_t addr,
                                            uint32_t data, bool success)
{
    nios_pkt_32x32_pack(buf, target, write, addr, data);

    if (success) {
        buf[NIOS_PKT_32x32_IDX_FLAGS] |= NIOS_PKT_32x32_FLAG_SUCCESS;
    }
}

/* Unpack the response buffer */
static inline void nios_pkt_32x32_resp_unpack(const uint8_t *buf,
                                             uint8_t *target, bool *write,
                                             uint32_t *addr, uint32_t *data,
                                             bool *success)
{
    nios_pkt_32x32_unpack(buf, target, write, addr, data);

    if ((buf[NIOS_PKT_32x32_IDX_FLAGS] & NIOS_PKT_32x32_FLAG_SUCCESS) != 0) {
        *success = true;
    } else {
        *success = false;
    }
}

#endif