]> rtime.felk.cvut.cz Git - fpga/zynq/canbench-sw.git/blob - system/ip/spi_leds_and_enc_1.0/hdl/spi_leds_and_enc_v1_0_spi_fsm.vhd
microzed_apo: Correct JX1_LVDS_21_N pin assignment on FPGA_IO header.
[fpga/zynq/canbench-sw.git] / system / ip / spi_leds_and_enc_1.0 / hdl / spi_leds_and_enc_v1_0_spi_fsm.vhd
1 library ieee;
2 use ieee.std_logic_1164.all;
3 use ieee.numeric_std.all;
4
5 entity spi_leds_and_enc_v1_0_spi_fsm is
6         generic (
7                 data_width      : integer       := 32;
8                 spi_clkdiv      : integer       := 10
9         );
10         port (
11                 reset_in        : in std_logic;
12
13                 clk_in          : in std_logic;
14                 clk_en          : in std_logic;
15
16                 spi_clk         : out std_logic;
17                 spi_cs          : out std_logic;
18                 spi_mosi        : out std_logic;
19                 spi_miso        : in std_logic;
20
21                 tx_data         : in std_logic_vector(data_width-1 downto 0);
22                 rx_data         : out std_logic_vector(data_width-1 downto 0);
23
24                 trasfer_rq      : in std_logic;
25                 transfer_ready  : out std_logic
26         );
27 end spi_leds_and_enc_v1_0_spi_fsm;
28
29 architecture arch_imp of spi_leds_and_enc_v1_0_spi_fsm is
30
31         signal  shift_reg       : std_logic_vector(data_width-1 downto 0);
32         signal  data_cnt        : natural range 0 to data_width;
33         signal  div_cnt         : natural range 0 to spi_clkdiv-1;
34         signal  clk_phase       : std_logic;
35         signal  in_progress     : std_logic;
36
37 begin
38
39         process is
40         begin
41           wait until rising_edge (clk_in);
42           if ( reset_in = '1' ) then
43             shift_reg <= (others => '0');
44             rx_data <= (others => '0');
45             div_cnt   <= spi_clkdiv-1;
46             transfer_ready <= '0';
47             data_cnt  <= 0;
48             spi_clk   <= '1';
49             spi_cs    <= '1';
50             spi_mosi  <= '0';
51             clk_phase <= '0';
52             in_progress <= '0';
53           elsif (clk_en = '1') then
54             transfer_ready <= '0';
55             if (div_cnt /= 0) then
56               div_cnt <= div_cnt - 1;
57             elsif clk_phase = '1' then
58               div_cnt   <= spi_clkdiv-1;
59               clk_phase <= '0';
60               spi_clk   <= '1';
61               if in_progress = '1' then
62                 shift_reg(data_width-1) <= spi_miso;
63               end if;
64             else
65               clk_phase <= '1';
66               div_cnt   <= spi_clkdiv-1;
67               spi_clk   <= '0';
68               if (data_cnt = 0) then
69                 spi_cs  <= '1';
70                 if in_progress = '1' then
71                   spi_cs    <= '1';
72                   rx_data   <= shift_reg;
73                 end if;
74                 transfer_ready <= in_progress;
75                 in_progress <= '0';
76                 if (trasfer_rq = '1') then
77                   shift_reg <= tx_data;
78                   data_cnt  <= data_width;
79                   spi_mosi  <= '0';
80                 end if;
81               else
82                 in_progress <= '1';
83                 spi_cs    <= '0';
84                 spi_mosi  <= shift_reg(0);
85                 shift_reg <= '0' & shift_reg(data_width-1 downto 1);
86                 data_cnt  <= data_cnt - 1;
87               end if;
88             end if;
89           end if;
90         end process;
91
92 end arch_imp;