Dear all,
I am trying to write a state machine that checks to see if correct data
is being received on the SPI link by lighting an LED.
I have written a code but I am not sure if I am writing the state
machine correctly, it definitely does not compile.
Here is the code that I have written in relation to the test bench,
please find the file attached.
1 | library IEEE; -- Reference for VHDL source code
|
2 | use IEEE.STD_LOGIC_1164.all; -- Package defined in the IEEE (Found in library IEEE)
|
3 |
|
4 | -- Entity declaration
|
5 | entity spi_statemachine is
|
6 | generic (n: positive := 16; -- Number of bits
|
7 | port (-- Master --
|
8 | di_m: in std_logic_vector(15 downto 0);
|
9 | wren_m: in std_logic;
|
10 | -- Slave --
|
11 | do_s: out std_logic_vector(15 downto 0);
|
12 | do_valid_s: out std_logic;
|
13 | -- Clock operation --
|
14 | rst_i: in std_logic;
|
15 | clk_i: in std_logic;
|
16 | -- Output detection --
|
17 | correct: out std_logic);
|
18 | end spi_statemachine;
|
19 |
|
20 | -- Architecture behaviour
|
21 | architecture detect of spi_statemachine is
|
22 | type state_type is (createData, writeData, delay, writeEnable,
|
23 | checkValid, receivedData, checkFinished); -- Enumeration type
|
24 | signal state: state_type;
|
25 | begin
|
26 | P1: process (clk_i, rst_i) -- Clock and reset
|
27 | variable dataLength: integer := n; -- Length of data
|
28 | variable count: integer := 1;
|
29 | begin
|
30 | if rst_i = '0' then -- Reset operation used initialize all signals to predetermined state
|
31 | state <= createData;
|
32 | elsif clk_i'event and clk_i = '1' then -- Signal attribute used to test for a change on a signal
|
33 | case state is
|
34 | when createData =>
|
35 | state <= writeData;
|
36 | else
|
37 | state <= createData;
|
38 | end if;
|
39 |
|
40 | when writeData =>
|
41 | di_m <= std_logic_vector(to_unsigned(dataLength,n)); -- Write data
|
42 | state <= delay;
|
43 |
|
44 | when delay =>
|
45 | count := count + 1;
|
46 | if (count > 1) then
|
47 | state <= writeEnable;
|
48 | count := 0;
|
49 | else
|
50 | state <= delay;
|
51 | end if;
|
52 |
|
53 | when writeEnable =>
|
54 | wren_m <= '1';
|
55 | state <= checkValid;
|
56 |
|
57 | when checkValid =>
|
58 | wren_m <= '0';
|
59 | state <= receivedData;
|
60 |
|
61 | when receivedData =>
|
62 | if do_s = di_m then
|
63 | state <= checkFinished;
|
64 | end if;
|
65 |
|
66 | when checkFinished =>
|
67 | correct <= '1';
|
68 | when others => null;
|
69 | end case;
|
70 | end if;
|
71 | end process;
|
72 | end detect;
|
Any kind of help will be appreciated.
Kind regards,
Nay