OpenCores
URL https://opencores.org/ocsvn/xulalx25soc/xulalx25soc/trunk

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [sdspi.v] - Blame information for rev 113

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 85 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    sdspi.v
4
//
5
// Project:     SD-Card controller, using a shared SPI interface
6
//
7
// Purpose:     
8
//
9
// Creator:     Dan Gisselquist, Ph.D.
10
//              Gisselquist Technology, LLC
11
//
12
////////////////////////////////////////////////////////////////////////////////
13
//
14
// Copyright (C) 2016, Gisselquist Technology, LLC
15
//
16
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
`define SDSPI_CMD_ADDRESS       2'h0
39
`define SDSPI_DAT_ADDRESS       2'h1
40
`define SDSPI_FIFO_A_ADDR       2'h2
41
`define SDSPI_FIFO_B_ADDR       2'h3
42
//
43 90 dgisselq
// `define      SDSPI_CMD_ID    3'h0
44
// `define      SDSPI_CMD_A1    3'h1
45
// `define      SDSPI_CMD_A2    3'h2
46
// `define      SDSPI_CMD_A3    3'h3
47
// `define      SDSPI_CMD_A4    3'h4
48
// `define      SDSPI_CMD_CRC   3'h5
49
// `define      SDSPI_CMD_FIFO  3'h6
50
// `define      SDSPI_CMD_WAIT  3'h7
51
//
52 85 dgisselq
`define SDSPI_EXPECT_R1         2'b00
53
`define SDSPI_EXPECT_R1B        2'b01
54
`define SDSPI_EXPECT_R3         2'b10
55
//
56 90 dgisselq
`define SDSPI_RSP_NONE          3'h0    // No response yet from device
57
`define SDSPI_RSP_BSYWAIT       3'h1    // R1b, wait for device to send nonzero
58
`define SDSPI_RSP_GETWORD       3'h2    // Get 32-bit data word from device
59
`define SDSPI_RSP_GETTOKEN      3'h4 // Write to device, read from FIFO, wait for completion token
60
`define SDSPI_RSP_WAIT_WHILE_BUSY               3'h5 // Read from device
61 85 dgisselq
`define SDSPI_RSP_RDCOMPLETE    3'h6
62 90 dgisselq
`define SDSPI_RSP_WRITING       3'h7 // Read from device, write into FIFO
63 85 dgisselq
//
64
module  sdspi(i_clk,
65
                // Wishbone interface
66
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
67
                        o_wb_ack, o_wb_stall, o_wb_data,
68
                // SDCard interface
69
                o_cs_n, o_sck, o_mosi, i_miso,
70
                // Our interrupt
71
                o_int,
72
                // And whether or not we own the bus
73
                i_bus_grant,
74
                // And some wires for debugging it all
75
                o_debug);
76
        parameter       LGFIFOLN = 7;
77
        input   i_clk;
78
        //
79
        input                   i_wb_cyc, i_wb_stb, i_wb_we;
80
        input           [1:0]    i_wb_addr;
81
        input           [31:0]   i_wb_data;
82
        output  reg             o_wb_ack;
83
        output  wire            o_wb_stall;
84
        output  reg     [31:0]   o_wb_data;
85
        //
86
        output  wire            o_cs_n, o_sck, o_mosi;
87
        input                   i_miso;
88
        // The interrupt
89
        output  reg             o_int;
90
        // .. and whether or not we can use the SPI port
91
        input                   i_bus_grant;
92
        //
93
        output  wire    [31:0]   o_debug;
94
 
95
        //
96
        // Some WB simplifications:
97
        //
98
        reg     r_cmd_busy;
99
        wire    wb_stb, write_stb, cmd_stb; // read_stb
100 113 dgisselq
        assign  wb_stb    = ((i_wb_stb)&&(~o_wb_stall));
101 85 dgisselq
        assign  write_stb = ((wb_stb)&&( i_wb_we));
102
        // assign       read_stb  = ((wb_stb)&&(~i_wb_we));
103
        assign  cmd_stb  = (~r_cmd_busy)&&(write_stb)
104
                                &&(i_wb_addr==`SDSPI_CMD_ADDRESS);
105
 
106
 
107
        //
108
        // Access to our lower-level SDSPI driver, the one that actually
109
        // uses/sets the SPI ports
110
        //
111
        reg     [6:0]    r_sdspi_clk;
112
        reg             ll_cmd_stb;
113
        reg     [7:0]    ll_cmd_dat;
114
        wire            ll_out_stb, ll_idle;
115
        wire    [7:0]    ll_out_dat;
116
        llsdspi lowlevel(i_clk, r_sdspi_clk, r_cmd_busy, ll_cmd_stb, ll_cmd_dat,
117
                        o_cs_n, o_sck, o_mosi, i_miso,
118
                        ll_out_stb, ll_out_dat, ll_idle,
119
                        i_bus_grant);
120
 
121
 
122
        // CRC
123
        reg             r_cmd_crc_stb;
124
        wire    [7:0]    cmd_crc;
125
        // State machine
126
        reg     [2:0]    r_cmd_state, r_rsp_state;
127
        // Current status, beyond state
128
        reg             r_have_resp, r_use_fifo, r_fifo_wr,
129
                                ll_fifo_rd_complete, ll_fifo_wr_complete,
130
                                r_fifo_id,
131
                                ll_fifo_wr, ll_fifo_rd,
132 90 dgisselq
                                r_have_data_response_token,
133
                                r_have_start_token;
134 85 dgisselq
        reg     [7:0]    fifo_byte;
135
        reg     [7:0]    r_last_r_one;
136
        //
137
        reg     [31:0]   r_data_reg;
138
        reg     [1:0]    r_data_fil, r_cmd_resp;
139
        //
140
        //
141
        wire            need_reset;
142
        reg             r_cmd_err;
143
        reg             r_cmd_sent;
144
        reg     [31:0]   fifo_a_reg, fifo_b_reg;
145
        //
146
        reg             q_busy;
147
        //
148 100 dgisselq
        reg     [7:0]    fifo_a_mem_0[0:((1<<LGFIFOLN)-1)],
149
                        fifo_a_mem_1[0:((1<<LGFIFOLN)-1)],
150
                        fifo_a_mem_2[0:((1<<LGFIFOLN)-1)],
151
                        fifo_a_mem_3[0:((1<<LGFIFOLN)-1)],
152
                        fifo_b_mem_0[0:((1<<LGFIFOLN)-1)],
153
                        fifo_b_mem_1[0:((1<<LGFIFOLN)-1)],
154
                        fifo_b_mem_2[0:((1<<LGFIFOLN)-1)],
155
                        fifo_b_mem_3[0:((1<<LGFIFOLN)-1)];
156 85 dgisselq
        reg     [(LGFIFOLN-1):0] fifo_wb_addr;
157
        reg     [(LGFIFOLN+1):0] rd_fifo_sd_addr;
158
        reg     [(LGFIFOLN+1):0] wr_fifo_sd_addr;
159
        //
160
        reg     [(LGFIFOLN+1):0] ll_fifo_addr;
161
        //
162
        reg             fifo_crc_err;
163
        reg     [1:0]    ll_fifo_wr_state;
164
        reg     [7:0]    fifo_a_byte, fifo_b_byte;
165
        //
166
        reg     [2:0]    ll_fifo_pkt_state;
167
        reg             fifo_rd_crc_stb, fifo_wr_crc_stb;
168
        //
169
        reg     [3:0]    fifo_rd_crc_count, fifo_wr_crc_count;
170
        reg     [15:0]   fifo_rd_crc_reg, fifo_wr_crc_reg;
171
        //
172
        reg     [3:0]    r_cmd_crc_cnt;
173
        reg     [7:0]    r_cmd_crc;
174
        //
175
        reg             r_cmd_crc_ff;
176
        //
177
        reg     [3:0]    r_lgblklen;
178
        wire    [3:0]    max_lgblklen;
179
        assign  max_lgblklen = LGFIFOLN;
180
        //
181 90 dgisselq
        reg     [25:0]   r_watchdog;
182 85 dgisselq
        reg             r_watchdog_err;
183
        reg     pre_cmd_state;
184
 
185
        initial r_cmd_busy = 1'b0;
186
        initial r_data_reg = 32'h00;
187
        initial r_last_r_one = 8'hff;
188
        initial ll_cmd_stb = 1'b0;
189
        initial ll_fifo_rd = 1'b0;
190
        initial ll_fifo_wr = 1'b0;
191
        initial r_rsp_state = 3'h0;
192
        initial r_cmd_state = 3'h0;
193
        initial r_use_fifo  = 1'b0;
194
        initial r_data_fil  = 2'b00;
195
        initial r_lgblklen  = LGFIFOLN;
196
        initial r_cmd_err   = 1'b0;
197
        always @(posedge i_clk)
198
        begin
199
                if (~ll_cmd_stb)
200
                begin
201
                        r_have_resp <= 1'b0;
202
                        ll_fifo_wr <= 1'b0;
203
                        ll_fifo_rd <= 1'b0;
204
                        // r_rsp_state <= 3'h0;
205
                        r_cmd_state <= 3'h0;
206
                        r_use_fifo  <= 1'b0;
207
                        r_data_fil <= 2'b00;
208
                        r_cmd_resp <= `SDSPI_EXPECT_R1;
209
                end
210
 
211
                r_cmd_crc_stb <= 1'b0;
212
                if (pre_cmd_state)
213
                begin // While we are actively sending data, and clocking the
214
                        // interface, do:
215
                        //
216
                        // Here we use the transmit command state, or
217
                        // r_cmd_state, to determine where we are at in this
218
                        // process, and we use (ll_cmd_stb)&&(ll_idle) to
219
                        // determine that we have sent a byte.  ll_cmd_dat is
220
                        // set here as well--it's the byte we wish to transmit.
221
                        if (r_cmd_state == 3'h0)
222
                        begin
223
                                r_cmd_state <= r_cmd_state + 3'h1;
224
                                ll_cmd_dat <= r_data_reg[31:24];
225
                                r_cmd_crc_stb <= 1'b1;
226
                        end else if (r_cmd_state == 3'h1)
227
                        begin
228
                                r_cmd_state <= r_cmd_state + 3'h1;
229
                                ll_cmd_dat <= r_data_reg[23:16];
230
                                r_cmd_crc_stb <= 1'b1;
231
                        end else if (r_cmd_state == 3'h2)
232
                        begin
233
                                r_cmd_state <= r_cmd_state + 3'h1;
234
                                ll_cmd_dat <= r_data_reg[15:8];
235
                                r_cmd_crc_stb <= 1'b1;
236
                        end else if (r_cmd_state == 3'h3)
237
                        begin
238
                                r_cmd_state <= r_cmd_state + 3'h1;
239
                                ll_cmd_dat <= r_data_reg[7:0];
240
                                r_cmd_crc_stb <= 1'b1;
241
                        end else if (r_cmd_state == 3'h4)
242
                        begin
243
                                r_cmd_state <= r_cmd_state + 3'h1;
244
                                ll_cmd_dat <= cmd_crc;
245
                        end else if (r_cmd_state == 3'h5)
246
                        begin
247 90 dgisselq
                                ll_cmd_dat <= 8'hff;
248 85 dgisselq
                                if (r_have_resp)
249
                                begin
250
                                        if (r_use_fifo)
251 90 dgisselq
                                                r_cmd_state <= r_cmd_state + 3'h1;
252 85 dgisselq
                                        else
253 90 dgisselq
                                                r_cmd_state <= r_cmd_state + 3'h2;
254 85 dgisselq
                                        ll_fifo_rd <= (r_use_fifo)&&(r_fifo_wr);
255 90 dgisselq
                                        if ((r_use_fifo)&&(r_fifo_wr))
256
                                                ll_cmd_dat <= 8'hfe;
257 85 dgisselq
                                end
258
                        end else if (r_cmd_state == 3'h6)
259
                        begin
260
                                ll_cmd_dat <= 8'hff;
261 90 dgisselq
                                if (ll_fifo_rd_complete)
262 85 dgisselq
                                begin // If we've finished reading from the
263
                                        // FIFO, then move on
264
                                        r_cmd_state <= r_cmd_state + 3'h1;
265
                                        ll_fifo_rd <= 1'b0;
266
                                end else if (ll_fifo_rd)
267
                                        ll_cmd_dat <= fifo_byte;
268
                        end else // if (r_cmd_state == 7)
269
                                ll_cmd_dat <= 8'hff;
270
 
271
 
272
                        // Here we handle the receive portion of the interface.
273
                        // Note that the IF begins with an if of ll_out_stb.
274
                        // That is, if a byte is ready from the lower level.
275
                        //
276
                        // Here we depend upon r_cmd_resp, the response we are
277
                        // expecting from the SDCard, and r_rsp_state, the
278
                        // state machine for where we are at receive what we
279
                        // are expecting.
280
                        if (pre_rsp_state)
281
                        begin
282
                                if (r_rsp_state == `SDSPI_RSP_NONE)
283
                                begin // Waiting on R1
284
                                        if (~ll_out_dat[7])
285
                                        begin
286
                                                r_last_r_one <= ll_out_dat;
287
                                                if (r_cmd_resp == `SDSPI_EXPECT_R1)
288
                                                begin // Expecting R1 alone
289
                                                        r_have_resp <= 1'b1;
290
                                                        ll_cmd_stb <= (r_use_fifo);
291
                                                        r_data_reg <= 32'hffffffff;
292
                                                        ll_fifo_wr<=(r_use_fifo)&&(~r_fifo_wr);
293
                                                end else if (r_cmd_resp == `SDSPI_EXPECT_R1B)
294
                                                begin // Go wait on R1b
295
                                                        r_data_reg <= 32'hffffffff;
296
                                                end // else wait on 32-bit rsp
297
                                        end
298
                                end else if (r_rsp_state == `SDSPI_RSP_BSYWAIT)
299
                                begin // Waiting on R1b, have R1
300
                                        if (nonzero_out)
301
                                                r_have_resp <= 1'b1;
302
                                        ll_cmd_stb <= (r_use_fifo);
303
                                end else if (r_rsp_state == `SDSPI_RSP_GETWORD)
304
                                begin // Have R1, waiting on all of R2/R3/R7
305
                                        r_data_reg <= { r_data_reg[23:0], ll_out_dat };
306
                                        r_data_fil <= r_data_fil+2'b01;
307
                                        if (r_data_fil == 2'b11)
308
                                        begin
309
                                                ll_cmd_stb <= (r_use_fifo);
310
                                                // r_rsp_state <= 3'h3;
311
                                        end
312 90 dgisselq
                                end else if (r_rsp_state == `SDSPI_RSP_WAIT_WHILE_BUSY)
313 85 dgisselq
                                begin // Wait while device is busy writing
314 90 dgisselq
                                        // if (nonzero_out)
315
                                        // begin
316
                                                // r_data_reg[31:8] <= 24'h00;
317
                                                // r_data_reg[7:0] <= ll_out_dat;
318
                                                // // r_rsp_state <= 3'h6;
319
                                        // end
320
                                        ;
321 85 dgisselq
                                end else if (r_rsp_state == `SDSPI_RSP_RDCOMPLETE)
322
                                begin // Block write command has completed
323
                                        ll_cmd_stb <= 1'b0;
324
                                end else if (r_rsp_state == `SDSPI_RSP_WRITING)
325
                                begin // We are reading from the device into
326
                                        // our FIFO
327
                                        if ((ll_fifo_wr_complete)
328
                                                // Or ... we receive an error
329
                                                ||((~r_have_start_token)
330
                                                &&(~ll_out_dat[4])
331
                                                &&(ll_out_dat[0])))
332
                                        begin
333
                                                ll_fifo_wr <= 1'b0;
334
                                                ll_cmd_stb <= 1'b0;
335
                                        end
336
                                end
337
                        end
338
 
339
                        if (r_watchdog_err)
340
                                ll_cmd_stb <= 1'b0;
341
                        r_cmd_err<= (r_cmd_err)|(fifo_crc_err)|(r_watchdog_err);
342
                end else if (r_cmd_busy)
343
                begin
344
                        r_cmd_busy <= (ll_cmd_stb)||(~ll_idle);
345
                end else if (cmd_stb)
346
                begin // Command write
347
                        // Clear the error on any write, whether a commanding
348
                        // one or not.  -- provided the user requests clearing
349
                        // it (by setting the bit high)
350
                        r_cmd_err  <= (r_cmd_err)&&(~i_wb_data[15]);
351
                        // In a similar fashion, we can switch fifos even if
352
                        // not in the middle of a command
353
                        r_fifo_id  <= i_wb_data[12];
354
                        //
355
                        // Doesn't matter what this is set to as long as we
356
                        // aren't busy, so we can set it irrelevantly here.
357
                        ll_cmd_dat <= i_wb_data[7:0];
358
                        //
359
                        // Note that we only issue a write upon receiving a
360
                        // valid command.  Such a command is 8 bits, and must
361
                        // start with its high order bits set to zero and one.
362
                        // Hence ... we test for that here.
363
                        if (i_wb_data[7:6] == 2'b01)
364
                        begin // Issue a command
365
                                //
366
                                r_cmd_busy <= 1'b1;
367
                                //
368
                                ll_cmd_stb <= 1'b1;
369
                                r_cmd_resp <= i_wb_data[9:8];
370
                                //
371
                                r_cmd_crc_stb <= 1'b1;
372
                                //
373
                                r_fifo_wr  <= i_wb_data[10];
374
                                r_use_fifo <= i_wb_data[11];
375
                                //
376
                        end else if (i_wb_data[7])
377
                        // If, on the other hand, the command was invalid,
378
                        // then it must have been an attempt to read our
379
                        // internal configuration.  So we'll place that on
380
                        // our data register.
381
                                r_data_reg <= { 8'h00,
382
                                        4'h0, max_lgblklen,
383
                                        4'h0, r_lgblklen, 1'b0, r_sdspi_clk };
384
                end else if ((write_stb)&&(i_wb_addr == `SDSPI_DAT_ADDRESS))
385
                begin // Data write
386
                        r_data_reg <= i_wb_data;
387
                end
388
        end
389
 
390
        always @(posedge i_clk)
391
                pre_cmd_state <= (ll_cmd_stb)&&(ll_idle);
392
 
393 90 dgisselq
        reg     ready_for_response_token;
394 85 dgisselq
        always @(posedge i_clk)
395 90 dgisselq
                if (~r_cmd_busy)
396
                        ready_for_response_token <= 1'b0;
397
                else if (ll_fifo_rd)
398
                        ready_for_response_token <= 1'b1;
399
        always @(posedge i_clk)
400
                if (~r_cmd_busy)
401
                        r_have_data_response_token <= 1'b0;
402
                else if ((ll_out_stb)&&(ready_for_response_token)&&(~ll_out_dat[4]))
403
                        r_have_data_response_token <= 1'b1;
404 85 dgisselq
 
405
        reg     [2:0]    second_rsp_state;
406
        always @(posedge i_clk)
407
                if((r_cmd_resp == `SDSPI_EXPECT_R1)&&(r_use_fifo)&&(r_fifo_wr))
408
                        second_rsp_state <= `SDSPI_RSP_GETTOKEN;
409
                else if (r_cmd_resp == `SDSPI_EXPECT_R1)
410
                        second_rsp_state <= `SDSPI_RSP_WRITING;
411
                else if (r_cmd_resp == `SDSPI_EXPECT_R1B)
412
                        second_rsp_state <= `SDSPI_RSP_BSYWAIT;
413
                else
414
                        second_rsp_state <= `SDSPI_RSP_GETWORD;
415
 
416
        reg     pre_rsp_state, nonzero_out;
417
        always @(posedge i_clk)
418
                if (ll_out_stb)
419
                        nonzero_out <= (|ll_out_dat);
420
        always @(posedge i_clk)
421
                pre_rsp_state <= (ll_out_stb)&&(r_cmd_sent);
422
 
423
        // Each bit depends upon 8 bits of input
424
        initial r_rsp_state = 3'h0;
425
        always @(posedge i_clk)
426
                if (~r_cmd_sent)
427
                        r_rsp_state <= 3'h0;
428
                else if (pre_rsp_state)
429
                begin
430
                        if ((r_rsp_state == `SDSPI_RSP_NONE)&&(~ll_out_dat[7]))
431
                        begin
432
                                r_rsp_state <= second_rsp_state;
433
                        end else if (r_rsp_state == `SDSPI_RSP_BSYWAIT)
434
                        begin // Waiting on R1b, have R1
435
                                // R1b never uses the FIFO
436
                                if (nonzero_out)
437
                                        r_rsp_state <= 3'h6;
438
                        end else if (r_rsp_state == `SDSPI_RSP_GETWORD)
439
                        begin // Have R1, waiting on all of R2/R3/R7
440
                                if (r_data_fil == 2'b11)
441
                                        r_rsp_state <= `SDSPI_RSP_RDCOMPLETE;
442
                        end else if (r_rsp_state == `SDSPI_RSP_GETTOKEN)
443
                        begin // Wait on data token response
444 90 dgisselq
                                if (r_have_data_response_token)
445
                                        r_rsp_state <= `SDSPI_RSP_WAIT_WHILE_BUSY;
446
                        end else if (r_rsp_state == `SDSPI_RSP_WAIT_WHILE_BUSY)
447 85 dgisselq
                        begin // Wait while device is busy writing
448
                                if (nonzero_out)
449
                                        r_rsp_state <= `SDSPI_RSP_RDCOMPLETE;
450
                        end
451
                        //else if (r_rsp_state == 3'h6)
452
                        //begin // Block write command has completed
453
                        //      // ll_cmd_stb <= 1'b0;
454
                        // end else if (r_rsp_state == 3'h7)
455
                        // begin // We are reading from the device into
456
                        //      // our FIFO
457
                        // end
458
                end
459
 
460
        always @(posedge i_clk)
461
                r_cmd_sent <= (ll_cmd_stb)&&(r_cmd_state >= 3'h5);
462
 
463
        // initial      r_sdspi_clk = 6'h3c;
464
        initial r_sdspi_clk = 7'h63;
465
        always @(posedge i_clk)
466
        begin
467
                // Update our internal configuration parameters, unconnected
468
                // with the card.  These include the speed of the interface,
469
                // and the size of the block length to expect as part of a FIFO
470
                // command.
471
                if ((cmd_stb)&&(i_wb_data[7:6]==2'b11)&&(~r_data_reg[7])
472
                        &&(r_data_reg[15:12]==4'h00))
473
                begin
474
                        if (|r_data_reg[6:0])
475
                                r_sdspi_clk <= r_data_reg[6:0];
476
                        if (|r_data_reg[11:8])
477
                                r_lgblklen <= r_data_reg[11:8];
478
                end if (r_lgblklen > max_lgblklen)
479
                        r_lgblklen <= max_lgblklen;
480
        end
481
 
482
        assign  need_reset = 1'b0;
483
        always @(posedge i_clk)
484
                case(i_wb_addr)
485
                `SDSPI_CMD_ADDRESS:
486
                        o_wb_data <= { need_reset, 11'h00,
487
                                        3'h0, fifo_crc_err,
488
                                        r_cmd_err, r_cmd_busy, 1'b0, r_fifo_id,
489
                                        r_use_fifo, r_fifo_wr, r_cmd_resp,
490
                                        r_last_r_one };
491
                `SDSPI_DAT_ADDRESS:
492
                        o_wb_data <= r_data_reg;
493
                `SDSPI_FIFO_A_ADDR:
494
                        o_wb_data <= fifo_a_reg;
495
                `SDSPI_FIFO_B_ADDR:
496
                        o_wb_data <= fifo_b_reg;
497
                endcase
498
 
499
        always @(posedge i_clk)
500
                o_wb_ack <= wb_stb;
501
 
502
        initial q_busy = 1'b1;
503
        always @(posedge i_clk)
504
                q_busy <= r_cmd_busy;
505
        always @(posedge i_clk)
506
                o_int <= (~r_cmd_busy)&&(q_busy);
507
 
508
        assign  o_wb_stall = 1'b0;
509
 
510
        //
511
        // Let's work with our FIFO memory here ...
512
        //
513
        //
514
        always @(posedge i_clk)
515
        begin
516
                if ((write_stb)&&(i_wb_addr == `SDSPI_CMD_ADDRESS))
517
                begin // Command write
518
                        // Clear the read/write address
519
                        fifo_wb_addr <= {(LGFIFOLN){1'b0}};
520
                end else if ((wb_stb)&&(i_wb_addr[1]))
521
                begin // On read or write, of either FIFO,
522
                        // we increase our pointer
523
                        fifo_wb_addr <= fifo_wb_addr + 1;
524
                        // And let ourselves know we need to update ourselves
525
                        // on the next clock
526
                end
527
        end
528
 
529
        // Prepare reading of the FIFO for the WB bus read
530
        // Memory read #1
531
        always @(posedge i_clk)
532
        begin
533
                fifo_a_reg <= {
534 100 dgisselq
                        fifo_a_mem_0[ fifo_wb_addr ],
535
                        fifo_a_mem_1[ fifo_wb_addr ],
536
                        fifo_a_mem_2[ fifo_wb_addr ],
537
                        fifo_a_mem_3[ fifo_wb_addr ] };
538 85 dgisselq
                fifo_b_reg <= {
539 100 dgisselq
                        fifo_b_mem_0[ fifo_wb_addr ],
540
                        fifo_b_mem_1[ fifo_wb_addr ],
541
                        fifo_b_mem_2[ fifo_wb_addr ],
542
                        fifo_b_mem_3[ fifo_wb_addr ] };
543 85 dgisselq
        end
544
 
545
        // Okay, now ... writing our FIFO ...
546 90 dgisselq
        reg     pre_fifo_addr_inc_rd;
547
        reg     pre_fifo_addr_inc_wr;
548
        initial pre_fifo_addr_inc_rd = 1'b0;
549
        initial pre_fifo_addr_inc_wr = 1'b0;
550 85 dgisselq
        always @(posedge i_clk)
551 90 dgisselq
                pre_fifo_addr_inc_wr <= ((ll_fifo_wr)&&(ll_out_stb)&&(r_have_start_token));
552 85 dgisselq
        always @(posedge i_clk)
553 90 dgisselq
                pre_fifo_addr_inc_rd <= ((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle));//&&(ll_fifo_pkt_state[2:0]!=3'b000));
554
        always @(posedge i_clk)
555 85 dgisselq
        begin
556 90 dgisselq
                // if ((write_stb)&&(i_wb_addr == `SDSPI_CMD_ADDRESS)&&(i_wb_data[11]))
557
                        // ll_fifo_addr <= {(LGFIFOLN+2){1'b0}};
558
                if (~r_cmd_busy)
559 85 dgisselq
                        ll_fifo_addr <= {(LGFIFOLN+2){1'b0}};
560 90 dgisselq
                else if ((pre_fifo_addr_inc_wr)||(pre_fifo_addr_inc_rd))
561 85 dgisselq
                        ll_fifo_addr <= ll_fifo_addr + 1;
562
        end
563
 
564
        // Look for that start token
565
        always @(posedge i_clk)
566
                if (~r_cmd_busy)
567
                        r_have_start_token <= 1'b0;
568
                else if ((ll_fifo_wr)&&(ll_out_stb)&&(ll_out_dat==8'hfe))
569
                        r_have_start_token <= 1'b1;
570
 
571
        reg     last_fifo_byte;
572
        initial last_fifo_byte = 1'b0;
573
        always @(posedge i_clk)
574
                if (ll_fifo_wr)
575
                        last_fifo_byte <= (ll_fifo_addr == w_blklimit);
576
                else
577
                        last_fifo_byte <= 1'b0;
578
 
579
        // This is the one (and only allowed) write to the FIFO memory always
580
        // block.
581
        //
582
        // If ll_fifo_wr is true, we'll be writing to the FIFO, and we'll do
583
        // that here.  This is different from r_fifo_wr, which specifies that
584
        // we will be writing to the SDCard from the FIFO, and hence READING
585
        // from the FIFO.
586
        //
587
        reg     pre_fifo_a_wr, pre_fifo_b_wr, pre_fifo_crc_a, pre_fifo_crc_b,
588
                clear_fifo_crc;
589
        always @(posedge i_clk)
590
        begin
591
                pre_fifo_a_wr <= (ll_fifo_wr)&&(ll_out_stb)&&(~r_fifo_id)&&(ll_fifo_wr_state == 2'b00);
592
                pre_fifo_b_wr <= (ll_fifo_wr)&&(ll_out_stb)&&( r_fifo_id)&&(ll_fifo_wr_state == 2'b00);
593
                fifo_wr_crc_stb <= (ll_fifo_wr)&&(ll_out_stb)&&(ll_fifo_wr_state == 2'b00)&&(r_have_start_token);
594
                pre_fifo_crc_a<= (ll_fifo_wr)&&(ll_out_stb)&&(ll_fifo_wr_state == 2'b01);
595
                pre_fifo_crc_b<= (ll_fifo_wr)&&(ll_out_stb)&&(ll_fifo_wr_state == 2'b10);
596
                clear_fifo_crc <= (cmd_stb)&&(i_wb_data[15]);
597
        end
598
 
599 100 dgisselq
        reg                             fifo_a_wr, fifo_b_wr;
600
        reg     [3:0]                    fifo_a_wr_mask, fifo_b_wr_mask;
601
        reg     [(LGFIFOLN-1):0] fifo_a_wr_addr, fifo_b_wr_addr;
602
        reg     [31:0]                   fifo_a_wr_data, fifo_b_wr_data;
603
 
604 85 dgisselq
        initial         fifo_crc_err = 1'b0;
605
        always @(posedge i_clk)
606
        begin // One and only memory write allowed
607 100 dgisselq
                fifo_a_wr <= 1'b0;
608
                fifo_a_wr_data <= { ll_out_dat, ll_out_dat, ll_out_dat, ll_out_dat };
609 85 dgisselq
                if ((write_stb)&&(i_wb_addr[1:0]==2'b10))
610 100 dgisselq
                begin
611
                        fifo_a_wr <= 1'b1;
612
                        fifo_a_wr_mask <= 4'b1111;
613
                        fifo_a_wr_addr <= fifo_wb_addr;
614
                        fifo_a_wr_data <= i_wb_data;
615
                end else if (pre_fifo_a_wr)
616
                begin
617
                        fifo_a_wr <= 1'b1;
618
                        fifo_a_wr_addr <= ll_fifo_addr[(LGFIFOLN+1):2];
619
                        case(ll_fifo_addr[1:0])
620
                        2'b00: fifo_a_wr_mask <= 4'b0001;
621
                        2'b01: fifo_a_wr_mask <= 4'b0010;
622
                        2'b10: fifo_a_wr_mask <= 4'b0100;
623
                        2'b11: fifo_a_wr_mask <= 4'b1000;
624
                        endcase
625
                end
626 85 dgisselq
 
627 100 dgisselq
                if ((fifo_a_wr)&&(fifo_a_wr_mask[0]))
628
                        fifo_a_mem_0[fifo_a_wr_addr] <= fifo_a_wr_data[7:0];
629
                if ((fifo_a_wr)&&(fifo_a_wr_mask[1]))
630
                        fifo_a_mem_1[fifo_a_wr_addr] <= fifo_a_wr_data[15:8];
631
                if ((fifo_a_wr)&&(fifo_a_wr_mask[2]))
632
                        fifo_a_mem_2[fifo_a_wr_addr] <= fifo_a_wr_data[23:16];
633
                if ((fifo_a_wr)&&(fifo_a_wr_mask[3]))
634
                        fifo_a_mem_3[fifo_a_wr_addr] <= fifo_a_wr_data[31:24];
635
 
636
                fifo_b_wr <= 1'b0;
637
                fifo_b_wr_data <= { ll_out_dat, ll_out_dat, ll_out_dat, ll_out_dat };
638 85 dgisselq
                if ((write_stb)&&(i_wb_addr[1:0]==2'b11))
639 100 dgisselq
                begin
640
                        fifo_b_wr <= 1'b1;
641
                        fifo_b_wr_mask <= 4'b1111;
642
                        fifo_b_wr_addr <= fifo_wb_addr;
643
                        fifo_b_wr_data <= i_wb_data;
644
                end else if (pre_fifo_b_wr)
645
                begin
646
                        fifo_b_wr <= 1'b1;
647
                        fifo_b_wr_addr <= ll_fifo_addr[(LGFIFOLN+1):2];
648
                        case(ll_fifo_addr[1:0])
649
                        2'b00: fifo_b_wr_mask <= 4'b0001;
650
                        2'b01: fifo_b_wr_mask <= 4'b0010;
651
                        2'b10: fifo_b_wr_mask <= 4'b0100;
652
                        2'b11: fifo_b_wr_mask <= 4'b1000;
653
                        endcase
654
                end
655 85 dgisselq
 
656 100 dgisselq
                if ((fifo_b_wr)&&(fifo_b_wr_mask[0]))
657
                        fifo_b_mem_0[fifo_b_wr_addr] <= fifo_b_wr_data[7:0];
658
                if ((fifo_b_wr)&&(fifo_b_wr_mask[1]))
659
                        fifo_b_mem_1[fifo_b_wr_addr] <= fifo_b_wr_data[15:8];
660
                if ((fifo_b_wr)&&(fifo_b_wr_mask[2]))
661
                        fifo_b_mem_2[fifo_b_wr_addr] <= fifo_b_wr_data[23:16];
662
                if ((fifo_b_wr)&&(fifo_b_wr_mask[3]))
663
                        fifo_b_mem_3[fifo_b_wr_addr] <= fifo_b_wr_data[31:24];
664
 
665 85 dgisselq
                if (~r_cmd_busy)
666
                        ll_fifo_wr_complete <= 1'b0;
667
 
668
                if (~r_cmd_busy)
669
                        ll_fifo_wr_state <= 2'b00;
670
                else if ((pre_fifo_a_wr)||(pre_fifo_b_wr))
671 90 dgisselq
                        ll_fifo_wr_state <= (last_fifo_byte)? 2'b01:2'b00;
672 85 dgisselq
 
673
                if (pre_fifo_crc_a)
674
                begin
675
                        fifo_crc_err <= fifo_crc_err | (fifo_wr_crc_reg[15:8]!=ll_out_dat);
676
                        ll_fifo_wr_state <= ll_fifo_wr_state + 2'b01;
677
                end if (pre_fifo_crc_b)
678
                begin
679
                        fifo_crc_err <= fifo_crc_err | (fifo_wr_crc_reg[7:0]!=ll_out_dat);
680
                        ll_fifo_wr_state <= ll_fifo_wr_state + 2'b01;
681
                        ll_fifo_wr_complete <= 1'b1;
682
                end else if (clear_fifo_crc)
683
                        fifo_crc_err <= 1'b0;
684
        end
685
 
686
        always @(posedge i_clk)
687
        begin // Second memory read, this time for the FIFO
688 100 dgisselq
                case(ll_fifo_addr[1:0])
689
                2'b00: begin
690
                        fifo_a_byte<=fifo_a_mem_0[ll_fifo_addr[(LGFIFOLN+1):2]];
691
                        fifo_b_byte<=fifo_b_mem_0[ll_fifo_addr[(LGFIFOLN+1):2]];
692
                        end
693
                2'b01: begin
694
                        fifo_a_byte<=fifo_a_mem_1[ll_fifo_addr[(LGFIFOLN+1):2]];
695
                        fifo_b_byte<=fifo_b_mem_1[ll_fifo_addr[(LGFIFOLN+1):2]];
696
                        end
697
                2'b10: begin
698
                        fifo_a_byte<=fifo_a_mem_2[ll_fifo_addr[(LGFIFOLN+1):2]];
699
                        fifo_b_byte<=fifo_b_mem_2[ll_fifo_addr[(LGFIFOLN+1):2]];
700
                        end
701
                2'b11: begin
702
                        fifo_a_byte<=fifo_a_mem_3[ll_fifo_addr[(LGFIFOLN+1):2]];
703
                        fifo_b_byte<=fifo_b_mem_3[ll_fifo_addr[(LGFIFOLN+1):2]];
704
                        end
705
                endcase
706 85 dgisselq
        end
707
 
708
        reg     [(LGFIFOLN-1):0] r_blklimit;
709
        wire    [(LGFIFOLN+1):0] w_blklimit;
710
        always @(posedge i_clk)
711
                r_blklimit[(LGFIFOLN-1):0] = (1<<r_lgblklen)-1;
712
        assign  w_blklimit = { r_blklimit, 2'b11 };
713
 
714
        // Package the FIFO reads up into a packet
715
        always @(posedge i_clk)
716
        begin
717
                fifo_rd_crc_stb <= 1'b0;
718
                if (r_cmd_busy)
719
                begin
720 90 dgisselq
                        if (ll_fifo_pkt_state[2:0] == 3'b000)
721 85 dgisselq
                        begin
722
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
723
                                begin
724 90 dgisselq
                                        ll_fifo_pkt_state <= ll_fifo_pkt_state + 3'b001;
725
                                end
726
                        end else if (ll_fifo_pkt_state[2:0] == 3'b001)
727
                        begin
728
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
729
                                begin
730
                                        ll_fifo_pkt_state <= ll_fifo_pkt_state + 3'b001;
731 85 dgisselq
                                        fifo_byte <= (r_fifo_id)
732
                                                ? fifo_b_byte : fifo_a_byte;
733 90 dgisselq
                                        fifo_rd_crc_stb <= 1'b1;
734 85 dgisselq
                                end
735 90 dgisselq
                        end else if (ll_fifo_pkt_state[2:0] == 3'b010)
736
                        begin
737
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
738
                                begin
739
                                        fifo_byte <= (r_fifo_id)
740
                                                ? fifo_b_byte : fifo_a_byte;
741
                                        fifo_rd_crc_stb <= 1'b1;
742
                                end
743
                                if (ll_fifo_addr == 0)
744
                                        ll_fifo_pkt_state <= 3'b011;
745
                        end else if (ll_fifo_pkt_state == 3'b011)
746 85 dgisselq
                        begin // 1st CRC byte
747
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
748
                                begin
749
                                        fifo_byte <= fifo_rd_crc_reg[15:8];
750 90 dgisselq
                                        ll_fifo_pkt_state <= 3'b100;
751 85 dgisselq
                                end
752 90 dgisselq
                        end else if (ll_fifo_pkt_state == 3'b100)
753 85 dgisselq
                        begin // 2nd CRC byte
754
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
755
                                begin
756
                                        fifo_byte <= fifo_rd_crc_reg[7:0];
757 90 dgisselq
                                        ll_fifo_pkt_state <= 3'b101;
758 85 dgisselq
                                end
759
                        end else if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
760 90 dgisselq
                        begin
761 85 dgisselq
                        // Idle the channel
762 90 dgisselq
                                ll_fifo_rd_complete <= 1'b1;
763 85 dgisselq
                                fifo_byte <= 8'hff;
764 90 dgisselq
                        end
765 85 dgisselq
                end else if ((write_stb)&&(i_wb_addr == `SDSPI_CMD_ADDRESS))
766
                begin
767
                        ll_fifo_pkt_state <= 3'h0;
768
                        ll_fifo_rd_complete <= 1'b0;
769 90 dgisselq
                        fifo_byte <= (i_wb_data[12]) ? fifo_b_byte : fifo_a_byte;
770
                        fifo_rd_crc_stb <= 1'b1;
771 85 dgisselq
                end else begin // Packet state is IDLE (clear the CRC registers)
772
                        ll_fifo_pkt_state <= 3'b111;
773
                        ll_fifo_rd_complete <= 1'b1;
774
                end
775
        end
776
 
777
        always @(posedge i_clk)
778
        begin
779
                if (~ll_fifo_wr)
780
                        fifo_wr_crc_reg <= 16'h00;
781
                else if (fifo_wr_crc_stb)
782
                begin
783
                        fifo_wr_crc_reg[15:8] <=fifo_wr_crc_reg[15:8]^ll_out_dat;
784
                        fifo_wr_crc_count <= 4'h8;
785
                end else if (|fifo_wr_crc_count)
786
                begin
787
                        fifo_wr_crc_count <= fifo_wr_crc_count - 4'h1;
788
                        if (fifo_wr_crc_reg[15])
789
                                fifo_wr_crc_reg <= { fifo_wr_crc_reg[14:0], 1'b0 }
790
                                        ^ 16'h1021;
791
                        else
792
                                fifo_wr_crc_reg <= { fifo_wr_crc_reg[14:0], 1'b0 };
793
                end
794
        end
795
 
796
        always @(posedge i_clk)
797
        begin
798 90 dgisselq
                if (~r_cmd_busy)
799
                begin
800 85 dgisselq
                        fifo_rd_crc_reg <= 16'h00;
801 90 dgisselq
                        fifo_rd_crc_count <= 4'h0;
802
                end else if (fifo_rd_crc_stb)
803 85 dgisselq
                begin
804 90 dgisselq
                        fifo_rd_crc_reg[15:8] <=fifo_rd_crc_reg[15:8]^fifo_byte;
805
                        fifo_rd_crc_count <= 4'h8;
806
                end else if (|fifo_rd_crc_count)
807
                begin
808
                        fifo_rd_crc_count <= fifo_rd_crc_count - 4'h1;
809
                        if (fifo_rd_crc_reg[15])
810
                                fifo_rd_crc_reg <= { fifo_rd_crc_reg[14:0], 1'b0 }
811
                                        ^ 16'h1021;
812
                        else
813
                                fifo_rd_crc_reg <= { fifo_rd_crc_reg[14:0], 1'b0 };
814 85 dgisselq
                end
815
        end
816
 
817
        //
818
        // Calculate a CRC for the command section of our output
819
        //
820
        initial r_cmd_crc_ff = 1'b0;
821
        always @(posedge i_clk)
822
        begin
823
                if (~r_cmd_busy)
824
                begin
825
                        r_cmd_crc <= 8'h00;
826
                        r_cmd_crc_cnt <= 4'hf;
827
                        r_cmd_crc_ff <= 1'b0;
828
                end else if (~r_cmd_crc_cnt[3])
829
                begin
830
                        r_cmd_crc_cnt <= r_cmd_crc_cnt - 4'h1;
831
                        if (r_cmd_crc[7])
832
                                r_cmd_crc <= { r_cmd_crc[6:0], 1'b0 } ^ 8'h12;
833
                        else
834
                                r_cmd_crc <= { r_cmd_crc[6:0], 1'b0 };
835
                        r_cmd_crc_ff <= (r_cmd_crc_ff)||(r_cmd_crc_stb);
836
                end else if ((r_cmd_crc_stb)||(r_cmd_crc_ff))
837
                begin
838
                        r_cmd_crc <= r_cmd_crc ^ ll_cmd_dat;
839
                        r_cmd_crc_cnt <= 4'h7;
840
                        r_cmd_crc_ff <= 1'b0;
841
                end
842
        end
843
        assign  cmd_crc = { r_cmd_crc[7:1], 1'b1 };
844
 
845
        //
846
        // Some watchdog logic for us.  This way, if we are waiting for the
847
        // card to respond, and something goes wrong, we can timeout the
848
        // transaction and ... figure out what to do about it later.  At least
849
        // we'll have an error indication.
850
        //
851 90 dgisselq
        initial r_watchdog = 26'h3ffffff;
852 85 dgisselq
        initial r_watchdog_err = 1'b0;
853
        always @(posedge i_clk)
854
                if (~r_cmd_busy)
855
                        r_watchdog_err <= 1'b0;
856
                else if (r_watchdog == 0)
857
                        r_watchdog_err <= 1'b1;
858
        always @(posedge i_clk)
859
                if (~r_cmd_busy)
860 90 dgisselq
                        r_watchdog <= 26'h3fffff;
861 85 dgisselq
                else if (|r_watchdog)
862 90 dgisselq
                        r_watchdog <= r_watchdog - 26'h1;
863 85 dgisselq
 
864
        assign o_debug = { ((ll_cmd_stb)&&(ll_idle))||(ll_out_stb),
865
                                ll_cmd_stb, ll_idle, ll_out_stb, // 4'h
866
                        o_cs_n, o_sck, o_mosi, i_miso,  // 4'h
867
                        r_cmd_state, i_bus_grant,       // 4'h
868
                        r_rsp_state, r_cmd_busy,        // 4'h
869
                        ll_cmd_dat,             // 8'b
870
                        ll_out_dat };           // 8'b
871
endmodule
872
 
873
////////////////////////////////////////////////////////////////////////////////
874
//
875
// Filename:    llsdspi.v
876
//
877
// Project:     SD-Card controller, using a shared SPI interface
878
//
879
// Purpose:     This file implements the "lower-level" interface to the
880
//              SD-Card controller.  Specifically, it turns byte-level
881
//      interaction requests into SPI bit-wise interactions.  Further, it
882
//      handles the request and grant for the SPI wires (i.e., it requests
883
//      the SPI port by pulling o_cs_n low, and then waits for i_bus_grant
884
//      to be true before continuing.).  Finally, the speed/clock rate of the
885
//      communication is adjustable as a division of the current clock rate.
886
//
887
//      i_speed
888
//              This is the number of clocks (minus one) between SPI clock
889
//              transitions.  Hence a '0' (not tested, doesn't work) would
890
//              result in a SPI clock that alternated on every input clock
891
//              equivalently dividing the input clock by two, whereas a '1' 
892
//              would divide the input clock by four.
893
//
894
//              In general, the SPI clock frequency will be given by the
895
//              master clock frequency divided by twice this number plus one.
896
//              In other words,
897
//
898
//              SPIFREQ=(i_clk FREQ) / (2*(i_speed+1))
899
//
900
//      i_stb
901
//              True if the master controller is requesting to send a byte.
902
//              This will be ignored unless o_idle is false.
903
//
904
//      i_byte
905
//              The byte that the master controller wishes to send across the
906
//              interface.
907
//
908
//      (The external SPI interface)
909
//
910
//      o_stb
911
//              Only true for one clock--when a byte is valid coming in from the
912
//              interface, this will be true for one clock (a strobe) indicating
913
//              that a valid byte is ready to be read.
914
//
915
//      o_byte
916
//              The value of the byte coming in.
917
//
918
//      o_idle
919
//              True if this low-level device handler is ready to accept a 
920
//              byte from the incoming interface, false otherwise.
921
//
922
//      i_bus_grant
923
//              True if the SPI bus has been granted to this interface, false
924
//              otherwise.  This has been placed here so that the interface of
925
//              the XuLA2 board may be shared between SPI-Flash and the SPI
926
//              based SDCard.  An external arbiter will determine which of the
927
//              two gets to control the clock and mosi outputs given their
928
//              cs_n requests.  If control is not granted, i_bus_grant will
929
//              remain low as will the actual cs_n going out of the FPGA.
930
//
931
//
932
//
933
// Creator:     Dan Gisselquist, Ph.D.
934
//              Gisselquist Technology, LLC
935
//
936
////////////////////////////////////////////////////////////////////////////////
937
//
938
// Copyright (C) 2016, Gisselquist Technology, LLC
939
//
940
// This program is free software (firmware): you can redistribute it and/or
941
// modify it under the terms of  the GNU General Public License as published
942
// by the Free Software Foundation, either version 3 of the License, or (at
943
// your option) any later version.
944
//
945
// This program is distributed in the hope that it will be useful, but WITHOUT
946
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
947
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
948
// for more details.
949
//
950
// You should have received a copy of the GNU General Public License along
951
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
952
// target there if the PDF file isn't present.)  If not, see
953
// <http://www.gnu.org/licenses/> for a copy.
954
//
955
// License:     GPL, v3, as defined and found on www.gnu.org,
956
//              http://www.gnu.org/licenses/gpl.html
957
//
958
//
959
////////////////////////////////////////////////////////////////////////////////
960
//
961
//
962
`define LLSDSPI_IDLE    4'h0
963
`define LLSDSPI_HOTIDLE 4'h1
964
`define LLSDSPI_WAIT    4'h2
965
`define LLSDSPI_START   4'h3
966
//
967
module  llsdspi(i_clk, i_speed, i_cs, i_stb, i_byte,
968
                o_cs_n, o_sclk, o_mosi, i_miso,
969
                o_stb, o_byte, o_idle, i_bus_grant);
970
        parameter       SPDBITS = 7;
971
        //
972
        input                   i_clk;
973
        // Parameters/setup
974
        input           [(SPDBITS-1):0]  i_speed;
975
        // The incoming interface
976
        input                   i_cs;
977
        input                   i_stb;
978
        input           [7:0]    i_byte;
979
        // The actual SPI interface
980
        output  reg             o_cs_n, o_sclk, o_mosi;
981
        input                   i_miso;
982
        // The outgoing interface
983
        output  reg             o_stb;
984
        output  reg     [7:0]    o_byte;
985
        output  wire            o_idle;
986
        // And whether or not we actually own the interface (yet)
987
        input                   i_bus_grant;
988
 
989
        reg                     r_z_counter;
990
        reg     [(SPDBITS-1):0]  r_clk_counter;
991
        reg                     r_idle;
992
        reg             [3:0]    r_state;
993
        reg             [7:0]    r_byte, r_ireg;
994
 
995
        wire    byte_accepted;
996
        assign  byte_accepted = (i_stb)&&(o_idle);
997
 
998
        initial r_clk_counter = 7'h0;
999
        always @(posedge i_clk)
1000
        begin
1001
                if ((~i_cs)||(~i_bus_grant))
1002
                        r_clk_counter <= 0;
1003
                else if (byte_accepted)
1004
                        r_clk_counter <= i_speed;
1005
                else if (~r_z_counter)
1006
                        r_clk_counter <= (r_clk_counter - {{(SPDBITS-1){1'b0}},1'b1});
1007
                else if ((r_state != `LLSDSPI_IDLE)&&(r_state != `LLSDSPI_HOTIDLE))
1008
                        r_clk_counter <= (i_speed);
1009
                // else 
1010
                //      r_clk_counter <= 16'h00;
1011
        end
1012
 
1013
        initial r_z_counter = 1'b1;
1014
        always @(posedge i_clk)
1015
        begin
1016
                if ((~i_cs)||(~i_bus_grant))
1017
                        r_z_counter <= 1'b1;
1018
                else if (byte_accepted)
1019
                        r_z_counter <= 1'b0;
1020
                else if (~r_z_counter)
1021
                        r_z_counter <= (r_clk_counter == 1);
1022
                else if ((r_state != `LLSDSPI_IDLE)&&(r_state != `LLSDSPI_HOTIDLE))
1023
                        r_z_counter <= 1'b0;
1024
        end
1025
 
1026
        initial r_state = `LLSDSPI_IDLE;
1027
        always @(posedge i_clk)
1028
        begin
1029
                o_stb <= 1'b0;
1030
                o_cs_n <= ~i_cs;
1031
                if (~i_cs)
1032
                begin
1033
                        r_state <= `LLSDSPI_IDLE;
1034
                        r_idle <= 1'b0;
1035
                        o_sclk <= 1'b1;
1036
                end else if (~r_z_counter)
1037
                begin
1038
                        r_idle <= 1'b0;
1039
                        if (byte_accepted)
1040
                        begin // Will only happen within a hot idle state
1041
                                r_byte <= { i_byte[6:0], 1'b1 };
1042
                                r_state <= `LLSDSPI_START+1;
1043
                                o_mosi <= i_byte[7];
1044
                        end
1045
                end else if (r_state == `LLSDSPI_IDLE)
1046
                begin
1047
                        o_sclk <= 1'b1;
1048
                        if (byte_accepted)
1049
                        begin
1050
                                r_byte <= i_byte[7:0];
1051
                                r_state <= (i_bus_grant)?`LLSDSPI_START:`LLSDSPI_WAIT;
1052
                                r_idle <= 1'b0;
1053
                                o_mosi <= i_byte[7];
1054
                        end else begin
1055
                                r_idle <= 1'b1;
1056
                        end
1057
                end else if (r_state == `LLSDSPI_WAIT)
1058
                begin
1059
                        r_idle <= 1'b0;
1060
                        if (i_bus_grant)
1061
                                r_state <= `LLSDSPI_START;
1062
                end else if (r_state == `LLSDSPI_HOTIDLE)
1063
                begin
1064
                        // The clock is low, the bus is granted, we're just
1065
                        // waiting for the next byte to transmit
1066
                        o_sclk <= 1'b0;
1067
                        if (byte_accepted)
1068
                        begin
1069
                                r_byte <= i_byte[7:0];
1070
                                r_state <= `LLSDSPI_START;
1071
                                r_idle <= 1'b0;
1072
                                o_mosi <= i_byte[7];
1073
                        end else
1074
                                r_idle <= 1'b1;
1075
                // end else if (r_state == `LLSDSPI_START)
1076
                // begin
1077
                        // o_sclk <= 1'b0;
1078
                        // r_state <= r_state + 1;
1079
                end else if (o_sclk)
1080
                begin
1081
                        o_mosi <= r_byte[7];
1082
                        r_byte <= { r_byte[6:0], 1'b1 };
1083
                        r_state <= r_state + 1;
1084
                        o_sclk <= 1'b0;
1085
                        if (r_state >= `LLSDSPI_START+8)
1086
                        begin
1087
                                r_state <= `LLSDSPI_HOTIDLE;
1088
                                r_idle <= 1'b1;
1089
                                o_stb <= 1'b1;
1090
                                o_byte <= r_ireg;
1091
                        end else
1092
                                r_state <= r_state + 1;
1093
                end else begin
1094
                        r_ireg <= { r_ireg[6:0], i_miso };
1095
                        o_sclk <= 1'b1;
1096
                end
1097
        end
1098
 
1099
        assign o_idle = (r_idle)&&( (i_cs)&&(i_bus_grant) );
1100
endmodule
1101
 
1102
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.