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

Subversion Repositories pdp8

[/] [pdp8/] [trunk/] [pdp8/] [rk8e/] [sdspi.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 trurl
--------------------------------------------------------------------
2
--!
3
--! PDP-8 Processor
4
--!
5
--! \brief
6
--!      RK8E Secure Digital SPI Interface
7
--!
8
--! \details
9
--!      This interface communicates with the Secure Digital Chip
10
--!      at the physical layer.
11
--!
12
--! \file
13
--!      sdspi.vhd
14
--!
15
--! \author
16
--!      Rob Doyle - doyle (at) cox (dot) net
17
--!
18
--------------------------------------------------------------------
19
--
20
--  Copyright (C) 2012 Rob Doyle
21
--
22
-- This source file may be used and distributed without
23
-- restriction provided that this copyright statement is not
24
-- removed from the file and that any derivative work contains
25
-- the original copyright notice and the associated disclaimer.
26
--
27
-- This source file is free software; you can redistribute it
28
-- and/or modify it under the terms of the GNU Lesser General
29
-- Public License as published by the Free Software Foundation;
30
-- version 2.1 of the License.
31
--
32
-- This source is distributed in the hope that it will be
33
-- useful, but WITHOUT ANY WARRANTY; without even the implied
34
-- warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
35
-- PURPOSE. See the GNU Lesser General Public License for more
36
-- details.
37
--
38
-- You should have received a copy of the GNU Lesser General
39
-- Public License along with this source; if not, download it
40
-- from http://www.gnu.org/licenses/lgpl.txt
41
--
42
--------------------------------------------------------------------
43
--
44
-- Comments are formatted for doxygen
45
--
46
 
47
library ieee;                                   --! IEEE Library
48
use ieee.std_logic_1164.all;                    --! IEEE 1164
49
use ieee.numeric_std.all;                       --! IEEE Numeric Std
50
use work.cpu_types.all;                         --! CPU Types
51
use work.sd_types.all;                          --! SD Types
52
use work.sdspi_types.all;                       --! SD SPI Types
53
 
54
--
55
--! RK8E Secure Digital SPI Interface Entity
56
--
57
 
58
entity eSDSPI is port (
59
    sys     : in  sys_t;                        --! Clock/Reset
60
    spiOP   : in  spiOP_t;                      --! Operation
61
    spiTXD  : in  sdBYTE_t;                     --! Transmit Data
62
    spiRXD  : out sdBYTE_t;                     --! Receive Data
63
    spiMISO : in  std_logic;                    --! Data In
64
    spiMOSI : out std_logic;                    --! Data Out
65
    spiSCLK : out std_logic;                    --! Clock
66
    spiCS   : out std_logic;                    --! Chip Select
67
    spiDONE : out std_logic                     --! Done
68
);
69
end eSDSPI;
70
 
71
--
72
--! RK8E Secure Digital SPI Interface RTL
73
--
74
 
75
architecture rtl of eSDSPI is
76
    type     state_t is (stateRESET,
77
                         stateIDLE,
78
                         stateTXH,
79
                         stateTXL,
80
                         stateTXM,
81
                         stateTXN);
82
    signal   state   : state_t;
83
    signal   bitcnt  : integer range 0 to  7;
84
    signal   txd     : sdBYTE_t;
85
    signal   rxd     : sdBYTE_t;
86
    signal   clkcnt  : integer range 0 to 63;
87
    signal   clkdiv  : integer range 0 to 63;
88
    constant slowDiv : integer := 63;
89
    constant fastDiv : integer :=  1;
90
 
91
begin
92
 
93
    SPI_STATE : process(sys)
94
    begin
95
        if sys.rst = '1' then
96
            spiDONE <= '0';
97
            txd     <= (others => '1');
98
            rxd     <= (others => '1');
99
            spiCS   <= '1';
100
            bitcnt  <= 0;
101
            clkcnt  <= 0;
102
            clkdiv  <= slowDiv;
103
            state   <= stateRESET;
104
        elsif rising_edge(sys.clk) then
105
            case state is
106
                when stateRESET =>
107
                    clkdiv  <= slowDiv;
108
                    state   <= stateIDLE;
109
                when stateIDLE =>
110
                    spiDONE <= '0';
111
                    case spiOP is
112
                        when spiNOP =>
113
                            null;
114
                        when spiCSL =>
115
                            spiCS <= '0';
116
                        when spiCSH =>
117
                            spiCS <= '1';
118
                        when spiFAST =>
119
                            clkDiv <= fastDiv;
120
                        when spiSLOW =>
121
                            clkDiv <= slowDiv;
122
                        when spiTR =>
123
                            clkcnt <= clkdiv;
124
                            bitcnt <= 7;
125
                            txd    <= spiTXD;
126
                            state  <= stateTXL;
127
                        when others =>
128
                            null;
129
                    end case;
130
                when stateTXL =>
131
                    if clkcnt = 0 then
132
                        clkcnt <= clkdiv;
133
                        rxd    <= rxd(1 to 7) & spiMISO;
134
                        state  <= stateTXH;
135
                    else
136
                        clkcnt <= clkcnt - 1;
137
                    end if;
138
                when stateTXH =>
139
                    if clkcnt = 0 then
140
                        if bitcnt = 0 then
141
                            clkcnt <= clkdiv;
142
                            state  <= stateTXM;
143
                        else
144
                            clkcnt <= clkdiv;
145
                            txd    <= txd(1 to 7) & '1';
146
                            bitcnt <= bitcnt - 1;
147
                            state  <= stateTXL;
148
                        end if;
149
                    else
150
                        clkcnt <= clkcnt - 1;
151
                    end if;
152
                when stateTXM =>
153
                    if clkcnt = 0 then
154
                        clkcnt <= clkdiv;
155
                        state  <= stateTXN;
156
                    else
157
                        clkcnt <= clkcnt - 1;
158
                    end if;
159
                when stateTXN =>
160
                    if clkcnt = 0 then
161
                        clkcnt  <= clkdiv;
162
                        spiDONE <= '1';
163
                        state   <= stateIDLE;
164
                    else
165
                        clkcnt <= clkcnt - 1;
166
                    end if;
167
                 when others =>
168
                    null;
169
            end case;
170
        end if;
171
    end process SPI_STATE;
172
 
173
    with state select
174
        spiSCLK <= '1'    when stateIDLE,
175
                   '0'    when stateTXL,
176
                   '1'    when stateTXH,
177
                   '1'    when stateTXN,
178
                   '1'    when others;
179
 
180
    spiMOSI <= txd(0);
181
    spiRXD  <= rxd;
182
 
183
end rtl;

powered by: WebSVN 2.1.0

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