source: firmware/FAD/FACT_FAD_lib/hdl/spi_controller_beha.vhd

Last change on this file was 11755, checked in by neise, 13 years ago
reinit of this svn repos .... it was all too messy deleted the old folders and restarted with FACT_FAD_lib only. (well and the testbenches)
File size: 4.4 KB
Line 
1--
2-- VHDL Architecture FACT_FAD_lib.spi_controller.beha
3--
4-- Created:
5-- by - Benjamin Krumm.UNKNOWN (EEPC8)
6-- at - 10:37:20 12.04.2010
7--
8-- using Mentor Graphics HDL Designer(TM) 2009.1 (Build 12)
9--
10LIBRARY ieee;
11USE ieee.std_logic_1164.all;
12USE ieee.std_logic_arith.all;
13USE ieee.std_logic_unsigned.all;
14
15ENTITY spi_controller IS
16 PORT(
17 clk : IN std_logic;
18 miso : INOUT std_logic := 'Z';
19 mosi : OUT std_logic := '0';
20 dac_cs : OUT std_logic := '1';
21 sensor_cs : OUT std_logic_vector (3 DOWNTO 0) := (others => '1');
22
23 dac_id : IN std_logic_vector (2 DOWNTO 0);
24 sensor_id : IN std_logic_vector (1 downto 0);
25 data : INOUT std_logic_vector (15 DOWNTO 0) := (others => 'Z');
26 measured_temp_data : out std_logic_vector (15 DOWNTO 0) := (others => '0');
27
28 dac_start : IN std_logic;
29 dac_ready : OUT std_logic := '0';
30 sensor_start : IN std_logic;
31 sensor_valid : OUT std_logic := '0';
32 spi_channel_ready : OUT std_logic := '1'
33 );
34END spi_controller ;
35
36ARCHITECTURE beha OF spi_controller IS
37
38 type TYPE_SPI_STATE is (SPI_IDLE, SPI_LOAD_DAC, SPI_LOAD_COMMAND, SPI_GET_TEMP);
39
40 signal spi_state : TYPE_SPI_STATE := SPI_IDLE;
41 signal spi_cycle_cnt : integer range 0 to 25 := 0;
42 signal shift_reg : std_logic_vector (23 downto 0) := (others => '0');
43 signal data_reg : std_logic_vector (15 downto 0) := (others => '0');
44
45 signal dac_start_sr : std_logic_vector (1 downto 0) := "00";
46 signal sensor_start_sr : std_logic_vector (1 downto 0) := "00";
47
48BEGIN
49
50 spi_write_proc: process (clk)
51 begin
52 if falling_edge(clk) then
53 dac_start_sr <= dac_start_sr(0) & dac_start;
54 sensor_start_sr <= sensor_start_sr(0) & sensor_start;
55
56
57 dac_cs <= '1';
58 sensor_cs <= (others => '1');
59 miso <= 'Z';
60 mosi <= '0';
61 data <= (others => 'Z');
62 case spi_state is
63 when SPI_IDLE =>
64 spi_channel_ready <= '1';
65 if (dac_start_sr(1) = '1') then
66 spi_channel_ready <= '0';
67 dac_ready <= '0';
68 spi_state <= SPI_LOAD_COMMAND;
69 elsif (sensor_start_sr(1) = '1') then
70 spi_channel_ready <= '0';
71 sensor_valid <= '0';
72 spi_state <= SPI_LOAD_COMMAND;
73 end if;
74
75 when SPI_LOAD_COMMAND =>
76 spi_cycle_cnt <= 0;
77 spi_channel_ready <= '0';
78
79 if (sensor_start_sr(1) = '1') then
80
81 --shift_reg <= X"C1" & X"0000"; -- command: Temperature register read
82 shift_reg <= X"C1" & "ZZZZZZZZZZZZZZZZ"; -- command: Temperature register read
83 spi_state <= SPI_GET_TEMP;
84 elsif (dac_start_sr(1) = '1') then
85 shift_reg <= "0011" & '0' & dac_id & data;
86 spi_state <= SPI_LOAD_DAC;
87 end if;
88
89 -- start temperature sensor read
90 when SPI_GET_TEMP =>
91 if (spi_cycle_cnt < 24) then -- must be one more cause MAX6662 provides data on falling edge
92 sensor_cs(conv_integer(sensor_id)) <= '0';
93 sensor_valid <= '0';
94 spi_cycle_cnt <= spi_cycle_cnt + 1;
95 if (spi_cycle_cnt < 9) then -- send data
96 miso <= shift_reg(23);
97 shift_reg <= shift_reg(22 downto 0) & shift_reg(23);
98 end if;
99 else
100 data <= data_reg;
101 measured_temp_data <= data_reg;
102 sensor_valid <= '1';
103 spi_state <= SPI_IDLE;
104 end if;
105
106 -- start loading DACs
107 when SPI_LOAD_DAC =>
108 dac_cs <= '0';
109 if (spi_cycle_cnt < 24) then
110 mosi <= shift_reg(23);
111 shift_reg <= shift_reg(22 downto 0) & shift_reg(23);
112 dac_ready <= '0';
113 spi_cycle_cnt <= spi_cycle_cnt + 1;
114 spi_state <= SPI_LOAD_DAC;
115 else
116 dac_cs <= '1';
117 dac_ready <= '1';
118 spi_state <= SPI_IDLE;
119 end if;
120 end case;
121 end if;
122 end process spi_write_proc;
123
124 -- MAX6662 input must be read with rising edge
125 spi_read_proc: process (clk)
126 begin
127 if rising_edge(clk) then
128 if (spi_state = SPI_GET_TEMP and spi_cycle_cnt >= 9) then
129 data_reg(0) <= miso;
130 data_reg(15 downto 1) <= data_reg(14 downto 0);
131 end if;
132 end if;
133 end process spi_read_proc;
134
135END ARCHITECTURE beha;
136
Note: See TracBrowser for help on using the repository browser.