| 1 |
-- Copyright (c) 2017 Nuand LLC |
| 2 |
-- |
| 3 |
-- This program is free software: you can redistribute it and/or modify |
| 4 |
-- it under the terms of the GNU Affero General Public License as |
| 5 |
-- published by the Free Software Foundation, either version 3 of the |
| 6 |
-- License, or (at your option) any later version. |
| 7 |
-- |
| 8 |
-- This program is distributed in the hope that it will be useful, |
| 9 |
-- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
-- GNU Affero General Public License for more details. |
| 12 |
-- |
| 13 |
-- You should have received a copy of the GNU Affero General Public License |
| 14 |
-- along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
library ieee ; |
| 17 |
use ieee.std_logic_1164.all ; |
| 18 |
use ieee.numeric_std.all ; |
| 19 |
use ieee.math_real.all ; |
| 20 |
|
| 21 |
entity bladerf_agc is |
| 22 |
port ( |
| 23 |
-- 40MHz clock and async asserted, sync deasserted reset |
| 24 |
clock : in std_logic ; |
| 25 |
reset : in std_logic ; |
| 26 |
|
| 27 |
agc_hold_req : in std_logic ; |
| 28 |
|
| 29 |
gain_inc_req : out std_logic ; |
| 30 |
gain_dec_req : out std_logic ; |
| 31 |
gain_rst_req : out std_logic ; |
| 32 |
gain_ack : in std_logic ; |
| 33 |
gain_nack : in std_logic ; |
| 34 |
gain_max : in std_logic ; |
| 35 |
|
| 36 |
rst_gains : out std_logic ; |
| 37 |
burst : out std_logic ; |
| 38 |
|
| 39 |
sample_i : in signed(15 downto 0 ) ; |
| 40 |
sample_q : in signed(15 downto 0 ) ; |
| 41 |
sample_valid : in std_logic |
| 42 |
) ; |
| 43 |
end entity ; |
| 44 |
|
| 45 |
architecture arch of bladerf_agc is |
| 46 |
|
| 47 |
type bladerf_sample_t is record |
| 48 |
i : signed(15 downto 0) ; |
| 49 |
q : signed(15 downto 0) ; |
| 50 |
valid : std_logic ; |
| 51 |
end record ; |
| 52 |
|
| 53 |
signal iir : signed( 31 downto 0 ) ; |
| 54 |
signal ptemp : signed( 31 downto 0 ) ; |
| 55 |
signal burst_cnt: signed( 7 downto 0 ) ; |
| 56 |
|
| 57 |
function run_iir( x : signed( 31 downto 0); y : signed ( 31 downto 0) ) |
| 58 |
return signed |
| 59 |
is |
| 60 |
variable amrea : signed(31 downto 0) ; |
| 61 |
begin |
| 62 |
amrea := resize( x - shift_right(x, 6) + shift_right(y, 6), 32 ); |
| 63 |
return amrea; |
| 64 |
end; |
| 65 |
|
| 66 |
type fsm_t is (IDLE, SETTLE, ATTACK, WAIT_GAIN_ACK, WAIT_GAIN_ACK_1, HOLD) ; |
| 67 |
|
| 68 |
type state_t is record |
| 69 |
fsm : fsm_t ; |
| 70 |
inc_req : std_logic ; |
| 71 |
dec_req : std_logic ; |
| 72 |
rst_req : std_logic ; |
| 73 |
timer : unsigned( 10 downto 0 ) ; |
| 74 |
end record ; |
| 75 |
|
| 76 |
function NULL_STATE return state_t is |
| 77 |
variable rv : state_t ; |
| 78 |
begin |
| 79 |
rv.fsm := IDLE ; |
| 80 |
rv.inc_req := '0' ; |
| 81 |
rv.dec_req := '0' ; |
| 82 |
rv.rst_req := '0' ; |
| 83 |
rv.timer := ( others => '0' ); |
| 84 |
return rv ; |
| 85 |
end function ; |
| 86 |
|
| 87 |
signal current, future : state_t := NULL_STATE ; |
| 88 |
signal sample, sample_out : bladerf_sample_t ; |
| 89 |
begin |
| 90 |
sample.i <= sample_i; |
| 91 |
sample.q <= sample_q; |
| 92 |
sample.valid <= sample_valid; |
| 93 |
|
| 94 |
gain_inc_req <= current.inc_req ; |
| 95 |
gain_dec_req <= current.dec_req ; |
| 96 |
gain_rst_req <= current.rst_req ; |
| 97 |
|
| 98 |
process( clock, reset ) |
| 99 |
begin |
| 100 |
if( reset = '1' ) then |
| 101 |
rst_gains <= '0' ; |
| 102 |
burst <= '0' ; |
| 103 |
elsif( rising_edge( clock )) then |
| 104 |
if( ( gain_max = '1' and iir < 1200 ) or current.fsm = SETTLE ) then |
| 105 |
rst_gains <= '1' ; |
| 106 |
else |
| 107 |
rst_gains <= '0' ; |
| 108 |
end if ; |
| 109 |
|
| 110 |
if( gain_max = '0' or iir > 1100 ) then |
| 111 |
burst <= '1' ; |
| 112 |
burst_cnt <= to_signed(18, burst_cnt'length) ; |
| 113 |
else |
| 114 |
if( ptemp > 1100 ) then |
| 115 |
burst <= '1' ; |
| 116 |
burst_cnt <= to_signed(6, burst_cnt'length) ; |
| 117 |
else |
| 118 |
if( burst_cnt > 0 ) then |
| 119 |
burst <= '1' ; |
| 120 |
burst_cnt <= burst_cnt - 1 ; |
| 121 |
else |
| 122 |
burst <= '0' ; |
| 123 |
end if ; |
| 124 |
end if ; |
| 125 |
end if ; |
| 126 |
end if ; |
| 127 |
end process ; |
| 128 |
|
| 129 |
process( clock, reset ) |
| 130 |
begin |
| 131 |
if( reset = '1' ) then |
| 132 |
iir <= ( others => '0' ) ; |
| 133 |
ptemp <= (others => '0' ) ; |
| 134 |
elsif( rising_edge( clock )) then |
| 135 |
if( current.timer = 18 ) then |
| 136 |
iir <= ptemp ; |
| 137 |
elsif( sample.valid = '1') then |
| 138 |
ptemp <= sample.i * sample.i + sample.q * sample.q ; |
| 139 |
iir <= run_iir(iir, ptemp) ; |
| 140 |
end if ; |
| 141 |
end if ; |
| 142 |
end process ; |
| 143 |
|
| 144 |
sync : process(clock, reset) |
| 145 |
begin |
| 146 |
if( reset = '1' ) then |
| 147 |
current <= NULL_STATE ; |
| 148 |
elsif( rising_edge(clock) ) then |
| 149 |
current <= future ; |
| 150 |
end if ; |
| 151 |
end process ; |
| 152 |
|
| 153 |
comb : process(all) |
| 154 |
begin |
| 155 |
future <= current ; |
| 156 |
future.inc_req <= '0' ; |
| 157 |
future.dec_req <= '0' ; |
| 158 |
future.rst_req <= '0' ; |
| 159 |
|
| 160 |
case current.fsm is |
| 161 |
when IDLE => |
| 162 |
future.fsm <= SETTLE ; |
| 163 |
|
| 164 |
when SETTLE => |
| 165 |
if( current.timer > 32 ) then -- 50 looks good 70 is overkill |
| 166 |
future.timer <= ( others => '0' ) ; |
| 167 |
future.fsm <= ATTACK ; |
| 168 |
else |
| 169 |
if( current.timer < 400 ) then |
| 170 |
future.timer <= current.timer + 1 ; |
| 171 |
end if ; |
| 172 |
end if ; |
| 173 |
|
| 174 |
when ATTACK => |
| 175 |
if( rst_gains = '1' ) then |
| 176 |
future.timer <= ( others => '0' ) ; |
| 177 |
else |
| 178 |
if( current.timer < 400 ) then |
| 179 |
future.timer <= current.timer + 1 ; |
| 180 |
end if ; |
| 181 |
end if ; |
| 182 |
|
| 183 |
if( agc_hold_req = '1' ) then |
| 184 |
future.fsm <= HOLD ; |
| 185 |
else |
| 186 |
-- 405000, -48dBm to enter mid, -28dBm to enter low |
| 187 |
if( current.timer > 24 and iir > 335000 ) then --335000 |
| 188 |
future.fsm <= WAIT_GAIN_ACK ; |
| 189 |
future.dec_req <= '1' ; |
| 190 |
elsif( current.timer < 24 and iir > 485000 ) then --335000 |
| 191 |
-- I = Q = sqrt(2048*2048/10)=657, IIR = I^2 + Q^2= 845000 |
| 192 |
future.fsm <= WAIT_GAIN_ACK ; |
| 193 |
future.dec_req <= '1' ; |
| 194 |
elsif( current.timer > 300 and iir < 1000 ) then |
| 195 |
--elsif( iir < 5000 ) then |
| 196 |
-- I = Q = 50, IIR = I^2 + Q^2= 5000 |
| 197 |
future.inc_req <= '1' ; |
| 198 |
future.fsm <= WAIT_GAIN_ACK ; |
| 199 |
end if ; |
| 200 |
end if ; |
| 201 |
|
| 202 |
when WAIT_GAIN_ACK => |
| 203 |
future.fsm <= WAIT_GAIN_ACK_1 ; |
| 204 |
when WAIT_GAIN_ACK_1 => |
| 205 |
if( gain_ack = '1' ) then |
| 206 |
future.fsm <= SETTLE ; |
| 207 |
future.timer <= ( others => '0' ) ; |
| 208 |
end if ; |
| 209 |
if( gain_nack = '1' ) then |
| 210 |
future.fsm <= ATTACK ; |
| 211 |
end if ; |
| 212 |
|
| 213 |
when HOLD => |
| 214 |
if( iir < 5000 ) then |
| 215 |
-- I = Q = 50, IIR = I^2 + Q^2= 5000 |
| 216 |
future.rst_req <= '1' ; |
| 217 |
future.fsm <= WAIT_GAIN_ACK ; |
| 218 |
end if ; |
| 219 |
|
| 220 |
end case ; |
| 221 |
end process ; |
| 222 |
|
| 223 |
end architecture ; |