aboutsummaryrefslogtreecommitdiffstats
path: root/fpga/usrp3/top/x400/cpld/ctrlport_to_spi.v
blob: d4b72a72d96efb51df83fcd91c6baa049bf7fea7 (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// Copyright 2021 Ettus Research, A National Instruments Brand
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// Module: ctrlport_to_spi
//
// Description:
//
//   This module wraps a SPI master and provides a ControlPort interface.
//
// Parameters:
//
//   BASE_ADDRESS : Base address for CtrlPort registers.
//

`default_nettype none


module ctrlport_to_spi #(
  parameter BASE_ADDRESS = 0
) (
  //---------------------------------------------------------------
  // ControlPort Slave
  //---------------------------------------------------------------

  input  wire        ctrlport_clk,
  input  wire        ctrlport_rst,

  input  wire        s_ctrlport_req_wr,
  input  wire        s_ctrlport_req_rd,
  input  wire [19:0] s_ctrlport_req_addr,
  input  wire [31:0] s_ctrlport_req_data,

  output reg         s_ctrlport_resp_ack,
  output reg  [ 1:0] s_ctrlport_resp_status  = 0,
  output reg  [31:0] s_ctrlport_resp_data    = 0,

  //---------------------------------------------------------------
  // SPI Signals
  //---------------------------------------------------------------

  output wire        sclk,
  output wire        mosi,
  output wire [15:0] ss,
  input  wire        miso
);

  `include "../../../lib/rfnoc/core/ctrlport.vh"
  `include "./regmap/spi_regmap_utils.vh"

  //---------------------------------------------------------------
  // Translating CtrlPort <-> Wishbone
  //---------------------------------------------------------------

  reg         wb_cyc_i;         // Active bus cycle
  reg         wb_we_i  = 1'b0;  // Write access
  reg  [ 4:0] wb_adr_i = 5'b0;
  reg  [31:0] wb_dat_i = 32'b0;
  wire        wb_ack_o;
  wire [31:0] wb_dat_o;
  wire        wb_err_o;

  // Check for address to be in range [base_addr..base_addr+32)
  localparam NUM_ADDRESSES = 32;
  wire address_in_range = (s_ctrlport_req_addr >= BASE_ADDRESS) &&
                          (s_ctrlport_req_addr < BASE_ADDRESS + NUM_ADDRESSES);

  // Following chapter 3.2.3 (classic standard SINGLE WRITE cycle) of
  // https://cdn.opencores.org/downloads/wbspec_b4.pdf
  always @(posedge ctrlport_clk) begin
    // Reset internal registers and responses
    if (ctrlport_rst) begin
      wb_cyc_i <= 1'b0;
      s_ctrlport_resp_ack <= 1'b0;

    end else begin
      // Request independent default assignments
      s_ctrlport_resp_ack <= 1'b0;

      // Wait for ack on active bus transactions
      if (wb_cyc_i) begin
        if (wb_ack_o) begin
          // End bus cycle and generate response
          wb_cyc_i <= 1'b0;
          s_ctrlport_resp_ack  <= 1'b1;
          s_ctrlport_resp_data <= wb_dat_o;

          if (wb_err_o) begin
            s_ctrlport_resp_status <= CTRL_STS_CMDERR;
          end else begin
            s_ctrlport_resp_status <= CTRL_STS_OKAY;
          end
        end

      // Write requests
      end else if (s_ctrlport_req_wr) begin
        // Assume there is a valid address
        wb_cyc_i <= 1'b1;
        wb_we_i <= 1'b1;
        wb_dat_i <= s_ctrlport_req_data;

        case (s_ctrlport_req_addr)
          BASE_ADDRESS + TX_DATA_LOW: begin
            wb_adr_i <= 5'h00;
          end

          BASE_ADDRESS + TX_DATA_HIGH: begin
            wb_adr_i <= 5'h04;
          end

          BASE_ADDRESS + CONTROL: begin
            wb_adr_i <= 5'h10;
          end

          BASE_ADDRESS + CLOCK_DIVIDER: begin
            wb_adr_i <= 5'h14;
          end

          BASE_ADDRESS + SLAVE_SELECT: begin
            wb_adr_i <= 5'h18;
          end

          // Error on undefined address
          default: begin
            wb_cyc_i <= 1'b0;

            if (address_in_range) begin
              s_ctrlport_resp_status <= CTRL_STS_CMDERR;

            // No response if out of range
            end else begin
              s_ctrlport_resp_ack <= 1'b0;
            end
          end
        endcase

      // Read requests
      end else if (s_ctrlport_req_rd) begin
        // Assume there is a valid address
        wb_cyc_i <= 1'b1;
        wb_we_i <= 1'b0;

        case (s_ctrlport_req_addr)
          BASE_ADDRESS + RX_DATA_LOW: begin
            wb_adr_i <= 5'h00;
          end

          BASE_ADDRESS + RX_DATA_HIGH: begin
            wb_adr_i <= 5'h04;
          end

          BASE_ADDRESS + CONTROL: begin
            wb_adr_i <= 5'h10;
          end

          BASE_ADDRESS + CLOCK_DIVIDER: begin
            wb_adr_i <= 5'h14;
          end

          BASE_ADDRESS + SLAVE_SELECT: begin
            wb_adr_i <= 5'h18;
          end

          // Error on undefined address
          default: begin
            wb_cyc_i <= 1'b0;

            if (address_in_range) begin
              s_ctrlport_resp_status <= CTRL_STS_CMDERR;

            // No response if out of range
            end else begin
              s_ctrlport_resp_ack <= 1'b0;
            end
          end
        endcase

      // No request
      end else begin
        s_ctrlport_resp_ack <= 1'b0;
      end
    end
  end

  //---------------------------------------------------------------
  // SPI Master
  //---------------------------------------------------------------

  spi_top spi_master (
    .wb_clk_i   (ctrlport_clk),
    .wb_rst_i   (ctrlport_rst),
    .wb_adr_i   (wb_adr_i),
    .wb_dat_i   (wb_dat_i),
    .wb_dat_o   (wb_dat_o),
    .wb_sel_i   (4'hF),
    .wb_we_i    (wb_we_i),
    .wb_stb_i   (wb_cyc_i),
    .wb_cyc_i   (wb_cyc_i),
    .wb_ack_o   (wb_ack_o),
    .wb_err_o   (wb_err_o),
    .wb_int_o   (),
    .ss_pad_o   (ss),
    .sclk_pad_o (sclk),
    .mosi_pad_o (mosi),
    .miso_pad_i (miso)
  );

endmodule


`default_nettype wire


//XmlParse xml_on
//<regmap name="SPI_REGMAP" readablestrobes="false" markdown="true" generatevhdl="true" ettusguidelines="true">
//
//  <group name="SPI_REGS">
//    <info>
//      This register map is present for each SPI master.
//
//      For information about the register content and the way to interact with the core see the
//      <a href="https://opencores.org/websvn/filedetails?repname=spi&path=%2Fspi%2Ftrunk%2Fdoc%2Fspi.pdf" target="_blank">documentation</a>
//      of the SPI master from opencores used internally.
//
//      The core is configured to operate with 16 slave signal signals, up to 128 bits per transmission and 8 bit clock divider.
//      Only 64 bits of data are available via this register interface.
//
//      For the different SPI modes use the following table to derive the bits in @.CONTROL register. Only option 0 (CPOL=0, CPHA=0) has been tested.
//
//| CPOL | CPHA | TX_NEG | RX_NEG |
//| ------- | -------- | -------- | ------- |
//| 0  | 0  | 1  | 0  |
//| 0  | 1  | 0  | 1  |
//| 1  | 0  | 0  | 1  |
//| 1  | 1  | 1  | 0  |
//    </info>
//
//    <register name="RX_DATA_LOW" offset="0x00" writable="false" size="32">
//      <info>Lower 32 bits of the received word. (RxWord[31:0])</info>
//    </register>
//
//    <register name="RX_DATA_HIGH" offset="0x04" writable="false" size="32">
//      <info>Higher 32 bits of the received word. (RxWord[63:32])</info>
//    </register>
//
//    <register name="TX_DATA_LOW" offset="0x08" readable="false" size="32">
//      <info>Lower 32 bits of the received word. (TxWord[31:0])</info>
//    </register>
//
//    <register name="TX_DATA_HIGH" offset="0x0C" readable="false" size="32">
//      <info>Higher 32 bits of the received word. (TxWord[63:32])</info>
//    </register>
//
//    <register name="CONTROL" offset="0x10" size="32">
//      <info>Control register</info>
//    </register>

//    <register name="CLOCK_DIVIDER" offset="0x14" size="8">
//      <bitfield name="Divider" range="7..0">
//        <info>
//          Clock Divider.
//        </info>
//      </bitfield>
//    </register>
//    <register name="SLAVE_SELECT" offset="0x18" size="16">
//      <bitfield name="SS" range="15..0">
//        <info>
//          Slave select.
//        </info>
//      </bitfield>
//    </register>
//
//  </group>
//</regmap>
//XmlParse xml_off