Skip to content

Commit f00cedc

Browse files
committed
SPI MASTER [FEATURE]: Add WORD_SIZE generic
1 parent 4196de2 commit f00cedc

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

rtl/spi_master.vhd

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ entity SPI_MASTER is
3636
Generic (
3737
CLK_FREQ : natural := 50e6; -- set system clock frequency in Hz
3838
SCLK_FREQ : natural := 5e6; -- set SPI clock frequency in Hz (condition: SCLK_FREQ <= CLK_FREQ/10)
39+
WORD_SIZE : natural := 8; -- size of transfer word in bits, must be power of two
3940
SLAVE_COUNT : natural := 1 -- count of SPI slaves
4041
);
4142
Port (
@@ -48,12 +49,12 @@ entity SPI_MASTER is
4849
MISO : in std_logic; -- SPI serial data from slave to master
4950
-- INPUT USER INTERFACE
5051
DIN_ADDR : in std_logic_vector(integer(ceil(log2(real(SLAVE_COUNT))))-1 downto 0); -- SPI slave address
51-
DIN : in std_logic_vector(7 downto 0); -- input data for SPI slave
52+
DIN : in std_logic_vector(WORD_SIZE-1 downto 0); -- input data for SPI slave
5253
DIN_LAST : in std_logic; -- when DIN_LAST = 1, after transmit these input data is asserted CS_N
5354
DIN_VLD : in std_logic; -- when DIN_VLD = 1, input data are valid
5455
DIN_RDY : out std_logic; -- when DIN_RDY = 1, valid input data are accept
5556
-- OUTPUT USER INTERFACE
56-
DOUT : out std_logic_vector(7 downto 0); -- output data from SPI slave
57+
DOUT : out std_logic_vector(WORD_SIZE-1 downto 0); -- output data from SPI slave
5758
DOUT_VLD : out std_logic -- when DOUT_VLD = 1, output data are valid
5859
);
5960
end entity;
@@ -63,6 +64,7 @@ architecture RTL of SPI_MASTER is
6364
constant DIVIDER_VALUE : integer := (CLK_FREQ/SCLK_FREQ)/2;
6465
constant WIDTH_CLK_CNT : integer := integer(ceil(log2(real(DIVIDER_VALUE))));
6566
constant WIDTH_ADDR : integer := integer(ceil(log2(real(SLAVE_COUNT))));
67+
constant BIT_CNT_WIDTH : natural := natural(ceil(log2(real(WORD_SIZE))));
6668

6769
signal addr_reg : std_logic_vector(WIDTH_ADDR-1 downto 0);
6870
signal sys_clk_cnt : unsigned(WIDTH_CLK_CNT-1 downto 0);
@@ -76,8 +78,8 @@ architecture RTL of SPI_MASTER is
7678
signal chip_select_n : std_logic;
7779
signal load_data : std_logic;
7880
signal miso_reg : std_logic;
79-
signal shreg : std_logic_vector(7 downto 0);
80-
signal bit_cnt : unsigned(2 downto 0);
81+
signal shreg : std_logic_vector(WORD_SIZE-1 downto 0);
82+
signal bit_cnt : unsigned(BIT_CNT_WIDTH-1 downto 0);
8183
signal bit_cnt_max : std_logic;
8284
signal bit_cnt_rst : std_logic;
8385
signal rx_data_vld : std_logic;
@@ -132,7 +134,7 @@ begin
132134
-- BIT COUNTER
133135
-- -------------------------------------------------------------------------
134136

135-
bit_cnt_max <= '1' when (bit_cnt = "111") else '0';
137+
bit_cnt_max <= '1' when (bit_cnt = WORD_SIZE-1) else '0';
136138
bit_cnt_rst <= RST or not spi_clk_en;
137139

138140
bit_cnt_p : process (CLK)
@@ -210,13 +212,13 @@ begin
210212
if (load_data = '1') then
211213
shreg <= DIN;
212214
elsif (second_edge_en = '1') then
213-
shreg <= shreg(6 downto 0) & miso_reg;
215+
shreg <= shreg(WORD_SIZE-2 downto 0) & miso_reg;
214216
end if;
215217
end if;
216218
end process;
217219

218220
DOUT <= shreg;
219-
MOSI <= shreg(7);
221+
MOSI <= shreg(WORD_SIZE-1);
220222

221223
-- -------------------------------------------------------------------------
222224
-- DATA OUT VALID RESISTER

0 commit comments

Comments
 (0)