From 0939e96168ed3df3dcf8c74471a4dede9a75fc4e Mon Sep 17 00:00:00 2001 From: Simon Southwell Date: Wed, 10 Jun 2020 06:48:45 +0100 Subject: [PATCH] Adding new multiple channel uart based STM8 alert receiver --- .../0/doc/multi_alert_uart_rx_0.json | 117 ++++++++ .../0/src/multi_alert_uart_rx_0.vhd | 263 ++++++++++++++++++ .../0/src/uart_1mbd_rx_0.vhd | 118 ++++++++ 3 files changed, 498 insertions(+) create mode 100644 generic_ip/multi_alert_uart_rx/0/doc/multi_alert_uart_rx_0.json create mode 100644 generic_ip/multi_alert_uart_rx/0/src/multi_alert_uart_rx_0.vhd create mode 100644 generic_ip/multi_alert_uart_rx/0/src/uart_1mbd_rx_0.vhd diff --git a/generic_ip/multi_alert_uart_rx/0/doc/multi_alert_uart_rx_0.json b/generic_ip/multi_alert_uart_rx/0/doc/multi_alert_uart_rx_0.json new file mode 100644 index 0000000..d9a45ef --- /dev/null +++ b/generic_ip/multi_alert_uart_rx/0/doc/multi_alert_uart_rx_0.json @@ -0,0 +1,117 @@ +[{ + "ip_name" : "multi_alert_uart_rx_0", + "bus" : "csr", + "addr_width" : "4", + "description" : "This block is the multi alter 1MBaud UART interface for alerts", + "notes" : "", + "registers" : { + "ip_id" : { + "address" : "0", + "width" : "32", + "description" : "Unique ID for this IP block", + "fields": { + "ip_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0x22", + "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" : "0x00", + "description" : "Major number" + + }, + "reserved" : { + "type" : "rsv", + "bit_len" : "16", + "description" : "Reserved bits" + } + } + }, + "chan_en" : { + "address" : "2", + "width" : "16", + "type" : "w", + "reset" : "0", + "description" : "Enable UART rx channels" + }, + "alert_status" : { + "address" : "3", + "width" : "25", + "description" : "Alert sttaus register", + "fields" : { + "over_range" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Over range trigger status" + }, + "over_range_rtn" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Over range return trigger status" + }, + "under_range" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Under range trigger status" + }, + "under_range_rtn" : { + "type" : "r", + "bit_len" : "1", + "reset" : "0", + "description" : "Underrange return trigger status" + }, + "alert_src" : { + "type" : "r", + "bit_len" : "16", + "reset" : "0", + "description" : "Alerts source status bit map (sticky)" + }, + "last_ordinal" : { + "type" : "r", + "bit_len" : "5", + "reset" : "0", + "description" : "Last ordninal received" + } + + } + }, + "clr_alert" : { + "address" : "4", + "width" : "1", + "type" : "w0", + "reset" : "0", + "description" : "Writing 1 to this register clears any alert status." + } + } +}] \ No newline at end of file diff --git a/generic_ip/multi_alert_uart_rx/0/src/multi_alert_uart_rx_0.vhd b/generic_ip/multi_alert_uart_rx/0/src/multi_alert_uart_rx_0.vhd new file mode 100644 index 0000000..dc3f821 --- /dev/null +++ b/generic_ip/multi_alert_uart_rx/0/src/multi_alert_uart_rx_0.vhd @@ -0,0 +1,263 @@ +------------------------------------------------------------------------------- +--! @file +--! @brief SOM FPGA STM8 alert code receiver 1MBaud UART +--! +--! @details This block is the 1MBaud UART receiver for receiving STM8 alert +--! codes and presenting them to the CPU via a receive register, and also for +--! generation of trigger pulse on code reception, with enable registers to +--! control which codes generate triggers. See +--! https://confluence.gisapps.co.uk/pages/viewpage.action?spaceKey=GMAHMB&title=HMB-FD-GMA+Design+Documentation +--! +--! @b Clock/resets: +--! @li clk : nominally 100MHz +--! @li rst_n : active low reset for clk (asynchronous assert) +--! +--! @b Interfaces: +--! @li Avalon MM slave avs_* : Control and status register memory mapped bus +--! @li UART_RXD : 1MBaud RS232 receiver interface +--! @li triggers : Trigger pulse output interface +--! +--! @b Clock-crossing considerations: +--! @li Module has UART_RXD as an asynchronous input, sync'd to clk in logic. +--! +--! @b Language build standard requirements: +--! @li VHDL 2008 +--! +--! @author Simon Southwell +--! @copyright (c) 2020 Global Inkjet Systems +--! @date 2020-06-08 +------------------------------------------------------------------------------- +-- altera vhdl_input_version vhdl_2008 +-- multi_alert_uart_rx_0.vhd + + +library IEEE; +use IEEE.std_logic_1164.all; +use IEEE.numeric_std.all; + +use work.multi_alert_uart_rx_0_pkg.all; + +entity multi_alert_uart_rx_0 is + + generic ( + CLK_FREQ_MHZ : integer := 100; + NUM_CHANS : integer := 12; + MAX_CHANS : integer := 16 + ); + port ( + clk : in std_logic; + rst_n : in std_logic; + + UART_RXD : in std_logic_vector(NUM_CHANS-1 downto 0); + + triggers : out std_logic_vector( 3 downto 0); + + avs_csr_address : in std_logic_vector( 3 downto 0); + avs_csr_read : in std_logic; + avs_csr_write : in std_logic; + avs_csr_writedata : in std_logic_vector(31 downto 0); + avs_csr_readdata : out std_logic_vector(31 downto 0) + + ); +end entity multi_alert_uart_rx_0; + +architecture rtl of multi_alert_uart_rx_0 is + + type array_t is array (natural range <>) of std_logic_vector; + + constant OVR_RNG_TRIG_BIT : integer := 0; + constant OVR_RNG_RTN_TRIG_BIT : integer := 1; + constant UNDR_RNG_TRIG_BIT : integer := 2; + constant UNDR_RNG_RTN_TRIG_BIT : integer := 3; + + constant OVR_RNG_CODE : std_logic_vector(2 downto 0) := 3x"3"; + constant OVR_RNG_RTN_CODE : std_logic_vector(2 downto 0) := 3x"4"; + constant UND_RNG_CODE : std_logic_vector(2 downto 0) := 3x"5"; + constant UND_RNG_RTN_CODE : std_logic_vector(2 downto 0) := 3x"6"; + + subtype ALERT_CODE_BITS is natural range 7 downto 5; + subtype ALERT_ORDINAL_BITS is natural range 4 downto 0; + + signal clr_alert : std_logic; + + signal chan_en : std_logic_vector(MAX_CHANS-1 downto 0); + + signal rx_data : std_logic_vector( 7 downto 0); + signal data_strobe : std_logic; + + signal rx_valid : std_logic_vector(NUM_CHANS-1 downto 0); + signal rx_byte : array_t(0 to NUM_CHANS-1)(7 downto 0); + + signal rx_byte_hold : array_t(0 to NUM_CHANS-1)(7 downto 0); + signal rx_valid_hold : std_logic_vector(NUM_CHANS-1 downto 0); + + signal ip_sel : std_logic_vector(NUM_CHANS-1 downto 0); + + signal alert_src : std_logic_vector(MAX_CHANS-1 downto 0); + signal last_ordinal : std_logic_vector(4 downto 0); + + signal triggers_int : std_logic_vector( 3 downto 0); + +begin + + ------------------------------------------------- + -- Local registers + ------------------------------------------------- + + multi_alert_uart_rx_0_csr_regs_inst : entity work.multi_alert_uart_rx_0_csr_regs + generic map( + ADDR_DECODE_WIDTH => 4 + ) + port map ( + -- Clock and reset + clk => clk, + rst_n => rst_n, + + chan_en => chan_en, + + clr_alert => clr_alert, + + alert_status_over_range => triggers_int(OVR_RNG_TRIG_BIT), + alert_status_over_range_rtn => triggers_int(OVR_RNG_RTN_TRIG_BIT), + alert_status_under_range => triggers_int(UNDR_RNG_TRIG_BIT), + alert_status_under_range_rtn => triggers_int(UNDR_RNG_RTN_TRIG_BIT), + alert_status_alert_src => alert_src, + alert_status_last_ordinal => last_ordinal, + + avs_address => avs_csr_address, + avs_write => avs_csr_write, + avs_writedata => avs_csr_writedata, + avs_read => avs_csr_read, + avs_readdata => avs_csr_readdata + + ); + + ------------------------------------------------- + -- UART receiver + ------------------------------------------------- + + GEN_UART : for i in 0 to NUM_CHANS-1 generate + + uart_1mbd_rx_inst : entity work.uart_1mbd_rx_0 + generic map ( + CLK_FREQ_MHZ => CLK_FREQ_MHZ + ) + port map ( + clk => clk, + rst_n => rst_n, + + uart_rxd => UART_RXD(i), + + rx_byte => rx_byte(i), + rx_valid => rx_valid(i) + ); + + end generate; + + ------------------------------------------------- + -- Combinatorial logic + ------------------------------------------------- + + -- Export triggers + triggers <= triggers_int; + + -- Flag if a valid byte is ready + data_strobe <= '1' when unsigned(ip_sel and rx_valid_hold) /= 0 else '0'; + + -- Get the selected RX byte value + process(all) + begin + rx_data <= (others => '0'); + + for i in 0 to NUM_CHANS-1 loop + if ip_sel(i) = '1' then + rx_data <= rx_byte_hold(i); + end if; + end loop; + end process; + + ------------------------------------------------- + -- Synchronous logic + ------------------------------------------------- + + SYNC_LOGIC : process (clk, rst_n) + begin + if rst_n = '0' then + + triggers_int <= (others => '0'); + alert_src <= (others => '0'); + rx_valid_hold <= (others => '0'); + ip_sel <= std_logic_vector(to_unsigned(1, ip_sel'length)); + last_ordinal <= (others => '0'); + + elsif clk'event and clk = '1' then + + ip_sel <= ip_sel(ip_sel'left-1 downto 0) & ip_sel(ip_sel'left); + + for i in 0 to NUM_CHANS-1 loop + + -- Clear the hold register valid when round-robin selects this channel + if ip_sel(i) = '1' then + rx_valid_hold(i) <= '0'; + end if; + + -- When a byte arrives from the UART for this channel latch the + -- byte and mark hold register as valid. Priority must be given + -- for this over ip_sel clearing so the byte is not dropped + -- accidentally. + if rx_valid(i) = '1' then + rx_valid_hold(i) <= chan_en(i); + rx_byte_hold(i) <= rx_byte(i); + end if; + + end loop; + + -- Default the return trigger outputs to 0 + triggers_int(OVR_RNG_RTN_TRIG_BIT) <= '0'; + triggers_int(UNDR_RNG_RTN_TRIG_BIT) <= '0'; + + -- Process the byte code when the strobe active + if data_strobe = '1' then + + -- When over-range, set the over-range triggers output if enabled at ordinal + if rx_data(ALERT_CODE_BITS) = OVR_RNG_CODE then + triggers_int(OVR_RNG_TRIG_BIT) <= '1'; + alert_src <= alert_src or std_logic_vector(resize(unsigned(ip_sel), MAX_CHANS)); + last_ordinal <= rx_data(ALERT_ORDINAL_BITS); + end if; + + -- When over-range return, set the over-range return triggers output if enabled at ordinal + if rx_data(ALERT_CODE_BITS) = OVR_RNG_RTN_CODE then + triggers_int(OVR_RNG_RTN_TRIG_BIT) <= '1'; + triggers_int(OVR_RNG_TRIG_BIT) <= '0'; + alert_src <= alert_src or std_logic_vector(resize(unsigned(ip_sel), MAX_CHANS)); + last_ordinal <= rx_data(ALERT_ORDINAL_BITS); + end if; + + -- When under-range, set the under-range triggers output if enabled at ordinal + if rx_data(ALERT_CODE_BITS) = UND_RNG_CODE then + triggers_int(UNDR_RNG_TRIG_BIT) <= '1'; + alert_src <= alert_src or std_logic_vector(resize(unsigned(ip_sel), MAX_CHANS)); + last_ordinal <= rx_data(ALERT_ORDINAL_BITS); + end if; + + -- When under-range return, set the under-range return triggers output if enabled at ordinal + if rx_data(ALERT_CODE_BITS) = UND_RNG_RTN_CODE then + triggers_int(UNDR_RNG_RTN_TRIG_BIT)<= '1'; + triggers_int(UNDR_RNG_TRIG_BIT) <= '0'; + alert_src <= alert_src or std_logic_vector(resize(unsigned(ip_sel), MAX_CHANS)); + last_ordinal <= rx_data(ALERT_ORDINAL_BITS); + end if; + + end if; + + -- Clear all alerts when CPU says to + if clr_alert = '1' then + triggers_int <= (others => '0'); + alert_src <= (others => '0'); + end if; + + end if; + end process; + +end architecture rtl; diff --git a/generic_ip/multi_alert_uart_rx/0/src/uart_1mbd_rx_0.vhd b/generic_ip/multi_alert_uart_rx/0/src/uart_1mbd_rx_0.vhd new file mode 100644 index 0000000..0aad15c --- /dev/null +++ b/generic_ip/multi_alert_uart_rx/0/src/uart_1mbd_rx_0.vhd @@ -0,0 +1,118 @@ +------------------------------------------------------------------------------- +--! @file +--! @brief SOM FPGA STM8 alert code receiver 1MBaud UART +--! + +--! +--! @b Clock/resets: +--! @li clk : nominally 100MHz +--! @li rst_n : active low reset for clk (asynchronous assert) +--! +--! @b Interfaces: +--! @li Avalon MM slave avs_* : Control and status register memory mapped bus +--! @li UART_RXD : 1MBaud RS232 receiver interface +--! @li triggers : Trigger pulse output interface +--! +--! @b Clock-crossing considerations: +--! @li Module has UART_RXD as an asynchronous input, sync'd to clk in Altera generated UART core. +--! +--! @b Language build standard requirements: +--! @li VHDL 2008 +--! +--! @author Simon Southwell +--! @copyright (c) 2020 Global Inkjet Systems +--! @date 2020-06-00 +------------------------------------------------------------------------------- +-- altera vhdl_input_version vhdl_2008 + +library IEEE; +use IEEE.std_logic_1164.all; +use IEEE.numeric_std.all; + +use work.common_pkg.all; + +entity uart_1mbd_rx_0 is + generic ( + CLK_FREQ_MHZ : integer := 100; + NUM_CHANS : integer := 12 + ); + port ( + clk : in std_logic; + rst_n : in std_logic; + + UART_RXD : in std_logic; + + rx_valid : out std_logic; + rx_byte : out std_logic_vector(7 downto 0) + + ); +end entity uart_1mbd_rx_0; + +architecture rtl of uart_1mbd_rx_0 is + + constant BIT_TICKS : integer := CLK_FREQ_MHZ/1; + + signal data : std_logic_vector(9 downto 0); + signal bit_count : std_logic_vector(3 downto 0); + signal sample_count : std_logic_vector(log2(BIT_TICKS)-1 downto 0); + signal receiving : std_logic; + signal rx_data_sync : std_logic_vector(2 downto 0); + +begin + + process(clk, rst_n) + begin + if rst_n = '0' then + + rx_valid <= '0'; + bit_count <= (others => '0'); + sample_count <= (others => '0'); + receiving <= '0'; + + elsif clk'event and clk = '1' then + + -- Valid will pulse + rx_valid <= '0'; + + -- Sync up async input + rx_data_sync <= UART_RXD & rx_data_sync(rx_data_sync'left downto 1); + + -- If start bit seen when not receiving, start receiving and start sample count. + -- Keep counting whilst receiving. + if (receiving = '0' and rx_data_sync(0) = '0') or receiving = '1' then + receiving <= '1'; + sample_count <= std_logic_vector(unsigned(sample_count) + 1); + end if; + + -- At half way into the bit, shift in data value + if unsigned(sample_count) = BIT_TICKS/2 - 1 then + data <= rx_data_sync(0) & data(data'high downto 1); + end if; + + -- After half way, up the bit count + if unsigned(sample_count) = BIT_TICKS/2 then + bit_count <= std_logic_vector(unsigned(bit_count) + 1); + end if; + + rx_byte <= data(8 downto 1); + + -- If at the end of the last (stop) bit, update data out and set valid. + -- Clear the receiving state. + if unsigned(sample_count) = BIT_TICKS-1 then + + sample_count <= (others => '0'); + + if unsigned(bit_count) = 10 then + + -- Data is valid if stop bit a 1 + rx_valid <= data(9); + + -- Clear receiving flag + receiving <= '0'; + end if; + end if; + + end if; + end process; + +end rtl; \ No newline at end of file