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