From f8065d8c467e10b8bc2aa1c0ae94895e1c2be542 Mon Sep 17 00:00:00 2001 From: David Steele Date: Tue, 20 Sep 2022 13:40:25 +0100 Subject: [PATCH] CORE-798 Make EL GIP Library FPGA Vendor Independent. v3 - Adds "swim_reset_ctrl" to drive STM reset. Was driven by Altera GPIO 44 which is not present on Xilinx Kria --- generic_ip/swim/3/doc/swim_3.json | 235 ++++++++ generic_ip/swim/3/src/swim_3.vhd | 867 ++++++++++++++++++++++++++++++ generic_ip/swim/versions.txt | 1 + 3 files changed, 1103 insertions(+) create mode 100644 generic_ip/swim/3/doc/swim_3.json create mode 100644 generic_ip/swim/3/src/swim_3.vhd create mode 100644 generic_ip/swim/versions.txt diff --git a/generic_ip/swim/3/doc/swim_3.json b/generic_ip/swim/3/doc/swim_3.json new file mode 100644 index 0000000..e07a541 --- /dev/null +++ b/generic_ip/swim/3/doc/swim_3.json @@ -0,0 +1,235 @@ +[{ + "ip_name" : "swim_3", + "bus" : "csr", + "addr_width" : "4", + "description" : "This block is the SWIM interface", + "notes" : "Usage of the interface to communicate with a slave device consists of sending an entry sequence (if none sent before) and then writing a byte to the SWIM_CSR register (at 0x7f80) to set the SWIM_DM bit and enable access to the slave's other registers etc. To send an entry sequence a write to the SWIM interface's CSR_SWIM_CONTROL register, setting SEND_ENTRY_SEQ bit, will instigate the operation. The TXBUSY bit of the CSR_SWIM_STATUS register will then go high until the operation has finished, or an error occurred. The TX_ERROR bit will be set if there was an error. Once an entry sequence has been sent, the ENTRY_SEQ_SENT bit of the CSR_SWIM_STATUS register will be set, and remain set until a hard reset. To send a byte to a given SWIM address, a write to the CSR_SWIM_TX_BYTE register, with the BYTE field set to the byte data, and the WADDR field set to the destination address. This will instigate a write over the SWIM and the TX_BUSY status will be set until the operation is complete or an error occurred. Any errors can be cleared by writing to the CSR_SWIM_CONTROL register wit the CLEAR_ERRORS bit set. To read from a location of the slave's memory space, a write to the CSR_SWIM_RX_BYTE register, setting the WADDR field to be the location for the read, will instigate the read operation. Both the RXBUSY and TXBUSY status bit of the CSR_SWIM_STATUS register will initially go busy (as the read command is transmitted). When both statuses are clear, the operation completed, possibly with errors, as indicated by the TX_ERROR and RX_ERROR status bits. If the read was successful, the RX_READY bit will indicate the availability of a read byte. Reading the BYTE field of the CSR_SWIM_RX_BYTE register gives the value of the read operation. Note that reading the value does not clear the status. To clear the ready signal a write to the CSW_SWIM_CONTROL register with the CLR_RX bit set, will clear this state. All state can be cleared by writing to the CSR_SWIM_CONTROL register, with the RESET_STATE bit set. An SRST command can be sent over the interface by writing to the CSR_SWIM_CONTROL register, with the SEND_SRST bit set. Whether the slave responds to this command is a matter of internal configuration of the slave. By default the interface is in low speed mode. It can be put into high speed mode by setting the HIGH_SPEED bit in the control register. THe slave interface also needs to be configured for the mode before entering the mode locally. NOTE: HIGH SPEED IS NOT YET FULLY SUPPORTED due to slew issues on the SOM board and work arounds in the embedded logic. The interface supports the writing of 128 byte blocks for configuring flash on an STM8 MCU. The MCU must be unlocked and configured for flash writes using byte write accesses. Once done, a block is written by writing to the CSR_SWIM_TX_BLOCK register with the start address of the block write set in the WADDR field. The TXBUSY status will go busy until the command has been sent (or an error). At this point, the CSR_SWIM_BLK_REMAINING register indicates that 128 bytes are still to be transmitted. The bytes are transmitted byte writing each byte to the CSR_SWIM_PUSH_BYTE register. At each byte, the TXBUSY goes high until done (or error), and the CSR_SWIM_BLK_REMAINING register is decremented. Register writes that instigate a SWIM interface communication will be ignored if the busy statuses are set, and so these must be checked before each write. More details on the SWIM interface protocol are at http://www.st.com/resource/en/reference_manual/cd00218714.pdf, and the STM8 MCU register map is defined in the STM8 datasheet at http://www.st.com/resource/en/datasheet/stm8l151k4.pdf", + "registers" : { + "ip_id" : { + "address" : "0", + "width" : "32", + "description" : "Unique ID for this IP block", + "fields": { + "ip_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0x0a", + "description" : "IP identifier number" + }, + "mfr_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0xff", + "description" : "Manufacturer number" + + }, + "reserved" : { + "type" : "rsv", + "bit_len" : "16", + "description" : "Reserved bits" + } + } + }, + + "ip_ver" : { + "address" : "1", + "width" : "32", + "description" : "Bits[7:0] = Minor version number, Bits[15:8] = Major version number, etc.", + "fields": { + "minor_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0x00", + "description" : "Minor number" + }, + "major_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0x03", + "description" : "Major number" + + }, + "reserved" : { + "type" : "rsv", + "bit_len" : "16", + "description" : "Reserved bits" + } + } + }, + "status" : { + "address" : "2", + "width" : "7", + "description" : "Status of interface", + "fields" : { + "rxbusy" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Receiver busy" + }, + "txbusy" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Transmitter busy (writing or sending entry sequence)" + }, + "rx_ready" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "A receive byte is ready for reading in rx_byte" + }, + "tx_error" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Attempt to send WOTF or SRST when interface is busy or timed out on retries or TX NAK error" + }, + "rx_error" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Attempt to send ROTF when interface is busy, or RX parity error" + }, + "tx_nak_error" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Failed to get an ACK or NAK in TX ACK slot" + }, + "entry_seq_sent" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Entry sequence has been sent." + } + } + }, + "control" : { + "address" : "3", + "width" : "14", + "description" : "Control of interface", + "fields" : { + "send_entry_seq" : { + "type" : "w0", + "bit_len" : "1", + "reset" : "0", + "description" : "Instigate the sending of an entry sequence" + }, + "high_speed" : { + "type" : "w", + "bit_len" : "1", + "reset" : "0", + "description" : "Set high speed" + }, + "clr_rx" : { + "type" : "w0", + "bit_len" : "1", + "reset" : "0", + "description" : "Clear the read byte" + }, + "send_srst" : { + "type" : "w0", + "bit_len" : "1", + "reset" : "0", + "description" : "Instigate the sending of a SRST" + }, + "tx_retry_timeout" : { + "type" : "w", + "bit_len" : "8", + "reset" : "10", + "description" : "Maximum retries on TX (0 is infinite)" + }, + "clear_errors" : { + "type" : "w0", + "bit_len" : "1", + "reset" : "0", + "description" : "Clear Tx and Rx errors" + }, + "reset_state" : { + "type" : "w0", + "bit_len" : "1", + "reset" : "0", + "description" : "Reset the interface's state" + } + } + }, + "tx_byte" : { + "address" : "4", + "width" : "32", + "description" : "Write byte", + "fields" : { + "byte" : { + "type" : "wp", + "bit_len" : "8", + "reset" : "0", + "description" : "Write byte value" + }, + "waddr" : { + "type" : "wp", + "bit_len" : "24", + "reset" : "0", + "description" : "Write address" + } + } + }, + "rx_byte" : { + "address" : "5", + "width" : "32", + "description" : "Read byte", + "fields" : { + "byte" : { + "type" : "wp", + "bit_len" : "8", + "reset" : "0", + "description" : "Read byte value. Don't care on write, valid value on read if rx_ready = 1" + }, + "raddr" : { + "type" : "wp", + "bit_len" : "24", + "reset" : "0", + "description" : "Read address" + } + } + }, + "tx_block" : { + "address" : "6", + "width" : "32", + "description" : "Instigate the transmission of a block of data (128 bytes), to destination location, starting at given address", + "fields" : { + "reserved" : { + "type" : "rsv", + "bit_len" : "8", + "description" : "reserved" + }, + "waddr" : { + "type" : "wp", + "bit_len" : "24", + "reset" : "0", + "description" : "Destination start address" + } + } + }, + "push_byte" : { + "address" : "7", + "width" : "8", + "type" : "wp", + "reset" : "0", + "description" : "Push next byte on block write. No effect if BLK_REMAINING register 0." + }, + "blk_remaining" : { + "address" : "8", + "width" : "8", + "type" : "r", + "reset" : "0", + "description" : "Remaining bytes left in block transfer. Stable if TXBUSY bit in STATUS reg is 0." + }, + "swim_reset_ctrl" : { + "address" : "9", + "width" : "1", + "type" : "w", + "reset" : "0", + "description" : "Control Swim Reset signal (1 - Hold STM8 in Reset, 0 - Normal Op). Was GPIO 44 nSwim_Reset to STM8." + } + } +}] diff --git a/generic_ip/swim/3/src/swim_3.vhd b/generic_ip/swim/3/src/swim_3.vhd new file mode 100644 index 0000000..b6646e6 --- /dev/null +++ b/generic_ip/swim/3/src/swim_3.vhd @@ -0,0 +1,867 @@ +------------------------------------------------------------------------------- +--! @file +--! @brief Generic STMicroelectronic's SWIM compliant master serial interface +--! +--! @details This block is a generic SWIM compliant interface capable of +--! interfacing to the STMicroelectronics STM8 MCU, or any other SWIM +--! compliant slave. It has an Avalon control and status slave bus, for access +--! to internal registers by the SoC processor, and a two wire SWIM serial +--! port, required to be combined at the top level as an open collector +--! single wire port. The interface has a transmitter and receiver, the +--! former of which can issue byte WOTF exchanges, or 128 byte WOTF exchanges. +--! The latter is capable only of single byte transfers. SWRST commands can also +--! be issued. All instigation of commands is done via writes to the internal +--! registers, and returned data read from status register. Details of the +--! register interface is given in auto-generated swim.txt build artifact. +--! The SWIM user document can be found at: +--! www.st.com/resource/en/user_manual/cd00173911.pdf +--! +--! @b Clock/resets: +--! @li clk : nominally 100MHz +--! @li reset_n : active low reset for clk (asynchronous assert) +--! +--! @b Interfaces: +--! @li Avalon MM slave avs_* : Control and status register memory mapped bus +--! @li SWIM master swim_* : SWIM serial interface as input (async) and output. +--! +--! @b Clock-crossing considerations: +--! @li swim_in : Asynchronous single bit input requires two register synchronisation +--! +--! @b Language build standard requirements: +--! @li VHDL 2008 +--! +--! @author Simon Southwell +--! @copyright (c) 2017, 2018 Global Inkjet Systems +--! @date 2017-12-18 +------------------------------------------------------------------------------- + +------------------------------------------------------------------------------- +-- Title : SWIM Block +-- Project : GMA +------------------------------------------------------------------------------- +-- File : swim_3.vhd +-- Author : Simon Southwell +-- Company : Global Inkjet Systems +-- Created : 2017-12-18 +-- Platform : +-- Standard : VHDL 2008 +------------------------------------------------------------------------------- +-- Description: +-- This block is the SWIM interface +------------------------------------------------------------------------------- +-- Copyright (c) 2017, 2018 Global Inkjet Systems +------------------------------------------------------------------------------- +-- altera vhdl_input_version vhdl_2008 + +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; +use ieee.std_logic_misc.all; + +entity swim_3 is + generic ( + CLK_FREQ_MHZ : integer := 100; + TEST_MODE : boolean := false + ); + port ( + clk : in std_logic; + reset_n : in std_logic; + + -- Avalon MM interface + avs_address : in std_logic_vector( 3 downto 0); + avs_write : in std_logic; + avs_writedata : in std_logic_vector(31 downto 0); + avs_read : in std_logic; + avs_readdata : out std_logic_vector(31 downto 0); + + swim_out : out std_logic; + swim_in : in std_logic; + swim_reset : out std_logic; + + -- Debug + dbg_port : out std_logic_vector(4 downto 0) + + ); +end entity swim_3; + +architecture rtl of swim_3 is + + -- Define local constants for bit-patterns and their lengths + constant LONG_ONE : std_logic_vector(10 downto 0) := "10011111111"; + constant LONG_ZERO : std_logic_vector(10 downto 0) := "10000000001"; + constant SHORT_ONE : std_logic_vector(10 downto 0) := "00000001111"; + constant SHORT_ZERO : std_logic_vector(10 downto 0) := "00000000001"; + + constant LONG_LENGTH : std_logic_vector( 3 downto 0) := 4x"B"; + constant SHORT_LENGTH : std_logic_vector( 3 downto 0) := 4x"5"; + + constant ENTRY_SEQ_LEN_M1 : integer := 28; + constant ENTRY_SEQ_PATTERN : std_logic_vector(ENTRY_SEQ_LEN_M1 downto 0) := "01001100110011001100101010101"; + + constant TX_SHIFT_REG_LEN_M1 : integer := 60; + constant WRITE_CMD_LEN_M1 : integer := TX_SHIFT_REG_LEN_M1; + constant READ_CMD_LEN_M1 : integer := 49; + constant BLK_CMD_LEN_M1 : integer := READ_CMD_LEN_M1; + constant BLK_BYTE_LEN_M1 : integer := 10; + constant SRST_CMD_LEN_M1 : integer := 5; + + constant RX_BIT_COUNT_THRESHOLD : integer := 62 * CLK_FREQ_MHZ / 100; -- Number relative to 100MHz default clock speed + constant SWIM_BITS_PER_BYTE : integer := 11; + + constant CLK_COUNT_4MHZ : integer := CLK_FREQ_MHZ/4; -- 15 for 60MHz, 20 for 80MHz and 25 for 100MHz + constant CLK4M_COUNT_4KHZ : integer := 1000; + constant CLK4M_LONG_COUNT_PER_BIT : integer := to_integer(unsigned(LONG_LENGTH)); + constant CLK4M_SHORT_COUNT_PER_BIT : integer := to_integer(unsigned(SHORT_LENGTH)); + + -- Internal signals for the register block connections + signal control_high_speed : std_logic; -- Data rate is high sppend (i.e. not low speed) + signal control_clr_rx : std_logic; -- Clear the RX byte (pulse) + signal control_send_entry_seq : std_logic; -- Start the transmission of the entry sequence (pulse) + signal control_tx_retry_timeout : std_logic_vector(7 downto 0); -- TX retry limit + signal control_reset_state : std_logic; -- Reset all TX/RX state (pulse) + signal control_send_srst : std_logic; -- Send a SWRST command (pulse) + signal control_clear_errors : std_logic; -- Clear errors (pulse) + + signal swim_reset_ctrl : std_logic; -- swim reset output + + signal tx_byte_pulse : std_logic; -- Pulse on tx_byte write + signal tx_byte_byte : std_logic_vector(7 downto 0); -- byte value for transmission + signal tx_byte_byte_in : std_logic_vector(7 downto 0); -- Readback value for tx_byte_byte + signal tx_byte_waddr : std_logic_vector(23 downto 0); -- Write address value for transmission + signal tx_byte_waddr_in : std_logic_vector(23 downto 0); -- Readback value for tx_byte_waddr + signal tx_byte_waddr_reg : std_logic_vector(23 downto 0); -- tx_byte waddr register value for readback + signal tx_byte_byte_reg : std_logic_vector( 7 downto 0); -- tx_byte byte register value for readback + + signal rx_byte_pulse : std_logic; -- Pulse on rx_byte write + signal rx_byte_byte_in : std_logic_vector( 7 downto 0); -- RX bye readback value + signal rx_byte_raddr : std_logic_vector(23 downto 0); -- Read address value for transmission + signal rx_byte_raddr_in : std_logic_vector(23 downto 0); -- Read address readback value + signal rx_byte_raddr_reg : std_logic_vector(23 downto 0); -- rx_byte raddr register value for readback + + signal status_rxbusy : std_logic; -- RX busy status input for reading + signal status_txbusy : std_logic; -- TX busy status input for reading + signal status_rx_ready : std_logic; -- RX ready with a byte for reading + signal status_tx_error : std_logic; -- TX error status input + signal status_rx_error : std_logic; -- RX error status input + signal status_tx_nak_error : std_logic; -- TX ACK/NAK status input + signal status_entry_seq_sent : std_logic; -- Entry sequence has been sent since last reset + + signal blk_remaining : std_logic_vector(7 downto 0); -- Count of remaining byte in block TX + signal tx_block_pulse : std_logic; + signal tx_block_waddr : std_logic_vector(23 downto 0); + signal tx_block_waddr_reg : std_logic_vector(23 downto 0); + signal push_byte_pulse : std_logic; + signal push_byte : std_logic_vector(7 downto 0); + + -- Signals for timing pulse generation + signal count_4KHz : std_logic_vector(9 downto 0); -- Counter to count 4MHz pulse for 4KHz timing + signal count_4mhz_for_4khz : std_logic_vector(9 downto 0); + signal count_4MHz : std_logic_vector(4 downto 0); -- Counter to count 100MHz clock for 4MHz timing + signal count_4Mhz_thresh : std_logic_vector(4 downto 0); + signal bit_count : std_logic_vector(4 downto 0); -- Counter to count 4MHz ticks for TX of return-to zero 0/1 + signal tick_4KHz : std_logic; -- Tick at 4KHz (100MHz pulse) + signal tick_4MHz : std_logic; -- Tick at 4MHz (100MHz pulse) + signal bit_clk : std_logic; -- Tick at end of each TX bit (for either high or low speed bits) + signal bit_rollover : std_logic_vector(4 downto 0); -- Count value for bit_count rollover dependent on high or low speed mode + signal active_hi_speed : std_logic; -- The active speed mode (control_high_speed, synced to bit period) + signal tick_active : std_logic; -- The active TX tick, either the 4KHz during entry seq, or 4MHz for transmission) + signal timeout : std_logic_vector(11 downto 0); -- Timeout counter + signal timed_out : std_logic; -- Timed out pulse + + -- Signals for transmission bit generation + signal tx_bits : std_logic_vector(12 downto 0); -- The bits for transmission, incl. header, parity and the ACK space + signal tx_bits_count : std_logic_vector(3 downto 0); -- The count of bits transmitted from tx_bits + signal op_active : std_logic; -- Flag for when the output is actively transmitting + signal tx_bit_active : std_logic; -- op_active, aligned to the bit_clk pulse + signal entry_active : std_logic; -- Flag set when entry sequence is in progress + signal tx_shift_reg : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- The TX output shift register. Holds up to an entire command, plus one byte + signal tx_shift_ack_reg : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Mirror shift register, marking the position of ACK slots + signal tx_wr_frame : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed WOTF command frame for loading into shift register + signal tx_wr_ack_pos : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed WOTF ack positions for loading into ack shift register + signal blk_wr_frame : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed WOTF block command frame for loading into shift register + signal blk_wr_ack_pos : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed WOTF ack positions for loading into ack shift register + signal push_wr_frame : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed WOTF byte for loading into shift register + signal tx_rd_frame : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed ROTF command frame for loading into shift register + signal tx_rd_ack_pos : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed ROTF ack positions for loading into ack shift register + signal sys_rst_frame : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed SWRST command frame for loading into shift register + signal sys_rst_ack_pos : std_logic_vector (TX_SHIFT_REG_LEN_M1 downto 0); -- Constructed SWRST ack positions for loading into ack shift register + + signal waddre_par : std_logic; -- Parity for upper most byte of write access address + signal waddrh_par : std_logic; -- Parity for mid byte of write access address + signal waddrl_par : std_logic; -- Parity for lowest most byte of write access address + signal raddre_par : std_logic; -- Parity for upper most byte of read access address + signal raddrh_par : std_logic; -- Parity for mid byte of read access address + signal raddrl_par : std_logic; -- Parity for lowest most byte of read access address + signal baddre_par : std_logic; -- Parity for upper most byte of block write access address + signal baddrh_par : std_logic; -- Parity for mid byte of block write access address + signal baddrl_par : std_logic; -- Parity for lowest most byte of block write access address + signal tx_byte_par : std_logic; -- Parity for the data byte + signal push_byte_par : std_logic; -- Parity for the pushed block data byte + signal tx_shift_count : std_logic_vector(5 downto 0); -- Count of bits sent from TX shift register + signal tx_shift_retry_pos : std_logic_vector(5 downto 0); -- State to hold retry position of shift register, for replay on NACK + signal tx_ack_slot : std_logic; -- Flag marking when in an ACK slot during transmission + signal tx_retry_count : std_logic_vector(7 downto 0); -- Count of retries since last successful transmission + signal seen_tx_ack_rising : std_logic; -- State flagging that a TX ACK rising edge has been seen + signal rx_ack_slot : std_logic; -- Flag marking when an RX ACK slot (aligned to 4MHz tick) + + -- Receiver signals + signal rx_shift_register : std_logic_vector(10 downto 0); -- RX input shift register + signal rx_falling_edge : std_logic; -- Pulse on input signal falling edge + signal rx_rising_edge : std_logic; -- Pulse on input signal rising edge + signal swim_in_last : std_logic; -- Cycle delayed version of (synced) swim input + signal rx_bit_count : std_logic_vector(9 downto 0); -- Count of received bits + signal rx_bit_count_thresh : std_logic_vector(9 downto 0); + signal receiving_bit : std_logic; -- Flag active when receiving a bit + signal rx_idx : std_logic_vector(3 downto 0); -- Count of cycles for low period of received bit + signal rx_parity : std_logic; -- Parity of the received byte + signal rx_expecting_byte : std_logic; -- Flag set when expecting a byte (after an ROTF command sent) + signal send_rx_ack : std_logic; -- Request to transmitter to send an ACK bit + signal send_rx_ack_data : std_logic; -- ACK data value to be transmitted + signal rx_ack_delay : std_logic_vector(3 downto 0); -- ACK delay + signal rx_nacked : std_logic; -- Flag if receiver NAKed + + signal swim_in_sync : std_logic_vector(1 downto 0); -- Synchroniser for SWIM input + + constant zero : std_logic_vector(31 downto 0) := 32x"00000000"; -- Convenience std_logic_vector value of 0 to save on conversions + constant one : std_logic_vector(31 downto 0) := 32x"00000001"; -- Convenience std_logic_vector value of 1 to save on conversions + +begin + + GEN_TEST_MODE : if TEST_MODE generate + count_4mhz_for_4khz <= std_logic_vector(to_unsigned(CLK4M_COUNT_4KHZ/100-1, 10)); + rx_bit_count_thresh <= std_logic_vector(to_unsigned(RX_BIT_COUNT_THRESHOLD/10, 10)); + count_4Mhz_thresh <= std_logic_vector(to_unsigned(CLK_COUNT_4MHZ/10-1, 5)); + end generate; + + GEN_NOT_TEST_MODE : if not TEST_MODE generate + count_4mhz_for_4khz <= std_logic_vector(to_unsigned(CLK4M_COUNT_4KHZ-1, 10)); + rx_bit_count_thresh <= std_logic_vector(to_unsigned(RX_BIT_COUNT_THRESHOLD, 10)); + count_4Mhz_thresh <= std_logic_vector(to_unsigned(CLK_COUNT_4MHZ-1, 5)); + end generate; + + -- Export for debug selected signals (up to 5) + dbg_port <= timed_out & status_rx_error & status_rxbusy & status_tx_nak_error & swim_in_sync(0) ; + + -- TX byte, and address from RX_address read back registered values + tx_byte_waddr_in <= tx_byte_waddr_reg; + tx_byte_byte_in <= tx_byte_byte_reg; + rx_byte_raddr_in <= rx_byte_raddr_reg; + + -- Readback of RX byte field gives received bytes from RX shift register. + -- Valid only of status_rx_ready is set + rx_byte_byte_in <= rx_shift_register(9 downto 2); + + -- Auto-generated register block + swim_csr_regs_inst : entity work.swim_3_csr_regs + generic map ( + ADDR_DECODE_WIDTH => 4 + ) + port map ( + -- Clock and reset + clk => clk, + rst_n => reset_n, + + -- Internal signal ports + control_high_speed => control_high_speed, + control_clr_rx => control_clr_rx, + control_send_entry_seq => control_send_entry_seq, + control_tx_retry_timeout => control_tx_retry_timeout, + control_send_srst => control_send_srst, + control_clear_errors => control_clear_errors, + control_reset_state => control_reset_state, + swim_reset_ctrl => swim_reset_ctrl, + tx_byte_pulse => tx_byte_pulse, + tx_byte_byte => tx_byte_byte, + tx_byte_byte_in => tx_byte_byte_in, + tx_byte_waddr => tx_byte_waddr, + tx_byte_waddr_in => tx_byte_waddr_in, + rx_byte_pulse => rx_byte_pulse, + rx_byte_byte => open, + rx_byte_byte_in => rx_byte_byte_in, + rx_byte_raddr => rx_byte_raddr, + rx_byte_raddr_in => rx_byte_raddr_in, + status_rxbusy => status_rxbusy, + status_txbusy => status_txbusy, + status_rx_ready => status_rx_ready, + status_tx_error => status_tx_error, + status_rx_error => status_rx_error, + status_tx_nak_error => status_tx_nak_error, + status_entry_seq_sent => status_entry_seq_sent, + + tx_block_pulse => tx_block_pulse, + tx_block_waddr => tx_block_waddr, + tx_block_waddr_in => tx_block_waddr_reg, + push_byte_pulse => push_byte_pulse, + push_byte => push_byte, + push_byte_in => (others => '0'), + blk_remaining => blk_remaining, + + -- Slave bus port + avs_address => avs_address, + avs_write => avs_write, + avs_writedata => avs_writedata, + avs_read => avs_read, + avs_readdata => avs_readdata + ); + + -- Signal to flag the bit counter rollover point, depending on speed + bit_rollover <= std_logic_vector(to_unsigned(CLK4M_LONG_COUNT_PER_BIT-1, 5)) when active_hi_speed = '0' else + std_logic_vector(to_unsigned(CLK4M_SHORT_COUNT_PER_BIT-1, 5)); + + -- Flag TX as busy to the Avalon interface for bit transmission or entry activation. + status_txbusy <= op_active or tx_bit_active or entry_active; + + -- Flag the RX as busy after sending a read command and yet to receive a byte. + status_rxbusy <= rx_expecting_byte; + + ----------------------------------------- + -- Process to generate the timing signals + ----------------------------------------- + SWIM_CLKS_PROC : process(clk, reset_n) is + begin + + if reset_n = '0' then + + count_4KHz <= (others => '0'); + count_4MHz <= (others => '0'); + bit_count <= (others => '0'); + tick_4KHz <= '0'; + tick_4MHz <= '0'; + bit_clk <= '0'; + active_hi_speed <= '0'; + timeout <= (others => '0'); + timed_out <= '0'; + + elsif (clk'event and clk = '1') then + + tick_4KHz <= '0'; + timed_out <= '0'; + + -- Generate a 4MHz tick + if count_4MHz = count_4Mhz_thresh then + count_4MHz <= (others => '0'); + else + count_4MHz <= std_logic_vector(unsigned(count_4MHz) + 1); + end if; + + if (count_4MHz = zero(4 downto 0)) then + tick_4MHz <= '1'; + else + tick_4MHz <= '0'; + end if; + + -- Generate a 4KHz tick + if (tick_4MHz = '1') then + + if count_4KHz = count_4mhz_for_4khz then + count_4KHz <= (others => '0'); + else + count_4KHz <= std_logic_vector(unsigned(count_4KHz) + 1); + end if; + + if (tick_4MHz = '1' and count_4KHz = zero(9 downto 0)) then + tick_4KHz <= '1'; + else + tick_4KHz <= '0'; + end if; + + -- At the bit clock counter rollover point, clear the counter back to 0 + -- and update some state + if (bit_count = bit_rollover) then + + bit_count <= (others => '0'); + + -- Update the active hi speed mode from the Avalon interface only + -- at the bit counter boundary to prevent a change mid-way through a bit + -- transmission. + if (op_active = '0') then + active_hi_speed <= control_high_speed; + end if; + else + -- When not at rollover, unconditionally increment the count + -- for the bit clock. + bit_count <= std_logic_vector(unsigned(bit_count) + 1); + end if; + + end if; + + -- Generate a pulse at the count rollover point + if (count_4MHz = zero(4 downto 0) and bit_count = bit_rollover) then + bit_clk <= '1'; + else + bit_clk <= '0'; + end if; + + if tick_4KHz = '1' then + if status_rxbusy = '1' or status_txbusy = '1' then + timeout <= std_logic_vector(unsigned(timeout) + 1); + else + timeout <= (others => '0'); + end if; + + if timeout = 12x"fff" then + timed_out <= '1'; + end if; + end if; + + + end if; + + end process; + + ----------------------------------------- + -- Process to select the TX bit pattern + ----------------------------------------- + SWIM_ONE_OR_ZERO : process(all) is + begin + tx_bits(0) <= '1'; + + if (active_hi_speed = '0') then + if tx_shift_reg(to_integer(unsigned(tx_shift_count))) = '1' then + tx_bits(11 downto 1) <= LONG_ONE; + else + tx_bits(11 downto 1) <= LONG_ZERO; + end if; + else + if (tx_shift_reg(to_integer(unsigned(tx_shift_count))) = '1') then + tx_bits(11 downto 1) <= SHORT_ONE; + else + tx_bits(11 downto 1) <= SHORT_ZERO; + end if; + end if; + end process; + + -- Calculate the command parity values + waddre_par <= XOR_REDUCE(tx_byte_waddr(23 downto 16)); + waddrh_par <= XOR_REDUCE(tx_byte_waddr(15 downto 8)); + waddrl_par <= XOR_REDUCE(tx_byte_waddr( 7 downto 0)); + baddre_par <= XOR_REDUCE(tx_block_waddr(23 downto 16)); + baddrh_par <= XOR_REDUCE(tx_block_waddr(15 downto 8)); + baddrl_par <= XOR_REDUCE(tx_block_waddr( 7 downto 0)); + raddre_par <= XOR_REDUCE(rx_byte_raddr(23 downto 16)); + raddrh_par <= XOR_REDUCE(rx_byte_raddr(15 downto 8)); + raddrl_par <= XOR_REDUCE(rx_byte_raddr( 7 downto 0)); + tx_byte_par <= XOR_REDUCE(tx_byte_byte); + push_byte_par <= XOR_REDUCE(push_byte); + + -- Construct the various command bit patterns + tx_wr_frame <= '0' & "010" & '1' & '1' & -- CMD + '0' & "00000001" & '1' & '1' & -- N (1 byte) + '0' & tx_byte_waddr(23 downto 16) & waddre_par & '1' & -- ADDR @E + '0' & tx_byte_waddr(15 downto 8) & waddrh_par & '1' & -- ADDR @H + '0' & tx_byte_waddr( 7 downto 0) & waddrl_par & '1' & -- ADDR @L + '0' & tx_byte_byte & tx_byte_par & '1'; -- D0 + + tx_rd_frame <= "00000000000" & -- Padding + '0' & "001" & '1' & '1' & -- CMD + '0' & "00000001" & '1' & '1' & -- N (1 byte) + '0' & rx_byte_raddr(23 downto 16) & raddre_par & '1' & -- ADDR @E + '0' & rx_byte_raddr(15 downto 8) & raddrh_par & '1' & -- ADDR @H + '0' & rx_byte_raddr( 7 downto 0) & raddrl_par & '1'; -- ADDR @L + + blk_wr_frame <= "00000000000" & -- Padding + '0' & "010" & '1' & '1' & -- CMD + '0' & "10000000" & '1' & '1' & -- N (128 bytes) + '0' & tx_block_waddr(23 downto 16) & baddre_par & '1' & -- ADDR @E + '0' & tx_block_waddr(15 downto 8) & baddrh_par & '1' & -- ADDR @H + '0' & tx_block_waddr( 7 downto 0) & baddrl_par & '1'; -- ADDR @L + + push_wr_frame <= "00000000000000000000000000000000000000000000000000"& -- Padding + '0' & push_byte & push_byte_par & '1'; -- Dn + + sys_rst_frame <= "0000000000000000000000000000000000000000000000000000000" & + '0' & "000" & '0' & '1'; -- CMD + + tx_wr_ack_pos <= "0000010000000000100000000001000000000010000000000100000000001"; + tx_rd_ack_pos <= "0000000000000000100000000001000000000010000000000100000000001"; + sys_rst_ack_pos <= "0000000000000000000000000000000000000000000000000000000000001"; + blk_wr_ack_pos <= tx_rd_ack_pos; + + + -- Select which timing pulse to use for transmission + -- depending on the active mode. + tick_active <= tick_4KHz when entry_active = '1' else + bit_clk when tx_bit_active = '1' else '0'; + + tx_ack_slot <= tx_shift_ack_reg(to_integer(unsigned(tx_shift_count))) and tx_bit_active; + + ----------------------------------------- + -- Process to generate transmission output + ----------------------------------------- + TX_BITS_PROC : process(clk, reset_n) is + + procedure rst_tx is + begin + tx_bits_count <= (others => '0'); + swim_out <= '1'; + op_active <= '0'; + tx_bit_active <= '0'; + seen_tx_ack_rising <= '0'; + entry_active <= '0'; + tx_shift_reg <= (others => '0'); + tx_shift_ack_reg <= (others => '0'); + tx_shift_count <= (others => '0'); + tx_shift_retry_pos <= (others => '0'); + tx_retry_count <= (others => '0'); + status_tx_error <= '0'; + status_tx_nak_error <= '0'; + swim_in_sync <= "11"; + tx_byte_waddr_reg <= (others => '0'); + tx_byte_byte_reg <= (others => '0'); + rx_byte_raddr_reg <= (others => '0'); + blk_remaining <= (others => '0'); + tx_block_waddr_reg <= (others => '0'); + rx_ack_slot <= '0'; + + end procedure; + + begin + if reset_n = '0' then + -- Reset state + rst_tx; + + -- State not cleared by a 'reset state' control register write + status_entry_seq_sent <= '0'; + + elsif (clk'event and clk = '1') then + + swim_in_sync <= swim_in & swim_in_sync(1); + + -- Clear all the TX state at the request over the Avalon slave interface + if control_reset_state = '1' then + rst_tx; + else + + -- Flag that a rising edge has been seen during a TX ACK slot. + seen_tx_ack_rising <= seen_tx_ack_rising or (tx_ack_slot and rx_rising_edge); + + -- Once the ack bit gone, clear any RX ACK slot + if tx_bits_count = zero(3 downto 0) then + rx_ack_slot <= '0'; + end if; + + -- Request from receiver for an RX ack + if (send_rx_ack = '1') then + tx_shift_reg(0) <= send_rx_ack_data; + tx_shift_ack_reg(0) <= '0'; + rx_ack_slot <= '1'; + if active_hi_speed = '0' then + tx_bits_count <= LONG_LENGTH; + else + tx_bits_count <= SHORT_LENGTH; + end if; + end if; + + -- If timed out whilst transmitting, flag an error + if status_txbusy = '1' and timed_out = '1' then + status_tx_error <= '1'; + end if; + + -- Update the transmission bit count only on the 4MHz tick pulse + if (tick_4MHz = '1') then + + -- Load the TX bit indexer with the length when counter 1 (whilst still active) or 0 (when starting up) + if (tx_bits_count <= one(3 downto 0) and tx_bit_active = '1') then + + -- The count of the transmission bits is dependent on the speed mode + if active_hi_speed = '0' then + tx_bits_count <= LONG_LENGTH; + else + tx_bits_count <= SHORT_LENGTH; + end if; + + -- If the TX shift register count is about the go to 0, clear the op_active state + if (tx_shift_count = zero(5 downto 0)) then + op_active <= '0'; + end if; + + -- If the TX bit counter is not zero, keep decrementing to the end of the bit + elsif (tx_bits_count /= zero(3 downto 0)) then + tx_bits_count <= std_logic_vector(unsigned(tx_bits_count) - 1); + end if; + end if; + + -- Generate an active TX state, aligned to the bit clock pulse. + if bit_clk = '1' then + tx_bit_active <= op_active; + end if; + + -- On active TX clock, decrement the shift counter until 0 + if tick_active = '1' then + seen_tx_ack_rising <= '0'; + if tx_shift_count /= zero(5 downto 0) then + tx_shift_count <= std_logic_vector(unsigned(tx_shift_count) - 1); + else + -- Clear the entry status, and export that the entry sequence is sent. + entry_active <= '0'; + if entry_active = '1' then + status_entry_seq_sent <= '1'; + end if; + end if; + end if; + + -- If interface is idle, check for new tx/rx activations from the Avalon interface + if op_active = '0' and entry_active = '0' then + + if tx_byte_pulse = '1' then + tx_shift_reg <= tx_wr_frame; + tx_shift_ack_reg <= tx_wr_ack_pos; + tx_shift_count <= std_logic_vector(to_unsigned(WRITE_CMD_LEN_M1, 6)); + tx_shift_retry_pos <= std_logic_vector(to_unsigned(WRITE_CMD_LEN_M1, 6)); + tx_byte_waddr_reg <= tx_byte_waddr; + tx_byte_byte_reg <= tx_byte_byte; + op_active <= '1'; + end if; + + if rx_byte_pulse = '1' then + tx_shift_reg <= tx_rd_frame; + tx_shift_count <= std_logic_vector(to_unsigned(READ_CMD_LEN_M1, 6)); + tx_shift_retry_pos <= std_logic_vector(to_unsigned(READ_CMD_LEN_M1, 6)); + tx_shift_ack_reg <= tx_rd_ack_pos; + rx_byte_raddr_reg <= rx_byte_raddr; + op_active <= '1'; + end if; + + if tx_block_pulse = '1' then + tx_shift_reg <= blk_wr_frame; + tx_shift_count <= std_logic_vector(to_unsigned(BLK_CMD_LEN_M1, 6)); + tx_shift_retry_pos <= std_logic_vector(to_unsigned(BLK_CMD_LEN_M1, 6)); + tx_shift_ack_reg <= blk_wr_ack_pos; + tx_block_waddr_reg <= tx_block_waddr; + blk_remaining <= std_logic_vector(to_unsigned(128, 8)); + op_active <= '1'; + end if; + + if push_byte_pulse = '1' and blk_remaining /= 8x"00" then + tx_shift_reg <= push_wr_frame; + tx_shift_count <= std_logic_vector(to_unsigned(BLK_BYTE_LEN_M1, 6)); + tx_shift_retry_pos <= std_logic_vector(to_unsigned(BLK_BYTE_LEN_M1, 6)); + tx_shift_ack_reg <= blk_wr_ack_pos; + blk_remaining <= std_logic_vector(unsigned(blk_remaining) - 1); + op_active <= '1'; + end if; + + if (control_send_srst = '1') then + tx_shift_reg <= sys_rst_frame; + tx_shift_ack_reg <= sys_rst_ack_pos; + tx_shift_count <= std_logic_vector(to_unsigned(SRST_CMD_LEN_M1, 6)); + tx_shift_retry_pos <= std_logic_vector(to_unsigned(SRST_CMD_LEN_M1, 6)); + op_active <= '1'; + end if; + + if (control_send_entry_seq = '1') then + entry_active <= '1'; + tx_shift_reg(ENTRY_SEQ_LEN_M1 downto 0) <= ENTRY_SEQ_PATTERN; + tx_shift_count <= std_logic_vector(to_unsigned(ENTRY_SEQ_LEN_M1, 6)); + end if; + end if; + + -- When in an ack slot, at the offset bit clock, check the ACK status + if tx_ack_slot = '1' and tick_active = '1' then + if seen_tx_ack_rising = '1' then + -- if a NACK, put the shift count back to the last retry position + if rx_shift_register(0) = '0' then + tx_shift_count <= tx_shift_retry_pos; + tx_retry_count <= std_logic_vector(unsigned(tx_retry_count) + 1); + else + tx_shift_retry_pos <= std_logic_vector(unsigned(tx_shift_count) - 1); + tx_retry_count <= (others => '0'); + end if; + else + status_tx_nak_error <= '1'; + status_tx_error <= '1'; + end if; + end if; + + -- Check for retry timeout, if enabled + if control_tx_retry_timeout /= zero(7 downto 0) and + tx_retry_count = control_tx_retry_timeout then + status_tx_error <= '1'; + op_active <= '0'; + tx_bit_active <= '0'; + elsif control_clear_errors = '1' then + status_tx_error <= '0'; + status_tx_nak_error <= '0'; + end if; + + -- The SWIM output is either the entry sequence or the TX bits, depending + -- on which output is active. Set the output to 1 when TX is idle. + if entry_active = '1' then + swim_out <= tx_shift_reg(to_integer(unsigned(tx_shift_count))); + elsif tx_bit_active = '1' or rx_ack_slot = '1' then + swim_out <= tx_bits(to_integer(unsigned(tx_bits_count))) or tx_ack_slot; + else + swim_out <= '1'; + end if; + end if; + end if; + end process; + + ----------------------------------------- + -- Process for receiver input + ----------------------------------------- + + -- Generate a pulse at the rising and falling of the SWIM input + rx_falling_edge <= not swim_in_sync(0) and swim_in_last; + rx_rising_edge <= swim_in_sync(0) and not swim_in_last; + + -- Calculate the RX parity from the shift register + rx_parity <= XOR_REDUCE(rx_shift_register(7 downto 0)); + + RX_BITS_PROC : process(clk, reset_n) is + + procedure rst_tx is + begin + + swim_in_last <= '1'; + receiving_bit <= '0'; + rx_bit_count <= (others => '0'); + rx_idx <= 4x"0A"; + status_rx_error <= '0'; + rx_expecting_byte <= '0'; + status_rx_ready <= '0'; + send_rx_ack <= '0'; + rx_ack_delay <= (others => '0'); + rx_nacked <= '0'; + + end procedure; + + variable rx_bit_var : std_logic; + + begin + if reset_n = '0' then + -- Reset state + rst_tx; + elsif (clk'event and clk = '1') then + + -- Clear all the RX state if requested + if control_reset_state = '1' then + rst_tx; + else + + -- Signal is a pulse + send_rx_ack <= '0'; + + -- Generate a delayed swim_in for use in edge detection; + swim_in_last <= swim_in_sync(0); + + -- Flag as receiving a bit between rising and falling edges, + -- when expecting a byte and TX is idle + receiving_bit <= (receiving_bit or rx_falling_edge) and not rx_rising_edge and + (rx_expecting_byte or (tx_ack_slot and tx_bit_active)) and + not entry_active; + + -- Flag that we're expecting a byte whenever an RX command is sent, + -- but flag an error if already busy + if rx_byte_pulse = '1' then + if rx_expecting_byte = '1' then + status_rx_error <= '1'; + else + rx_expecting_byte <= '1'; + end if; + -- Always clear rx_expecting_byte state when seen an error. + elsif status_rx_error = '1' then + rx_expecting_byte <= '0'; + end if; + + -- If timed out waiting for a byte + if rx_expecting_byte = '1' and timed_out = '1' then + status_rx_error <= '1'; + end if; + + -- Whilst receiving a bit, increment the bit counter + if receiving_bit = '1' then + rx_bit_count <= std_logic_vector(unsigned(rx_bit_count) + 1); + end if; + + -- If we see a rising edge on the swim input, determine if + -- it is a one or a zero dependent on the rx_bit count since + -- the rising edge. A long count is a zero, a short count is a 1. + if rx_rising_edge = '1' then + + if unsigned(rx_bit_count) > unsigned(rx_bit_count_thresh) then + rx_bit_var := '0'; + else + rx_bit_var := '1'; + end if; + + -- Shift the bit onto the shift register + rx_shift_register <= rx_shift_register(9 downto 0) & rx_bit_var; + + if receiving_bit = '1' and tx_bit_active = '0' then + + -- If the bits index is not yet reached zero, keep decrementing, + -- otherwise reset to count the bits for a RX byte, and clear the receiver + -- active state + if unsigned(rx_idx) > 0 then + rx_idx <= std_logic_vector(unsigned(rx_idx) - 1); + else + rx_idx <= std_logic_vector(to_unsigned(SWIM_BITS_PER_BYTE, 4)); + rx_expecting_byte <= rx_nacked; -- Still expecting a byte if NAKed + status_rx_ready <= not rx_nacked; -- Byte ready if ACKed (not NAKed) + rx_nacked <= '0'; -- Clear naked state for retry. + end if; + + -- Last bit in byte + if rx_idx = one(3 downto 0) then + -- See out the rest of the bit slot after the parity bit is received + if rx_bit_var = '1' then + rx_ack_delay <= 4x"A"; + else + rx_ack_delay <= 4x"1"; + end if; + + -- Check for parity errors + if rx_parity /= rx_bit_var then + send_rx_ack_data <= '0'; + rx_nacked <= '1'; + else + send_rx_ack_data <= '1'; + rx_nacked <= '0'; + end if; + end if; + end if; + + -- At the boundary of each bit, reset the bit counter + rx_bit_count <= (others => '0'); + end if; + + -- At 4MHz boundaries, whilst the RX ACK delay is not zero, decrement it. + if tick_4MHz = '1' and rx_ack_delay /= zero(3 downto 0) then + rx_ack_delay <= std_logic_vector(unsigned(rx_ack_delay) - 1); + + -- When about to go to zero, request the TX to send the ACK/NAK + if rx_ack_delay = one(3 downto 0) then + send_rx_ack <= '1'; + end if; + end if; + + -- Clear RX error if requested + if control_clear_errors = '1' then + status_rx_error <= '0'; + end if; + + -- Clear RX status if requested + if control_clr_rx = '1' then + status_rx_ready <= '0'; + end if; + end if; + end if; + end process; + + SWIM_RESET_PROC : process(clk, reset_n) is + begin + if reset_n = '0' then + swim_reset <= '0'; + elsif (clk'event and clk = '1') then + swim_reset <= '0'; + + if swim_reset_ctrl = '1' then + swim_reset <= '1'; + end if; + + end if; + end process; + + + end architecture rtl; diff --git a/generic_ip/swim/versions.txt b/generic_ip/swim/versions.txt new file mode 100644 index 0000000..a27106a --- /dev/null +++ b/generic_ip/swim/versions.txt @@ -0,0 +1 @@ +v3 - fpga vendor independent. Adds "swim_reset_ctrl" to drive STM reset. Was driven by Altera GPIO 44 which is not present on Xilinx Kria \ No newline at end of file