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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [sdspi.v] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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