summaryrefslogtreecommitdiffstats
path: root/fpga/hw-v2/src/usbrx/toplevel/usbrx_spi.vhd
blob: 1c3b54117f24425c8adf7a7cb2d3f3d778fba36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
---------------------------------------------------------------------------------------------------
-- Filename    : usbrx_spi.vhd
-- Project     : OsmoSDR FPGA Firmware
-- Purpose     : SPI Slave Implementation
---------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------
-- Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany --
-- written by Matthias Kleffel                                                   --
--                                                                               --
-- This program is free software; you can redistribute it and/or modify          --
-- it under the terms of the GNU General Public License as published by          --
-- the Free Software Foundation as version 3 of the License, or                  --
--                                                                               --
-- This program is distributed in the hope that it will be useful,               --
-- but WITHOUT ANY WARRANTY; without even the implied warranty of                --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                  --
-- GNU General Public License V3 for more details.                               --
--                                                                               --
-- You should have received a copy of the GNU General Public License             --
-- along with this program. If not, see <http://www.gnu.org/licenses/>.          --
-----------------------------------------------------------------------------------

library ieee;
	use ieee.std_logic_1164.all;
	use ieee.numeric_std.all;
library work;
	use work.all;
	use work.mt_toolbox.all;

entity usbrx_spi is
	port(
		-- common
		clk       : in  std_logic;
		reset     : in  std_logic;

		-- SPI interface
		spi_ncs   : in  std_logic;
		spi_sclk  : in  std_logic;
		spi_mosi  : in  std_logic;
		spi_miso  : out std_logic;
		
		-- bus interface
		bus_rena  : out std_logic;
		bus_wena  : out std_logic;
		bus_addr  : out unsigned(6 downto 0);
		bus_rdata : in  std_logic_vector(31 downto 0);
		bus_wdata : out std_logic_vector(31 downto 0)
	);
end usbrx_spi;

architecture rtl of usbrx_spi is

	-- internal types
	type state_t is (S_COMMAND, S_WRITE, S_READ2, S_READ3, S_READ4);
	
	-- IO-registers
	signal iob_ncs  : std_logic;
	signal iob_sclk : std_logic;
	signal iob_mosi : std_logic;

	-- synchronized inputs
	signal sync_ncs  : std_logic;
	signal sync_sclk : std_logic;
	signal sync_mosi : std_logic;
	
	-- edge detection
	signal last_sclk : std_logic;
	signal last_re   : std_logic;
	signal last_fe   : std_logic;
	
	-- SPI slave
	signal state  : state_t;
	signal remain : unsigned(5 downto 0);
	signal sireg  : std_logic_vector(31 downto 0);
	signal soreg  : std_logic_vector(32 downto 0);
	signal addr   : unsigned(6 downto 0);

begin
	
	-- IOBs
	process(clk)
	begin
		if rising_edge(clk) then
			iob_ncs  <= spi_ncs;
			iob_sclk <= spi_sclk;
			iob_mosi <= spi_mosi;
			if iob_ncs='0' 
				then spi_miso <= soreg(32);
				else spi_miso <= '1';
			end if;
		end if;
	end process;
	
	-- input synchronizer
	process(clk,reset)
	begin
		if reset = '1' then
			sync_ncs  <= '1';
			sync_sclk <= '0';
			sync_mosi <= '0';
		elsif rising_edge(clk) then
			sync_ncs  <= iob_ncs;
			sync_sclk <= iob_sclk;
			sync_mosi <= iob_mosi;
		end if;
	end process;

	-- SPI slave
	process(clk,reset)
		variable re,fe : std_logic;
	begin
		if reset = '1' then
			last_sclk <= '0';
			last_re   <= '0';
			last_fe   <= '0';
			state     <= S_COMMAND;
			remain    <= (others=>'0');
			sireg     <= (others=>'1');
			soreg     <= (others=>'1');
			addr      <= (others=>'0');
			bus_rena  <= '0';
			bus_wena  <= '0';
			bus_addr  <= (others=>'0');
			bus_wdata <= (others=>'0');
		elsif rising_edge(clk) then
			-- set default values
			bus_rena  <= '0';
			bus_wena <= '0';
	
			-- detect edges on clock line
			last_sclk <= sync_sclk;
			re := sync_sclk and (not last_sclk);
			fe := (not sync_sclk) and last_sclk;
			last_re <= re;
			last_fe <= fe;
		
			-- update shift-registers
			if re='1' then
				sireg <= sireg(30 downto 0) & sync_mosi;
			end if;
			if fe='1' then
				soreg <= soreg(31 downto 0) & '1';
			end if;
	
			-- check state of chip-select
			if sync_ncs='1' then
				--> CS deasserted, reset slave
				state  <= S_COMMAND;
				remain <= to_unsigned(7,6);
			else
				--> CS asserted, tick state-machine
				case state is
					when S_COMMAND =>
						-- wait until 8 bits were received
						if last_re='1' then
							remain <= remain - 1;
							if remain=0 then
								--> got 8 bits, decode address & direction
								addr   <= unsigned(sireg(6 downto 0));
								remain <= to_unsigned(31,6);
								if sireg(7)='1' then
									state    <= S_READ2;
									bus_rena <= '1';
									bus_addr <= unsigned(sireg(6 downto 0));
								else 
									state <= S_WRITE;
								end if;
							end if;
						end if;
						
					when S_WRITE =>
						-- wait until 32 bits were received
						if last_re='1' then
							remain <= remain - 1;
							if remain=0 then
								-- issue write-request
								bus_wena  <= '1';
								bus_addr  <= addr;
								bus_wdata <= sireg;
								
								-- continue with next word
								addr   <= addr + 1;
								remain <= to_unsigned(31,6);
							end if;
						end if;
					
					when S_READ2 =>
						-- wait-state
						state  <= S_READ3;
						
					when S_READ3 =>
						-- load shift-register
						soreg  <= soreg(32) & bus_rdata;
						state  <= S_READ4;
					    remain <= to_unsigned(31,6);
						
					when S_READ4 =>
						-- wait until 32 bits were transmitted
						if fe='1' then
							remain <= remain - 1;
							if remain=0 then
								-- continue with next word
								bus_rena <= '1';
								bus_addr <= addr + 1;
								addr     <= addr + 1;
								state    <= S_READ2;
							end if;
						end if;
				end case;
			end if;
		end if;
	end process;
	
end rtl;