File: bladerf_agc_adi_drv.vhd

package info (click to toggle)
bladerf 0.2022.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 373,752 kB
  • sloc: ansic: 1,186,428; xml: 150,799; vhdl: 24,182; tcl: 15,408; python: 3,409; sh: 1,551; makefile: 1,255; asm: 158; csh: 18; cpp: 9
file content (219 lines) | stat: -rw-r--r-- 7,280 bytes parent folder | download | duplicates (2)
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
-- Copyright (c) 2020 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_adi_drv is
  port (
    -- 80 MHz 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_max        :  out  std_logic ;

    -- Physical Interface
    ctrl_gain_inc   :   out std_logic ;
    ctrl_gain_dec   :   out std_logic

  ) ;
end entity ;

architecture arch of bladerf_agc_adi_drv is
    type gain_state_t is ( UNSET_GAIN_STATE, HIGH_GAIN_STATE, MID_GAIN_STATE, LOW_GAIN_STATE ) ;

    type fsm_t is ( INIT, IDLE, CTRL_WRITE_SETUP, CTRL_WRITE, UPDATE_GAINS, CTRL_WAIT ) ;


    -- High gain, total gain: 51dB
    -- -82dBm - -52dBm

    -- Mid gain, total gain: 33dB
    -- -52dBm - -30dBm

    -- Low gain, total gain: 15dB
    -- -30dBm - -17dBm

    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 ;

        gain_inc_req        :   std_logic ;
        gain_dec_req        :   std_logic ;
        gain_rst_req        :   std_logic ;

        gain_dir_inc        :   std_logic ;
        gain_dir_num        :   natural range 0 to 10 ;

        timeout_cnt         :   natural range 0 to 10 ;

        ctrl_gain_inc       :   std_logic ;
        ctrl_gain_dec       :   std_logic ;

    end record ;

    function NULL_STATE return state_t is
        variable rv : state_t ;
    begin
        rv.fsm           := INIT ;
        rv.nfsm          := INIT ;
        rv.initializing  := '0' ;

        rv.gain_inc_req  := '0' ;
        rv.gain_dec_req  := '0' ;
        rv.gain_rst_req  := '0' ;
        rv.gain_state    := UNSET_GAIN_STATE ;

        rv.gain_dir_inc  := '0';
        rv.gain_dir_num  := 0;

        rv.timeout_cnt   := 0;
        rv.ctrl_gain_inc := '0';
        rv.ctrl_gain_dec := '0';
        return rv ;
    end function ;

    signal current, future  :   state_t := NULL_STATE ;

begin

    gain_max  <= '1' when current.gain_state = HIGH_GAIN_STATE else '0' ;
    gain_ack  <= current.ack ; --'1' when current.fsm = UPDATE_GAINS else '0' ;
    gain_nack <= current.nack ;

    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.ack <= '0' ;
        future.nack <= '0' ;

        future.gain_inc_req <= gain_inc_req ;
        future.gain_dec_req <= gain_dec_req ;
        future.gain_rst_req <= gain_rst_req ;

        future.ctrl_gain_inc <= '0' ;
        future.ctrl_gain_dec <= '0' ;

        case current.fsm is
            when INIT =>
                future.nack <= '1' ;
                if( enable = '1' ) then
                    future.nack <= '0' ;
                    future.gain_dir_inc <= '0' ;
                    future.gain_dir_num <= 3 ;

                    future.gain_state <= HIGH_GAIN_STATE ;
                    future.initializing <= '1' ;

                    future.fsm <= CTRL_WRITE_SETUP ;
                end if ;

            when IDLE =>
                if( current.gain_inc_req = '1' ) then
                    future.gain_dir_inc <= '1' ;
                    if( current.gain_state = LOW_GAIN_STATE ) then
                        future.gain_state <= MID_GAIN_STATE ;
                        future.gain_dir_num <= 1;
                        future.fsm <= CTRL_WRITE_SETUP ;
                    elsif( current.gain_state = MID_GAIN_STATE ) then
                        future.gain_state <= HIGH_GAIN_STATE ;
                        future.gain_dir_num <= 1;
                        future.fsm <= CTRL_WRITE_SETUP ;
                    else
                        future.nack <= '1' ;
                        -- gain is already maxed out
                    end if ;
                end if ;
                if( current.gain_dec_req = '1' ) then
                    future.gain_dir_inc <= '0' ;
                    if( current.gain_state = MID_GAIN_STATE ) then
                        future.gain_state <= LOW_GAIN_STATE ;
                        future.gain_dir_num <= 1;
                        future.fsm <= CTRL_WRITE_SETUP ;
                    elsif( current.gain_state = HIGH_GAIN_STATE ) then
                        future.gain_state <= MID_GAIN_STATE ;
                        future.gain_dir_num <= 1;
                        future.fsm <= CTRL_WRITE_SETUP ;
                    else
                        future.nack <= '1' ;
                        -- gain is already at absolute minimum
                    end if ;
                end if ;
                if( current.gain_rst_req = '1' ) then
                    future.fsm <= INIT ;
                end if ;
                if( enable = '0' ) then
                    future.fsm <= INIT ;
                end if ;

            when CTRL_WRITE_SETUP =>
                future.timeout_cnt <= 0 ;
                future.fsm <= CTRL_WRITE ;

            when CTRL_WRITE =>
                if( current.gain_dir_inc = '1' ) then
                    future.ctrl_gain_inc <= '1' ;
                else
                    future.ctrl_gain_dec <= '1' ;
                end if;

                future.timeout_cnt <= current.timeout_cnt + 1;
                if( current.timeout_cnt > 4 ) then
                    future.gain_dir_num <= current.gain_dir_num - 1;
                    future.fsm <= CTRL_WAIT ;
                end if ;

            when CTRL_WAIT =>
                future.timeout_cnt <= current.timeout_cnt + 1;
                if( current.timeout_cnt > 4 ) then
                    if( current.gain_dir_num = 0 ) then
                        future.fsm <= UPDATE_GAINS ;
                    else
                        future.fsm <= CTRL_WRITE_SETUP ;
                    end if;
                end if;

            when UPDATE_GAINS =>
                future.ack <= '1' ;
                future.initializing <= '0' ;
                future.fsm <= IDLE ;

        end case ;
    end process ;

end architecture ;