| 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_lms_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_high : out std_logic ; |
| 35 |
gain_mid : out std_logic ; |
| 36 |
gain_low : out std_logic ; |
| 37 |
|
| 38 |
-- Arbiter |
| 39 |
arbiter_req : out std_logic ; |
| 40 |
arbiter_grant : in std_logic ; |
| 41 |
arbiter_done : out std_logic ; |
| 42 |
|
| 43 |
-- Misc |
| 44 |
band_sel : in std_logic ; |
| 45 |
-- Physical Interface |
| 46 |
sclk : out std_logic ; |
| 47 |
miso : in std_logic ; |
| 48 |
mosi : out std_logic ; |
| 49 |
cs_n : out std_logic |
| 50 |
|
| 51 |
) ; |
| 52 |
end entity ; |
| 53 |
|
| 54 |
architecture arch of bladerf_agc_lms_drv is |
| 55 |
type gain_state_t is ( UNSET_GAIN_STATE, HIGH_GAIN_STATE, MID_GAIN_STATE, LOW_GAIN_STATE ) ; |
| 56 |
type lna_gain_t is ( LNA_MID_GAIN, LNA_MAX_GAIN ) ; |
| 57 |
type rxvga1_gain_t is ( RXVGA1_MID_GAIN, RXVGA1_MAX_GAIN ) ; |
| 58 |
type rxvga2_gain_t is ( RXVGA2_LOW_GAIN, RXVGA2_MID_GAIN ) ; |
| 59 |
|
| 60 |
type fsm_t is ( INIT, IDLE, SPI_WRITE, SPI_WRITE_GRANTED, WRITE_RXVGA1, WRITE_RXVGA2, |
| 61 |
UPDATE_GAINS, SPI_WAIT, SPI_WAIT_1 ) ; |
| 62 |
|
| 63 |
type gain_t is record |
| 64 |
lna_gain : lna_gain_t ; |
| 65 |
rxvga1_gain : rxvga1_gain_t ; |
| 66 |
rxvga2_gain : rxvga2_gain_t ; |
| 67 |
end record ; |
| 68 |
|
| 69 |
-- -82dBm - -52dBm |
| 70 |
constant HIGH_GAIN : gain_t := ( |
| 71 |
lna_gain => LNA_MAX_GAIN, -- 6 dB (Max) |
| 72 |
rxvga1_gain => RXVGA1_MAX_GAIN, -- 30 dB |
| 73 |
rxvga2_gain => RXVGA2_MID_GAIN -- 15 dB |
| 74 |
) ; |
| 75 |
|
| 76 |
-- -52dBm - -30dBm |
| 77 |
constant MID_GAIN : gain_t := ( |
| 78 |
lna_gain => LNA_MID_GAIN, -- 3 dB (Mid) |
| 79 |
rxvga1_gain => RXVGA1_MAX_GAIN, -- 30 dB |
| 80 |
rxvga2_gain => RXVGA2_LOW_GAIN -- 0 dB |
| 81 |
) ; |
| 82 |
|
| 83 |
-- -30dBm - -17dBm |
| 84 |
constant LOW_GAIN : gain_t := ( |
| 85 |
lna_gain => LNA_MID_GAIN, -- 3 dB (Mid) |
| 86 |
rxvga1_gain => RXVGA1_MID_GAIN, -- 12 dB |
| 87 |
rxvga2_gain => RXVGA2_LOW_GAIN -- 0 dB |
| 88 |
) ; |
| 89 |
|
| 90 |
type state_t is record |
| 91 |
fsm : fsm_t ; |
| 92 |
nfsm : fsm_t ; |
| 93 |
|
| 94 |
initializing : std_logic ; |
| 95 |
ack : std_logic ; |
| 96 |
nack : std_logic ; |
| 97 |
gain_state : gain_state_t ; |
| 98 |
current_gain : gain_t ; |
| 99 |
future_gain : gain_t ; |
| 100 |
|
| 101 |
arbiter_req : std_logic ; |
| 102 |
arbiter_done : std_logic ; |
| 103 |
|
| 104 |
gain_inc_req : std_logic ; |
| 105 |
gain_dec_req : std_logic ; |
| 106 |
gain_rst_req : std_logic ; |
| 107 |
|
| 108 |
-- Avalon-MM Interface |
| 109 |
mm_write : std_logic ; |
| 110 |
mm_addr : std_logic_vector(7 downto 0) ; |
| 111 |
mm_din : std_logic_vector(7 downto 0) ; |
| 112 |
end record ; |
| 113 |
|
| 114 |
function NULL_STATE return state_t is |
| 115 |
variable rv : state_t ; |
| 116 |
begin |
| 117 |
rv.fsm := INIT ; |
| 118 |
rv.nfsm := INIT ; |
| 119 |
rv.initializing := '0' ; |
| 120 |
rv.arbiter_req := '0' ; |
| 121 |
rv.arbiter_done := '0' ; |
| 122 |
rv.gain_inc_req := '0' ; |
| 123 |
rv.gain_dec_req := '0' ; |
| 124 |
rv.gain_rst_req := '0' ; |
| 125 |
rv.gain_state := UNSET_GAIN_STATE ; |
| 126 |
rv.mm_addr := ( others => '0' ) ; |
| 127 |
rv.mm_din := ( others => '0' ) ; |
| 128 |
return rv ; |
| 129 |
end function ; |
| 130 |
|
| 131 |
signal current, future : state_t := NULL_STATE ; |
| 132 |
|
| 133 |
signal mm_busy : std_logic ; |
| 134 |
begin |
| 135 |
|
| 136 |
arbiter_req <= current.arbiter_req ; |
| 137 |
arbiter_done <= current.arbiter_done ; |
| 138 |
|
| 139 |
gain_high <= '1' when current.gain_state = HIGH_GAIN_STATE else '0' ; |
| 140 |
gain_mid <= '1' when current.gain_state = MID_GAIN_STATE else '0' ; |
| 141 |
gain_low <= '1' when current.gain_state = LOW_GAIN_STATE else '0' ; |
| 142 |
gain_ack <= current.ack ; --'1' when current.fsm = UPDATE_GAINS else '0' ; |
| 143 |
gain_nack <= current.nack ; |
| 144 |
|
| 145 |
U_spi_controller: entity work.lms6_spi_controller |
| 146 |
generic map ( |
| 147 |
CLOCK_DIV => 4 |
| 148 |
) |
| 149 |
port map ( |
| 150 |
-- Physical Interface |
| 151 |
sclk => sclk, |
| 152 |
miso => '0', |
| 153 |
mosi => mosi, |
| 154 |
cs_n => cs_n, |
| 155 |
|
| 156 |
-- Avalon-MM Interface |
| 157 |
mm_clock => clock, |
| 158 |
mm_reset => reset, |
| 159 |
mm_read => '0', |
| 160 |
mm_write => current.mm_write, |
| 161 |
mm_addr => current.mm_addr, |
| 162 |
mm_din => current.mm_din, |
| 163 |
mm_dout => open, |
| 164 |
mm_dout_val => open, -- Read data valid. |
| 165 |
mm_busy => mm_busy |
| 166 |
); |
| 167 |
|
| 168 |
sync : process(clock, reset) |
| 169 |
begin |
| 170 |
if( reset = '1' ) then |
| 171 |
current <= NULL_STATE ; |
| 172 |
elsif( rising_edge(clock) ) then |
| 173 |
current <= future ; |
| 174 |
end if ; |
| 175 |
end process ; |
| 176 |
|
| 177 |
comb : process(all) |
| 178 |
begin |
| 179 |
future <= current ; |
| 180 |
future.mm_write <= '0' ; |
| 181 |
future.ack <= '0' ; |
| 182 |
future.nack <= '0' ; |
| 183 |
future.arbiter_done <= '0' ; |
| 184 |
future.arbiter_req <= '0' ; |
| 185 |
|
| 186 |
future.gain_inc_req <= gain_inc_req ; |
| 187 |
future.gain_dec_req <= gain_dec_req ; |
| 188 |
future.gain_rst_req <= gain_rst_req ; |
| 189 |
|
| 190 |
case current.fsm is |
| 191 |
when INIT => |
| 192 |
future.nack <= '1' ; |
| 193 |
if( enable = '1' ) then |
| 194 |
future.nack <= '0' ; |
| 195 |
future.gain_state <= HIGH_GAIN_STATE ; |
| 196 |
future.future_gain <= HIGH_GAIN ; |
| 197 |
future.initializing <= '1' ; |
| 198 |
future.fsm <= SPI_WRITE ; |
| 199 |
end if ; |
| 200 |
|
| 201 |
when IDLE => |
| 202 |
if( current.gain_inc_req = '1' ) then |
| 203 |
if( current.gain_state = LOW_GAIN_STATE ) then |
| 204 |
future.gain_state <= MID_GAIN_STATE ; |
| 205 |
future.future_gain <= MID_GAIN ; |
| 206 |
future.fsm <= SPI_WRITE ; |
| 207 |
elsif( current.gain_state = MID_GAIN_STATE ) then |
| 208 |
future.gain_state <= HIGH_GAIN_STATE ; |
| 209 |
future.future_gain <= HIGH_GAIN ; |
| 210 |
future.fsm <= SPI_WRITE ; |
| 211 |
else |
| 212 |
future.nack <= '1' ; |
| 213 |
-- we are already as high as can be |
| 214 |
end if ; |
| 215 |
end if ; |
| 216 |
if( current.gain_dec_req = '1' ) then |
| 217 |
if( current.gain_state = MID_GAIN_STATE ) then |
| 218 |
future.gain_state <= LOW_GAIN_STATE ; |
| 219 |
future.future_gain <= LOW_GAIN ; |
| 220 |
future.fsm <= SPI_WRITE ; |
| 221 |
elsif( current.gain_state = HIGH_GAIN_STATE ) then |
| 222 |
future.gain_state <= MID_GAIN_STATE ; |
| 223 |
future.future_gain <= MID_GAIN ; |
| 224 |
future.fsm <= SPI_WRITE ; |
| 225 |
else |
| 226 |
future.nack <= '1' ; |
| 227 |
-- we are already as low as can be |
| 228 |
end if ; |
| 229 |
end if ; |
| 230 |
if( current.gain_rst_req = '1' ) then |
| 231 |
future.gain_state <= HIGH_GAIN_STATE ; |
| 232 |
future.future_gain <= HIGH_GAIN ; |
| 233 |
end if ; |
| 234 |
if( enable = '0' ) then |
| 235 |
future.fsm <= INIT ; |
| 236 |
end if ; |
| 237 |
|
| 238 |
when SPI_WRITE => |
| 239 |
future.arbiter_req <= '1' ; |
| 240 |
if( arbiter_grant = '1' ) then |
| 241 |
future.fsm <= SPI_WRITE_GRANTED ; |
| 242 |
end if ; |
| 243 |
|
| 244 |
when SPI_WRITE_GRANTED => |
| 245 |
if( current.current_gain.lna_gain /= current.future_gain.lna_gain or |
| 246 |
current.initializing = '1' ) then |
| 247 |
future.mm_addr <= x"75" ; |
| 248 |
if (current.future_gain.lna_gain = LNA_MAX_GAIN ) then |
| 249 |
if (band_sel = '0' ) then |
| 250 |
future.mm_din <= x"D0"; |
| 251 |
else |
| 252 |
future.mm_din <= x"E0"; |
| 253 |
end if ; |
| 254 |
elsif (current.future_gain.lna_gain = LNA_MID_GAIN ) then |
| 255 |
if (band_sel = '0' ) then |
| 256 |
future.mm_din <= x"90"; |
| 257 |
else |
| 258 |
future.mm_din <= x"A0"; |
| 259 |
end if ; |
| 260 |
end if ; |
| 261 |
future.mm_write <= '1' ; |
| 262 |
future.fsm <= SPI_WAIT ; |
| 263 |
future.nfsm <= WRITE_RXVGA1 ; |
| 264 |
else |
| 265 |
future.fsm <= WRITE_RXVGA1 ; |
| 266 |
end if ; |
| 267 |
|
| 268 |
when WRITE_RXVGA1 => |
| 269 |
if( current.current_gain.rxvga1_gain /= current.future_gain.rxvga1_gain or |
| 270 |
current.initializing = '1' ) then |
| 271 |
future.mm_addr <= x"76" ; |
| 272 |
if (current.future_gain.rxvga1_gain = RXVGA1_MAX_GAIN ) then |
| 273 |
future.mm_din <= x"78" ; |
| 274 |
elsif (current.future_gain.rxvga1_gain = RXVGA1_MID_GAIN ) then |
| 275 |
future.mm_din <= x"46" ; |
| 276 |
end if ; |
| 277 |
future.mm_write <= '1' ; |
| 278 |
future.fsm <= SPI_WAIT ; |
| 279 |
future.nfsm <= WRITE_RXVGA2 ; |
| 280 |
else |
| 281 |
future.fsm <= WRITE_RXVGA2 ; |
| 282 |
end if ; |
| 283 |
|
| 284 |
when WRITE_RXVGA2 => |
| 285 |
if( current.current_gain.rxvga2_gain /= current.future_gain.rxvga2_gain or |
| 286 |
current.initializing = '1' ) then |
| 287 |
future.mm_addr <= x"65" ; |
| 288 |
if (current.future_gain.rxvga2_gain = RXVGA2_MID_GAIN ) then |
| 289 |
future.mm_din <= x"05" ; |
| 290 |
elsif (current.future_gain.rxvga2_gain = RXVGA2_LOW_GAIN ) then |
| 291 |
future.mm_din <= x"00" ; |
| 292 |
end if ; |
| 293 |
future.mm_write <= '1' ; |
| 294 |
future.fsm <= SPI_WAIT ; |
| 295 |
future.nfsm <= UPDATE_GAINS ; |
| 296 |
else |
| 297 |
future.fsm <= UPDATE_GAINS ; |
| 298 |
end if ; |
| 299 |
|
| 300 |
when UPDATE_GAINS => |
| 301 |
future.ack <= '1' ; |
| 302 |
future.initializing <= '0' ; |
| 303 |
future.current_gain <= current.future_gain ; |
| 304 |
future.arbiter_done <= '1' ; |
| 305 |
future.fsm <= IDLE ; |
| 306 |
|
| 307 |
when SPI_WAIT => |
| 308 |
future.fsm <= SPI_WAIT_1 ; |
| 309 |
|
| 310 |
when SPI_WAIT_1 => |
| 311 |
if( mm_busy = '0' ) then |
| 312 |
future.fsm <= current.nfsm ; |
| 313 |
end if ; |
| 314 |
|
| 315 |
end case ; |
| 316 |
end process ; |
| 317 |
|
| 318 |
end architecture ; |