1 |
2 |
jclaytons |
--------------------------------------------------------------------------
|
2 |
|
|
-- Package containing SD Card interface modules,
|
3 |
|
|
-- and related support modules.
|
4 |
|
|
--
|
5 |
|
|
|
6 |
|
|
library IEEE;
|
7 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
8 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
9 |
|
|
|
10 |
|
|
package sd_host_pack is
|
11 |
|
|
|
12 |
|
|
-- Constants relating to the SD Card interface
|
13 |
|
|
|
14 |
|
|
-- Register size constants
|
15 |
|
|
constant BLKSIZE_W : integer := 12;
|
16 |
|
|
constant BLKCNT_W : integer := 16;
|
17 |
|
|
|
18 |
|
|
-- cmd module interrupts
|
19 |
|
|
constant INT_CMD_CC : integer := 0;
|
20 |
|
|
constant INT_CMD_EI : integer := 1;
|
21 |
|
|
constant INT_CMD_CTE : integer := 2;
|
22 |
|
|
constant INT_CMD_CCRCE : integer := 3;
|
23 |
|
|
constant INT_CMD_CIE : integer := 4;
|
24 |
|
|
constant INT_CMD_SIZE : integer := 5; -- Size of register field, in bits
|
25 |
|
|
|
26 |
|
|
-- data module interrupts
|
27 |
|
|
constant INT_DATA_CC : integer := 0;
|
28 |
|
|
constant INT_DATA_CCRCE : integer := 1;
|
29 |
|
|
constant INT_DATA_CFE : integer := 2;
|
30 |
|
|
constant INT_DATA_SIZE : integer := 3; -- Size of register field, in bits
|
31 |
|
|
|
32 |
|
|
component sd_cmd_host
|
33 |
|
|
port(
|
34 |
|
|
sys_rst : in std_logic;
|
35 |
|
|
sd_clk : in std_logic;
|
36 |
|
|
-- Control and settings
|
37 |
|
|
start_i : in std_logic;
|
38 |
|
|
int_rst_i : in std_logic;
|
39 |
|
|
busy_i : in std_logic; --direct signal from data sd data input (data[0])
|
40 |
|
|
cmd_index_i : in unsigned(5 downto 0);
|
41 |
|
|
argument_i : in unsigned(31 downto 0);
|
42 |
|
|
timeout_i : in unsigned(15 downto 0);
|
43 |
|
|
int_status_o : out unsigned(4 downto 0);
|
44 |
|
|
response_0_o : out unsigned(31 downto 0);
|
45 |
|
|
response_1_o : out unsigned(31 downto 0);
|
46 |
|
|
response_2_o : out unsigned(31 downto 0);
|
47 |
|
|
response_3_o : out unsigned(31 downto 0);
|
48 |
|
|
-- SD/MMC card command signals
|
49 |
|
|
sd_cmd_i : in std_logic;
|
50 |
|
|
sd_cmd_o : out std_logic;
|
51 |
|
|
sd_cmd_oe_o : out std_logic
|
52 |
|
|
);
|
53 |
|
|
end component;
|
54 |
|
|
|
55 |
|
|
component sd_data_8bit_host
|
56 |
|
|
port(
|
57 |
|
|
sd_clk : in std_logic;
|
58 |
|
|
sys_rst : in std_logic;
|
59 |
|
|
--Tx Fifo
|
60 |
|
|
tx_dat_i : in unsigned(7 downto 0);
|
61 |
|
|
tx_dat_rd_o : out std_logic;
|
62 |
|
|
--Rx Fifo
|
63 |
|
|
rx_dat_o : out unsigned(7 downto 0);
|
64 |
|
|
rx_dat_we_o : out std_logic;
|
65 |
|
|
--SD data
|
66 |
|
|
sd_dat_siz_o : out unsigned(1 downto 0);
|
67 |
|
|
sd_dat_oe_o : out std_logic;
|
68 |
|
|
sd_dat_o : out unsigned(7 downto 0);
|
69 |
|
|
sd_dat_i : in unsigned(7 downto 0);
|
70 |
|
|
--Control signals
|
71 |
|
|
blksize_i : in unsigned(BLKSIZE_W-1 downto 0);
|
72 |
|
|
bus_size_i : in unsigned(1 downto 0);
|
73 |
|
|
blkcnt_i : in unsigned(BLKCNT_W-1 downto 0);
|
74 |
|
|
d_stop_i : in std_logic;
|
75 |
|
|
d_read_i : in std_logic;
|
76 |
|
|
d_write_i : in std_logic;
|
77 |
|
|
bustest_w_i : in std_logic;
|
78 |
|
|
bustest_r_i : in std_logic;
|
79 |
|
|
bustest_res_o : out unsigned(2 downto 0);
|
80 |
|
|
sd_dat_busy_o : out std_logic;
|
81 |
|
|
fsm_busy_o : out std_logic;
|
82 |
|
|
crc_ok_o : out std_logic
|
83 |
|
|
);
|
84 |
|
|
end component;
|
85 |
|
|
|
86 |
|
|
component sd_controller_8bit_bram
|
87 |
|
|
port (
|
88 |
|
|
-- WISHBONE common
|
89 |
|
|
wb_clk_i : in std_logic;
|
90 |
|
|
wb_rst_i : in std_logic;
|
91 |
|
|
-- WISHBONE slave (register interface)
|
92 |
|
|
wb_dat_i : in unsigned(31 downto 0);
|
93 |
|
|
wb_dat_o : out unsigned(31 downto 0);
|
94 |
|
|
wb_adr_i : in unsigned(3 downto 0);
|
95 |
|
|
wb_we_i : in std_logic;
|
96 |
|
|
wb_cyc_i : in std_logic;
|
97 |
|
|
wb_ack_o : out std_logic;
|
98 |
|
|
-- Dedicated BRAM port without acknowledge.
|
99 |
|
|
-- Access cycles must complete immediately.
|
100 |
|
|
-- (data to cross clock domains by this dual-ported BRAM)
|
101 |
|
|
bram_clk_o : out std_logic; -- Same as sd_clk_o_pad
|
102 |
|
|
bram_dat_o : out unsigned(7 downto 0);
|
103 |
|
|
bram_dat_i : in unsigned(7 downto 0);
|
104 |
|
|
bram_adr_o : out unsigned(31 downto 0);
|
105 |
|
|
bram_we_o : out std_logic;
|
106 |
|
|
bram_cyc_o : out std_logic;
|
107 |
|
|
--SD Card Interface
|
108 |
|
|
sd_cmd_i : in std_logic;
|
109 |
|
|
sd_cmd_o : out std_logic;
|
110 |
|
|
sd_cmd_oe_o : out std_logic;
|
111 |
|
|
sd_dat_i : in unsigned(7 downto 0);
|
112 |
|
|
sd_dat_o : out unsigned(7 downto 0);
|
113 |
|
|
sd_dat_oe_o : out std_logic;
|
114 |
|
|
sd_dat_siz_o : out unsigned(1 downto 0);
|
115 |
|
|
sd_clk_o_pad : out std_logic;
|
116 |
|
|
-- Interrupt outputs
|
117 |
|
|
int_cmd_o : out std_logic;
|
118 |
|
|
int_data_o : out std_logic
|
119 |
|
|
);
|
120 |
|
|
end component;
|
121 |
|
|
|
122 |
|
|
end sd_host_pack;
|
123 |
|
|
|
124 |
|
|
package body sd_host_pack is
|
125 |
|
|
end sd_host_pack;
|
126 |
|
|
|
127 |
|
|
-------------------------------------------------------------------------------
|
128 |
|
|
-- SD/MMC Command Host
|
129 |
|
|
-------------------------------------------------------------------------------
|
130 |
|
|
--
|
131 |
|
|
-- Author: John Clayton
|
132 |
|
|
-- Update: June 11, 2016 Combined sd_cmd_master with sd_cmd_serial_host from
|
133 |
|
|
-- the opencores.org VHDL code, in order to make this
|
134 |
|
|
-- unit. The reason for combining them is that eight
|
135 |
|
|
-- interface signals were shared only between those two
|
136 |
|
|
-- modules when they were instantiated, and they are not
|
137 |
|
|
-- needed individually.
|
138 |
|
|
--
|
139 |
|
|
-- Description
|
140 |
|
|
-------------------------------------------------------------------------------
|
141 |
|
|
-- This module handles generating the serial SD/MMC command output, and
|
142 |
|
|
-- receiving the SD/MMC responses from the card.
|
143 |
|
|
--
|
144 |
|
|
-- This unit runs entirely within the sd_clk_i clock domain.
|
145 |
|
|
--
|
146 |
|
|
|
147 |
|
|
library IEEE;
|
148 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
149 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
150 |
|
|
|
151 |
|
|
library work;
|
152 |
|
|
use work.ucrc_pack.all;
|
153 |
|
|
use work.sd_host_pack.all;
|
154 |
|
|
|
155 |
|
|
entity sd_cmd_host is
|
156 |
|
|
port(
|
157 |
|
|
sys_rst : in std_logic;
|
158 |
|
|
sd_clk : in std_logic;
|
159 |
|
|
-- Control and settings
|
160 |
|
|
start_i : in std_logic;
|
161 |
|
|
int_rst_i : in std_logic;
|
162 |
|
|
busy_i : in std_logic; --direct signal from data sd data input (data[0])
|
163 |
|
|
cmd_index_i : in unsigned(5 downto 0);
|
164 |
|
|
argument_i : in unsigned(31 downto 0);
|
165 |
|
|
timeout_i : in unsigned(15 downto 0);
|
166 |
|
|
int_status_o : out unsigned(4 downto 0);
|
167 |
|
|
response_0_o : out unsigned(31 downto 0);
|
168 |
|
|
response_1_o : out unsigned(31 downto 0);
|
169 |
|
|
response_2_o : out unsigned(31 downto 0);
|
170 |
|
|
response_3_o : out unsigned(31 downto 0);
|
171 |
|
|
-- SD/MMC card command signals
|
172 |
|
|
sd_cmd_i : in std_logic;
|
173 |
|
|
sd_cmd_o : out std_logic;
|
174 |
|
|
sd_cmd_oe_o : out std_logic
|
175 |
|
|
);
|
176 |
|
|
end sd_cmd_host;
|
177 |
|
|
|
178 |
|
|
architecture beh of sd_cmd_host is
|
179 |
|
|
|
180 |
|
|
---------------Internal Constants-------------
|
181 |
|
|
constant INIT_DELAY : integer := 4;
|
182 |
|
|
constant BITS_TO_SEND : integer := 48;
|
183 |
|
|
constant CMD_SIZE : integer := 40;
|
184 |
|
|
constant RESP_SIZE : integer := 128;
|
185 |
|
|
|
186 |
|
|
-----------------Internal Signals-----------
|
187 |
|
|
signal cmd_dat_reg : std_logic;
|
188 |
|
|
signal resp_len : integer;
|
189 |
|
|
signal cmd_buff : unsigned(CMD_SIZE-1 downto 0);
|
190 |
|
|
signal resp_buff : unsigned(RESP_SIZE-1 downto 0);
|
191 |
|
|
signal resp_idx : integer;
|
192 |
|
|
--CRC related
|
193 |
|
|
signal crc_rst_n : std_logic;
|
194 |
|
|
signal crc_in : unsigned(6 downto 0);
|
195 |
|
|
signal crc_val : unsigned(6 downto 0);
|
196 |
|
|
signal crc_enable : std_logic;
|
197 |
|
|
signal crc_bit : std_logic;
|
198 |
|
|
signal crc_ok : std_logic;
|
199 |
|
|
--Internal Counters
|
200 |
|
|
signal counter : integer;
|
201 |
|
|
--State Machines
|
202 |
|
|
type SERIAL_STATE_TYPE is (INIT, IDLE, SETUP_CRC, WRITE, READ_WAIT, READ, FINISH_WR);
|
203 |
|
|
signal serial_state : SERIAL_STATE_TYPE;
|
204 |
|
|
type CMD_STATE_TYPE is (IDLE, EXECUTE, BUSY_WAIT);
|
205 |
|
|
signal cmd_state : CMD_STATE_TYPE;
|
206 |
|
|
|
207 |
|
|
signal expect_response : std_logic;
|
208 |
|
|
signal watchdog : unsigned(15 downto 0);
|
209 |
|
|
|
210 |
|
|
-- Signals originally from sd_cmd_master (DELETE THIS COMMENT SOON)
|
211 |
|
|
signal timeout_reg : unsigned(15 downto 0);
|
212 |
|
|
signal int_status_reg : unsigned(4 downto 0);
|
213 |
|
|
|
214 |
|
|
begin
|
215 |
|
|
|
216 |
|
|
-- Form a signal indicating when a response is expected
|
217 |
|
|
expect_response <= '0' when (cmd_index_i=0 or cmd_index_i=4 or cmd_index_i=15) else '1';
|
218 |
|
|
|
219 |
|
|
-- Command Finite State Machine
|
220 |
|
|
cmd_fsm_proc : process(sys_rst,sd_clk)
|
221 |
|
|
begin
|
222 |
|
|
if (sys_rst='1') then
|
223 |
|
|
response_0_o <= (others=>'0');
|
224 |
|
|
response_1_o <= (others=>'0');
|
225 |
|
|
response_2_o <= (others=>'0');
|
226 |
|
|
response_3_o <= (others=>'0');
|
227 |
|
|
int_status_reg <= (others=>'0');
|
228 |
|
|
resp_len <= 0;
|
229 |
|
|
watchdog <= (others=>'0');
|
230 |
|
|
timeout_reg <= (others=>'0');
|
231 |
|
|
cmd_state <= IDLE;
|
232 |
|
|
cmd_buff <= (others=>'0');
|
233 |
|
|
elsif (sd_clk'event and sd_clk='1') then
|
234 |
|
|
watchdog <= watchdog+1;
|
235 |
|
|
case (cmd_state) is
|
236 |
|
|
when IDLE =>
|
237 |
|
|
-- Only CMD2, CMD9 and CMD10 have long responses...
|
238 |
|
|
if (cmd_index_i=2 or cmd_index_i=9 or cmd_index_i=10) then
|
239 |
|
|
resp_len <= 127;
|
240 |
|
|
else
|
241 |
|
|
resp_len <= 39;
|
242 |
|
|
end if;
|
243 |
|
|
cmd_buff(39 downto 38) <= "01";
|
244 |
|
|
cmd_buff(37 downto 32) <= cmd_index_i;
|
245 |
|
|
cmd_buff(31 downto 0) <= argument_i; --CMD_Argument
|
246 |
|
|
timeout_reg <= timeout_i;
|
247 |
|
|
watchdog <= (others=>'0');
|
248 |
|
|
if (start_i='1') then
|
249 |
|
|
int_status_reg <= (others=>'0');
|
250 |
|
|
end if;
|
251 |
|
|
-- State transition
|
252 |
|
|
if (start_i='1') then
|
253 |
|
|
response_0_o <= (others=>'0');
|
254 |
|
|
response_1_o <= (others=>'0');
|
255 |
|
|
response_2_o <= (others=>'0');
|
256 |
|
|
response_3_o <= (others=>'0');
|
257 |
|
|
cmd_state <= EXECUTE;
|
258 |
|
|
end if;
|
259 |
|
|
|
260 |
|
|
when EXECUTE =>
|
261 |
|
|
if (watchdog > timeout_reg) then
|
262 |
|
|
int_status_reg(INT_CMD_CTE) <= '1';
|
263 |
|
|
int_status_reg(INT_CMD_EI) <= '1';
|
264 |
|
|
response_0_o <= to_unsigned(16#55555555#,32);
|
265 |
|
|
response_1_o <= to_unsigned(16#55555555#,32);
|
266 |
|
|
response_2_o <= to_unsigned(16#55555555#,32);
|
267 |
|
|
response_3_o <= to_unsigned(16#55555555#,32);
|
268 |
|
|
cmd_state <= IDLE;
|
269 |
|
|
else --Incoming New Status
|
270 |
|
|
if (serial_state=FINISH_WR) then --Data available
|
271 |
|
|
-- CMD1 has "1111111" for the CRC field...
|
272 |
|
|
if (cmd_index_i/=1 and crc_ok='0') then
|
273 |
|
|
int_status_reg(INT_CMD_CCRCE) <= '1';
|
274 |
|
|
int_status_reg(INT_CMD_EI) <= '1';
|
275 |
|
|
end if;
|
276 |
|
|
if (resp_len=39 and cmd_index_i/=1 and cmd_buff(37 downto 32)/=resp_buff(125 downto 120)) then
|
277 |
|
|
int_status_reg(INT_CMD_CIE) <= '1';
|
278 |
|
|
int_status_reg(INT_CMD_EI) <= '1';
|
279 |
|
|
end if;
|
280 |
|
|
int_status_reg(INT_CMD_CC) <= '1';
|
281 |
|
|
if (expect_response/='0') then
|
282 |
|
|
response_0_o <= resp_buff(119 downto 88);
|
283 |
|
|
response_1_o <= resp_buff(87 downto 56);
|
284 |
|
|
response_2_o <= resp_buff(55 downto 24);
|
285 |
|
|
response_3_o <= resp_buff(23 downto 0) & "00000000";
|
286 |
|
|
end if;
|
287 |
|
|
-- end
|
288 |
|
|
end if; --Data avaible
|
289 |
|
|
end if; --Status change
|
290 |
|
|
-- State transition
|
291 |
|
|
if (watchdog > timeout_reg) then
|
292 |
|
|
cmd_state <= IDLE;
|
293 |
|
|
elsif (serial_state=FINISH_WR) then
|
294 |
|
|
cmd_state <= BUSY_WAIT;
|
295 |
|
|
end if;
|
296 |
|
|
|
297 |
|
|
when BUSY_WAIT =>
|
298 |
|
|
if (watchdog > timeout_reg) then
|
299 |
|
|
int_status_reg(INT_CMD_CTE) <= '1';
|
300 |
|
|
int_status_reg(INT_CMD_EI) <= '1';
|
301 |
|
|
cmd_state <= IDLE;
|
302 |
|
|
end if;
|
303 |
|
|
-- State transition
|
304 |
|
|
if (busy_i='0') then
|
305 |
|
|
cmd_state <= IDLE;
|
306 |
|
|
end if;
|
307 |
|
|
|
308 |
|
|
when others =>
|
309 |
|
|
cmd_state <= IDLE;
|
310 |
|
|
|
311 |
|
|
end case;
|
312 |
|
|
|
313 |
|
|
if (int_rst_i='1') then
|
314 |
|
|
int_status_reg <= (others=>'0');
|
315 |
|
|
end if;
|
316 |
|
|
|
317 |
|
|
end if;
|
318 |
|
|
end process;
|
319 |
|
|
int_status_o <= int_status_reg;
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
--sd cmd input pad register
|
324 |
|
|
process(sd_clk, sys_rst) -- JLC added sys_rst to sensitivity list.
|
325 |
|
|
begin
|
326 |
|
|
if (sys_rst='1') then
|
327 |
|
|
cmd_dat_reg <= '0';
|
328 |
|
|
-- elsif (sd_clk'event and sd_clk='0') then -- use falling edge, when data is stable
|
329 |
|
|
elsif (sd_clk'event and sd_clk='0') then -- use rising edge, per the specification
|
330 |
|
|
cmd_dat_reg <= sd_cmd_i;
|
331 |
|
|
end if;
|
332 |
|
|
end process;
|
333 |
|
|
|
334 |
|
|
--------------------------------------------
|
335 |
|
|
-- CRC generator
|
336 |
|
|
|
337 |
|
|
crc0 : ucrc_ser
|
338 |
|
|
generic map (
|
339 |
|
|
POLYNOMIAL => "0001001",
|
340 |
|
|
INIT_VALUE => "0000000"
|
341 |
|
|
)
|
342 |
|
|
port map (
|
343 |
|
|
-- System clock and asynchronous reset
|
344 |
|
|
sys_clk => sd_clk,
|
345 |
|
|
sys_rst_n => crc_rst_n,
|
346 |
|
|
sys_clk_en => crc_enable,
|
347 |
|
|
|
348 |
|
|
-- Input and Control
|
349 |
|
|
clear_i => '0',
|
350 |
|
|
data_i => crc_bit,
|
351 |
|
|
flush_i => '0',
|
352 |
|
|
|
353 |
|
|
-- Output
|
354 |
|
|
match_o => open,
|
355 |
|
|
crc_o => crc_val
|
356 |
|
|
);
|
357 |
|
|
|
358 |
|
|
--------------------------------------------
|
359 |
|
|
-- This is the serial_state machine
|
360 |
|
|
--------------------------------------------
|
361 |
|
|
serial_fsm_proc : process(sd_clk, sys_rst)
|
362 |
|
|
begin
|
363 |
|
|
if (sys_rst='1') then
|
364 |
|
|
serial_state <= INIT;
|
365 |
|
|
crc_enable <= '0';
|
366 |
|
|
resp_idx <= 0;
|
367 |
|
|
sd_cmd_oe_o <= '1';
|
368 |
|
|
sd_cmd_o <= '1';
|
369 |
|
|
resp_buff <= (others=>'0');
|
370 |
|
|
crc_rst_n <= '0';
|
371 |
|
|
crc_bit <= '0';
|
372 |
|
|
crc_in <= (others=>'0');
|
373 |
|
|
crc_ok <= '0';
|
374 |
|
|
counter <= 0;
|
375 |
|
|
elsif (sd_clk'event and sd_clk='1') then
|
376 |
|
|
|
377 |
|
|
case (serial_state) is
|
378 |
|
|
|
379 |
|
|
when INIT =>
|
380 |
|
|
counter <= counter+1;
|
381 |
|
|
sd_cmd_oe_o <= '1';
|
382 |
|
|
sd_cmd_o <= '1';
|
383 |
|
|
if (counter >= INIT_DELAY) then
|
384 |
|
|
serial_state <= IDLE;
|
385 |
|
|
end if;
|
386 |
|
|
|
387 |
|
|
when IDLE =>
|
388 |
|
|
sd_cmd_oe_o <= '0'; --Put CMD to Z
|
389 |
|
|
counter <= 0;
|
390 |
|
|
crc_rst_n <= '0';
|
391 |
|
|
crc_enable <= '0';
|
392 |
|
|
resp_idx <= 0;
|
393 |
|
|
if (start_i='1') then
|
394 |
|
|
serial_state <= SETUP_CRC;
|
395 |
|
|
resp_buff <= (others=>'0');
|
396 |
|
|
end if;
|
397 |
|
|
|
398 |
|
|
when SETUP_CRC =>
|
399 |
|
|
crc_rst_n <= '1';
|
400 |
|
|
crc_enable <= '1';
|
401 |
|
|
crc_bit <= cmd_buff(CMD_SIZE-1-counter);
|
402 |
|
|
serial_state <= WRITE;
|
403 |
|
|
|
404 |
|
|
when WRITE =>
|
405 |
|
|
if (counter < BITS_TO_SEND-8) then -- 1->40 CMD, (41 <= CNT <=47) CRC, 48 stop_bit
|
406 |
|
|
sd_cmd_oe_o <= '1';
|
407 |
|
|
sd_cmd_o <= cmd_buff(CMD_SIZE-1-counter);
|
408 |
|
|
if (counter < BITS_TO_SEND-9) then --1 step ahead
|
409 |
|
|
crc_bit <= cmd_buff((CMD_SIZE-1-counter)-1);
|
410 |
|
|
else
|
411 |
|
|
crc_enable <= '0';
|
412 |
|
|
end if;
|
413 |
|
|
elsif (counter < BITS_TO_SEND-1) then
|
414 |
|
|
crc_enable <= '0';
|
415 |
|
|
sd_cmd_o <= crc_val(BITS_TO_SEND-counter-2);
|
416 |
|
|
sd_cmd_oe_o <= '1';
|
417 |
|
|
elsif (counter = BITS_TO_SEND-1) then
|
418 |
|
|
sd_cmd_oe_o <= '1';
|
419 |
|
|
sd_cmd_o <= '1';
|
420 |
|
|
else
|
421 |
|
|
sd_cmd_oe_o <= '0';
|
422 |
|
|
sd_cmd_o <= '1';
|
423 |
|
|
end if;
|
424 |
|
|
counter <= counter+1;
|
425 |
|
|
|
426 |
|
|
if (counter >= BITS_TO_SEND and expect_response='1') then
|
427 |
|
|
serial_state <= READ_WAIT;
|
428 |
|
|
elsif (counter >= BITS_TO_SEND) then
|
429 |
|
|
serial_state <= FINISH_WR;
|
430 |
|
|
end if;
|
431 |
|
|
|
432 |
|
|
when READ_WAIT =>
|
433 |
|
|
crc_enable <= '0';
|
434 |
|
|
crc_rst_n <= '0';
|
435 |
|
|
counter <= 1;
|
436 |
|
|
sd_cmd_oe_o <= '0';
|
437 |
|
|
resp_buff(RESP_SIZE-1) <= cmd_dat_reg;
|
438 |
|
|
if (cmd_dat_reg='0') then
|
439 |
|
|
serial_state <= READ;
|
440 |
|
|
end if;
|
441 |
|
|
|
442 |
|
|
when READ =>
|
443 |
|
|
crc_rst_n <= '1';
|
444 |
|
|
if ((resp_len/=RESP_SIZE-1) or (counter>7)) then
|
445 |
|
|
crc_enable <= '1';
|
446 |
|
|
end if;
|
447 |
|
|
sd_cmd_oe_o <= '0';
|
448 |
|
|
if (counter <= resp_len) then
|
449 |
|
|
if (counter < 8) then --1+1+6 (S,T,Index)
|
450 |
|
|
resp_buff(RESP_SIZE-1-counter) <= cmd_dat_reg;
|
451 |
|
|
else
|
452 |
|
|
resp_idx <= resp_idx + 1;
|
453 |
|
|
resp_buff(RESP_SIZE-9-resp_idx) <= cmd_dat_reg;
|
454 |
|
|
end if;
|
455 |
|
|
crc_bit <= cmd_dat_reg;
|
456 |
|
|
elsif (counter-resp_len <= 7) then
|
457 |
|
|
crc_in((resp_len+7)-counter) <= cmd_dat_reg;
|
458 |
|
|
crc_enable <= '0';
|
459 |
|
|
else
|
460 |
|
|
crc_enable <= '0';
|
461 |
|
|
if (crc_in = crc_val) then
|
462 |
|
|
crc_ok <= '1';
|
463 |
|
|
else
|
464 |
|
|
crc_ok <= '0';
|
465 |
|
|
end if;
|
466 |
|
|
end if;
|
467 |
|
|
counter <= counter + 1;
|
468 |
|
|
if (counter >= resp_len+8) then
|
469 |
|
|
serial_state <= FINISH_WR;
|
470 |
|
|
end if;
|
471 |
|
|
|
472 |
|
|
when FINISH_WR =>
|
473 |
|
|
crc_enable <= '0';
|
474 |
|
|
crc_rst_n <= '0';
|
475 |
|
|
counter <= 0;
|
476 |
|
|
sd_cmd_oe_o <= '0';
|
477 |
|
|
serial_state <= IDLE;
|
478 |
|
|
|
479 |
|
|
when others =>
|
480 |
|
|
serial_state <= INIT;
|
481 |
|
|
|
482 |
|
|
end case;
|
483 |
|
|
|
484 |
|
|
end if;
|
485 |
|
|
end process;
|
486 |
|
|
|
487 |
|
|
end beh;
|
488 |
|
|
|
489 |
|
|
----------------------------------------------------------------------
|
490 |
|
|
---- ----
|
491 |
|
|
---- WISHBONE SD Card Controller IP Core ----
|
492 |
|
|
---- ----
|
493 |
|
|
---- sd_data_8bit_host.vhd ----
|
494 |
|
|
---- ----
|
495 |
|
|
---- This file is part of the WISHBONE SD Card ----
|
496 |
|
|
---- Controller IP Core project ----
|
497 |
|
|
---- http:--opencores.org/project,sd_card_controller ----
|
498 |
|
|
---- ----
|
499 |
|
|
---- Description ----
|
500 |
|
|
---- Module resposible for sending and receiving data through ----
|
501 |
|
|
---- sd/mmc card data interface ----
|
502 |
|
|
---- ----
|
503 |
|
|
---- Author(s): ----
|
504 |
|
|
---- - John Clayton, morianton@gmail.com ----
|
505 |
|
|
---- ----
|
506 |
|
|
----------------------------------------------------------------------
|
507 |
|
|
---- ----
|
508 |
|
|
---- Copyright (C) 2016 Authors ----
|
509 |
|
|
---- ----
|
510 |
|
|
---- Based on original work by ----
|
511 |
|
|
---- Adam Edvardsson (adam.edvardsson@orsoc.se) ----
|
512 |
|
|
---- ----
|
513 |
|
|
---- Copyright (C) 2009 Authors ----
|
514 |
|
|
---- ----
|
515 |
|
|
---- This source file may be used and distributed without ----
|
516 |
|
|
---- restriction provided that this copyright statement is not ----
|
517 |
|
|
---- removed from the file and that any derivative work contains ----
|
518 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
519 |
|
|
---- ----
|
520 |
|
|
---- This source file is free software; you can redistribute it ----
|
521 |
|
|
---- and/or modify it under the terms of the GNU Lesser General ----
|
522 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
523 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
524 |
|
|
---- later version. ----
|
525 |
|
|
---- ----
|
526 |
|
|
---- This source is distributed in the hope that it will be ----
|
527 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
528 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
529 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
530 |
|
|
---- details. ----
|
531 |
|
|
---- ----
|
532 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
533 |
|
|
---- Public License along with this source; if not, download it ----
|
534 |
|
|
---- from http:--www.opencores.org/lgpl.shtml ----
|
535 |
|
|
---- ----
|
536 |
|
|
-- Author: John Clayton --
|
537 |
|
|
-- Update: June 11, 2016 Added bustest_w_i and bustest_r_i, plus --
|
538 |
|
|
-- bustest_res_o result output. --
|
539 |
|
|
---- ----
|
540 |
|
|
----------------------------------------------------------------------
|
541 |
|
|
--
|
542 |
|
|
-- Explanation:
|
543 |
|
|
-- bustest_res_o values are:
|
544 |
|
|
-- 000 = BUSTEST_R (CMD14) not yet performed
|
545 |
|
|
-- 001 = 1 bit data width
|
546 |
|
|
-- 010 = 4 bit data width
|
547 |
|
|
-- 011 = 8 bit data width
|
548 |
|
|
-- 100 = Unspecified Error
|
549 |
|
|
--
|
550 |
|
|
|
551 |
|
|
library IEEE;
|
552 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
553 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
554 |
|
|
|
555 |
|
|
library work;
|
556 |
|
|
use work.sd_host_pack.all;
|
557 |
|
|
use work.ucrc_pack.all;
|
558 |
|
|
|
559 |
|
|
entity sd_data_8bit_host is
|
560 |
|
|
port(
|
561 |
|
|
sd_clk : in std_logic;
|
562 |
|
|
sys_rst : in std_logic;
|
563 |
|
|
--Tx Fifo
|
564 |
|
|
tx_dat_i : in unsigned(7 downto 0);
|
565 |
|
|
tx_dat_rd_o : out std_logic;
|
566 |
|
|
--Rx Fifo
|
567 |
|
|
rx_dat_o : out unsigned(7 downto 0);
|
568 |
|
|
rx_dat_we_o : out std_logic;
|
569 |
|
|
--SD data
|
570 |
|
|
sd_dat_siz_o : out unsigned(1 downto 0);
|
571 |
|
|
sd_dat_oe_o : out std_logic;
|
572 |
|
|
sd_dat_o : out unsigned(7 downto 0);
|
573 |
|
|
sd_dat_i : in unsigned(7 downto 0);
|
574 |
|
|
--Control signals
|
575 |
|
|
blksize_i : in unsigned(BLKSIZE_W-1 downto 0);
|
576 |
|
|
bus_size_i : in unsigned(1 downto 0);
|
577 |
|
|
blkcnt_i : in unsigned(BLKCNT_W-1 downto 0);
|
578 |
|
|
d_stop_i : in std_logic;
|
579 |
|
|
d_read_i : in std_logic;
|
580 |
|
|
d_write_i : in std_logic;
|
581 |
|
|
bustest_w_i : in std_logic;
|
582 |
|
|
bustest_r_i : in std_logic;
|
583 |
|
|
bustest_res_o : out unsigned(2 downto 0);
|
584 |
|
|
sd_dat_busy_o : out std_logic;
|
585 |
|
|
fsm_busy_o : out std_logic;
|
586 |
|
|
crc_ok_o : out std_logic
|
587 |
|
|
);
|
588 |
|
|
end sd_data_8bit_host;
|
589 |
|
|
|
590 |
|
|
architecture beh of sd_data_8bit_host is
|
591 |
|
|
|
592 |
|
|
-- Internal constants
|
593 |
|
|
|
594 |
|
|
-- Internal signals
|
595 |
|
|
signal dat_reg : unsigned(7 downto 0);
|
596 |
|
|
signal data_cycles : unsigned(BLKSIZE_W+2 downto 0);
|
597 |
|
|
signal bus_size_reg : unsigned(1 downto 0);
|
598 |
|
|
--CRC16
|
599 |
|
|
signal crc_in : unsigned(7 downto 0);
|
600 |
|
|
signal crc_enable : std_logic;
|
601 |
|
|
signal crc_rst_n : std_logic;
|
602 |
|
|
type crc_out_type is
|
603 |
|
|
array (integer range 0 to 7) of unsigned(15 downto 0);
|
604 |
|
|
signal crc_out : crc_out_type;
|
605 |
|
|
signal crc_ok_l : std_logic;
|
606 |
|
|
signal transf_cnt : unsigned(BLKSIZE_W+3 downto 0);
|
607 |
|
|
--State Machine
|
608 |
|
|
type FSM_STATE_TYPE is (IDLE, WRITE_DAT, CHECK_CRC_STATUS, WRITE_BUSY,
|
609 |
|
|
READ_WAIT, READ_DAT, SEND_BUSTEST,
|
610 |
|
|
READ_BUSTEST_WAIT, READ_BUSTEST, ANALYZE_BUSTEST);
|
611 |
|
|
signal state : FSM_STATE_TYPE;
|
612 |
|
|
|
613 |
|
|
signal crc_s_cnt : unsigned(2 downto 0);
|
614 |
|
|
signal busy_int : std_logic;
|
615 |
|
|
signal blkcnt_reg : unsigned(BLKCNT_W-1 downto 0);
|
616 |
|
|
signal start_bit : std_logic;
|
617 |
|
|
signal crc_c : unsigned(4 downto 0);
|
618 |
|
|
signal crc_s : unsigned(3 downto 0);
|
619 |
|
|
signal data_index : unsigned(2 downto 0);
|
620 |
|
|
signal last_din : unsigned(7 downto 0);
|
621 |
|
|
|
622 |
|
|
signal bustest_0 : unsigned(7 downto 0);
|
623 |
|
|
signal bustest_1 : unsigned(7 downto 0);
|
624 |
|
|
signal bustest_x : unsigned(7 downto 0);
|
625 |
|
|
|
626 |
|
|
begin
|
627 |
|
|
|
628 |
|
|
--sd data input pad register
|
629 |
|
|
process(sd_clk)
|
630 |
|
|
begin
|
631 |
|
|
if (sd_clk'event and sd_clk='1') then
|
632 |
|
|
dat_reg <= sd_dat_i;
|
633 |
|
|
end if;
|
634 |
|
|
end process;
|
635 |
|
|
|
636 |
|
|
-- There are eight different CRC generators
|
637 |
|
|
sd_crc_gen : for nvar in 0 to 7 generate
|
638 |
|
|
begin
|
639 |
|
|
crc_unit : ucrc_ser
|
640 |
|
|
generic map (
|
641 |
|
|
POLYNOMIAL => "0001000000100001",
|
642 |
|
|
INIT_VALUE => "0000000000000000"
|
643 |
|
|
)
|
644 |
|
|
port map (
|
645 |
|
|
-- System clock and asynchronous reset
|
646 |
|
|
sys_clk => sd_clk,
|
647 |
|
|
sys_rst_n => crc_rst_n,
|
648 |
|
|
sys_clk_en => crc_enable,
|
649 |
|
|
|
650 |
|
|
-- Input and Control
|
651 |
|
|
clear_i => '0',
|
652 |
|
|
data_i => crc_in(nvar),
|
653 |
|
|
flush_i => '0',
|
654 |
|
|
|
655 |
|
|
-- Output
|
656 |
|
|
match_o => open,
|
657 |
|
|
crc_o => crc_out(nvar)
|
658 |
|
|
);
|
659 |
|
|
end generate;
|
660 |
|
|
|
661 |
|
|
crc_ok_o <= crc_ok_l;
|
662 |
|
|
fsm_busy_o <= '1' when (state/=IDLE) else '0';
|
663 |
|
|
start_bit <= '1' when (dat_reg(0)='0') else '0';
|
664 |
|
|
sd_dat_busy_o <= '1' when (dat_reg(0)='0') else '0';
|
665 |
|
|
-- Provide external bus size signals, for controlling data bus tri-states
|
666 |
|
|
sd_dat_siz_o <= "10" when state=SEND_BUSTEST else bus_size_i;
|
667 |
|
|
|
668 |
|
|
-- Create bus test analysis, by XORing the two returned patterns
|
669 |
|
|
-- together.
|
670 |
|
|
bustest_x <= bustest_0 xor bustest_1;
|
671 |
|
|
|
672 |
|
|
fsm_proc : process(sys_rst,sd_clk)
|
673 |
|
|
begin
|
674 |
|
|
if (sys_rst='1') then
|
675 |
|
|
state <= IDLE;
|
676 |
|
|
sd_dat_oe_o <= '0';
|
677 |
|
|
crc_enable <= '0';
|
678 |
|
|
crc_rst_n <= '0';
|
679 |
|
|
transf_cnt <= (others=>'0');
|
680 |
|
|
tx_dat_rd_o <= '0';
|
681 |
|
|
last_din <= (others=>'0');
|
682 |
|
|
crc_c <= (others=>'0');
|
683 |
|
|
crc_in <= (others=>'0');
|
684 |
|
|
sd_dat_o <= (others=>'0');
|
685 |
|
|
crc_s_cnt <= (others=>'0');
|
686 |
|
|
crc_s <= (others=>'0');
|
687 |
|
|
rx_dat_we_o <= '0';
|
688 |
|
|
rx_dat_o <= (others=>'0');
|
689 |
|
|
crc_ok_l <= '0';
|
690 |
|
|
busy_int <= '0';
|
691 |
|
|
data_index <= (others=>'0');
|
692 |
|
|
blkcnt_reg <= (others=>'0');
|
693 |
|
|
data_cycles <= (others=>'0');
|
694 |
|
|
bus_size_reg <= (others=>'0');
|
695 |
|
|
bustest_0 <= (others=>'0');
|
696 |
|
|
bustest_1 <= (others=>'0');
|
697 |
|
|
bustest_res_o <= (others=>'0');
|
698 |
|
|
elsif (sd_clk'event and sd_clk='1') then
|
699 |
|
|
case(state) is
|
700 |
|
|
when IDLE =>
|
701 |
|
|
sd_dat_oe_o <= '0';
|
702 |
|
|
sd_dat_o <= "11111111";
|
703 |
|
|
crc_enable <= '0';
|
704 |
|
|
crc_rst_n <= '0';
|
705 |
|
|
transf_cnt <= (others=>'0');
|
706 |
|
|
crc_c <= to_unsigned(16,crc_c'length);
|
707 |
|
|
crc_s_cnt <= (others=>'0');
|
708 |
|
|
crc_s <= (others=>'0');
|
709 |
|
|
rx_dat_we_o <= '0';
|
710 |
|
|
tx_dat_rd_o <= '0';
|
711 |
|
|
data_index <= (others=>'1');
|
712 |
|
|
blkcnt_reg <= blkcnt_i;
|
713 |
|
|
if (bus_size_i=2) then
|
714 |
|
|
data_cycles <= "000" & blksize_i; -- (<<0) operation
|
715 |
|
|
elsif (bus_size_i=1) then
|
716 |
|
|
data_cycles <= "00" & blksize_i & '0'; -- (<<1) operation
|
717 |
|
|
else
|
718 |
|
|
data_cycles <= blksize_i & "000"; -- (<<3) operation
|
719 |
|
|
end if;
|
720 |
|
|
bus_size_reg <= bus_size_i;
|
721 |
|
|
-- state transition
|
722 |
|
|
if (d_read_i='0' and d_write_i='1' and bustest_w_i='0' and bustest_r_i='0') then
|
723 |
|
|
state <= WRITE_DAT;
|
724 |
|
|
elsif (d_read_i='1' and d_write_i='0' and bustest_w_i='0' and bustest_r_i='0') then
|
725 |
|
|
state <= READ_WAIT;
|
726 |
|
|
elsif (d_read_i='0' and d_write_i='0' and bustest_w_i='1' and bustest_r_i='0') then
|
727 |
|
|
data_cycles <= to_unsigned(24,data_cycles'length);
|
728 |
|
|
state <= SEND_BUSTEST;
|
729 |
|
|
elsif (d_read_i='0' and d_write_i='0' and bustest_w_i='0' and bustest_r_i='1') then
|
730 |
|
|
state <= READ_BUSTEST_WAIT;
|
731 |
|
|
end if;
|
732 |
|
|
|
733 |
|
|
when SEND_BUSTEST =>
|
734 |
|
|
transf_cnt <= transf_cnt+1;
|
735 |
|
|
tx_dat_rd_o <= '0';
|
736 |
|
|
-- Send out start bits
|
737 |
|
|
if (transf_cnt = 1) then
|
738 |
|
|
sd_dat_oe_o <= '1';
|
739 |
|
|
sd_dat_o <= "00000000";
|
740 |
|
|
end if;
|
741 |
|
|
-- Send out first pattern
|
742 |
|
|
if (transf_cnt = 2) then
|
743 |
|
|
sd_dat_o <= "10101010";
|
744 |
|
|
end if;
|
745 |
|
|
-- Send out second pattern
|
746 |
|
|
if (transf_cnt = 3) then
|
747 |
|
|
sd_dat_o <= "01010101";
|
748 |
|
|
end if;
|
749 |
|
|
-- Send out zeros
|
750 |
|
|
if ((transf_cnt >= 4) and (transf_cnt < data_cycles)) then
|
751 |
|
|
sd_dat_o <= "00000000";
|
752 |
|
|
end if;
|
753 |
|
|
if (transf_cnt = data_cycles) then -- stop bits
|
754 |
|
|
sd_dat_o <= "11111111";
|
755 |
|
|
end if;
|
756 |
|
|
if (transf_cnt = data_cycles+1) then
|
757 |
|
|
sd_dat_oe_o <= '0';
|
758 |
|
|
end if;
|
759 |
|
|
-- state transition
|
760 |
|
|
if (transf_cnt >= data_cycles+1) then
|
761 |
|
|
transf_cnt <= (others=>'0');
|
762 |
|
|
state <= IDLE;
|
763 |
|
|
end if;
|
764 |
|
|
|
765 |
|
|
when READ_BUSTEST_WAIT =>
|
766 |
|
|
sd_dat_oe_o <= '0';
|
767 |
|
|
transf_cnt <= (others=>'0');
|
768 |
|
|
bustest_0 <= (others=>'0');
|
769 |
|
|
bustest_1 <= (others=>'0');
|
770 |
|
|
bustest_res_o <= (others=>'0');
|
771 |
|
|
-- state transition
|
772 |
|
|
if (start_bit='1') then
|
773 |
|
|
state <= READ_BUSTEST;
|
774 |
|
|
end if;
|
775 |
|
|
|
776 |
|
|
when READ_BUSTEST =>
|
777 |
|
|
transf_cnt <= transf_cnt+1;
|
778 |
|
|
if (transf_cnt = 0) then
|
779 |
|
|
bustest_0 <= dat_reg;
|
780 |
|
|
end if;
|
781 |
|
|
if (transf_cnt = 1) then
|
782 |
|
|
bustest_1 <= dat_reg;
|
783 |
|
|
end if;
|
784 |
|
|
-- state transition
|
785 |
|
|
-- No CRC status response is needed
|
786 |
|
|
-- Look for stop bits
|
787 |
|
|
if (dat_reg = "11111111") then
|
788 |
|
|
state <= ANALYZE_BUSTEST;
|
789 |
|
|
end if;
|
790 |
|
|
|
791 |
|
|
when ANALYZE_BUSTEST =>
|
792 |
|
|
if (bustest_x="00000001") then
|
793 |
|
|
bustest_res_o <= "001";
|
794 |
|
|
elsif (bustest_x="00001111") then
|
795 |
|
|
bustest_res_o <= "010";
|
796 |
|
|
elsif (bustest_x="11111111") then
|
797 |
|
|
bustest_res_o <= "011";
|
798 |
|
|
else
|
799 |
|
|
bustest_res_o <= "100";
|
800 |
|
|
end if;
|
801 |
|
|
state <= IDLE;
|
802 |
|
|
|
803 |
|
|
when WRITE_DAT =>
|
804 |
|
|
crc_ok_l <= '0';
|
805 |
|
|
transf_cnt <= transf_cnt+1;
|
806 |
|
|
tx_dat_rd_o <= '0';
|
807 |
|
|
-- Load values for last_din and crc_in
|
808 |
|
|
if (bus_size_reg=2) then
|
809 |
|
|
last_din <= tx_dat_i;
|
810 |
|
|
crc_in <= tx_dat_i;
|
811 |
|
|
if (transf_cnt<data_cycles-1) then
|
812 |
|
|
tx_dat_rd_o <= '1';
|
813 |
|
|
else
|
814 |
|
|
tx_dat_rd_o <= '0';
|
815 |
|
|
end if;
|
816 |
|
|
elsif (bus_size_reg=1) then
|
817 |
|
|
last_din <=
|
818 |
|
|
"1111" &
|
819 |
|
|
tx_dat_i(7-(4*to_integer(data_index(0 downto 0)))) &
|
820 |
|
|
tx_dat_i(6-(4*to_integer(data_index(0 downto 0)))) &
|
821 |
|
|
tx_dat_i(5-(4*to_integer(data_index(0 downto 0)))) &
|
822 |
|
|
tx_dat_i(4-(4*to_integer(data_index(0 downto 0))));
|
823 |
|
|
crc_in <=
|
824 |
|
|
"1111" &
|
825 |
|
|
tx_dat_i(7-(4*to_integer(data_index(0 downto 0)))) &
|
826 |
|
|
tx_dat_i(6-(4*to_integer(data_index(0 downto 0)))) &
|
827 |
|
|
tx_dat_i(5-(4*to_integer(data_index(0 downto 0)))) &
|
828 |
|
|
tx_dat_i(4-(4*to_integer(data_index(0 downto 0))));
|
829 |
|
|
if (transf_cnt<data_cycles) and (data_index(0 downto 0)=0) then
|
830 |
|
|
tx_dat_rd_o <= '1';
|
831 |
|
|
end if;
|
832 |
|
|
if (data_index(0 downto 0)=1) then
|
833 |
|
|
data_index <= (others=>'0');
|
834 |
|
|
else
|
835 |
|
|
data_index <= data_index+1;
|
836 |
|
|
end if;
|
837 |
|
|
else
|
838 |
|
|
last_din <= "1111111" & tx_dat_i(7-to_integer(data_index));
|
839 |
|
|
crc_in <= "1111111" & tx_dat_i(7-to_integer(data_index));
|
840 |
|
|
if (transf_cnt<data_cycles) and (data_index = 6) then
|
841 |
|
|
tx_dat_rd_o <= '1';
|
842 |
|
|
end if;
|
843 |
|
|
if (data_index = 7) then
|
844 |
|
|
data_index <= (others=>'0');
|
845 |
|
|
else
|
846 |
|
|
data_index <= data_index+1;
|
847 |
|
|
end if;
|
848 |
|
|
end if;
|
849 |
|
|
-- Treat first transfer differently
|
850 |
|
|
if (transf_cnt = 1) then
|
851 |
|
|
crc_rst_n <= '1';
|
852 |
|
|
crc_enable <= '1';
|
853 |
|
|
if (bus_size_reg=2) then
|
854 |
|
|
last_din <= tx_dat_i;
|
855 |
|
|
crc_in <= tx_dat_i;
|
856 |
|
|
elsif (bus_size_reg=1) then
|
857 |
|
|
last_din <= "1111" & tx_dat_i(7 downto 4);
|
858 |
|
|
crc_in <= "1111" & tx_dat_i(7 downto 4);
|
859 |
|
|
else
|
860 |
|
|
last_din <= "1111111" & tx_dat_i(7);
|
861 |
|
|
crc_in <= "1111111" & tx_dat_i(7);
|
862 |
|
|
end if;
|
863 |
|
|
sd_dat_oe_o <= '1';
|
864 |
|
|
if (bus_size_reg=2) then -- start bits
|
865 |
|
|
sd_dat_o <= "00000000";
|
866 |
|
|
elsif (bus_size_reg=1) then
|
867 |
|
|
sd_dat_o <= "11110000";
|
868 |
|
|
else
|
869 |
|
|
sd_dat_o <= "11111110";
|
870 |
|
|
end if;
|
871 |
|
|
data_index <= to_unsigned(1,data_index'length);
|
872 |
|
|
elsif ((transf_cnt >= 2) and (transf_cnt <= data_cycles+1)) then
|
873 |
|
|
sd_dat_o <= last_din;
|
874 |
|
|
if (transf_cnt = data_cycles+1) then
|
875 |
|
|
crc_enable <= '0';
|
876 |
|
|
end if;
|
877 |
|
|
elsif (transf_cnt > data_cycles+1) and (crc_c/=0) then
|
878 |
|
|
crc_enable <= '0';
|
879 |
|
|
crc_c <= crc_c-1;
|
880 |
|
|
sd_dat_o(0) <= crc_out(0)(to_integer(crc_c)-1);
|
881 |
|
|
if (bus_size_reg=2) then
|
882 |
|
|
sd_dat_o(7 downto 1) <= crc_out(7)(to_integer(crc_c)-1) & crc_out(6)(to_integer(crc_c)-1) &
|
883 |
|
|
crc_out(5)(to_integer(crc_c)-1) & crc_out(4)(to_integer(crc_c)-1) &
|
884 |
|
|
crc_out(3)(to_integer(crc_c)-1) & crc_out(2)(to_integer(crc_c)-1) &
|
885 |
|
|
crc_out(1)(to_integer(crc_c)-1);
|
886 |
|
|
elsif (bus_size_reg=1) then
|
887 |
|
|
sd_dat_o(3 downto 1) <= crc_out(3)(to_integer(crc_c)-1) & crc_out(2)(to_integer(crc_c)-1) &
|
888 |
|
|
crc_out(1)(to_integer(crc_c)-1);
|
889 |
|
|
sd_dat_o(7 downto 4) <= (others=>'1');
|
890 |
|
|
else
|
891 |
|
|
sd_dat_o(7 downto 1) <= (others=>'1');
|
892 |
|
|
end if;
|
893 |
|
|
elsif (transf_cnt = data_cycles+18) then -- stop bits
|
894 |
|
|
sd_dat_o <= "11111111";
|
895 |
|
|
elsif (transf_cnt >= data_cycles+19) then
|
896 |
|
|
sd_dat_oe_o <= '0';
|
897 |
|
|
end if;
|
898 |
|
|
-- state transition
|
899 |
|
|
-- There are two clocks of bus turnaround time, plus one extra
|
900 |
|
|
-- because of the data input register, then the start bit of
|
901 |
|
|
-- the CRC status occurs.
|
902 |
|
|
if (d_stop_i='1') then
|
903 |
|
|
state <= IDLE;
|
904 |
|
|
elsif (transf_cnt = data_cycles+23) then
|
905 |
|
|
if (start_bit='1') then
|
906 |
|
|
state <= CHECK_CRC_STATUS;
|
907 |
|
|
else
|
908 |
|
|
transf_cnt <= (others=>'0');
|
909 |
|
|
blkcnt_reg <= blkcnt_reg-1;
|
910 |
|
|
crc_rst_n <= '0';
|
911 |
|
|
crc_c <= to_unsigned(16,crc_c'length);
|
912 |
|
|
crc_s_cnt <= (others=>'0');
|
913 |
|
|
if (blkcnt_reg>1) then
|
914 |
|
|
state <= WRITE_DAT;
|
915 |
|
|
else
|
916 |
|
|
state <= IDLE;
|
917 |
|
|
end if;
|
918 |
|
|
end if;
|
919 |
|
|
end if;
|
920 |
|
|
|
921 |
|
|
when CHECK_CRC_STATUS =>
|
922 |
|
|
if (crc_s_cnt < 4) then
|
923 |
|
|
crc_s(to_integer(crc_s_cnt)) <= dat_reg(0);
|
924 |
|
|
end if;
|
925 |
|
|
crc_s_cnt <= crc_s_cnt+1;
|
926 |
|
|
busy_int <= '1';
|
927 |
|
|
-- state transition
|
928 |
|
|
if (crc_s_cnt = 4) then
|
929 |
|
|
if (crc_s="1010") then
|
930 |
|
|
crc_ok_l <= '1';
|
931 |
|
|
else
|
932 |
|
|
crc_ok_l <= '0';
|
933 |
|
|
end if;
|
934 |
|
|
state <= WRITE_BUSY;
|
935 |
|
|
end if;
|
936 |
|
|
|
937 |
|
|
when WRITE_BUSY =>
|
938 |
|
|
busy_int <= not dat_reg(0);
|
939 |
|
|
transf_cnt <= (others=>'0');
|
940 |
|
|
-- state transition
|
941 |
|
|
if (busy_int='0') then
|
942 |
|
|
blkcnt_reg <= blkcnt_reg-1;
|
943 |
|
|
crc_rst_n <= '0';
|
944 |
|
|
crc_c <= to_unsigned(16,crc_c'length);
|
945 |
|
|
crc_s_cnt <= (others=>'0');
|
946 |
|
|
if (blkcnt_reg>1 and crc_ok_l='1') then
|
947 |
|
|
state <= WRITE_DAT;
|
948 |
|
|
elsif (busy_int='0') then
|
949 |
|
|
state <= IDLE;
|
950 |
|
|
end if;
|
951 |
|
|
end if;
|
952 |
|
|
|
953 |
|
|
when READ_WAIT =>
|
954 |
|
|
crc_rst_n <= '1';
|
955 |
|
|
crc_enable <= '1';
|
956 |
|
|
crc_in <= (others=>'0');
|
957 |
|
|
crc_c <= to_unsigned(15,crc_c'length);-- end
|
958 |
|
|
transf_cnt <= (others=>'0');
|
959 |
|
|
-- state transition
|
960 |
|
|
if (start_bit='1') then
|
961 |
|
|
state <= READ_DAT;
|
962 |
|
|
end if;
|
963 |
|
|
|
964 |
|
|
when READ_DAT =>
|
965 |
|
|
transf_cnt <= transf_cnt+1;
|
966 |
|
|
if (transf_cnt < data_cycles) then
|
967 |
|
|
if (bus_size_reg=2) then
|
968 |
|
|
rx_dat_we_o <= '1';
|
969 |
|
|
rx_dat_o <= dat_reg;
|
970 |
|
|
elsif (bus_size_reg=1) then
|
971 |
|
|
if (transf_cnt(0 downto 0)=1) then
|
972 |
|
|
rx_dat_we_o <= '1';
|
973 |
|
|
else
|
974 |
|
|
rx_dat_we_o <= '0';
|
975 |
|
|
end if;
|
976 |
|
|
rx_dat_o(7-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(3);
|
977 |
|
|
rx_dat_o(6-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(2);
|
978 |
|
|
rx_dat_o(5-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(1);
|
979 |
|
|
rx_dat_o(4-(4*to_integer(transf_cnt(0 downto 0)))) <= dat_reg(0);
|
980 |
|
|
else
|
981 |
|
|
if (transf_cnt(2 downto 0)=7) then
|
982 |
|
|
rx_dat_we_o <= '1';
|
983 |
|
|
else
|
984 |
|
|
rx_dat_we_o <= '0';
|
985 |
|
|
end if;
|
986 |
|
|
rx_dat_o(7-to_integer(transf_cnt(2 downto 0))) <= dat_reg(0);
|
987 |
|
|
end if;
|
988 |
|
|
crc_in <= dat_reg;
|
989 |
|
|
crc_ok_l <= '1';
|
990 |
|
|
elsif (transf_cnt <= data_cycles+16) then
|
991 |
|
|
crc_enable <= '0';
|
992 |
|
|
last_din <= dat_reg;
|
993 |
|
|
rx_dat_we_o <= '0';
|
994 |
|
|
if (transf_cnt > data_cycles) then
|
995 |
|
|
crc_c <= crc_c-1;
|
996 |
|
|
if (crc_out(0)(to_integer(crc_c)) /= last_din(0)) then
|
997 |
|
|
crc_ok_l <= '0';
|
998 |
|
|
end if;
|
999 |
|
|
if (crc_out(1)(to_integer(crc_c)) /= last_din(1) and bus_size_reg>0) then
|
1000 |
|
|
crc_ok_l <= '0';
|
1001 |
|
|
end if;
|
1002 |
|
|
if (crc_out(2)(to_integer(crc_c)) /= last_din(2) and bus_size_reg>0) then
|
1003 |
|
|
crc_ok_l <= '0';
|
1004 |
|
|
end if;
|
1005 |
|
|
if (crc_out(3)(to_integer(crc_c)) /= last_din(3) and bus_size_reg>0) then
|
1006 |
|
|
crc_ok_l <= '0';
|
1007 |
|
|
end if;
|
1008 |
|
|
if (crc_out(4)(to_integer(crc_c)) /= last_din(4) and bus_size_reg>1) then
|
1009 |
|
|
crc_ok_l <= '0';
|
1010 |
|
|
end if;
|
1011 |
|
|
if (crc_out(5)(to_integer(crc_c)) /= last_din(5) and bus_size_reg>1) then
|
1012 |
|
|
crc_ok_l <= '0';
|
1013 |
|
|
end if;
|
1014 |
|
|
if (crc_out(6)(to_integer(crc_c)) /= last_din(6) and bus_size_reg>1) then
|
1015 |
|
|
crc_ok_l <= '0';
|
1016 |
|
|
end if;
|
1017 |
|
|
if (crc_out(7)(to_integer(crc_c)) /= last_din(7) and bus_size_reg>1) then
|
1018 |
|
|
crc_ok_l <= '0';
|
1019 |
|
|
end if;
|
1020 |
|
|
if (crc_c=0) then
|
1021 |
|
|
crc_rst_n <= '0';
|
1022 |
|
|
end if;
|
1023 |
|
|
end if;
|
1024 |
|
|
end if;
|
1025 |
|
|
-- state transition
|
1026 |
|
|
if (d_stop_i='1') then
|
1027 |
|
|
state <= IDLE;
|
1028 |
|
|
elsif (transf_cnt=(data_cycles+17) and blkcnt_reg>1 and crc_ok_l='1') then
|
1029 |
|
|
blkcnt_reg <= blkcnt_reg-1;
|
1030 |
|
|
state <= READ_WAIT;
|
1031 |
|
|
elsif (transf_cnt=data_cycles+17) then
|
1032 |
|
|
state <= IDLE;
|
1033 |
|
|
end if;
|
1034 |
|
|
|
1035 |
|
|
when others =>
|
1036 |
|
|
null;
|
1037 |
|
|
|
1038 |
|
|
end case;
|
1039 |
|
|
end if;
|
1040 |
|
|
end process;
|
1041 |
|
|
|
1042 |
|
|
end beh;
|
1043 |
|
|
|
1044 |
|
|
----------------------------------------------------------------------
|
1045 |
|
|
---- ----
|
1046 |
|
|
---- WISHBONE SD Card Controller IP Core ----
|
1047 |
|
|
---- ----
|
1048 |
|
|
---- sd_controller_8bit.vhd ----
|
1049 |
|
|
---- ----
|
1050 |
|
|
---- This file is part of the WISHBONE SD Card ----
|
1051 |
|
|
---- Controller IP Core project ----
|
1052 |
|
|
---- http:--opencores.org/project,sd_card_controller ----
|
1053 |
|
|
---- ----
|
1054 |
|
|
---- Description ----
|
1055 |
|
|
---- Top level entity. ----
|
1056 |
|
|
---- This core is based on the "sd card controller" project from ----
|
1057 |
|
|
---- http:--opencores.org/project,sdcard_mass_storage_controller ----
|
1058 |
|
|
---- but has been largely rewritten. A lot of effort has been ----
|
1059 |
|
|
---- made to make the core more generic and easily usable ----
|
1060 |
|
|
---- with OSs like Linux. ----
|
1061 |
|
|
---- - data transfer commands are not fixed ----
|
1062 |
|
|
---- - data transfer block size is configurable ----
|
1063 |
|
|
---- - multiple block transfer support ----
|
1064 |
|
|
---- - R2 responses (136 bit) support ----
|
1065 |
|
|
---- ----
|
1066 |
|
|
---- Author(s): ----
|
1067 |
|
|
---- - John Clayton, morianton@gmail.com ----
|
1068 |
|
|
---- ----
|
1069 |
|
|
----------------------------------------------------------------------
|
1070 |
|
|
---- ----
|
1071 |
|
|
---- Copyright (C) 2016 Authors ----
|
1072 |
|
|
---- ----
|
1073 |
|
|
---- Based on original work by ----
|
1074 |
|
|
---- Adam Edvardsson (adam.edvardsson@orsoc.se) ----
|
1075 |
|
|
---- ----
|
1076 |
|
|
---- Copyright (C) 2009 Authors ----
|
1077 |
|
|
---- ----
|
1078 |
|
|
---- This source file may be used and distributed without ----
|
1079 |
|
|
---- restriction provided that this copyright statement is not ----
|
1080 |
|
|
---- removed from the file and that any derivative work contains ----
|
1081 |
|
|
---- the original copyright notice and the associated disclaimer. ----
|
1082 |
|
|
---- ----
|
1083 |
|
|
---- This source file is free software; you can redistribute it ----
|
1084 |
|
|
---- and/or modify it under the terms of the GNU Lesser General ----
|
1085 |
|
|
---- Public License as published by the Free Software Foundation; ----
|
1086 |
|
|
---- either version 2.1 of the License, or (at your option) any ----
|
1087 |
|
|
---- later version. ----
|
1088 |
|
|
---- ----
|
1089 |
|
|
---- This source is distributed in the hope that it will be ----
|
1090 |
|
|
---- useful, but WITHOUT ANY WARRANTY; without even the implied ----
|
1091 |
|
|
---- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR ----
|
1092 |
|
|
---- PURPOSE. See the GNU Lesser General Public License for more ----
|
1093 |
|
|
---- details. ----
|
1094 |
|
|
---- ----
|
1095 |
|
|
---- You should have received a copy of the GNU Lesser General ----
|
1096 |
|
|
---- Public License along with this source; if not, download it ----
|
1097 |
|
|
---- from http:--www.opencores.org/lgpl.shtml ----
|
1098 |
|
|
---- ----
|
1099 |
|
|
----------------------------------------------------------------------
|
1100 |
|
|
|
1101 |
|
|
library IEEE;
|
1102 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
1103 |
|
|
use IEEE.NUMERIC_STD.ALL;
|
1104 |
|
|
|
1105 |
|
|
library work;
|
1106 |
|
|
use work.sd_host_pack.all;
|
1107 |
|
|
use work.dds_pack.all;
|
1108 |
|
|
use work.fifo_pack.all;
|
1109 |
|
|
use work.convert_pack.all;
|
1110 |
|
|
use work.signal_conditioning_pack.all;
|
1111 |
|
|
|
1112 |
|
|
entity sd_controller_8bit_bram is
|
1113 |
|
|
port (
|
1114 |
|
|
-- WISHBONE common
|
1115 |
|
|
wb_clk_i : in std_logic; -- system clock
|
1116 |
|
|
wb_rst_i : in std_logic; -- asynchronous
|
1117 |
|
|
-- WISHBONE slave (register interface)
|
1118 |
|
|
wb_dat_i : in unsigned(31 downto 0);
|
1119 |
|
|
wb_dat_o : out unsigned(31 downto 0);
|
1120 |
|
|
wb_adr_i : in unsigned(3 downto 0);
|
1121 |
|
|
wb_we_i : in std_logic;
|
1122 |
|
|
wb_cyc_i : in std_logic;
|
1123 |
|
|
wb_ack_o : out std_logic;
|
1124 |
|
|
-- Dedicated BRAM port without acknowledge.
|
1125 |
|
|
-- Access cycles must complete immediately.
|
1126 |
|
|
-- (data to cross clock domains by this dual-ported BRAM)
|
1127 |
|
|
bram_clk_o : out std_logic; -- Same as sd_clk_o_pad
|
1128 |
|
|
bram_dat_o : out unsigned(7 downto 0);
|
1129 |
|
|
bram_dat_i : in unsigned(7 downto 0);
|
1130 |
|
|
bram_adr_o : out unsigned(31 downto 0);
|
1131 |
|
|
bram_we_o : out std_logic;
|
1132 |
|
|
bram_cyc_o : out std_logic;
|
1133 |
|
|
--SD Card Interface
|
1134 |
|
|
sd_cmd_i : in std_logic;
|
1135 |
|
|
sd_cmd_o : out std_logic;
|
1136 |
|
|
sd_cmd_oe_o : out std_logic;
|
1137 |
|
|
sd_dat_i : in unsigned(7 downto 0);
|
1138 |
|
|
sd_dat_o : out unsigned(7 downto 0);
|
1139 |
|
|
sd_dat_oe_o : out std_logic;
|
1140 |
|
|
sd_dat_siz_o : out unsigned(1 downto 0);
|
1141 |
|
|
sd_clk_o_pad : out std_logic;
|
1142 |
|
|
-- Interrupt outputs
|
1143 |
|
|
int_cmd_o : out std_logic;
|
1144 |
|
|
int_data_o : out std_logic
|
1145 |
|
|
);
|
1146 |
|
|
end sd_controller_8bit_bram;
|
1147 |
|
|
|
1148 |
|
|
architecture beh of sd_controller_8bit_bram is
|
1149 |
|
|
|
1150 |
|
|
--SD clock
|
1151 |
|
|
signal sd_clk_l : std_logic; --sd_clk used in the system
|
1152 |
|
|
signal sd_clk_en_l : std_logic; --clock enable pulse of sd_clk
|
1153 |
|
|
signal combo_rst : std_logic; -- combines two reset signals
|
1154 |
|
|
signal combo_rst_n : std_logic;
|
1155 |
|
|
signal wb_ack_r1 : std_logic;
|
1156 |
|
|
signal wb_ack_r2 : std_logic;
|
1157 |
|
|
|
1158 |
|
|
signal cmd_start_pend : std_logic;
|
1159 |
|
|
signal cmd_start : std_logic;
|
1160 |
|
|
signal cmd_start_r1 : std_logic;
|
1161 |
|
|
signal cmd_with_data : std_logic;
|
1162 |
|
|
signal sd_data_rd : std_logic;
|
1163 |
|
|
|
1164 |
|
|
-- Related to BRAM interface port
|
1165 |
|
|
signal bram_cyc_l : std_logic;
|
1166 |
|
|
signal offset : unsigned(31 downto 0);
|
1167 |
|
|
|
1168 |
|
|
-- Related to data master
|
1169 |
|
|
signal d_write : std_logic;
|
1170 |
|
|
signal d_read : std_logic;
|
1171 |
|
|
signal bustest_r : std_logic;
|
1172 |
|
|
signal bustest_w : std_logic;
|
1173 |
|
|
signal bustest_res : unsigned(2 downto 0);
|
1174 |
|
|
signal sd_dat_busy : std_logic;
|
1175 |
|
|
signal data_busy : std_logic;
|
1176 |
|
|
signal data_crc_ok : std_logic;
|
1177 |
|
|
signal bram_rd : std_logic;
|
1178 |
|
|
signal bram_we : std_logic;
|
1179 |
|
|
|
1180 |
|
|
signal cmd_int_rst_pend : std_logic;
|
1181 |
|
|
signal cmd_int_rst : std_logic;
|
1182 |
|
|
signal cmd_int_rst_edge : std_logic;
|
1183 |
|
|
signal data_int_rst_pend : std_logic;
|
1184 |
|
|
signal data_int_rst : std_logic;
|
1185 |
|
|
signal data_int_rst_edge : std_logic;
|
1186 |
|
|
|
1187 |
|
|
--SD Data Master State Machine
|
1188 |
|
|
type DM_STATE_TYPE is (IDLE, WAIT_FOR_CMD_INT, DATA_TX_START, DATA_TX,
|
1189 |
|
|
DATA_RX, BUSTEST_ACTIVE, BUSTEST_W_START);
|
1190 |
|
|
signal dm_state : DM_STATE_TYPE;
|
1191 |
|
|
|
1192 |
|
|
-- registers
|
1193 |
|
|
signal argument_reg : unsigned(31 downto 0);
|
1194 |
|
|
signal cmd_index_reg : unsigned(5 downto 0);
|
1195 |
|
|
signal blk_size_reg : unsigned(BLKSIZE_W-1 downto 0);
|
1196 |
|
|
signal blk_count_reg : unsigned(BLKCNT_W-1 downto 0);
|
1197 |
|
|
signal resp_0_reg : unsigned(31 downto 0);
|
1198 |
|
|
signal resp_1_reg : unsigned(31 downto 0);
|
1199 |
|
|
signal resp_2_reg : unsigned(31 downto 0);
|
1200 |
|
|
signal resp_3_reg : unsigned(31 downto 0);
|
1201 |
|
|
-- Fields of the settings register
|
1202 |
|
|
signal bus_siz_reg : unsigned(1 downto 0);
|
1203 |
|
|
signal sw_rst_reg : std_logic;
|
1204 |
|
|
signal d_stop : std_logic;
|
1205 |
|
|
signal sd_freq_reg : unsigned(31 downto 0);
|
1206 |
|
|
signal timeout_reg : unsigned(15 downto 0);
|
1207 |
|
|
-- Registers 0x9..0xC are interrupt related
|
1208 |
|
|
signal cmd_int_status_reg : unsigned(4 downto 0);
|
1209 |
|
|
signal cmd_int_enable_reg : unsigned(4 downto 0);
|
1210 |
|
|
signal data_int_status_reg : unsigned(2 downto 0);
|
1211 |
|
|
signal data_int_enable_reg : unsigned(2 downto 0);
|
1212 |
|
|
-- Register holding current bus address
|
1213 |
|
|
signal dma_adr_reg : unsigned(31 downto 0);
|
1214 |
|
|
|
1215 |
|
|
begin
|
1216 |
|
|
|
1217 |
|
|
-- Register read mux
|
1218 |
|
|
with (to_integer(wb_adr_i)) select
|
1219 |
|
|
wb_dat_o <=
|
1220 |
|
|
u_resize(blk_size_reg,32) when 0,
|
1221 |
|
|
u_resize(blk_count_reg,32) when 1,
|
1222 |
|
|
u_resize(cmd_index_reg,32) when 2,
|
1223 |
|
|
argument_reg when 3,
|
1224 |
|
|
resp_0_reg when 4,
|
1225 |
|
|
resp_1_reg when 5,
|
1226 |
|
|
resp_2_reg when 6,
|
1227 |
|
|
resp_3_reg when 7,
|
1228 |
|
|
timeout_reg & "00000" & bustest_res & "000" & sw_rst_reg & "00" & bus_siz_reg when 8,
|
1229 |
|
|
sd_freq_reg when 9,
|
1230 |
|
|
u_resize(cmd_int_status_reg,32) when 10,
|
1231 |
|
|
u_resize(cmd_int_enable_reg,32) when 11,
|
1232 |
|
|
u_resize(data_int_status_reg,32) when 12,
|
1233 |
|
|
u_resize(data_int_enable_reg,32) when 13,
|
1234 |
|
|
dma_adr_reg when 14,
|
1235 |
|
|
str2u("55555555",32) when others;
|
1236 |
|
|
|
1237 |
|
|
-- Register Writing Process
|
1238 |
|
|
reg_wr_proc : process(wb_rst_i, wb_clk_i)
|
1239 |
|
|
begin
|
1240 |
|
|
if (wb_rst_i='1') then
|
1241 |
|
|
argument_reg <= (others=>'0');
|
1242 |
|
|
cmd_index_reg <= (others=>'0');
|
1243 |
|
|
blk_size_reg <= to_unsigned(512,blk_size_reg'length);
|
1244 |
|
|
blk_count_reg <= to_unsigned(1,blk_count_reg'length);
|
1245 |
|
|
bus_siz_reg <= "00";
|
1246 |
|
|
sw_rst_reg <= '0';
|
1247 |
|
|
sd_freq_reg <= str2u("010624DD",sd_freq_reg'length); -- Set for 400 kHz default (100 MHz sys_clk)
|
1248 |
|
|
timeout_reg <= to_unsigned(1000,timeout_reg'length);
|
1249 |
|
|
cmd_int_rst_pend <= '0';
|
1250 |
|
|
cmd_int_rst <= '0';
|
1251 |
|
|
d_stop <= '0';
|
1252 |
|
|
cmd_int_enable_reg <= (others=>'0');
|
1253 |
|
|
data_int_rst_pend <= '0';
|
1254 |
|
|
data_int_rst <= '0';
|
1255 |
|
|
data_int_enable_reg <= (others=>'0');
|
1256 |
|
|
wb_ack_r1 <= '0';
|
1257 |
|
|
wb_ack_r2 <= '0';
|
1258 |
|
|
cmd_start_pend <= '0';
|
1259 |
|
|
cmd_start <= '0';
|
1260 |
|
|
cmd_start_r1 <= '0';
|
1261 |
|
|
dma_adr_reg <= (others=>'0');
|
1262 |
|
|
elsif (wb_clk_i'event and wb_clk_i='1') then
|
1263 |
|
|
-- default values, clocked on sd_clk_en_l
|
1264 |
|
|
if (sd_clk_en_l='1') then
|
1265 |
|
|
cmd_start <= '0';
|
1266 |
|
|
data_int_rst <= '0';
|
1267 |
|
|
cmd_int_rst <= '0';
|
1268 |
|
|
d_stop <= '0';
|
1269 |
|
|
end if;
|
1270 |
|
|
-- signals raised on sd_clk_en_l
|
1271 |
|
|
-- higher priority than the default
|
1272 |
|
|
if (sd_clk_en_l='1') then
|
1273 |
|
|
if (cmd_start_pend='1') then
|
1274 |
|
|
cmd_start <= '1';
|
1275 |
|
|
cmd_start_pend <= '0';
|
1276 |
|
|
end if;
|
1277 |
|
|
if (data_int_rst_pend='1') then
|
1278 |
|
|
data_int_rst <= '1';
|
1279 |
|
|
data_int_rst_pend <= '0';
|
1280 |
|
|
end if;
|
1281 |
|
|
if (cmd_int_rst_pend='1') then
|
1282 |
|
|
cmd_int_rst <= '1';
|
1283 |
|
|
cmd_int_rst_pend <= '0';
|
1284 |
|
|
end if;
|
1285 |
|
|
end if;
|
1286 |
|
|
-- delayed version
|
1287 |
|
|
cmd_start_r1 <= cmd_start;
|
1288 |
|
|
|
1289 |
|
|
|
1290 |
|
|
if (wb_ack_r1='1' and wb_ack_r2='0') then
|
1291 |
|
|
if (wb_we_i='1') then
|
1292 |
|
|
case (to_integer(wb_adr_i)) is
|
1293 |
|
|
when 0 =>
|
1294 |
|
|
blk_size_reg <= wb_dat_i(BLKSIZE_W-1 downto 0);
|
1295 |
|
|
|
1296 |
|
|
when 1 =>
|
1297 |
|
|
blk_count_reg <= wb_dat_i(BLKCNT_W-1 downto 0);
|
1298 |
|
|
|
1299 |
|
|
when 2 =>
|
1300 |
|
|
cmd_index_reg <= wb_dat_i(5 downto 0);
|
1301 |
|
|
|
1302 |
|
|
when 3 =>
|
1303 |
|
|
argument_reg <= wb_dat_i;
|
1304 |
|
|
cmd_start_pend <= '1';
|
1305 |
|
|
|
1306 |
|
|
when 8 =>
|
1307 |
|
|
bus_siz_reg <= wb_dat_i(1 downto 0);
|
1308 |
|
|
sw_rst_reg <= wb_dat_i(4);
|
1309 |
|
|
d_stop <= wb_dat_i(5); -- write only bit
|
1310 |
|
|
timeout_reg <= wb_dat_i(31 downto 16);
|
1311 |
|
|
|
1312 |
|
|
when 9 =>
|
1313 |
|
|
sd_freq_reg <= wb_dat_i;
|
1314 |
|
|
|
1315 |
|
|
when 10 =>
|
1316 |
|
|
cmd_int_rst_pend <= '1';
|
1317 |
|
|
|
1318 |
|
|
when 11 =>
|
1319 |
|
|
cmd_int_enable_reg <= wb_dat_i(cmd_int_enable_reg'length-1 downto 0);
|
1320 |
|
|
|
1321 |
|
|
when 12 =>
|
1322 |
|
|
data_int_rst_pend <= '1';
|
1323 |
|
|
|
1324 |
|
|
when 13 =>
|
1325 |
|
|
data_int_enable_reg <= wb_dat_i(data_int_enable_reg'length-1 downto 0);
|
1326 |
|
|
|
1327 |
|
|
when 14 =>
|
1328 |
|
|
dma_adr_reg <= wb_dat_i;
|
1329 |
|
|
|
1330 |
|
|
when others =>
|
1331 |
|
|
null;
|
1332 |
|
|
end case;
|
1333 |
|
|
end if;
|
1334 |
|
|
end if;
|
1335 |
|
|
-- Detect rising edge of wb_cyc_i
|
1336 |
|
|
wb_ack_r1 <= wb_cyc_i;
|
1337 |
|
|
wb_ack_r2 <= wb_ack_r1;
|
1338 |
|
|
end if;
|
1339 |
|
|
end process;
|
1340 |
|
|
|
1341 |
|
|
-- Provide Wishbone bus cycle acknowledge
|
1342 |
|
|
wb_ack_o <= wb_ack_r1;
|
1343 |
|
|
|
1344 |
|
|
-- Form a combined reset signal
|
1345 |
|
|
combo_rst <= wb_rst_i or sw_rst_reg;
|
1346 |
|
|
combo_rst_n <= not combo_rst;
|
1347 |
|
|
|
1348 |
|
|
-- SD Card clock generation
|
1349 |
|
|
-- A DDS is used.
|
1350 |
|
|
sd_clk_dds : dds_squarewave
|
1351 |
|
|
generic map(
|
1352 |
|
|
ACC_BITS => sd_freq_reg'length -- Bit width of DDS phase accumulator
|
1353 |
|
|
)
|
1354 |
|
|
port map(
|
1355 |
|
|
|
1356 |
|
|
sys_rst_n => combo_rst_n,
|
1357 |
|
|
sys_clk => wb_clk_i,
|
1358 |
|
|
sys_clk_en => '1',
|
1359 |
|
|
|
1360 |
|
|
-- Frequency setting
|
1361 |
|
|
freq_i => sd_freq_reg,
|
1362 |
|
|
|
1363 |
|
|
-- Output
|
1364 |
|
|
pulse_o => sd_clk_en_l,
|
1365 |
|
|
squarewave_o => sd_clk_l
|
1366 |
|
|
);
|
1367 |
|
|
sd_clk_o_pad <= sd_clk_l;
|
1368 |
|
|
|
1369 |
|
|
|
1370 |
|
|
cmd_host_0 : sd_cmd_host
|
1371 |
|
|
port map(
|
1372 |
|
|
sd_clk => sd_clk_l,
|
1373 |
|
|
sys_rst => combo_rst,
|
1374 |
|
|
-- Control and settings
|
1375 |
|
|
start_i => cmd_start_r1,
|
1376 |
|
|
int_rst_i => cmd_int_rst_edge,
|
1377 |
|
|
busy_i => sd_dat_busy,
|
1378 |
|
|
cmd_index_i => cmd_index_reg,
|
1379 |
|
|
argument_i => argument_reg,
|
1380 |
|
|
timeout_i => timeout_reg,
|
1381 |
|
|
int_status_o => cmd_int_status_reg,
|
1382 |
|
|
response_0_o => resp_0_reg,
|
1383 |
|
|
response_1_o => resp_1_reg,
|
1384 |
|
|
response_2_o => resp_2_reg,
|
1385 |
|
|
response_3_o => resp_3_reg,
|
1386 |
|
|
-- SD/MMC card command signals
|
1387 |
|
|
sd_cmd_i => sd_cmd_i,
|
1388 |
|
|
sd_cmd_o => sd_cmd_o,
|
1389 |
|
|
sd_cmd_oe_o => sd_cmd_oe_o
|
1390 |
|
|
);
|
1391 |
|
|
|
1392 |
|
|
cmd_with_data <= '1' when cmd_index_reg=20 or cmd_index_reg=24 or cmd_index_reg=25 or
|
1393 |
|
|
cmd_index_reg=8 or cmd_index_reg=11 or cmd_index_reg=17 or
|
1394 |
|
|
cmd_index_reg=18 or cmd_index_reg=14 or cmd_index_reg=19 else '0';
|
1395 |
|
|
sd_data_rd <= '1' when cmd_index_reg=8 or cmd_index_reg=11 or
|
1396 |
|
|
cmd_index_reg=17 or cmd_index_reg=18 else '0';
|
1397 |
|
|
|
1398 |
|
|
-- SD Data Master Finite State Machine
|
1399 |
|
|
d_read <= '1' when dm_state=IDLE and cmd_start_r1='1' and cmd_with_data='1' and sd_data_rd='1' else '0';
|
1400 |
|
|
d_write <= '1' when dm_state=DATA_TX_START else '0';
|
1401 |
|
|
bustest_r <= '1' when dm_state=IDLE and cmd_start_r1='1' and cmd_index_reg=14 else '0';
|
1402 |
|
|
bustest_w <= '1' when dm_state=BUSTEST_W_START else '0';
|
1403 |
|
|
|
1404 |
|
|
dm_fsm_proc : process(combo_rst, sd_clk_l)
|
1405 |
|
|
begin
|
1406 |
|
|
if (combo_rst='1') then
|
1407 |
|
|
offset <= (others=>'0');
|
1408 |
|
|
data_int_status_reg <= (others=>'0');
|
1409 |
|
|
dm_state <= IDLE;
|
1410 |
|
|
elsif (sd_clk_l'event and sd_clk_l='1') then
|
1411 |
|
|
case (dm_state) is
|
1412 |
|
|
when IDLE =>
|
1413 |
|
|
offset <= (others=>'0');
|
1414 |
|
|
-- state transition
|
1415 |
|
|
if (cmd_start_r1='1') then
|
1416 |
|
|
if (cmd_with_data='1') then
|
1417 |
|
|
if (cmd_index_reg=14) then -- BUSTEST_R
|
1418 |
|
|
dm_state <= BUSTEST_ACTIVE;
|
1419 |
|
|
elsif (sd_data_rd='1') then -- Reading
|
1420 |
|
|
dm_state <= DATA_RX;
|
1421 |
|
|
else
|
1422 |
|
|
dm_state <= WAIT_FOR_CMD_INT;
|
1423 |
|
|
end if;
|
1424 |
|
|
end if;
|
1425 |
|
|
end if;
|
1426 |
|
|
|
1427 |
|
|
when BUSTEST_ACTIVE =>
|
1428 |
|
|
if (data_busy='0') then --Transfer complete
|
1429 |
|
|
dm_state <= IDLE;
|
1430 |
|
|
end if;
|
1431 |
|
|
|
1432 |
|
|
when BUSTEST_W_START =>
|
1433 |
|
|
dm_state <= BUSTEST_ACTIVE;
|
1434 |
|
|
|
1435 |
|
|
when WAIT_FOR_CMD_INT =>
|
1436 |
|
|
if (cmd_int_status_reg(INT_CMD_CC)='1') then
|
1437 |
|
|
if (cmd_index_reg=19) then
|
1438 |
|
|
dm_state <= BUSTEST_W_START;
|
1439 |
|
|
else
|
1440 |
|
|
dm_state <= DATA_TX_START;
|
1441 |
|
|
end if;
|
1442 |
|
|
elsif (cmd_int_status_reg(INT_CMD_EI)='1') then
|
1443 |
|
|
dm_state <= IDLE;
|
1444 |
|
|
end if;
|
1445 |
|
|
|
1446 |
|
|
when DATA_TX_START =>
|
1447 |
|
|
dm_state <= DATA_TX;
|
1448 |
|
|
|
1449 |
|
|
when DATA_TX =>
|
1450 |
|
|
if (bram_cyc_l='1') then
|
1451 |
|
|
offset <= offset+1;
|
1452 |
|
|
end if;
|
1453 |
|
|
if (data_busy='0') then --Transfer complete
|
1454 |
|
|
if (data_crc_ok='0') then --Wrong CRC
|
1455 |
|
|
data_int_status_reg(INT_DATA_CCRCE) <= '1';
|
1456 |
|
|
else
|
1457 |
|
|
data_int_status_reg(INT_DATA_CC) <= '1';
|
1458 |
|
|
end if;
|
1459 |
|
|
dm_state <= IDLE;
|
1460 |
|
|
end if;
|
1461 |
|
|
|
1462 |
|
|
when DATA_RX =>
|
1463 |
|
|
if (bram_cyc_l='1') then
|
1464 |
|
|
offset <= offset+1;
|
1465 |
|
|
end if;
|
1466 |
|
|
if (data_busy='0') then --Transfer complete
|
1467 |
|
|
if (data_crc_ok='0') then --Wrong CRC
|
1468 |
|
|
data_int_status_reg(INT_DATA_CCRCE) <= '1';
|
1469 |
|
|
else
|
1470 |
|
|
data_int_status_reg(INT_DATA_CC) <= '1';
|
1471 |
|
|
end if;
|
1472 |
|
|
dm_state <= IDLE;
|
1473 |
|
|
end if;
|
1474 |
|
|
|
1475 |
|
|
when others =>
|
1476 |
|
|
null;
|
1477 |
|
|
|
1478 |
|
|
end case;
|
1479 |
|
|
|
1480 |
|
|
if (data_int_rst_edge='1') then
|
1481 |
|
|
data_int_status_reg <= (others=>'0');
|
1482 |
|
|
end if;
|
1483 |
|
|
end if;
|
1484 |
|
|
end process;
|
1485 |
|
|
|
1486 |
|
|
|
1487 |
|
|
sd_data_host0 : sd_data_8bit_host
|
1488 |
|
|
port map(
|
1489 |
|
|
sd_clk => sd_clk_l,
|
1490 |
|
|
sys_rst => combo_rst,
|
1491 |
|
|
--Tx Fifo
|
1492 |
|
|
tx_dat_i => bram_dat_i,
|
1493 |
|
|
tx_dat_rd_o => bram_rd,
|
1494 |
|
|
--Rx Fifo
|
1495 |
|
|
rx_dat_o => bram_dat_o,
|
1496 |
|
|
rx_dat_we_o => bram_we,
|
1497 |
|
|
--tristate data
|
1498 |
|
|
sd_dat_siz_o => sd_dat_siz_o,
|
1499 |
|
|
sd_dat_oe_o => sd_dat_oe_o,
|
1500 |
|
|
sd_dat_o => sd_dat_o,
|
1501 |
|
|
sd_dat_i => sd_dat_i,
|
1502 |
|
|
--Control signals
|
1503 |
|
|
blksize_i => blk_size_reg,
|
1504 |
|
|
bus_size_i => bus_siz_reg,
|
1505 |
|
|
blkcnt_i => blk_count_reg,
|
1506 |
|
|
d_stop_i => d_stop,
|
1507 |
|
|
d_read_i => d_read,
|
1508 |
|
|
d_write_i => d_write,
|
1509 |
|
|
bustest_w_i => bustest_w,
|
1510 |
|
|
bustest_r_i => bustest_r,
|
1511 |
|
|
bustest_res_o => bustest_res,
|
1512 |
|
|
sd_dat_busy_o => sd_dat_busy,
|
1513 |
|
|
fsm_busy_o => data_busy,
|
1514 |
|
|
crc_ok_o => data_crc_ok
|
1515 |
|
|
);
|
1516 |
|
|
|
1517 |
|
|
bram_clk_o <= not sd_clk_l;
|
1518 |
|
|
bram_we_o <= bram_we;
|
1519 |
|
|
bram_cyc_l <= bram_rd or bram_we;
|
1520 |
|
|
bram_cyc_o <= bram_cyc_l;
|
1521 |
|
|
bram_adr_o <= dma_adr_reg+offset;
|
1522 |
|
|
|
1523 |
|
|
cmd_int_rst_edge_detect : edge_detector
|
1524 |
|
|
generic map(
|
1525 |
|
|
DETECT_RISING => 1,
|
1526 |
|
|
DETECT_FALLING => 0
|
1527 |
|
|
)
|
1528 |
|
|
port map(
|
1529 |
|
|
-- System Clock and Clock Enable
|
1530 |
|
|
sys_rst_n => combo_rst_n,
|
1531 |
|
|
sys_clk => sd_clk_l,
|
1532 |
|
|
sys_clk_en => '1',
|
1533 |
|
|
|
1534 |
|
|
-- Input Signal
|
1535 |
|
|
sig_i => cmd_int_rst,
|
1536 |
|
|
|
1537 |
|
|
-- Output pulse
|
1538 |
|
|
pulse_o => cmd_int_rst_edge
|
1539 |
|
|
);
|
1540 |
|
|
|
1541 |
|
|
data_int_rst_edge_detect : edge_detector
|
1542 |
|
|
generic map(
|
1543 |
|
|
DETECT_RISING => 1,
|
1544 |
|
|
DETECT_FALLING => 0
|
1545 |
|
|
)
|
1546 |
|
|
port map(
|
1547 |
|
|
-- System Clock and Clock Enable
|
1548 |
|
|
sys_rst_n => combo_rst_n,
|
1549 |
|
|
sys_clk => sd_clk_l,
|
1550 |
|
|
sys_clk_en => '1',
|
1551 |
|
|
|
1552 |
|
|
-- Input Signal
|
1553 |
|
|
sig_i => data_int_rst,
|
1554 |
|
|
|
1555 |
|
|
-- Output pulse
|
1556 |
|
|
pulse_o => data_int_rst_edge
|
1557 |
|
|
);
|
1558 |
|
|
|
1559 |
|
|
-- provide outputs
|
1560 |
|
|
int_cmd_o <= '1' when (cmd_int_status_reg or cmd_int_enable_reg)/=0 else '0';
|
1561 |
|
|
int_data_o <= '1' when (data_int_status_reg or data_int_enable_reg)/=0 else '0';
|
1562 |
|
|
|
1563 |
|
|
end beh;
|
1564 |
|
|
|