OpenCores
URL https://opencores.org/ocsvn/spi_master/spi_master/trunk

Subversion Repositories spi_master

[/] [spi_master/] [trunk/] [hw/] [simulations/] [Testbench_SPIMaster.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ldalmasso
------------------------------------------------------------------------
2
-- Engineer:    Dalmasso Loic
3
-- Create Date: 11/11/2024
4
-- Module Name: SPIMaster
5
-- Description:
6
--      SPI Master allowing Write/Read operations on slave devices.
7
--              Features:
8
--          - CPOL Configuration
9
--          - CPHA Configuration
10
--          - Slave Select Polarity (active Low or High)
11
--          - 2-Byte Delay Interval (0 to 7 SPI Clock Cycles)
12
--          - Byte Number Configuration
13
--          - Daisy-Chain (Slaves MUST support this feature)
14
--
15
-- Usage:
16
--      The Ready signal indicates no operation is on going and the SPI Master is waiting operation.
17
--              The Busy signal indicates operation is on going.
18
--      Reset input can be trigger at any time to reset the SPI Master to the IDLE state.
19
--              1. Set all necessary inputs
20
--                      * Byte Number (number of byte required to write/read)
21
--                      * Byte Delay (number of SPI SCLK Clock Cycles between 2 bytes to write/read)
22
--                      * Slave Select (set to '1' the Slave Select Line to enable)
23
--                      * Data to Write
24
--      2. Asserts Start input. The Ready signal is de-asserted and the Busy signal is asserted.
25
--              3. SPI Master re-asserts the Ready signal at the end of transmission (Master is ready for a new transmission)
26
--              4. The read value is available when its validity signal is asserted
27
--
28
-- Generics
29
--      input_clock: Module Input Clock Frequency
30
--      spi_clock: SPI Serial Clock Frequency
31
--      cpol: SPI Clock Polarity ('0': SCLK IDLE at Low, '1': SCLK IDLE at High)
32
--      cpha: SPI Clock Phase ('0': Data valid on Leading/First Edge of SCLK, '1': Data valid on Trailing/Second Edge of SCLK)
33
--      ss_polarity: SPI Slave Select Polarity ('0': active Low, '1': active High)
34
--      ss_length: Number of Chip/Slave Select Lines
35
--      max_data_register_length: Maximum SPI Data Register Length in bits
36
--
37
-- Ports
38
--              Input   -       i_clock: Module Input Clock
39
--              Input   -       i_reset: Reset ('0': No Reset, '1': Reset)
40
--              Input   -       i_byte_number: SPI Byte Number during the Transmission
41
--              Input   -       i_byte_delay: SPI Delay between 2-Byte Transmission (0 to 7 SPI Clock Cycles)
42
--              Input   -       i_slave_select: SPI Slave Selection ('0': Not Selected, '1': Selected)
43
--              Input   -       i_start: Start SPI Transmission ('0': No Start, '1': Start)
44
--              Input   -       i_write_value: Data to Write
45
--              Output  -       o_read_value: Data Read from Slave
46
--              Output  -       o_read_value_valid: Validity of the Data Read ('0': Not Valid, '1': Valid)
47
--              Output  -       o_ready: Ready State of SPI Master ('0': Not Ready, '1': Ready)
48
--              Output  -       o_busy: Busy State of SPI Master ('0': Not Busy, '1': Busy)
49
--              Output  -       o_sclk: SPI Serial Clock
50
--              Output  -       o_mosi: SPI Master Output Slave Input Data line
51
--              Input   -       i_miso: SPI Master Input Slave Output Data line
52
--              Output  -       o_ss: SPI Slave Select Line (inverted ss_polarity: Not Selected, ss_polarity: Selected)
53
------------------------------------------------------------------------
54
 
55
library IEEE;
56
use IEEE.STD_LOGIC_1164.ALL;
57
 
58
entity Testbench_SPIMaster is
59
end Testbench_SPIMaster;
60
 
61
architecture Behavioral of Testbench_SPIMaster is
62
 
63
COMPONENT SPIMaster is
64
 
65
GENERIC(
66
    input_clock: INTEGER := 12_000_000;
67
    spi_clock: INTEGER := 100_000;
68
    cpol: STD_LOGIC := '0';
69
    cpha: STD_LOGIC := '0';
70
    ss_polarity: STD_LOGIC := '0';
71
    ss_length: INTEGER := 1;
72
    max_data_register_length: INTEGER := 8
73
);
74
 
75
PORT(
76
    i_clock: IN STD_LOGIC;
77
    i_reset: IN STD_LOGIC;
78
    i_byte_number: IN INTEGER range 0 to max_data_register_length/8;
79
    i_byte_delay: IN INTEGER range 0 to 7;
80
    i_slave_select: IN STD_LOGIC_VECTOR(ss_length-1 downto 0);
81
    i_start: IN STD_LOGIC;
82
    i_write_value: IN STD_LOGIC_VECTOR(max_data_register_length-1 downto 0);
83
        o_read_value: OUT STD_LOGIC_VECTOR(max_data_register_length-1 downto 0);
84
    o_read_value_valid: OUT STD_LOGIC;
85
    o_ready: OUT STD_LOGIC;
86
    o_busy: OUT STD_LOGIC;
87
    o_sclk: OUT STD_LOGIC;
88
    o_mosi: OUT STD_LOGIC;
89
    i_miso: IN STD_LOGIC;
90
    o_ss: OUT STD_LOGIC_VECTOR(ss_length-1 downto 0)
91
);
92
 
93
END COMPONENT;
94
 
95
signal clock_12M: STD_LOGIC := '0';
96
signal reset: STD_LOGIC := '0';
97
signal start: STD_LOGIC := '0';
98
signal ready: STD_LOGIC := '0';
99
signal busy: STD_LOGIC := '0';
100
signal read_value_valid: STD_LOGIC := '0';
101
signal read_value: STD_LOGIC_VECTOR(7 downto 0):= (others => '0');
102
signal sclk: STD_LOGIC := '1';
103
signal mosi: STD_LOGIC := '1';
104
signal miso: STD_LOGIC := '1';
105
signal ss: STD_LOGIC_VECTOR(0 downto 0):= (others => '0');
106
 
107
 
108
begin
109
 
110
-- Clock 12 MHz
111
clock_12M <= not(clock_12M) after 41.6667 ns;
112
 
113
-- Reset
114
reset <= '1', '0' after 50 ns;
115
 
116
-- Start
117
start <= '0', '1' after 111 us, '0' after 113 us, '1' after 600 us, '0' after 620 us;
118
 
119
-- MISO
120
miso <= '0',
121
        -- Read 1
122
        '1' after 600.213135 us,
123
                '1' after 620.129961 us,
124
                '0' after 630.130041 us,
125
                '0' after 640.130121 us,
126
                '1' after 650.130201 us,
127
                '0' after 660.130281 us,
128
                '1' after 670.130361 us,
129
                '1' after 680.130441 us,
130
                '0' after 690.130521 us;
131
 
132
uut: SPIMaster
133
    GENERIC map(
134
        input_clock => 12_000_000,
135
        spi_clock => 100_000,
136
        cpol => '0',
137
        cpha => '0',
138
        ss_polarity => '0',
139
        ss_length => 1,
140
        max_data_register_length => 8
141
    )
142
 
143
    PORT map(
144
        i_clock => clock_12M,
145
        i_reset => reset,
146
        i_byte_number => 0,
147
        i_byte_delay => 0,
148
        i_slave_select => "1",
149
        i_start => start,
150
        i_write_value => "10100101",
151
        o_read_value => read_value,
152
        o_read_value_valid => read_value_valid,
153
        o_ready => ready,
154
        o_busy => busy,
155
        o_sclk => sclk,
156
        o_mosi => mosi,
157
        i_miso => miso,
158
        o_ss => ss);
159
 
160
end Behavioral;

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.