Skip to content

Commit 5b598ce

Browse files
committed
SPI SLAVE [FEATURE]: Add WORD_SIZE generic
1 parent 410650a commit 5b598ce

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

rtl/spi_slave.vhd

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@
2828
library IEEE;
2929
use IEEE.STD_LOGIC_1164.ALL;
3030
use IEEE.NUMERIC_STD.ALL;
31+
use IEEE.MATH_REAL.ALL;
3132

3233
-- THE SPI SLAVE MODULE SUPPORT ONLY SPI MODE 0 (CPOL=0, CPHA=0)!!!
3334

3435
entity SPI_SLAVE is
36+
Generic(
37+
WORD_SIZE : natural := 8 -- size of transfer word in bits, must be power of two
38+
);
3539
Port (
3640
CLK : in std_logic; -- system clock
3741
RST : in std_logic; -- high active synchronous reset
@@ -41,24 +45,26 @@ entity SPI_SLAVE is
4145
MOSI : in std_logic; -- SPI serial data from master to slave
4246
MISO : out std_logic; -- SPI serial data from slave to master
4347
-- 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
4549
DIN_VLD : in std_logic; -- when DIN_VLD = 1, input data are valid
4650
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
4852
DOUT_VLD : out std_logic -- when DOUT_VLD = 1, output data are valid
4953
);
5054
end SPI_SLAVE;
5155

5256
architecture RTL of SPI_SLAVE is
5357

58+
constant BIT_CNT_WIDTH : natural := natural(ceil(log2(real(WORD_SIZE))));
59+
5460
signal spi_clk_reg : std_logic;
5561
signal spi_clk_redge_en : std_logic;
5662
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);
5864
signal bit_cnt_max : std_logic;
5965
signal last_bit_en : std_logic;
6066
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);
6268
signal slave_ready : std_logic;
6369
signal shreg_busy : std_logic;
6470
signal rx_data_vld : std_logic;
@@ -112,7 +118,7 @@ begin
112118
end process;
113119

114120
-- 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';
116122

117123
-- -------------------------------------------------------------------------
118124
-- LAST BIT FLAG REGISTER
@@ -181,7 +187,7 @@ begin
181187
if (load_data_en = '1') then
182188
data_shreg <= DIN;
183189
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;
185191
end if;
186192
end if;
187193
end process;
@@ -196,9 +202,9 @@ begin
196202
begin
197203
if (rising_edge(CLK)) then
198204
if (load_data_en = '1') then
199-
MISO <= DIN(7);
205+
MISO <= DIN(WORD_SIZE-1);
200206
elsif (spi_clk_fedge_en = '1' and CS_N = '0') then
201-
MISO <= data_shreg(7);
207+
MISO <= data_shreg(WORD_SIZE-1);
202208
end if;
203209
end if;
204210
end process;

0 commit comments

Comments
 (0)