Debian Codegraph @

hdl/fpga/ip/nuand/rfic_spi_controller/vhdl/rfic_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: rfic_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 rfic_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 := 16; -- 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 rfic of rfic_spi_controller is
43
44 constant RFIC_ADDR_WIDTH : positive := 15;
45 constant RFIC_DATA_WIDTH : positive := 8;
46 constant MSG_LENGTH : positive := 1 + RFIC_ADDR_WIDTH + RFIC_DATA_WIDTH;
47
48 -- FSM States
49 type fsm_t is (
50 IDLE,
51 SHIFT
52 );
53
54 -- State of internal signals
55 type state_t is record
56 state : fsm_t;
57 clock_count : unsigned(integer(log2(real(CLOCK_DIV)))-1 downto 0);
58 shift_en : std_logic;
59 shift_count : natural range 0 to MSG_LENGTH;
60 shift_out_reg : unsigned(MSG_LENGTH-1 downto 0);
61 shift_in_reg : unsigned(MSG_LENGTH-1 downto 0);
62 sclk : std_logic;
63 cs_n : std_logic;
64 read_op : std_logic;
65 rd_data_valid : std_logic;
66 end record;
67
68 constant reset_value : state_t := (
69 state => IDLE,
70 clock_count => (others => '0'),
71 shift_en => '0',
72 shift_count => 0,
73 shift_out_reg => (others => '0'),
74 shift_in_reg => (others => '0'),
75 sclk => '1',
76 cs_n => '1',
77 read_op => '0',
78 rd_data_valid => '0'
79 );
80
81 signal current : state_t;
82 signal future : state_t;
83
84 begin
85
86 sync_proc : process( mm_clock, mm_reset )
87 begin
88 if( mm_reset = '1' ) then
89 current <= reset_value;
90 elsif( rising_edge(mm_clock) ) then
91 current <= future;
92 end if;
93 end process;
94
95 comb_proc : process( all )
96 begin
97
98 -- Physical Outputs
99 sclk <= current.sclk;
100 cs_n <= current.cs_n;
101 mosi <= current.shift_out_reg(state_t.shift_out_reg'left);
102
103 -- Avalon-MM outputs
104 mm_dout <= std_logic_vector(current.shift_in_reg(RFIC_DATA_WIDTH-1 downto 0));
105 mm_dout_val <= current.rd_data_valid;
106 mm_busy <= '1';
107
108 -- Next state defaults
109 future <= current;
110 future.sclk <= '1';
111 future.cs_n <= '1';
112 future.shift_en <= '0';
113 future.rd_data_valid <= '0';
114
115 case( current.state ) is
116
117 when IDLE =>
118
119 future.clock_count <= (others => '0');
120 future.read_op <= mm_read;
121 mm_busy <= '0';
122
123 future.shift_count <= MSG_LENGTH;
124
125 if( (mm_read xor mm_write) = '1' ) then
126
127 future.shift_out_reg <= mm_write & unsigned(mm_addr(RFIC_ADDR_WIDTH-1 downto 0)) & unsigned(mm_din);
128
129 -- Next state logic
130 future.state <= SHIFT;
131 future.cs_n <= '0';
132 end if;
133
134 when SHIFT =>
135 future.cs_n <= '0';
136 future.clock_count <= current.clock_count + 1;
137 future.sclk <= current.clock_count(current.clock_count'left);
138
139 if( current.clock_count = 0 ) then
140 future.shift_en <= '1';
141 else
142 future.shift_en <= '0';
143 end if;
144
145 if( current.shift_en = '1' ) then
146 future.shift_in_reg <= current.shift_in_reg(current.shift_in_reg'left-1 downto current.shift_in_reg'right) & miso;
147 future.shift_out_reg <= current.shift_out_reg(current.shift_out_reg'left-1 downto current.shift_out_reg'right) & '0';
148 future.shift_count <= current.shift_count - 1;
149
150 if( current.shift_count = 1 ) then
151 -- Only assert read_valid if a read command was issued
152 future.rd_data_valid <= current.read_op;
153 future.state <= IDLE;
154 end if;
155 end if;
156
157 end case;
158
159 end process;
160
161 end architecture;