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

Subversion Repositories utosnet

[/] [utosnet/] [trunk/] [gateware/] [uTosNet_spi/] [uTosNet_spi.vhd] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 sonicwave
----------------------------------------------------------------------------------
2
-- Company:             University of Southern Denmark
3
-- Engineer:            Simon Falsig
4
-- 
5
-- Create Date:         19/3/2010 
6
-- Design Name:         uTosNet
7
-- Module Name:         uTosNet_spi - Behavioral 
8
-- Project Name:        uTosNet
9
-- Target Devices:      SDU XC3S50AN Board
10
-- Tool versions:       Xilinx ISE 11.4
11
-- Description:         PseudoTosNet is designed to provide an interface similar to 
12
--                                      the full-blown TosNet core, but usable on the SDU XC3S50AN 
13
--                                      Board. It features a SPI module which is made for use in 
14
--                                      conjunction with a Digi Connect ME 9210 with the Generic 
15
--                                      TosNet Masternode application. By using this combination, it 
16
--                                      is possible to access the blockram from any Ethernet-enabled 
17
--                                      device.
18
--
19
-- Revision: 
20
-- Revision 0.10 -      Initial release
21
--
22
----------------------------------------------------------------------------------
23
library IEEE;
24
use IEEE.STD_LOGIC_1164.ALL;
25
use IEEE.STD_LOGIC_ARITH.ALL;
26
use IEEE.STD_LOGIC_UNSIGNED.ALL;
27
 
28
---- Uncomment the following library declaration if instantiating
29
---- any Xilinx primitives in this code.
30
--library UNISIM;
31
--use UNISIM.VComponents.all;
32
 
33
entity uTosNet_spi is
34
Port (  clk_50M                                         : in    STD_LOGIC;
35
                spi_miso                                        : out   STD_LOGIC;
36
                spi_mosi                                        : in    STD_LOGIC;
37
                spi_clk                                         : in    STD_LOGIC;
38
                spi_en                                          : in    STD_LOGIC;
39
                dataReg_addr                            : in    STD_LOGIC_VECTOR(5 downto 0);
40
                dataReg_dataIn                          : in    STD_LOGIC_VECTOR(31 downto 0);
41
                dataReg_dataOut                         : out   STD_LOGIC_VECTOR(31 downto 0);
42
                dataReg_clk                                     : in    STD_LOGIC;
43
                dataReg_writeEnable                     : in    STD_LOGIC);
44
end uTosNet_spi;
45
 
46
architecture Behavioral of uTosNet_spi is
47
 
48
        component dataRegister
49
        Port (  clka                                    : in    STD_LOGIC;
50
                        wea                                             : in    STD_LOGIC_VECTOR(0 downto 0);
51
                        addra                                   : in    STD_LOGIC_VECTOR(5 downto 0);
52
                        dina                                    : in    STD_LOGIC_VECTOR(31 downto 0);
53
                        douta                                   : out   STD_LOGIC_VECTOR(31 downto 0);
54
                        clkb                                    : in    STD_LOGIC;
55
                        web                                             : in    STD_LOGIC_VECTOR(0 downto 0);
56
                        addrb                                   : in    STD_LOGIC_VECTOR(5 downto 0);
57
                        dinb                                    : in    STD_LOGIC_VECTOR(31 downto 0);
58
                        doutb                                   : out   STD_LOGIC_VECTOR(31 downto 0));
59
        end component;
60
 
61
        signal int_dataReg_dataIn               : STD_LOGIC_VECTOR(31 downto 0);
62
        signal int_dataReg_addr                 : STD_LOGIC_VECTOR(5 downto 0);
63
        signal int_dataReg_dataOut              : STD_LOGIC_VECTOR(31 downto 0);
64
        signal int_dataReg_we                   : STD_LOGIC_VECTOR(0 downto 0);
65
        signal int_dataReg_clk                  : STD_LOGIC;
66
 
67
        signal dataReg_writeEnable_V    : STD_LOGIC_VECTOR(0 downto 0);
68
 
69
        signal readData                                 : STD_LOGIC_VECTOR(31 downto 0) := (others => '0');
70
        signal writeAddress                             : STD_LOGIC_VECTOR(5 downto 0) := (others => '0');
71
        signal readAddress                              : STD_LOGIC_VECTOR(5 downto 0) := (others => '0');
72
        signal doWrite                                  : STD_LOGIC := '0';
73
        signal doRead                                   : STD_LOGIC := '0';
74
 
75
        signal int_spi_mosi                             : STD_LOGIC;
76
        signal int_spi_clk                              : STD_LOGIC;
77
        signal int_spi_en                               : STD_LOGIC;
78
        signal last_spi_clk                             : STD_LOGIC;
79
 
80
        signal dataInBuffer                             : STD_LOGIC_VECTOR(31 downto 0) := (others => '0');
81
        signal dataOutBuffer                    : STD_LOGIC_VECTOR(31 downto 0) := (others => '1');
82
 
83
        signal bitCounter                               : STD_LOGIC_VECTOR(6 downto 0) := (others => '0');
84
 
85
begin
86
 
87
        dataReg_writeEnable_V(0) <= dataReg_writeEnable;                                                 --Conversion from std_logic to std_logic_vector(0 downto 0) - to allow for dataReg_writeEnable to be a std_logic, which is nicer...:)
88
 
89
        dataRegisterInst : dataRegister                                                                                         --Instantation of the dual-port blockram used for the dataregister
90
        Port map (      clka => dataReg_clk,                                                                                    --PortA is used for the user application
91
                                wea => dataReg_writeEnable_V,                                                                   --
92
                                addra => dataReg_addr,                                                                                  --
93
                                dina => dataReg_dataIn,                                                                                 --
94
                                douta => dataReg_dataOut,                                                                               --
95
                                clkb => int_dataReg_clk,                                                                                --PortB is used for the SPI interface
96
                                web => int_dataReg_we,                                                                                  --
97
                                addrb => int_dataReg_addr,                                                                              --
98
                                dinb => int_dataReg_dataIn,                                                                             --
99
                                doutb => int_dataReg_dataOut);                                                                  --
100
 
101
        --Synchronize inputs
102
        process(clk_50M)
103
        begin
104
                if(clk_50M = '0' and clk_50M'event) then
105
                        int_spi_mosi <= spi_mosi;
106
                        int_spi_clk <= spi_clk;
107
                        int_spi_en <= spi_en;
108
                end if;
109
        end process;
110
 
111
        --SPI Process
112
        process(clk_50M)
113
        begin
114
                if(clk_50M = '1' and clk_50M'event) then
115
                        last_spi_clk <= int_spi_clk;                                                                            --Save current value to use for manual edge triggering
116
 
117
                        if(int_spi_en = '1') then                                                                                       --SPI is not enabled (spi_en is active low)
118
                                bitCounter <= (others => '0');                                                                   --Reset the bitcounter
119
 
120
                                if((doWrite = '1') and (int_dataReg_we = "0")) then                              --If a write was requested in the previously received command,
121
                                        int_dataReg_addr <= writeAddress;                                                       -- then prepare it,
122
                                        int_dataReg_dataIn <= dataInBuffer;                                                     -- the data to write are those left in the input buffer,
123
                                        int_dataReg_we <= "1";                                                                          --
124
                                        int_dataReg_clk <= '0';                                                                          --
125
                                elsif((doWrite = '1') and (int_dataReg_clk = '0')) then                  --
126
                                        int_dataReg_clk <= '1';                                                                         -- and perform it by pulling the dataReg clock high
127
                                        doWrite <= '0';                                                                                          --Write is done
128
                                else                                                                                                                    --If there aren't any writes to perform,
129
                                        int_dataReg_clk <= '0';                                                                          -- just clear the various signals
130
                                        int_dataReg_we <= "0";                                                                           --
131
                                        doRead <= '0';                                                                                           --
132
                                        doWrite <= '0';                                                                                          --
133
                                end if;
134
                        else                                                                                                                            --SPI is enabled
135
                                if(int_spi_clk = '0' and last_spi_clk = '1') then                                --Falling edge on spi_clk
136
                                        dataInBuffer <= dataInBuffer(30 downto 0) & int_spi_mosi;        --Read next received bit into the input buffer,
137
                                        bitCounter <= bitCounter + 1;                                                           -- and increment the bitcounter
138
                                elsif(int_spi_clk = '1' and last_spi_clk = '0') then                     --Rising edge on spi_clk
139
                                        spi_miso <= dataOutBuffer(31);                                                          --Write out the next bit from the output buffer,
140
                                        dataOutBuffer <= dataOutBuffer(30 downto 0) & '0';                        -- and left-shift the buffer
141
                                end if;
142
 
143
                                case bitCounter is                                                                                              --Parse the command
144
                                        when "0000101" =>                                                                                       --Bit 27 (the 5th read bit),
145
                                                doRead <= dataInBuffer(0);                                                               -- contains the 'doRead' flag
146
                                        when "0010000" =>                                                                                       --Bits 16-25 (available when 16 bits have been read),
147
                                                readAddress <= dataInBuffer(5 downto 0);                         -- contain the address to read from
148
                                        when "0010001" =>                                                                                       --Bit 15 (the 17th read bit),
149
                                                int_dataReg_addr <= readAddress;                                                -- doesn't contain anything useful, but we can easily use the timeslot for reading from the dataregister
150
                                                int_dataReg_we <= "0";                                                                   --
151
                                                int_dataReg_clk <= '0';                                                                  --
152
                                        when "0010010" =>                                                                                       --Bit 14 (the 18th read bit),
153
                                                int_dataReg_clk <= '1';                                                                 -- still nothing, now performing the read by pulling the dataregister clock high
154
                                        when "0010011" =>                                                                                       --Bit 13 (the 19th read bit),
155
                                                int_dataReg_clk <= '0';                                                                  -- the read is finished,
156
                                                readData <= int_dataReg_dataOut;                                                -- and the read value is stored
157
                                        when "0010101" =>                                                                                       --Bit 11 (the 21st read bit),
158
                                                doWrite <= dataInBuffer(0);                                                              -- contains the 'doWrite' flag
159
                                        when "0011111" =>                                                                                       --Bit 1 (the 31st read bit),
160
                                                if(doRead = '1') then                                                                   -- we're not using this bit for anything right now, but we need to put the previously read data value into the output buffer now
161
                                                        dataOutBuffer <= readData;                                                      --
162
                                                else                                                                                                    --If a read was not requested,
163
                                                        dataOutBuffer <= (others => '0');                                        -- the output buffer is just filled with zeros instead
164
                                                end if;
165
                                        when "0100000" =>                                                                                       --Bits 9-0 (available when 32 bits have been read),
166
                                                writeAddress <= dataInBuffer(5 downto 0);                                -- contain the address to write to
167
                                        when others =>                                                                                          --Other bit positions are ignored
168
                                end case;
169
                        end if;
170
 
171
                end if;
172
        end process;
173
 
174
end Behavioral;

powered by: WebSVN 2.1.0

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