| 1 |
-- Copyright (c) 2020 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_adi_drv is |
| 22 |
port ( |
| 23 |
-- 80 MHz clock and async asserted, sync deasserted reset |
| 24 |
clock : in std_logic ; |
| 25 |
reset : in std_logic ; |
| 26 |
|
| 27 |
enable : in std_logic ; |
| 28 |
gain_inc_req : in std_logic ; |
| 29 |
gain_dec_req : in std_logic ; |
| 30 |
gain_rst_req : in std_logic ; |
| 31 |
gain_ack : out std_logic ; |
| 32 |
gain_nack : out std_logic ; |
| 33 |
|
| 34 |
gain_max : out std_logic ; |
| 35 |
|
| 36 |
-- Physical Interface |
| 37 |
ctrl_gain_inc : out std_logic ; |
| 38 |
ctrl_gain_dec : out std_logic |
| 39 |
|
| 40 |
) ; |
| 41 |
end entity ; |
| 42 |
|
| 43 |
architecture arch of bladerf_agc_adi_drv is |
| 44 |
type gain_state_t is ( UNSET_GAIN_STATE, HIGH_GAIN_STATE, MID_GAIN_STATE, LOW_GAIN_STATE ) ; |
| 45 |
|
| 46 |
type fsm_t is ( INIT, IDLE, CTRL_WRITE_SETUP, CTRL_WRITE, UPDATE_GAINS, CTRL_WAIT ) ; |
| 47 |
|
| 48 |
|
| 49 |
-- High gain, total gain: 51dB |
| 50 |
-- -82dBm - -52dBm |
| 51 |
|
| 52 |
-- Mid gain, total gain: 33dB |
| 53 |
-- -52dBm - -30dBm |
| 54 |
|
| 55 |
-- Low gain, total gain: 15dB |
| 56 |
-- -30dBm - -17dBm |
| 57 |
|
| 58 |
type state_t is record |
| 59 |
fsm : fsm_t ; |
| 60 |
nfsm : fsm_t ; |
| 61 |
|
| 62 |
initializing : std_logic ; |
| 63 |
ack : std_logic ; |
| 64 |
nack : std_logic ; |
| 65 |
gain_state : gain_state_t ; |
| 66 |
|
| 67 |
gain_inc_req : std_logic ; |
| 68 |
gain_dec_req : std_logic ; |
| 69 |
gain_rst_req : std_logic ; |
| 70 |
|
| 71 |
gain_dir_inc : std_logic ; |
| 72 |
gain_dir_num : natural range 0 to 10 ; |
| 73 |
|
| 74 |
timeout_cnt : natural range 0 to 10 ; |
| 75 |
|
| 76 |
ctrl_gain_inc : std_logic ; |
| 77 |
ctrl_gain_dec : std_logic ; |
| 78 |
|
| 79 |
end record ; |
| 80 |
|
| 81 |
function NULL_STATE return state_t is |
| 82 |
variable rv : state_t ; |
| 83 |
begin |
| 84 |
rv.fsm := INIT ; |
| 85 |
rv.nfsm := INIT ; |
| 86 |
rv.initializing := '0' ; |
| 87 |
|
| 88 |
rv.gain_inc_req := '0' ; |
| 89 |
rv.gain_dec_req := '0' ; |
| 90 |
rv.gain_rst_req := '0' ; |
| 91 |
rv.gain_state := UNSET_GAIN_STATE ; |
| 92 |
|
| 93 |
rv.gain_dir_inc := '0'; |
| 94 |
rv.gain_dir_num := 0; |
| 95 |
|
| 96 |
rv.timeout_cnt := 0; |
| 97 |
rv.ctrl_gain_inc := '0'; |
| 98 |
rv.ctrl_gain_dec := '0'; |
| 99 |
return rv ; |
| 100 |
end function ; |
| 101 |
|
| 102 |
signal current, future : state_t := NULL_STATE ; |
| 103 |
|
| 104 |
begin |
| 105 |
|
| 106 |
gain_max <= '1' when current.gain_state = HIGH_GAIN_STATE else '0' ; |
| 107 |
gain_ack <= current.ack ; --'1' when current.fsm = UPDATE_GAINS else '0' ; |
| 108 |
gain_nack <= current.nack ; |
| 109 |
|
| 110 |
sync : process(clock, reset) |
| 111 |
begin |
| 112 |
if( reset = '1' ) then |
| 113 |
current <= NULL_STATE ; |
| 114 |
elsif( rising_edge(clock) ) then |
| 115 |
current <= future ; |
| 116 |
end if ; |
| 117 |
end process ; |
| 118 |
|
| 119 |
comb : process(all) |
| 120 |
begin |
| 121 |
future <= current ; |
| 122 |
future.ack <= '0' ; |
| 123 |
future.nack <= '0' ; |
| 124 |
|
| 125 |
future.gain_inc_req <= gain_inc_req ; |
| 126 |
future.gain_dec_req <= gain_dec_req ; |
| 127 |
future.gain_rst_req <= gain_rst_req ; |
| 128 |
|
| 129 |
future.ctrl_gain_inc <= '0' ; |
| 130 |
future.ctrl_gain_dec <= '0' ; |
| 131 |
|
| 132 |
case current.fsm is |
| 133 |
when INIT => |
| 134 |
future.nack <= '1' ; |
| 135 |
if( enable = '1' ) then |
| 136 |
future.nack <= '0' ; |
| 137 |
future.gain_dir_inc <= '0' ; |
| 138 |
future.gain_dir_num <= 3 ; |
| 139 |
|
| 140 |
future.gain_state <= HIGH_GAIN_STATE ; |
| 141 |
future.initializing <= '1' ; |
| 142 |
|
| 143 |
future.fsm <= CTRL_WRITE_SETUP ; |
| 144 |
end if ; |
| 145 |
|
| 146 |
when IDLE => |
| 147 |
if( current.gain_inc_req = '1' ) then |
| 148 |
future.gain_dir_inc <= '1' ; |
| 149 |
if( current.gain_state = LOW_GAIN_STATE ) then |
| 150 |
future.gain_state <= MID_GAIN_STATE ; |
| 151 |
future.gain_dir_num <= 1; |
| 152 |
future.fsm <= CTRL_WRITE_SETUP ; |
| 153 |
elsif( current.gain_state = MID_GAIN_STATE ) then |
| 154 |
future.gain_state <= HIGH_GAIN_STATE ; |
| 155 |
future.gain_dir_num <= 1; |
| 156 |
future.fsm <= CTRL_WRITE_SETUP ; |
| 157 |
else |
| 158 |
future.nack <= '1' ; |
| 159 |
-- gain is already maxed out |
| 160 |
end if ; |
| 161 |
end if ; |
| 162 |
if( current.gain_dec_req = '1' ) then |
| 163 |
future.gain_dir_inc <= '0' ; |
| 164 |
if( current.gain_state = MID_GAIN_STATE ) then |
| 165 |
future.gain_state <= LOW_GAIN_STATE ; |
| 166 |
future.gain_dir_num <= 1; |
| 167 |
future.fsm <= CTRL_WRITE_SETUP ; |
| 168 |
elsif( current.gain_state = HIGH_GAIN_STATE ) then |
| 169 |
future.gain_state <= MID_GAIN_STATE ; |
| 170 |
future.gain_dir_num <= 1; |
| 171 |
future.fsm <= CTRL_WRITE_SETUP ; |
| 172 |
else |
| 173 |
future.nack <= '1' ; |
| 174 |
-- gain is already at absolute minimum |
| 175 |
end if ; |
| 176 |
end if ; |
| 177 |
if( current.gain_rst_req = '1' ) then |
| 178 |
future.fsm <= INIT ; |
| 179 |
end if ; |
| 180 |
if( enable = '0' ) then |
| 181 |
future.fsm <= INIT ; |
| 182 |
end if ; |
| 183 |
|
| 184 |
when CTRL_WRITE_SETUP => |
| 185 |
future.timeout_cnt <= 0 ; |
| 186 |
future.fsm <= CTRL_WRITE ; |
| 187 |
|
| 188 |
when CTRL_WRITE => |
| 189 |
if( current.gain_dir_inc = '1' ) then |
| 190 |
future.ctrl_gain_inc <= '1' ; |
| 191 |
else |
| 192 |
future.ctrl_gain_dec <= '1' ; |
| 193 |
end if; |
| 194 |
|
| 195 |
future.timeout_cnt <= current.timeout_cnt + 1; |
| 196 |
if( current.timeout_cnt > 4 ) then |
| 197 |
future.gain_dir_num <= current.gain_dir_num - 1; |
| 198 |
future.fsm <= CTRL_WAIT ; |
| 199 |
end if ; |
| 200 |
|
| 201 |
when CTRL_WAIT => |
| 202 |
future.timeout_cnt <= current.timeout_cnt + 1; |
| 203 |
if( current.timeout_cnt > 4 ) then |
| 204 |
if( current.gain_dir_num = 0 ) then |
| 205 |
future.fsm <= UPDATE_GAINS ; |
| 206 |
else |
| 207 |
future.fsm <= CTRL_WRITE_SETUP ; |
| 208 |
end if; |
| 209 |
end if; |
| 210 |
|
| 211 |
when UPDATE_GAINS => |
| 212 |
future.ack <= '1' ; |
| 213 |
future.initializing <= '0' ; |
| 214 |
future.fsm <= IDLE ; |
| 215 |
|
| 216 |
end case ; |
| 217 |
end process ; |
| 218 |
|
| 219 |
end architecture ; |