1 | library IEEE;
|
2 | use IEEE.std_logic_1164.all;
|
3 | use IEEE.std_logic_arith.all;
|
4 | use IEEE.std_logic_unsigned.all;
|
5 |
|
6 | entity Spi_Master is
|
7 | generic (data_width: integer := 16);
|
8 | Port ( CLK : in std_logic;
|
9 | RESET : in std_logic;
|
10 | Datain : in std_logic_vector( data_width-1 downto 0);
|
11 | SCLK : out std_logic;
|
12 | MOSI : out std_logic;
|
13 | MISO : in std_logic;
|
14 | CS1 : out std_logic); -- Slave Chip Select#1
|
15 | -- SS : out std_logic_vector (7 downto 0);
|
16 | end Spi_Master;
|
17 |
|
18 | architecture behave of Spi_Master is
|
19 | signal shreg: std_logic_vector( data_width-1 downto 0);
|
20 | type states is ( Idle, Data_transfer, Stop);
|
21 | signal State: states := Idle;
|
22 | begin
|
23 | MOSI <= shreg(shreg'left);
|
24 | SCLK <= CLK
|
25 | when State = Data_Transfer -- Master clock send to SCLK
|
26 | else '0';
|
27 |
|
28 | process( CLK)
|
29 | variable Count: integer range 0 to data_width;
|
30 | begin
|
31 | if rising_edge( CLK) then
|
32 | case State is
|
33 | when Idle =>
|
34 | CS1 <= '1';
|
35 | if RESET='1' then
|
36 | Count := 0; -- Init Data bit counter
|
37 | shreg <= Datain; -- Load parallel data
|
38 | CS1 <= '0'; -- Select Slave aktive
|
39 | State <= Data_Transfer; -- Change state
|
40 | end if;
|
41 | when Data_transfer =>
|
42 | if Count < (data_width-1) then
|
43 | Count := Count+1;
|
44 | shreg <= shreg( shreg'left-1 downto 0)& MISO; -- Shift
|
45 | else
|
46 | State <= Stop; -- Change state
|
47 | end if;
|
48 | when Stop =>
|
49 | CS1 <= '1'; -- Deselect slave
|
50 | if RESET ='0' then
|
51 | State <= Idle;
|
52 | end if;
|
53 | end case;
|
54 | end if; end process;
|
55 | end behave;
|
56 | ----------------------Simulation--------------------
|
57 | library IEEE;
|
58 | use IEEE.std_logic_1164.ALL;
|
59 | use IEEE.std_logic_arith.ALL;
|
60 | use IEEE.std_logic_unsigned.ALL;
|
61 |
|
62 | entity tb_Spi_Master is
|
63 | end tb_Spi_Master;
|
64 |
|
65 | architecture RTL of tb_Spi_Master is
|
66 | component Spi_Master
|
67 | Port ( CLK : in std_logic;
|
68 | RESET : in std_logic;
|
69 | Datain : in std_logic_vector( 15 downto 0);
|
70 | SCLK : out std_logic;
|
71 | MOSI : out std_logic;
|
72 | MISO : in std_logic;
|
73 | CS1 : out std_logic); -- Slave Chip Select#1
|
74 | end component;
|
75 | -----------------------------------------------------
|
76 | signal CLK : std_logic:= '0';
|
77 | signal RESET : std_logic;
|
78 | signal Datain : std_logic_vector( 15 downto 0);
|
79 | signal SCLK : std_logic := '0';
|
80 | signal MOSI : std_logic;
|
81 | signal MISO : std_logic;
|
82 | signal CS1 : std_logic;
|
83 | ----------------------------------------------------
|
84 | signal data_serial : std_logic;
|
85 | constant T : time:= 10 ns;
|
86 | ----------------------------------------------------
|
87 | begin
|
88 | -- ******************* Port Map ***************
|
89 | DUT : Spi_Master
|
90 | port map (CLK => CLK,
|
91 | RESET => RESET ,
|
92 | Datain => Datain,
|
93 | SCLK => SCLK,
|
94 | MOSI => MOSI,
|
95 | MISO => MISO,
|
96 | CS1 => CS1);
|
97 | -- *********** clk and reset definition ***********
|
98 | clk <= not clk after T; -- 50 MHz
|
99 | process
|
100 | begin
|
101 | reset <= '0'; wait for T + 10 ns;
|
102 | reset <= '1'; wait for T + 10 ns;
|
103 | end process;
|
104 | -- *********** Generating serial data ************
|
105 | Datain <= "1010101010101010"; -- 16 Bit
|
106 | process
|
107 | begin
|
108 | for i in 15 downto 0 loop
|
109 | wait until SCLK = '1' and SCLK'event;
|
110 | data_serial <= Datain(i);
|
111 | end loop;
|
112 | end process;
|
113 | end RTL;
|