28
28
library IEEE;
29
29
use IEEE.STD_LOGIC_1164.ALL ;
30
30
use IEEE.NUMERIC_STD.ALL ;
31
+ use IEEE.MATH_REAL.ALL ;
31
32
32
33
-- THE SPI SLAVE MODULE SUPPORT ONLY SPI MODE 0 (CPOL=0, CPHA=0)!!!
33
34
34
35
entity SPI_SLAVE is
36
+ Generic (
37
+ WORD_SIZE : natural := 8 -- size of transfer word in bits, must be power of two
38
+ );
35
39
Port (
36
40
CLK : in std_logic ; -- system clock
37
41
RST : in std_logic ; -- high active synchronous reset
@@ -41,24 +45,26 @@ entity SPI_SLAVE is
41
45
MOSI : in std_logic ; -- SPI serial data from master to slave
42
46
MISO : out std_logic ; -- SPI serial data from slave to master
43
47
-- USER INTERFACE
44
- DIN : in std_logic_vector (7 downto 0 ); -- input data for SPI master
48
+ DIN : in std_logic_vector (WORD_SIZE - 1 downto 0 ); -- input data for SPI master
45
49
DIN_VLD : in std_logic ; -- when DIN_VLD = 1, input data are valid
46
50
DIN_RDY : out std_logic ; -- when DIN_RDY = 1, valid input data are accept
47
- DOUT : out std_logic_vector (7 downto 0 ); -- output data from SPI master
51
+ DOUT : out std_logic_vector (WORD_SIZE - 1 downto 0 ); -- output data from SPI master
48
52
DOUT_VLD : out std_logic -- when DOUT_VLD = 1, output data are valid
49
53
);
50
54
end SPI_SLAVE ;
51
55
52
56
architecture RTL of SPI_SLAVE is
53
57
58
+ constant BIT_CNT_WIDTH : natural := natural (ceil (log2 (real (WORD_SIZE))));
59
+
54
60
signal spi_clk_reg : std_logic ;
55
61
signal spi_clk_redge_en : std_logic ;
56
62
signal spi_clk_fedge_en : std_logic ;
57
- signal bit_cnt : unsigned (2 downto 0 );
63
+ signal bit_cnt : unsigned (BIT_CNT_WIDTH - 1 downto 0 );
58
64
signal bit_cnt_max : std_logic ;
59
65
signal last_bit_en : std_logic ;
60
66
signal load_data_en : std_logic ;
61
- signal data_shreg : std_logic_vector (7 downto 0 );
67
+ signal data_shreg : std_logic_vector (WORD_SIZE - 1 downto 0 );
62
68
signal slave_ready : std_logic ;
63
69
signal shreg_busy : std_logic ;
64
70
signal rx_data_vld : std_logic ;
@@ -112,7 +118,7 @@ begin
112
118
end process ;
113
119
114
120
-- The flag of maximal value of the bit counter.
115
- bit_cnt_max <= '1' when (bit_cnt = "111" ) else '0' ;
121
+ bit_cnt_max <= '1' when (bit_cnt = WORD_SIZE - 1 ) else '0' ;
116
122
117
123
-- -------------------------------------------------------------------------
118
124
-- LAST BIT FLAG REGISTER
@@ -181,7 +187,7 @@ begin
181
187
if (load_data_en = '1' ) then
182
188
data_shreg <= DIN;
183
189
elsif (spi_clk_redge_en = '1' and CS_N = '0' ) then
184
- data_shreg <= data_shreg(6 downto 0 ) & MOSI;
190
+ data_shreg <= data_shreg(WORD_SIZE - 2 downto 0 ) & MOSI;
185
191
end if ;
186
192
end if ;
187
193
end process ;
@@ -196,9 +202,9 @@ begin
196
202
begin
197
203
if (rising_edge (CLK)) then
198
204
if (load_data_en = '1' ) then
199
- MISO <= DIN(7 );
205
+ MISO <= DIN(WORD_SIZE - 1 );
200
206
elsif (spi_clk_fedge_en = '1' and CS_N = '0' ) then
201
- MISO <= data_shreg(7 );
207
+ MISO <= data_shreg(WORD_SIZE - 1 );
202
208
end if ;
203
209
end if ;
204
210
end process ;
0 commit comments