diff --git a/hdl/fpga/ip/altera/clk_ctrl/clk_ctrl.vhd b/hdl/fpga/ip/altera/clk_ctrl/clk_ctrl.vhd new file mode 100644 index 00000000..08747087 --- /dev/null +++ b/hdl/fpga/ip/altera/clk_ctrl/clk_ctrl.vhd @@ -0,0 +1,61 @@ +-- Copyright (c) 2024 Nuand LLC +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy +-- of this software and associated documentation files (the "Software"), to deal +-- in the Software without restriction, including without limitation the rights +-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +-- copies of the Software, and to permit persons to whom the Software is +-- furnished to do so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in +-- all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +-- THE SOFTWARE. + +library ieee; + use ieee.std_logic_1164.all; + use ieee.math_real.all; + +entity clk_ctrl is + port ( + inclk :in std_logic; + ena :in std_logic; + outclk :out std_logic + ); +end entity clk_ctrl; + +architecture rtl of clk_ctrl is + + component altclkctrl + generic ( + clock_type : string := "Auto"; + ena_register_mode : string := "none"; + number_of_clocks : integer := 1 + ); + port ( + inclk : in std_logic_vector(0 downto 0); + ena : in std_logic; + outclk : out std_logic + ); + end component; + + signal clk_ctrl_out : std_logic; + +begin + + clkctrl : altclkctrl + port map ( + inclk => (0 => inclk), + ena => ena, + outclk => clk_ctrl_out + ); + + outclk <= clk_ctrl_out; + +end architecture rtl; diff --git a/hdl/fpga/ip/nuand/synthesis/fifo_reader.vhd b/hdl/fpga/ip/nuand/synthesis/fifo_reader.vhd index a525ab2b..d4cfb406 100644 --- a/hdl/fpga/ip/nuand/synthesis/fifo_reader.vhd +++ b/hdl/fpga/ip/nuand/synthesis/fifo_reader.vhd @@ -92,7 +92,10 @@ architecture simple of fifo_reader is meta_read : std_logic; meta_cache : std_logic_vector(META_FIFO_DATA_WIDTH-1 downto 0); meta_p_time : unsigned(63 downto 0); + meta_p_time_r : unsigned(63 downto 0); meta_time_go : std_logic; + meta_fifo_empty : std_logic; + meta_fifo_data : std_logic_vector(META_FIFO_DATA_WIDTH-1 downto 0); end record; constant META_FSM_RESET_VALUE : meta_fsm_t := ( @@ -104,7 +107,10 @@ architecture simple of fifo_reader is meta_read => '0', meta_cache => (others => '0'), meta_p_time => (others => '-'), - meta_time_go => '0' + meta_p_time_r => (others => '-'), + meta_time_go => '0', + meta_fifo_empty => '1', + meta_fifo_data => (others => '0') ); signal meta_current : meta_fsm_t := META_FSM_RESET_VALUE; @@ -193,7 +199,7 @@ begin end if; end process; - packet_empty <= '1' when ( meta_fifo_empty = '1' and meta_current.state /= META_WAIT ) else '0' ; + packet_empty <= '1' when ( meta_current.meta_fifo_empty = '1' and meta_current.state /= META_WAIT ) else '0' ; -- Meta FIFO combinatorial process meta_fsm_comb : process( all ) @@ -205,26 +211,29 @@ begin meta_future.meta_read <= '0'; meta_future.meta_pkt_sop <= '0'; meta_future.meta_pkt_eop <= '0'; + meta_future.meta_fifo_empty <= meta_fifo_empty; + meta_future.meta_fifo_data <= meta_fifo_data; + meta_time := unsigned(meta_current.meta_fifo_data(95 downto 32)) - 1; + meta_future.meta_p_time_r <= meta_time; case meta_current.state is when META_LOAD => meta_future.skip_padding <= '0'; - meta_time := unsigned(meta_fifo_data(95 downto 32)) - 1; - meta_future.meta_p_time <= meta_time; - meta_future.meta_cache <= meta_fifo_data; + meta_future.meta_p_time <= meta_current.meta_p_time_r; + meta_future.meta_cache <= meta_current.meta_fifo_data; if( meta_current.dma_downcount = NUM_STREAMS ) then meta_future.dma_downcount <= 0; end if; if( fifo_current.ch_shift = 0 ) then - if( meta_fifo_empty = '0' and (packet_en = '0' or + if( meta_current.meta_fifo_empty = '0' and (packet_en = '0' or (packet_en = '1' and packet_ready = '1') ) ) then meta_future.meta_read <= '1'; meta_future.state <= META_WAIT; - if( packet_en = '1' or meta_time /= timestamp ) then + if( packet_en = '1' or meta_current.meta_p_time_r /= timestamp ) then meta_future.meta_time_go <= '0'; else meta_future.meta_time_go <= '1'; diff --git a/hdl/fpga/ip/nuand/synthesis/fifo_writer.vhd b/hdl/fpga/ip/nuand/synthesis/fifo_writer.vhd index a6cdfed9..9ae1d24e 100644 --- a/hdl/fpga/ip/nuand/synthesis/fifo_writer.vhd +++ b/hdl/fpga/ip/nuand/synthesis/fifo_writer.vhd @@ -119,6 +119,8 @@ architecture simple of fifo_writer is fifo_write : std_logic; fifo_data : unsigned(fifo_data'range); samples_left : natural range 0 to in_sample_controls'length; + in_sample_controls_r: sample_controls_t(0 to NUM_STREAMS-1); + in_samples_r : sample_streams_t(0 to NUM_STREAMS-1); eight_bit_delay : std_logic; end record; @@ -128,6 +130,8 @@ architecture simple of fifo_writer is fifo_write => '0', fifo_data => (others => '-'), samples_left => 0, + in_sample_controls_r=> (others => SAMPLE_CONTROL_DISABLE), + in_samples_r => (others => ZERO_SAMPLE), eight_bit_delay => '0' ); @@ -136,6 +140,9 @@ architecture simple of fifo_writer is signal sync_mini_exp: std_logic_vector(1 downto 0); + signal meta_fifo_used_v_r : unsigned(meta_fifo_usedw'length downto 0) := (others => '0'); + signal fifo_used_v_r : unsigned(fifo_usedw'length downto 0) := (others => '0'); + begin -- Throw an error if port widths don't make sense @@ -167,11 +174,21 @@ begin if( reset = '1' ) then fifo_enough <= false; fifo_used_v := (others => '0'); + meta_fifo_used_v_r <= (others => '0'); + fifo_used_v_r <= (others => '0'); elsif( rising_edge(clock) ) then + -- the outputs of the dcfifo are not registered so give the long combinatorial + -- data path, let's register the values. one extra clock cycle will not change + -- the fidelity of the result. the meta_fifo represents at minimum 204 timestamps. + -- there is no way for an off by 1 clock cycle timing to overflow the meta fifo + -- when the buffer leaves 4 full meta buffers empty fifo_used_v := unsigned(fifo_full & fifo_usedw); + fifo_used_v_r <= fifo_used_v; meta_fifo_used_v := unsigned(meta_fifo_full & meta_fifo_usedw); - if( fifo_full = '0' and ((FIFO_MAX - fifo_used_v) > ( dma_buf_size * 4 )) and - ( ( meta_en = '1' and meta_fifo_full = '0' and ( META_MAX - 4 ) > meta_fifo_used_v ) + meta_fifo_used_v_r <= meta_fifo_used_v; + + if( fifo_full = '0' and ((FIFO_MAX - fifo_used_v_r) > ( dma_buf_size * 4 )) and + ( ( meta_en = '1' and meta_fifo_full = '0' and ( META_MAX - 4 ) > meta_fifo_used_v_r ) or (meta_en = '0') ) ) then fifo_enough <= true; else @@ -263,11 +280,10 @@ begin when META_WRITE => for i in in_samples'range loop - if (meta_fifo_full = '0') and (in_samples(i).data_v = '1') then + if (meta_fifo_full = '0' and in_samples(i).data_v = '1') then meta_future.meta_write <= '1'; meta_future.meta_written <= '1'; meta_future.state <= META_DOWNCOUNT; - exit; -- Exit loop after first match end if; end loop; @@ -414,6 +430,48 @@ begin return rv; end function; + procedure handle_fifo_write( + signal fifo_current : in fifo_fsm_t; + signal meta_current : in meta_fsm_t; + signal meta_en : in std_logic; + signal eight_bit_mode_en : in std_logic; + variable write_req : out std_logic; + signal fifo_future : out fifo_fsm_t + ) is + variable in_sample_controls : sample_controls_t(0 to NUM_STREAMS-1); + variable in_samples : sample_streams_t(0 to NUM_STREAMS-1); + begin + in_sample_controls := fifo_current.in_sample_controls_r; + in_samples := fifo_current.in_samples_r; + + if( ((meta_current.meta_written = '1') or (meta_en = '0')) ) then + -- Check for valid data + write_req := '0'; + for i in in_sample_controls'range loop + if( in_sample_controls(i).enable = '1' ) then + write_req := write_req or in_samples(i).data_v; + end if; + end loop; + + -- Received valid data + if( write_req = '1' ) then + if( fifo_current.samples_left = 0 ) then + fifo_future.samples_left <= NUM_STREAMS - count_enabled_channels(in_sample_controls); + if( eight_bit_mode_en = '1' ) then + fifo_future.fifo_write <= write_req and fifo_current.eight_bit_delay; + fifo_future.eight_bit_delay <= not fifo_current.eight_bit_delay; + else + fifo_future.fifo_write <= write_req; + end if; + else + -- MIMO PACKER: STEP 3 of 3 + fifo_future.samples_left <= fifo_current.samples_left - 1; + end if; + end if; + else + fifo_future.fifo_write <= '0'; + end if; + end; variable write_req : std_logic := '0'; @@ -424,12 +482,19 @@ begin fifo_future.fifo_clear <= '0'; fifo_future.fifo_write <= '0'; + fifo_future.in_samples_r <= in_samples; + fifo_future.in_sample_controls_r <= in_sample_controls; + -- MIMO PACKER: STEP 1 of 3 if( packet_en = '0' ) then if( eight_bit_mode_en = '1' ) then - fifo_future.fifo_data <= pack_eight_bit_mode(in_sample_controls, in_samples, fifo_current.fifo_data); + fifo_future.fifo_data <= pack_eight_bit_mode(fifo_current.in_sample_controls_r, + fifo_current.in_samples_r, + fifo_current.fifo_data); else - fifo_future.fifo_data <= pack(in_sample_controls, in_samples, fifo_current.fifo_data); + fifo_future.fifo_data <= pack(fifo_current.in_sample_controls_r, + fifo_current.in_samples_r, + fifo_current.fifo_data); end if; end if; @@ -499,34 +564,14 @@ begin when WRITE_SAMPLES => - if( ((meta_current.meta_written = '1') or (meta_en = '0')) ) then - - -- Check for valid data - write_req := '0'; - for i in in_sample_controls'range loop - if( in_sample_controls(i).enable = '1' ) then - write_req := write_req or in_samples(i).data_v; - end if; - end loop; - - -- Received valid data - if( write_req = '1' ) then - if( fifo_current.samples_left = 0 ) then - fifo_future.samples_left <= NUM_STREAMS - count_enabled_channels(in_sample_controls); - if( eight_bit_mode_en = '1' ) then - fifo_future.fifo_write <= write_req and fifo_current.eight_bit_delay; - fifo_future.eight_bit_delay <= not fifo_current.eight_bit_delay; - else - fifo_future.fifo_write <= write_req; - end if; - else - -- MIMO PACKER: STEP 3 of 3 - fifo_future.samples_left <= fifo_current.samples_left - 1; - end if; - end if; - else - fifo_future.fifo_write <= '0'; - end if; + handle_fifo_write( + fifo_current => fifo_current, + meta_current => meta_current, + meta_en => meta_en, + eight_bit_mode_en => eight_bit_mode_en, + write_req => write_req, + fifo_future => fifo_future + ); if( fifo_full = '1' or ( meta_current.meta_written = '0' and meta_en = '1') ) then fifo_future.fifo_write <= '0'; @@ -537,6 +582,16 @@ begin if( fifo_enough ) then fifo_future.state <= WRITE_SAMPLES; + + handle_fifo_write( + fifo_current => fifo_current, + meta_current => meta_current, + meta_en => meta_en, + eight_bit_mode_en => eight_bit_mode_en, + write_req => write_req, + fifo_future => fifo_future + ); + end if; when others => diff --git a/hdl/fpga/platforms/bladerf-micro/bladerf-hosted.qip b/hdl/fpga/platforms/bladerf-micro/bladerf-hosted.qip index 0fc25dad..42f5e944 100644 --- a/hdl/fpga/platforms/bladerf-micro/bladerf-hosted.qip +++ b/hdl/fpga/platforms/bladerf-micro/bladerf-hosted.qip @@ -8,6 +8,7 @@ set opencores_ip [file normalize [file join $here ../../ip/opencores]] # Altera IP set_global_assignment -name VHDL_FILE [file normalize [file join $altera_ip common_dcfifo/common_dcfifo.vhd]] +set_global_assignment -name VHDL_FILE [file normalize [file join $altera_ip clk_ctrl/clk_ctrl.vhd]] set_global_assignment -name VHDL_FILE [file normalize [file join $here vhdl/wrappers/tx_fifo.vhd]] set_global_assignment -name VHDL_FILE [file normalize [file join $here vhdl/wrappers/tx_meta_fifo.vhd]] set_global_assignment -name VHDL_FILE [file normalize [file join $here vhdl/wrappers/rx_fifo.vhd]] diff --git a/hdl/fpga/platforms/bladerf-micro/constraints/bladerf.sdc b/hdl/fpga/platforms/bladerf-micro/constraints/bladerf.sdc index d01c8a29..41b5ca59 100644 --- a/hdl/fpga/platforms/bladerf-micro/constraints/bladerf.sdc +++ b/hdl/fpga/platforms/bladerf-micro/constraints/bladerf.sdc @@ -71,7 +71,7 @@ set_false_path -from exp_present -to * set_false_path -from pwr_status -to * # JTAG settings -create_clock -name {altera_reserved_tck} -period 10 [get_ports {altera_reserved_tck}] +create_clock -name {altera_reserved_tck} -period 100 [get_ports {altera_reserved_tck}] set_clock_groups -asynchronous -group {altera_reserved_tck} set_false_path -from [get_ports {altera_reserved_tms}] set_false_path -from [get_ports {altera_reserved_tdi}] diff --git a/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf-hosted.vhd b/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf-hosted.vhd index 009b2a23..5d634e5d 100644 --- a/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf-hosted.vhd +++ b/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf-hosted.vhd @@ -39,10 +39,12 @@ architecture hosted_bladerf of bladerf is signal sys_reset : std_logic; signal sys_clock : std_logic; + signal sys_clock_out : std_logic; signal sys_pll_locked : std_logic; signal sys_pll_reset : std_logic; signal fx3_pclk_pll : std_logic; + signal fx3_pclk_pll_out : std_logic; signal fx3_pclk_pll_locked : std_logic; signal fx3_pclk_pll_reset : std_logic; @@ -202,10 +204,17 @@ begin port map ( refclk => c5_clock2, rst => sys_pll_reset, - outclk_0 => sys_clock, + outclk_0 => sys_clock_out, locked => sys_pll_locked ); + U_system_pll_ctrl : component clk_ctrl + port map ( + inclk => sys_clock_out, + ena => sys_pll_locked, + outclk => sys_clock + ); + U_pll_reset_pll : entity work.pll_reset generic map ( SYS_CLOCK_FREQ_HZ => 38_400_000, @@ -223,10 +232,17 @@ begin port map ( refclk => fx3_pclk, rst => fx3_pclk_pll_reset, - outclk_0 => fx3_pclk_pll, + outclk_0 => fx3_pclk_pll_out, locked => fx3_pclk_pll_locked ); + U_fx3_pll_ctrl : component clk_ctrl + port map ( + inclk => fx3_pclk_pll_out, + ena => fx3_pclk_pll_locked, + outclk => fx3_pclk_pll + ); + U_pll_reset_fx3_pll : entity work.pll_reset generic map ( SYS_CLOCK_FREQ_HZ => 100_000_000, diff --git a/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf_p.vhd b/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf_p.vhd index 8555b527..519d57db 100644 --- a/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf_p.vhd +++ b/hdl/fpga/platforms/bladerf-micro/vhdl/bladerf_p.vhd @@ -50,6 +50,14 @@ package bladerf_p is ); end component; + component clk_ctrl + port ( + inclk :in std_logic; + ena :in std_logic; + outclk :out std_logic + ); + end component; + component nios_system is port ( clk_clk : in std_logic := 'X'; diff --git a/hdl/fpga/platforms/common/bladerf/software/bladeRF_nios/src/devices_inline.h b/hdl/fpga/platforms/common/bladerf/software/bladeRF_nios/src/devices_inline.h index 7339b94d..430fbefc 100644 --- a/hdl/fpga/platforms/common/bladerf/software/bladeRF_nios/src/devices_inline.h +++ b/hdl/fpga/platforms/common/bladerf/software/bladeRF_nios/src/devices_inline.h @@ -27,6 +27,7 @@ #include #include +#include #include "system.h" #include "altera_avalon_spi.h" @@ -41,7 +42,23 @@ static inline uint32_t control_reg_read(void) static inline void control_reg_write(uint32_t value) { + const size_t CFG_GPIO_CLOCK_SELECT = 18; // Refer to bladerf2_common.h + const uint32_t CLK_SEL_MASK = (1 << CFG_GPIO_CLOCK_SELECT); + uint32_t current_clock_select, requested_clock_select; + bool delay_nios_response = false; + + current_clock_select = control_reg_read() & CLK_SEL_MASK; + requested_clock_select = value & CLK_SEL_MASK; + delay_nios_response = current_clock_select != requested_clock_select; + IOWR_ALTERA_AVALON_PIO_DATA(CONTROL_BASE, value); + + // Adding a delay to allow the Nios and FX3 Plls + // to stabilize before we send back the Nios response. + if (delay_nios_response) { + DBG("__resp_delay__"); + usleep(5); + } } static inline uint32_t rffe_csr_read(void)