Debian Codegraph @

hdl/fpga/ip/nuand/lms6_spi_controller/vhdl/lms6_spi_controller.vhd

raw
1 -- From Altera Solution ID rd05312011_49, need the following for Qsys to use VHDL 2008:
2 -- altera vhdl_input_version vhdl_2008
3
4 library ieee;
5 use ieee.std_logic_1164.all;
6 use ieee.numeric_std.all;
7 use ieee.math_real.log2;
8
9 -- -----------------------------------------------------------------------------
10 -- Entity: lms6_spi_controller
11 -- Description: Avalon Memory-Mapped SPI controller for the LMS6002D.
12 -- Standard: VHDL-2008
13 --
14 -- WARNING: This module has not been tested with non-default generics.
15 -- -----------------------------------------------------------------------------
16 entity lms6_spi_controller is
17 generic(
18 CLOCK_DIV : positive range 2 to 16 := 2; -- Ratio of system clock to SPI clock
19 ADDR_WIDTH : positive := 8; -- Number of address bits in an operation
20 DATA_WIDTH : positive := 8 -- Number of data bits in an operation
21 );
22 port(
23 -- Physical Interface
24 sclk : out std_logic;
25 miso : in std_logic;
26 mosi : out std_logic;
27 cs_n : out std_logic;
28
29 -- Avalon-MM Interface
30 mm_clock : in std_logic; -- System clock
31 mm_reset : in std_logic; -- System reset
32 mm_read : in std_logic; -- Initiates a SPI read operation
33 mm_write : in std_logic; -- Initiates a SPI write operation
34 mm_addr : in std_logic_vector(ADDR_WIDTH-1 downto 0); -- SPI address
35 mm_din : in std_logic_vector(DATA_WIDTH-1 downto 0); -- SPI write data
36 mm_dout : out std_logic_vector(DATA_WIDTH-1 downto 0); -- SPI read data
37 mm_dout_val : out std_logic; -- Read data valid.
38 mm_busy : out std_logic -- Indicates the module is busy processing a command
39 );
40 end entity;
41
42 architecture lms6 of lms6_spi_controller is
43
44 constant LMS_ADDR_WIDTH : positive := 7;
45 constant LMS_DATA_WIDTH : positive := 8;
46 constant NUM_PLL_REGS : positive := 4;
47 constant MSG_LENGTH : positive := 1 + LMS_ADDR_WIDTH + LMS_DATA_WIDTH;
48
49 -- Special LMS PLL Register Addresses
50 constant TX0_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"90";
51 constant TX1_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"91";
52 constant TX2_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"92";
53 constant TX3_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"93";
54
55 constant RX0_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"A0";
56 constant RX1_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"A1";
57 constant RX2_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"A2";
58 constant RX3_ADDR : std_logic_vector(ADDR_WIDTH-1 downto 0) := x"A3";
59
60 -- FSM States
61 type fsm_t is (
62 IDLE,
63 SHIFT
64 );
65
66 -- State of internal signals
67 type state_t is record
68 state : fsm_t;
69 clock_count : unsigned(integer(log2(real(CLOCK_DIV)))-1 downto 0);
70 shift_en : std_logic;
71 shift_count : natural range 0 to MSG_LENGTH*NUM_PLL_REGS;
72 shift_out_reg : unsigned(MSG_LENGTH*NUM_PLL_REGS-1 downto 0);
73 shift_in_reg : unsigned(MSG_LENGTH-1 downto 0);
74 sclk : std_logic;
75 cs_n : std_logic;
76 read_op : std_logic;
77 rd_data_valid : std_logic;
78 end record;
79
80 constant reset_value : state_t := (
81 state => IDLE,
82 clock_count => (others => '0'),
83 shift_en => '0',
84 shift_count => 0,
85 shift_out_reg => (others => '0'),
86 shift_in_reg => (others => '0'),
87 sclk => '0',
88 cs_n => '1',
89 read_op => '0',
90 rd_data_valid => '0'
91 );
92
93 signal current : state_t;
94 signal future : state_t;
95
96 begin
97
98 sync_proc : process( mm_clock, mm_reset )
99 begin
100 if( mm_reset = '1' ) then
101 current <= reset_value;
102 elsif( rising_edge(mm_clock) ) then
103 current <= future;
104 end if;
105 end process;
106
107 comb_proc : process( all )
108 begin
109
110 -- Physical Outputs
111 sclk <= current.sclk;
112 cs_n <= current.cs_n;
113 mosi <= current.shift_out_reg(state_t.shift_out_reg'left);
114
115 -- Avalon-MM outputs
116 mm_dout <= std_logic_vector(current.shift_in_reg(LMS_DATA_WIDTH-1 downto 0));
117 mm_dout_val <= current.rd_data_valid;
118 mm_busy <= '1';
119
120 -- Next state defaults
121 future <= current;
122 future.sclk <= '0';
123 future.cs_n <= '1';
124 future.shift_en <= '0';
125 future.rd_data_valid <= '0';
126
127 case( current.state ) is
128
129 when IDLE =>
130
131 future.clock_count <= (others => '0');
132 future.read_op <= mm_read;
133 mm_busy <= '0';
134
135 -- Take advantage of the multi-write capability of the LMS6 SPI
136 -- interface. The only registers that we are going to support
137 -- for now are the TX and RX NINT and NFRAC registers
138 -- controlling the PLL settings. An extra address bit was added
139 -- to the Avalon-MM interface and indicates that a multi-write
140 -- message is intended. We queue up to NUM_PLL_REGS write
141 -- transactions in the shift_out_reg. Upon detecting a write to
142 -- the final register, the shift_count is set to the max.
143 -- Otherwise, the default for a single message is used.
144 case (mm_addr) is
145 when TX3_ADDR =>
146 -- Multi-message
147 future.shift_count <= MSG_LENGTH*NUM_PLL_REGS;
148 when RX3_ADDR =>
149 -- Multi-message
150 future.shift_count <= MSG_LENGTH*NUM_PLL_REGS;
151 when others =>
152 -- Single message
153 future.shift_count <= MSG_LENGTH;
154 end case;
155
156 if( (mm_read xor mm_write) = '1' ) then
157
158 future.shift_out_reg <= mm_write & unsigned(mm_addr(LMS_ADDR_WIDTH-1 downto 0)) & unsigned(mm_din)
159 & current.shift_out_reg(current.shift_out_reg'left downto MSG_LENGTH);
160
161 -- Next state logic
162 case (mm_addr) is
163 when TX0_ADDR | TX1_ADDR | TX2_ADDR =>
164 future.state <= IDLE;
165 when RX0_ADDR | RX1_ADDR | RX2_ADDR =>
166 future.state <= IDLE;
167 when others =>
168 future.state <= SHIFT;
169 end case;
170 end if;
171
172 when SHIFT =>
173
174 future.clock_count <= current.clock_count + 1;
175 future.cs_n <= '0';
176 future.sclk <= current.clock_count(current.clock_count'left);
177
178 if( current.clock_count = CLOCK_DIV-1 ) then
179 future.shift_en <= '1';
180 else
181 future.shift_en <= '0';
182 end if;
183
184 if( current.shift_en = '1' ) then
185 future.shift_in_reg <= current.shift_in_reg(current.shift_in_reg'left-1 downto current.shift_in_reg'right) & miso;
186 future.shift_out_reg <= current.shift_out_reg(current.shift_out_reg'left-1 downto current.shift_out_reg'right) & '0';
187 future.shift_count <= current.shift_count - 1;
188
189 if( current.shift_count = 1 ) then
190 -- Only assert read_valid if a read command was issued
191 future.rd_data_valid <= current.read_op;
192 future.state <= IDLE;
193 end if;
194 end if;
195
196 end case;
197
198 end process;
199
200 end architecture;