File: bladerf_agc_lms_drv.vhd

package info (click to toggle)
bladerf 0.2017.12~rc1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,620 kB
  • sloc: ansic: 50,123; vhdl: 12,873; python: 1,062; tcl: 1,060; xml: 1,017; makefile: 657; sh: 589; csh: 18; cpp: 9
file content (304 lines) | stat: -rw-r--r-- 11,337 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
-- Copyright (c) 2017 Nuand LLC
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

library ieee ;
    use ieee.std_logic_1164.all ;
    use ieee.numeric_std.all ;
    use ieee.math_real.all ;

entity bladerf_agc_lms_drv is
  port (
    -- 40MHz clock and async asserted, sync deasserted reset
    clock           :   in  std_logic ;
    reset           :   in  std_logic ;

    enable          :   in  std_logic ;
    gain_inc_req    :   in  std_logic ;
    gain_dec_req    :   in  std_logic ;
    gain_rst_req    :   in  std_logic ;
    gain_ack        :  out  std_logic ;
    gain_nack       :  out  std_logic ;

    gain_high       :  out  std_logic ;
    gain_mid        :  out  std_logic ;
    gain_low        :  out  std_logic ;

    -- Arbiter
    arbiter_req     :  out  std_logic ;
    arbiter_grant   :   in  std_logic ;
    arbiter_done    :  out  std_logic ;

    -- Misc
    band_sel        :   in  std_logic ;
        -- Physical Interface
    sclk            :   out std_logic ;
    miso            :   in  std_logic ;
    mosi            :   out std_logic ;
    cs_n            :   out std_logic

  ) ;
end entity ;

architecture arch of bladerf_agc_lms_drv is
    type gain_state_t is ( UNSET_GAIN_STATE, HIGH_GAIN_STATE, MID_GAIN_STATE, LOW_GAIN_STATE ) ;
    type lna_gain_t is ( LNA_MID_GAIN, LNA_MAX_GAIN ) ;
    type rxvga1_gain_t is ( RXVGA1_MID_GAIN, RXVGA1_MAX_GAIN ) ;
    type rxvga2_gain_t is ( RXVGA2_LOW_GAIN, RXVGA2_MID_GAIN ) ;

    type fsm_t is ( INIT, IDLE, SPI_WRITE, SPI_WRITE_GRANTED, WRITE_RXVGA1, WRITE_RXVGA2,
                    UPDATE_GAINS, SPI_WAIT, SPI_WAIT_1 ) ;

    type gain_t is record
        lna_gain            :   lna_gain_t ;
        rxvga1_gain         :   rxvga1_gain_t ;
        rxvga2_gain         :   rxvga2_gain_t ;
    end record ;

    -- -82dBm - -52dBm
    constant HIGH_GAIN : gain_t := (
            lna_gain     =>  LNA_MAX_GAIN,    --  6 dB (Max)
            rxvga1_gain  =>  RXVGA1_MAX_GAIN, -- 30 dB
            rxvga2_gain  =>  RXVGA2_MID_GAIN  -- 15 dB
        ) ;

    -- -52dBm - -30dBm
    constant MID_GAIN : gain_t := (
            lna_gain     =>  LNA_MID_GAIN,    --  3 dB (Mid)
            rxvga1_gain  =>  RXVGA1_MAX_GAIN, -- 30 dB
            rxvga2_gain  =>  RXVGA2_LOW_GAIN  --  0 dB
        ) ;

    -- -30dBm - -17dBm
    constant LOW_GAIN : gain_t := (
            lna_gain     =>  LNA_MID_GAIN,    --  3 dB (Mid)
            rxvga1_gain  =>  RXVGA1_MID_GAIN, -- 12 dB
            rxvga2_gain  =>  RXVGA2_LOW_GAIN  --  0 dB
        ) ;

    type state_t is record
        fsm                 :   fsm_t ;
        nfsm                :   fsm_t ;

        initializing        :   std_logic ;
        ack                 :   std_logic ;
        nack                :   std_logic ;
        gain_state          :   gain_state_t ;
        current_gain        :   gain_t ;
        future_gain         :   gain_t ;

        arbiter_req         :   std_logic ;
        arbiter_done        :   std_logic ;

        -- Avalon-MM Interface
        mm_write            :   std_logic ;
        mm_addr             :   std_logic_vector(7 downto 0) ;
        mm_din              :   std_logic_vector(7 downto 0) ;
    end record ;

    function NULL_STATE return state_t is
        variable rv : state_t ;
    begin
        rv.fsm          := INIT ;
        rv.nfsm         := INIT ;
        rv.initializing := '0' ;
        rv.arbiter_req  := '0' ;
        rv.arbiter_done := '0' ;
        rv.gain_state   := UNSET_GAIN_STATE ;
        rv.mm_addr      := ( others => '0' ) ;
        rv.mm_din       := ( others => '0' ) ;
        return rv ;
    end function ;

    signal current, future  :   state_t := NULL_STATE ;

    signal mm_busy          :   std_logic ;
begin

    arbiter_req  <= current.arbiter_req ;
    arbiter_done <= current.arbiter_done ;

    gain_high <= '1' when current.gain_state = HIGH_GAIN_STATE else '0' ;
    gain_mid  <= '1' when current.gain_state = MID_GAIN_STATE else '0' ;
    gain_low  <= '1' when current.gain_state = LOW_GAIN_STATE else '0' ;
    gain_ack <= current.ack ; --'1' when current.fsm = UPDATE_GAINS else '0' ;
    gain_nack <= current.nack ;

    U_spi_controller: entity work.lms6_spi_controller
      port map (
        -- Physical Interface
        sclk            => sclk,
        miso            => '0',
        mosi            => mosi,
        cs_n            => cs_n,

        -- Avalon-MM Interface
        mm_clock        => clock,
        mm_reset        => reset,
        mm_read         => '0',
        mm_write        => current.mm_write,
        mm_addr         => current.mm_addr,
        mm_din          => current.mm_din,
        mm_dout         => open,
        mm_dout_val     => open,                                  -- Read data valid.
        mm_busy         => mm_busy
    );

    sync : process(clock, reset)
    begin
        if( reset = '1' ) then
            current <= NULL_STATE ;
        elsif( rising_edge(clock) ) then
            current <= future ;
        end if ;
    end process ;

    comb : process(all)
    begin
        future <= current ;
        future.mm_write <= '0' ;
        future.ack <= '0' ;
        future.nack <= '0' ;
        future.arbiter_done <= '0' ;
        future.arbiter_req  <= '0' ;

        case current.fsm is
            when INIT =>
                future.nack <= '1' ;
                if( enable = '1' ) then
                    future.nack <= '0' ;
                    future.gain_state <= HIGH_GAIN_STATE ;
                    future.future_gain <= HIGH_GAIN ;
                    future.initializing <= '1' ;
                    future.fsm <= SPI_WRITE ;
                end if ;

            when IDLE =>
                if( gain_inc_req = '1' ) then
                    if( current.gain_state = LOW_GAIN_STATE ) then
                        future.gain_state <= MID_GAIN_STATE ;
                        future.future_gain <= MID_GAIN ;
                        future.fsm <= SPI_WRITE ;
                    elsif( current.gain_state = MID_GAIN_STATE ) then
                        future.gain_state <= HIGH_GAIN_STATE ;
                        future.future_gain <= HIGH_GAIN ;
                        future.fsm <= SPI_WRITE ;
                    else
                        future.nack <= '1' ;
                        -- we are already as high as can be
                    end if ;
                end if ;
                if( gain_dec_req = '1' ) then
                    if( current.gain_state = MID_GAIN_STATE ) then
                        future.gain_state <= LOW_GAIN_STATE ;
                        future.future_gain <= LOW_GAIN ;
                        future.fsm <= SPI_WRITE ;
                    elsif( current.gain_state = HIGH_GAIN_STATE ) then
                        future.gain_state <= MID_GAIN_STATE ;
                        future.future_gain <= MID_GAIN ;
                        future.fsm <= SPI_WRITE ;
                    else
                        future.nack <= '1' ;
                        -- we are already as low as can be
                    end if ;
                end if ;
                if( gain_rst_req = '1' ) then
                    future.gain_state <= HIGH_GAIN_STATE ;
                    future.future_gain <= HIGH_GAIN ;
                end if ;
                if( enable = '0' ) then
                    future.fsm <= INIT ;
                end if ;

            when SPI_WRITE =>
                future.arbiter_req <= '1' ;
                if( arbiter_grant = '1' ) then
                    future.fsm <= SPI_WRITE_GRANTED ;
                end if ;

            when SPI_WRITE_GRANTED =>
                if( current.current_gain.lna_gain /= current.future_gain.lna_gain or
                      current.initializing = '1' ) then
                    future.mm_addr <= x"75" ;
                    if (current.future_gain.lna_gain = LNA_MAX_GAIN ) then
                        if (band_sel = '0' ) then
                            future.mm_din <= x"D0";
                        else
                            future.mm_din <= x"E0";
                        end if ;
                    elsif (current.future_gain.lna_gain = LNA_MID_GAIN ) then
                        if (band_sel = '0' ) then
                            future.mm_din <= x"90";
                        else
                            future.mm_din <= x"A0";
                        end if ;
                    end if ;
                    future.mm_write <= '1' ;
                    future.fsm <= SPI_WAIT ;
                    future.nfsm <= WRITE_RXVGA1 ;
                else
                    future.fsm <= WRITE_RXVGA1 ;
                end if ;

            when WRITE_RXVGA1 =>
                if( current.current_gain.rxvga1_gain /= current.future_gain.rxvga1_gain or
                      current.initializing = '1' ) then
                    future.mm_addr <= x"76" ;
                    if (current.future_gain.rxvga1_gain = RXVGA1_MAX_GAIN ) then
                        future.mm_din <= x"78" ;
                    elsif (current.future_gain.rxvga1_gain = RXVGA1_MID_GAIN ) then
                        future.mm_din <= x"46" ;
                    end if ;
                    future.mm_write <= '1' ;
                    future.fsm <= SPI_WAIT ;
                    future.nfsm <= WRITE_RXVGA2 ;
                else
                    future.fsm <= WRITE_RXVGA2 ;
                end if ;

            when WRITE_RXVGA2 =>
                if( current.current_gain.rxvga2_gain /= current.future_gain.rxvga2_gain or
                      current.initializing = '1' ) then
                    future.mm_addr <= x"65" ;
                    if (current.future_gain.rxvga2_gain = RXVGA2_MID_GAIN ) then
                        future.mm_din <= x"05" ;
                    elsif (current.future_gain.rxvga2_gain = RXVGA2_LOW_GAIN ) then
                        future.mm_din <= x"00" ;
                    end if ;
                    future.mm_write <= '1' ;
                    future.fsm <= SPI_WAIT ;
                    future.nfsm <= UPDATE_GAINS ;
                else
                    future.fsm <= UPDATE_GAINS ;
                end if ;

            when UPDATE_GAINS =>
                future.ack <= '1' ;
                future.initializing <= '0' ;
                future.current_gain <= current.future_gain ;
                future.arbiter_done <= '1' ;
                future.fsm <= IDLE ;

            when SPI_WAIT =>
                future.fsm <= SPI_WAIT_1 ;

            when SPI_WAIT_1 =>
                if( mm_busy = '0' ) then
                    future.fsm <= current.nfsm ;
                end if ;

        end case ;
    end process ;

end architecture ;