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

Subversion Repositories sd_card_controller

[/] [sd_card_controller/] [trunk/] [rtl/] [verilog/] [sd_cmd_master.v] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 rozpruwacz
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// WISHBONE SD Card Controller IP Core                          ////
4
////                                                              ////
5
//// sd_cmd_master.v                                              ////
6
////                                                              ////
7
//// This file is part of the WISHBONE SD Card                    ////
8
//// Controller IP Core project                                   ////
9 8 rozpruwacz
//// http://opencores.org/project,sd_card_controller              ////
10 3 rozpruwacz
////                                                              ////
11
//// Description                                                  ////
12
//// State machine resposible for controlling command transfers   ////
13
//// on 1-bit sd card command interface                           ////
14
////                                                              ////
15
//// Author(s):                                                   ////
16
////     - Marek Czerski, ma.czerski@gmail.com                    ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2013 Authors                                   ////
21
////                                                              ////
22
//// Based on original work by                                    ////
23
////     Adam Edvardsson (adam.edvardsson@orsoc.se)               ////
24
////                                                              ////
25
////     Copyright (C) 2009 Authors                               ////
26
////                                                              ////
27
//// This source file may be used and distributed without         ////
28
//// restriction provided that this copyright statement is not    ////
29
//// removed from the file and that any derivative work contains  ////
30
//// the original copyright notice and the associated disclaimer. ////
31
////                                                              ////
32
//// This source file is free software; you can redistribute it   ////
33
//// and/or modify it under the terms of the GNU Lesser General   ////
34
//// Public License as published by the Free Software Foundation; ////
35
//// either version 2.1 of the License, or (at your option) any   ////
36
//// later version.                                               ////
37
////                                                              ////
38
//// This source is distributed in the hope that it will be       ////
39
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
40
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
41
//// PURPOSE. See the GNU Lesser General Public License for more  ////
42
//// details.                                                     ////
43
////                                                              ////
44
//// You should have received a copy of the GNU Lesser General    ////
45
//// Public License along with this source; if not, download it   ////
46
//// from http://www.opencores.org/lgpl.shtml                     ////
47
////                                                              ////
48
//////////////////////////////////////////////////////////////////////
49
`include "sd_defines.h"
50
 
51
module sd_cmd_master(
52
           input sd_clk,
53
           input rst,
54
           input start_i,
55
           input int_status_rst_i,
56
           output [1:0] setting_o,
57
           output reg start_xfr_o,
58
           output reg go_idle_o,
59
           output reg  [39:0] cmd_o,
60
           input [119:0] response_i,
61
           input crc_ok_i,
62
           input index_ok_i,
63
           input finish_i,
64
           input busy_i, //direct signal from data sd data input (data[0])
65
           //input card_detect,
66
           input [31:0] argument_i,
67
           input [`CMD_REG_SIZE-1:0] command_i,
68
           input [15:0] timeout_i,
69
           output [`INT_CMD_SIZE-1:0] int_status_o,
70
           output reg [31:0] response_0_o,
71
           output reg [31:0] response_1_o,
72
           output reg [31:0] response_2_o,
73
           output reg [31:0] response_3_o
74
       );
75
 
76
//-----------Types--------------------------------------------------------
77
reg [15:0] timeout_reg;
78
reg crc_check;
79
reg index_check;
80
reg busy_check;
81
reg expect_response;
82
reg long_response;
83
reg [`INT_CMD_SIZE-1:0] int_status_reg;
84
//reg card_present;
85
//reg [3:0]debounce;
86
reg [15:0] watchdog;
87
parameter SIZE = 2;
88
reg [SIZE-1:0] state;
89
reg [SIZE-1:0] next_state;
90
parameter IDLE       = 2'b00;
91
parameter EXECUTE    = 2'b01;
92
parameter BUSY_CHECK = 2'b10;
93
 
94
assign setting_o[1:0] = {long_response, expect_response};
95
assign int_status_o = state == IDLE ? int_status_reg : 5'h0;
96
 
97
//---------------Input ports---------------
98
 
99
// always @ (posedge sd_clk or posedge rst   )
100
// begin
101
//     if (rst) begin
102
//         debounce<=0;
103
//         card_present<=0;
104
//     end
105
//     else begin
106
//         if (!card_detect) begin//Card present
107
//             if (debounce!=4'b1111)
108
//                 debounce<=debounce+1'b1;
109
//         end
110
//         else
111
//             debounce<=0;
112
// 
113
//         if (debounce==4'b1111)
114
//             card_present<=1'b1;
115
//         else
116
//             card_present<=1'b0;
117
//     end
118
// end
119
 
120
always @(state or start_i or finish_i or go_idle_o or busy_check or busy_i)
121
begin: FSM_COMBO
122
    case(state)
123
        IDLE: begin
124
            if (start_i)
125
                next_state <= EXECUTE;
126
            else
127
                next_state <= IDLE;
128
        end
129
        EXECUTE: begin
130
            if ((finish_i && !busy_check) || go_idle_o)
131
                next_state <= IDLE;
132
            else if (finish_i && busy_check)
133
                next_state <= BUSY_CHECK;
134
            else
135
                next_state <= EXECUTE;
136
        end
137
        BUSY_CHECK: begin
138
            if (!busy_i)
139
                next_state <= IDLE;
140
            else
141
                next_state <= BUSY_CHECK;
142
        end
143
        default: next_state <= IDLE;
144
    endcase
145
end
146
 
147
always @(posedge sd_clk or posedge rst)
148
begin: FSM_SEQ
149
    if (rst) begin
150
        state <= IDLE;
151
    end
152
    else begin
153
        state <= next_state;
154
    end
155
end
156
 
157
always @(posedge sd_clk or posedge rst)
158
begin
159
    if (rst) begin
160
        crc_check <= 0;
161
        response_0_o <= 0;
162
        response_1_o <= 0;
163
        response_2_o <= 0;
164
        response_3_o <= 0;
165
        int_status_reg <= 0;
166
        expect_response <= 0;
167
        long_response <= 0;
168
        cmd_o <= 0;
169
        start_xfr_o <= 0;
170
        index_check <= 0;
171
        busy_check <= 0;
172
        watchdog <= 0;
173
        timeout_reg <= 0;
174
        go_idle_o <= 0;
175
    end
176
    else begin
177
        case(state)
178
            IDLE: begin
179
                go_idle_o <= 0;
180
                index_check <= command_i[`CMD_IDX_CHECK];
181
                crc_check <= command_i[`CMD_CRC_CHECK];
182
                busy_check <= command_i[`CMD_BUSY_CHECK];
183
                if (command_i[`CMD_RESPONSE_CHECK]  == 2'b10 || command_i[`CMD_RESPONSE_CHECK] == 2'b11) begin
184
                    expect_response <=  1;
185
                    long_response <= 1;
186
                end
187
                else if (command_i[`CMD_RESPONSE_CHECK] == 2'b01) begin
188
                    expect_response <= 1;
189
                    long_response <= 0;
190
                end
191
                else begin
192
                    expect_response <= 0;
193
                    long_response <= 0;
194
                end
195
                cmd_o[39:38] <= 2'b01;
196
                cmd_o[37:32] <= command_i[`CMD_INDEX];  //CMD_INDEX
197
                cmd_o[31:0] <= argument_i; //CMD_Argument
198
                timeout_reg <= timeout_i;
199
                watchdog <= 0;
200
                if (start_i) begin
201
                    start_xfr_o <= 1;
202
                    int_status_reg <= 0;
203
                end
204
            end
205
            EXECUTE: begin
206
                start_xfr_o <= 0;
207
                watchdog <= watchdog + 16'd1;
208
                if (watchdog > timeout_reg) begin
209
                    int_status_reg[`INT_CMD_CTE] <= 1;
210
                    int_status_reg[`INT_CMD_EI] <= 1;
211
                    go_idle_o <= 1;
212
                end
213
                //Incoming New Status
214
                else begin //if ( req_in_int == 1) begin
215
                    if (finish_i) begin //Data avaible
216
                        if (crc_check & !crc_ok_i) begin
217
                            int_status_reg[`INT_CMD_CCRCE] <= 1;
218
                            int_status_reg[`INT_CMD_EI] <= 1;
219
                        end
220
                        if (index_check & !index_ok_i) begin
221
                            int_status_reg[`INT_CMD_CIE] <= 1;
222
                            int_status_reg[`INT_CMD_EI] <= 1;
223
                        end
224
                        int_status_reg[`INT_CMD_CC] <= 1;
225
                        if (expect_response != 0) begin
226
                            response_0_o <= response_i[119:88];
227
                            response_1_o <= response_i[87:56];
228
                            response_2_o <= response_i[55:24];
229
                            response_3_o <= {response_i[23:0], 8'h00};
230
                        end
231
                        // end
232
                    end ////Data avaible
233
                end //Status change
234
            end //EXECUTE state
235
            BUSY_CHECK: begin
236
                start_xfr_o <= 0;
237
                go_idle_o <= 0;
238
            end
239
        endcase
240
        if (int_status_rst_i)
241
            int_status_reg <= 0;
242
    end
243
end
244
 
245
endmodule

powered by: WebSVN 2.1.0

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