From cf7de501ad7af2da097fa8acd1fa52f17ebe40da Mon Sep 17 00:00:00 2001 From: David Steele Date: Tue, 20 Sep 2022 13:33:00 +0100 Subject: [PATCH] CORE-798 Make EL GIP Library FPGA Vendor Independent. Removed reliance on Altera copyright serialiser code --- generic_ip/uart_1MBd/4/doc/uart_1MBd_4.json | 245 +++++++++++ generic_ip/uart_1MBd/4/sim/compile.do | 15 + generic_ip/uart_1MBd/4/sim/files.tcl | 11 + generic_ip/uart_1MBd/4/sim/glbl.v | 84 ++++ generic_ip/uart_1MBd/4/sim/readme.txt | 9 + generic_ip/uart_1MBd/4/sim/tb.vhd | 382 ++++++++++++++++++ generic_ip/uart_1MBd/4/sim/uart_tx.vhd | 147 +++++++ generic_ip/uart_1MBd/4/sim/wave.do | 100 +++++ generic_ip/uart_1MBd/4/src/uart_1MBd_4.vhd | 248 ++++++++++++ .../uart_1MBd/4/src/uart_1MBd_rs232_4.vhd | 372 +++++++++++++++++ .../uart_1MBd/4/src/uart_1MBd_uart_txrd_4.v | 265 ++++++++++++ generic_ip/uart_1MBd/versions.txt | 1 + 12 files changed, 1879 insertions(+) create mode 100644 generic_ip/uart_1MBd/4/doc/uart_1MBd_4.json create mode 100644 generic_ip/uart_1MBd/4/sim/compile.do create mode 100644 generic_ip/uart_1MBd/4/sim/files.tcl create mode 100644 generic_ip/uart_1MBd/4/sim/glbl.v create mode 100644 generic_ip/uart_1MBd/4/sim/readme.txt create mode 100644 generic_ip/uart_1MBd/4/sim/tb.vhd create mode 100644 generic_ip/uart_1MBd/4/sim/uart_tx.vhd create mode 100644 generic_ip/uart_1MBd/4/sim/wave.do create mode 100644 generic_ip/uart_1MBd/4/src/uart_1MBd_4.vhd create mode 100644 generic_ip/uart_1MBd/4/src/uart_1MBd_rs232_4.vhd create mode 100644 generic_ip/uart_1MBd/4/src/uart_1MBd_uart_txrd_4.v create mode 100644 generic_ip/uart_1MBd/versions.txt diff --git a/generic_ip/uart_1MBd/4/doc/uart_1MBd_4.json b/generic_ip/uart_1MBd/4/doc/uart_1MBd_4.json new file mode 100644 index 0000000..2ac05da --- /dev/null +++ b/generic_ip/uart_1MBd/4/doc/uart_1MBd_4.json @@ -0,0 +1,245 @@ +[{ + "ip_name" : "uart_1MBd_4", + "bus" : "csr", + "addr_width" : "4", + "description" : "This block is the 1MBaud UART interface for alerts", + "notes" : "The 1MBaud UART is used as a receiver only UART for STM8 alert data, mapping received byte codes to trigger pulse, with enables. The core logic is generated from the Altera soft IP suite. The RX register is used to give the CPU access to the received data, with the READ_DATA fields giving the received byte. When reading the register, if read data is valid (READ_DATA_VALID bit), the byte is 'popped' from the receiver fifo queue. Parity error status is given (PARITY_ERROR bit), along with the data valid and a count of queued received data (READ_DATA_AVAIL bits). The CONTROL register allows the enabling of receive interrupts (RD_INT_EN), as well as (on reads) the status of the interrupt (RD_INT). All the fields associated with transmission are not used. For the generation of emergency shutdown request, the CODES register is used to define the 3 upper bit codes for the four events (by default they are the values defined at https://confluence.gisapps.co.uk/display/SOMG/Rolling+ADC+UART+Alerts). The OVRG_EN register is a bitmap for the ADC ordinals of the over-range event. Reception of an over-range event with a bit set in this register, as indexed by the ordinal, will generate an over-range trigger output. Similarly for the UNRG_EN register and under-range trigger, the OVRG_RTN_EN register and the over-range return trigger and the UNRG_RTN_EN register and under-range return trigger outputs. The block will generate an interrupt if the over- or under-range trigger outputs pulse (and thus enabled by the enable registers being non-zero), or by the UART core, enabled by the UART's CONTROL register.", + "registers" : { + "ip_id" : { + "address" : "0", + "width" : "32", + "description" : "Unique ID for this IP block", + "fields": { + "ip_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0x0e", + "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" : "0x04", + "description" : "Minor number" + }, + "major_number" : { + "type" : "c", + "bit_len" : "8", + "reset" : "0x00", + "description" : "Major number" + + }, + "reserved" : { + "type" : "rsv", + "bit_len" : "16", + "description" : "Reserved bits" + } + } + }, + "codes" : { + "address" : "2", + "width" : "16", + "description" : "Three bit codes for the over/under range and return events", + "fields" : { + "over" : { + "type" : "w", + "bit_len" : "3", + "reset" : "3", + "description" : "Code for over-range alert (bits 7 down to 5 of received byte)." + }, + "reserved0" : { + "type" : "rsv", + "bit_len" : "1", + "description" : "Reserved bits" + }, + "over_rtn" : { + "type" : "w", + "bit_len" : "3", + "reset" : "4", + "description" : "Code for over-range return alert (bits 7 down to 5 of received byte)." + }, + "reserved1" : { + "type" : "rsv", + "bit_len" : "1", + "description" : "Reserved bits" + }, + "under" : { + "type" : "w", + "bit_len" : "3", + "reset" : "5", + "description" : "Code for over-range alert (bits 7 down to 5 of received byte)." + }, + "reserved2" : { + "type" : "rsv", + "bit_len" : "1", + "description" : "Reserved bits" + }, + "under_rtn" : { + "type" : "w", + "bit_len" : "3", + "reset" : "6", + "description" : "Code for under-range return alert (bits 7 down to 5 of received byte)." + }, + "reserved3" : { + "type" : "rsv", + "bit_len" : "1", + "description" : "Reserved bits" + } + } + }, + "ovrg_en" : { + "address" : "3", + "width" : "32", + "type" : "w", + "reset" : "0", + "description" : "Enable bitmap of ADC ordinals for over-range events for over-range trigger output" + }, + "unrg_en" : { + "address" : "4", + "width" : "32", + "type" : "w", + "reset" : "0", + "description" : "Enable bitmap of ADC ordinals for under-range events for under-range output" + }, + "ovrg_rtn_en" : { + "address" : "5", + "width" : "32", + "type" : "w", + "reset" : "0", + "description" : "Enable bitmap of ADC ordinals for over-range return events for over-range return trigger output" + }, + "unrg_rtn_en" : { + "address" : "6", + "width" : "32", + "type" : "w", + "reset" : "0", + "description" : "Enable bitmap of ADC ordinals for under-range return events for under-range trigger output" + }, + "clr_alert_int" : { + "address" : "7", + "width" : "1", + "type" : "wp", + "reset" : "0", + "description" : "Writing to this register clears any alert interrupts status. Reading returns interrupts status." + }, + "rx" : { + "address" : "8", + "width" : "32", + "description" : "UART Receive register. Writing to this register enables the write FIFO.", + "fields": { + "read_data" : { + "type" : "rp", + "bit_len" : "8", + "reset" : "0x00", + "description" : "RX read data byte" + }, + "reserved0" : { + "type" : "rsv", + "bit_len" : "1", + "description" : "Reserved bits" + }, + "parity_error" : { + "type" : "rp", + "bit_len" : "1", + "reset" : "0x0", + "description" : "Parity error status" + }, + "reserved1" : { + "type" : "rsv", + "bit_len" : "5", + "description" : "Reserved bits" + }, + "read_data_valid" : { + "type" : "rp", + "bit_len" : "1", + "reset" : "0x0", + "description" : "Receiver read data is valid" + + }, + "read_data_avail" : { + "type" : "rp", + "bit_len" : "8", + "reset" : "0x0", + "description" : "Receiver read data is available" + + }, + "reserved2" : { + "type" : "rsv", + "bit_len" : "8", + "description" : "Reserved bits" + } + } + }, + "control" : { + "address" : "9", + "width" : "32", + "description" : "UART control (and status) register", + "fields" : { + "rd_int_en" : { + "type" : "rwp", + "bit_len" : "1", + "reset" : "0", + "description" : "Read interrupt enable" + }, + "wr_int_en" : { + "type" : "rwp", + "bit_len" : "1", + "reset" : "0", + "description" : "Write interrupt enable" + }, + "reserved0" : { + "type" : "rsv", + "bit_len" : "6", + "description" : "Reserved bits" + }, + "rd_int" : { + "type" : "rwp", + "bit_len" : "1", + "reset" : "0", + "description" : "Read interrupt active (actually read-only, but violates mixing rules)" + }, + "wr_int" : { + "type" : "rwp", + "bit_len" : "1", + "reset" : "0", + "description" : "Write interrupt active (actually read-only, but violates mixing rules)" + }, + "reserved1" : { + "type" : "rsv", + "bit_len" : "6", + "description" : "Reserved bits" + }, + "wr_space" : { + "type" : "rwp", + "bit_len" : "8", + "reset" : "0", + "description" : "Write FIFO space available (actually read-only, but violates mixing rules)" + }, + "reserved2" : { + "type" : "rsv", + "bit_len" : "8", + "description" : "Reserved bits" + } + } + } + } +}] \ No newline at end of file diff --git a/generic_ip/uart_1MBd/4/sim/compile.do b/generic_ip/uart_1MBd/4/sim/compile.do new file mode 100644 index 0000000..d451fec --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/compile.do @@ -0,0 +1,15 @@ +# Compile the code into the appropriate libraries +foreach lib [list work tb_pkg] { + file delete -force -- $lib + vlib $lib +} + +vlog -quiet glbl.v -work work +vcom -quiet -2008 ../../../common/0/src/common_pkg.vhd -work work +vlog -quiet ../../3/src/altera/altera_up_rs232_counters_3.v -work work +vlog -quiet ../../3/src/altera/altera_up_sync_fifo_3.v -work work +vlog -quiet ../../3/src/altera/altera_up_rs232_out_serializer_3.v -work work +vlog -quiet ../../3/src/altera/altera_up_rs232_in_deserializer_3.v -work work +vlog -quiet ../../3/src/altera/uart_1MBd_rs232_3.v -work work +vlog -quiet ../src/uart_1MBd_uart_txrd_4.v -work work +vcom -quiet -2008 -f files.tcl -work work diff --git a/generic_ip/uart_1MBd/4/sim/files.tcl b/generic_ip/uart_1MBd/4/sim/files.tcl new file mode 100644 index 0000000..8a480f0 --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/files.tcl @@ -0,0 +1,11 @@ +../../../common/1/src/altera/alt_sync_fifo.vhd +../../../common/1/src/xilinx/xil_sync_fifo.vhd +../../../common/1/src/shim/sync_fifo.vhd +../../3/src/uart_1MBd_3_pkg_auto.vhd +../../3/src/uart_1MBd_3_csr_regs_auto.vhd +../../3/src/uart_1MBd_3.vhd +../src/uart_1MBd_4_pkg_auto.vhd +../src/uart_1MBd_4_csr_regs_auto.vhd +../src/uart_1MBd_rs232_4.vhd +../src/uart_1MBd_4.vhd +tb.vhd diff --git a/generic_ip/uart_1MBd/4/sim/glbl.v b/generic_ip/uart_1MBd/4/sim/glbl.v new file mode 100644 index 0000000..ed3b249 --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/glbl.v @@ -0,0 +1,84 @@ +// $Header: /devl/xcs/repo/env/Databases/CAEInterfaces/verunilibs/data/glbl.v,v 1.14 2010/10/28 20:44:00 fphillip Exp $ +`ifndef GLBL +`define GLBL +`timescale 1 ps / 1 ps + +module glbl (); + + parameter ROC_WIDTH = 100000; + parameter TOC_WIDTH = 0; + parameter GRES_WIDTH = 10000; + parameter GRES_START = 10000; + +//-------- STARTUP Globals -------------- + wire GSR; + wire GTS; + wire GWE; + wire PRLD; + wire GRESTORE; + tri1 p_up_tmp; + tri (weak1, strong0) PLL_LOCKG = p_up_tmp; + + wire PROGB_GLBL; + wire CCLKO_GLBL; + wire FCSBO_GLBL; + wire [3:0] DO_GLBL; + wire [3:0] DI_GLBL; + + reg GSR_int; + reg GTS_int; + reg PRLD_int; + reg GRESTORE_int; + +//-------- JTAG Globals -------------- + wire JTAG_TDO_GLBL; + wire JTAG_TCK_GLBL; + wire JTAG_TDI_GLBL; + wire JTAG_TMS_GLBL; + wire JTAG_TRST_GLBL; + + reg JTAG_CAPTURE_GLBL; + reg JTAG_RESET_GLBL; + reg JTAG_SHIFT_GLBL; + reg JTAG_UPDATE_GLBL; + reg JTAG_RUNTEST_GLBL; + + reg JTAG_SEL1_GLBL = 0; + reg JTAG_SEL2_GLBL = 0 ; + reg JTAG_SEL3_GLBL = 0; + reg JTAG_SEL4_GLBL = 0; + + reg JTAG_USER_TDO1_GLBL = 1'bz; + reg JTAG_USER_TDO2_GLBL = 1'bz; + reg JTAG_USER_TDO3_GLBL = 1'bz; + reg JTAG_USER_TDO4_GLBL = 1'bz; + + assign (strong1, weak0) GSR = GSR_int; + assign (strong1, weak0) GTS = GTS_int; + assign (weak1, weak0) PRLD = PRLD_int; + assign (strong1, weak0) GRESTORE = GRESTORE_int; + + initial begin + GSR_int = 1'b1; + PRLD_int = 1'b1; + #(ROC_WIDTH) + GSR_int = 1'b0; + PRLD_int = 1'b0; + end + + initial begin + GTS_int = 1'b1; + #(TOC_WIDTH) + GTS_int = 1'b0; + end + + initial begin + GRESTORE_int = 1'b0; + #(GRES_START); + GRESTORE_int = 1'b1; + #(GRES_WIDTH); + GRESTORE_int = 1'b0; + end + +endmodule +`endif diff --git a/generic_ip/uart_1MBd/4/sim/readme.txt b/generic_ip/uart_1MBd/4/sim/readme.txt new file mode 100644 index 0000000..0c3445a --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/readme.txt @@ -0,0 +1,9 @@ +To sim + +1/. open cmd. Type modelsim +2/. In modelsim transcript : type do compile.do +3/. In modelsim transcript : type vsim work.tb glbl -L altera_mf_ver +4/. In modelsim transcript : type run 10us. +5/. Test pass prints :"# ** Error: Test complete (Not an Error - just stops testbench)" + Test fails prints :"# ** Failure:" + diff --git a/generic_ip/uart_1MBd/4/sim/tb.vhd b/generic_ip/uart_1MBd/4/sim/tb.vhd new file mode 100644 index 0000000..ea7d1ef --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/tb.vhd @@ -0,0 +1,382 @@ +------------------------------------------------------------------------------- +-- Copyright (c) 2022 Global Inkjet Systems +------------------------------------------------------------------------------- + +-- Self checking test bench to ensure Xilinx xil_lvds_serdes_tx_2 is functionally equivalent to alt_lvds_serdes_tx_2 + +library modelsim_lib; +use modelsim_lib.util.all; + +library ieee; +use ieee.std_logic_1164.all; +use ieee.numeric_std.all; + +use work.common_pkg.all; + +use STD.textio.all; + +--pragma translate_off +--{{{ +entity tb is end; +--}}} +--{{{ +architecture sim of tb is + constant ALT_DEVICE_FAMILY : string := "Cyclone V"; + constant XIL_DEVICE_FAMILY : string := "Ultrascale+"; + + constant CLK_FREQ_MHZ : integer := 100; + + 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; + + subtype ALERT_CODE_BITS is natural range 7 downto 5; + subtype ALERT_ORDINAL_BITS is natural range 4 downto 0; + + --{{{ signals + signal clk : std_logic := '0'; + signal rst_n : std_logic; + + signal rs232_source_address : std_logic_vector( 3 downto 0); + signal rs232_source_read : std_logic; + signal rs232_source_write : std_logic; + signal rs232_source_writedata : std_logic_vector(31 downto 0); + signal rs232_source_readdata : std_logic_vector(31 downto 0); + signal rs232_source_readdata_alt : std_logic_vector(31 downto 0); + signal rs232_source_readdata_xil : std_logic_vector(31 downto 0); + + + signal avs_csr_address : std_logic_vector( 3 downto 0); + signal avs_csr_read : std_logic; + signal avs_csr_write : std_logic; + signal avs_csr_writedata : std_logic_vector(31 downto 0); + signal avs_csr_readdata_ref : std_logic_vector(31 downto 0); + signal avs_csr_readdata_alt : std_logic_vector(31 downto 0); + signal avs_csr_readdata_xil : std_logic_vector(31 downto 0); + + signal triggers_ref : std_logic_vector(3 downto 0); + signal triggers_ref_d : std_logic_vector(3 downto 0); + signal triggers_ref_cnt : integer; + signal triggers_alt : std_logic_vector(3 downto 0); + signal triggers_xil : std_logic_vector(3 downto 0); + signal irq_ref : std_logic; + signal irq_ref_d : std_logic; + signal irq_ref_cnt : integer; + signal irq_alt : std_logic; + signal irq_xil : std_logic; + + signal uart_txd : std_logic; + signal uart_txd_d : std_logic; + signal uart_txd_cnt : integer; + signal uart_txd_alt : std_logic; + signal uart_txd_xil : std_logic; + + signal rs232_source_sniff_data : std_logic_vector(7 downto 0); + signal rs232_source_sniff_data_alt : std_logic_vector(7 downto 0); + signal rs232_source_sniff_data_xil : std_logic_vector(7 downto 0); + + signal docheck : boolean; + --}}} + + --constant mem_depth : integer := 256; + --type mem is array (mem_depth-1 downto 0) of std_logic_vector (NUM_CHANS*SER_FACTOR - 1 DOWNTO 0); + --shared variable test_array : mem; +begin + + rs232_source : entity work.uart_1MBd_rs232_3 + generic map ( + BAUD_TICK_COUNT => CLK_FREQ_MHZ-1 + ) + port map ( + clk => clk, + reset => not rst_n, + address => rs232_source_address(0), + chipselect => rs232_source_read or rs232_source_write, + byteenable => "1111", + read => rs232_source_read, + write => rs232_source_write and not rs232_source_address(1), + writedata => rs232_source_writedata, + readdata => rs232_source_readdata, + irq => open, + UART_RXD => uart_txd, + UART_TXD => uart_txd, + sniff_strobe => open, + sniff_data => rs232_source_sniff_data + ); + + rs232_source_alt : entity work.uart_1MBd_rs232_4 + generic map ( + BAUD_TICK_COUNT => CLK_FREQ_MHZ-1, + DEVICE_FAMILY => ALT_DEVICE_FAMILY + ) + port map ( + clk => clk, + reset => not rst_n, + address => rs232_source_address(0), + chipselect => rs232_source_read or rs232_source_write, + byteenable => "1111", + read => rs232_source_read, + write => rs232_source_write and not rs232_source_address(1), + writedata => rs232_source_writedata, + readdata => rs232_source_readdata_alt, + irq => open, + UART_RXD => uart_txd_alt, + UART_TXD => uart_txd_alt, + sniff_strobe => open, + sniff_data => rs232_source_sniff_data_alt + ); + + rs232_source_xil : entity work.uart_1MBd_rs232_4 + generic map ( + BAUD_TICK_COUNT => CLK_FREQ_MHZ-1, + DEVICE_FAMILY => XIL_DEVICE_FAMILY + ) + port map ( + clk => clk, + reset => not rst_n, + address => rs232_source_address(0), + chipselect => rs232_source_read or rs232_source_write, + byteenable => "1111", + read => rs232_source_read, + write => rs232_source_write and not rs232_source_address(1), + writedata => rs232_source_writedata, + readdata => rs232_source_readdata_xil, + irq => open, + UART_RXD => uart_txd_xil, + UART_TXD => uart_txd_xil, + sniff_strobe => open, + sniff_data => rs232_source_sniff_data_xil + ); + + --{{{ + c_alt_uart_1MBd_ref : entity work.uart_1MBd_3 -- altera Ref version + generic map ( + CLK_FREQ_MHZ => CLK_FREQ_MHZ + ) + port map ( + clk => clk, + rst_n => rst_n, + + avs_csr_address => avs_csr_address, + avs_csr_read => avs_csr_read, + avs_csr_write => avs_csr_write, + avs_csr_writedata => avs_csr_writedata, + avs_csr_readdata => avs_csr_readdata_ref, + + UART_RXD => uart_txd, + + triggers => triggers_ref, + + irq => irq_ref + ); + --}}} + + --{{{ + c_alt_uart_1MBd_4 : entity work.uart_1MBd_4 -- altera version + generic map ( + DEVICE_FAMILY => ALT_DEVICE_FAMILY, + CLK_FREQ_MHZ => CLK_FREQ_MHZ + ) + port map ( + clk => clk, + rst_n => rst_n, + + avs_csr_address => avs_csr_address, + avs_csr_read => avs_csr_read, + avs_csr_write => avs_csr_write, + avs_csr_writedata => avs_csr_writedata, + avs_csr_readdata => avs_csr_readdata_alt, + + UART_RXD => uart_txd, + + triggers => triggers_alt, + + irq => irq_alt + ); + --}}} + + --{{{ + c_xil_uart_1MBd_4 : entity work.uart_1MBd_4 -- xilinx version + generic map ( + DEVICE_FAMILY => XIL_DEVICE_FAMILY, + CLK_FREQ_MHZ => CLK_FREQ_MHZ + ) + port map ( + clk => clk, + rst_n => rst_n, + + avs_csr_address => avs_csr_address, + avs_csr_read => avs_csr_read, + avs_csr_write => avs_csr_write, + avs_csr_writedata => avs_csr_writedata, + avs_csr_readdata => avs_csr_readdata_xil, + + UART_RXD => uart_txd, + + triggers => triggers_xil, + + irq => irq_xil + ); + --}}} + + + --{{{ + p_clk : process + begin + clk <= not clk; + wait for 5 ns; + end process; + --}}} + --{{{ + p_tb : process + begin + rst_n <= '0'; + + rs232_source_address <= (others => '0'); + rs232_source_read <= '0'; + rs232_source_write <= '0'; + rs232_source_writedata <= (others => '0'); + + avs_csr_address <= (others => '0'); + avs_csr_read <= '0'; + avs_csr_write <= '0'; + avs_csr_writedata <= (others => '0'); + + docheck <= false; + wait for 0.1 us; + wait until clk'event and clk = '1'; + rst_n <= '1' after 0.1 ns; + for i in 0 to 10 loop + wait until clk'event and clk = '1'; + end loop; + + avs_csr_address <= x"3"; + avs_csr_write <= '1'; + avs_csr_writedata <= x"19661066"; -- set over range en + wait until clk'event and clk = '1'; + avs_csr_address <= (others => '0'); + avs_csr_read <= '0'; + avs_csr_write <= '0'; + avs_csr_writedata <= (others => '0'); + + docheck <= true; + + rs232_source_address <= (others => '0'); + rs232_source_write <= '1'; + rs232_source_writedata <= x"00000000"; + wait until clk'event and clk = '1'; + rs232_source_writedata <= x"00000061"; -- OVR_RNG_ALERT , ordinal 1 + wait until clk'event and clk = '1'; + --rs232_source_writedata <= x"000000A1"; -- UNDR_RNG_RTN_TRIG_BIT := 3, OVR_RNG_RTN_TRIG_BIT := 1, ordinal 1 + --wait until clk'event and clk = '1'; + rs232_source_write <= '0'; + + + for i in 0 to 2285 loop + wait until clk'event and clk = '1'; + end loop; + avs_csr_address <= x"8"; + avs_csr_read <= '1'; + wait until clk'event and clk = '1'; + avs_csr_read <= '0'; + wait until clk'event and clk = '1'; + wait until clk'event and clk = '1'; + avs_csr_address <= (others => '0'); + avs_csr_write <= '0'; + avs_csr_writedata <= (others => '0'); + + for i in 0 to 100 loop + wait until clk'event and clk = '1'; + end loop; + + assert false + report "Test complete (Not an Error - just stops testbench)" + severity error; + + wait; + end process; + --}}} + --{{{ + p_check : process + variable dont_check_triggers : boolean; + variable dont_check_irq : boolean; + variable dont_check_uart_txd : boolean; + begin + wait until rising_edge(clk); + dont_check_triggers := false; + if triggers_ref /= triggers_ref_d or triggers_ref_cnt /= 0 then + dont_check_triggers := true; + end if; + dont_check_irq := false; + if irq_ref /= irq_ref_d or irq_ref_cnt /= 0 then + dont_check_irq := true; + end if; + dont_check_uart_txd := false; + if uart_txd /= uart_txd_d or uart_txd_cnt /= 0 then + dont_check_uart_txd := true; + end if; + + if docheck then + assert (avs_csr_readdata_ref = avs_csr_readdata_alt) + report "************Check read Error - Ref and Altera different***************" + severity error; + + assert (avs_csr_readdata_ref = avs_csr_readdata_xil) + report "************Check read Error - Ref and Xilinx different***************" + severity error; + + assert (triggers_ref = triggers_alt or dont_check_triggers) + report "************Check Triggers Error - Ref and Altera different***************" + severity error; + + assert (triggers_ref = triggers_xil or dont_check_triggers) + report "************Check Triggers Error - Ref and Xilinx different***************" + severity error; + + assert (irq_ref = irq_alt or dont_check_irq) + report "************Check irq Error - Ref and Altera different***************" + severity error; + + assert (irq_ref = irq_xil or dont_check_irq) + report "************Check irq Error - Ref and Xilinx different***************" + severity error; + + assert (uart_txd = uart_txd_alt or dont_check_uart_txd) + report "************Check Uart Tx Error - Ref and Altera different***************" + severity error; + + assert (uart_txd = uart_txd_xil or dont_check_uart_txd) + report "************Check Uart Tx Error - Ref and Xilinx different***************" + severity error; + + + end if; + + triggers_ref_d <= triggers_ref; + if triggers_ref /= triggers_ref_d then + triggers_ref_cnt <= 10; + elsif triggers_ref_cnt > 0 then + triggers_ref_cnt <= triggers_ref_cnt - 1; + end if; + + irq_ref_d <= irq_ref; + if irq_ref /= irq_ref_d then + irq_ref_cnt <= 10; + elsif irq_ref_cnt > 0 then + irq_ref_cnt <= irq_ref_cnt - 1; + end if; + + uart_txd_d <= uart_txd; + if uart_txd /= uart_txd_d then + uart_txd_cnt <= 10; + elsif uart_txd_cnt > 0 then + uart_txd_cnt <= uart_txd_cnt - 1; + end if; + + end process; + --}}} + +end sim; +--}}} +--pragma translate_on diff --git a/generic_ip/uart_1MBd/4/sim/uart_tx.vhd b/generic_ip/uart_1MBd/4/sim/uart_tx.vhd new file mode 100644 index 0000000..8b00316 --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/uart_tx.vhd @@ -0,0 +1,147 @@ +------------------------------------------------------------------------------- +-- Title : UART1MBd test bench transmitter +-- Project : GENERIC +------------------------------------------------------------------------------- +-- File : uart_tx.vhd +-- Author : Simon Southwell +-- Company : Global Inkjet Systems +-- Created : 2019-9-16 +-- Platform : +-- Standard : VHDL 2008 +------------------------------------------------------------------------------- +-- Description: +-- This block is a uart 1MBd protocol transmitter for use in simulation test +-- benches +------------------------------------------------------------------------------- +-- Copyright (c) 2019 Global Inkjet Systems +------------------------------------------------------------------------------- + + +library IEEE; +use IEEE.std_logic_1164.all; +use IEEE.numeric_std.all; + +entity uart_tx is + generic ( + CLK_FREQ_MHZ : integer := 100 + ); + port ( + clk : in std_logic; + rst_n : in std_logic; + + avs_csr_address : in std_logic_vector(31 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); + + irq_in : in std_logic; + + uart_tx_en : out std_logic; + + tx_active : out std_logic; + + UART_TXD : out std_logic_vector(2 downto 0) + ); +end entity uart_tx; + +architecture rtl of uart_tx is + + component uart_1MBd_rs232_3 is + generic ( + BAUD_TICK_COUNT : integer + ); + port ( + clk : in std_logic := 'X'; -- clk + reset : in std_logic := 'X'; -- reset + address : in std_logic := 'X'; -- address + chipselect : in std_logic := 'X'; -- chipselect + byteenable : in std_logic_vector(3 downto 0) := (others => 'X'); -- byteenable + read : in std_logic := 'X'; -- read + write : in std_logic := 'X'; -- write + writedata : in std_logic_vector(31 downto 0) := (others => 'X'); -- writedata + readdata : out std_logic_vector(31 downto 0); -- readdata + irq : out std_logic; -- irq + UART_RXD : in std_logic := 'X'; -- export + UART_TXD : out std_logic; -- export + sniff_strobe : out std_logic; + sniff_data : out std_logic_vector(7 downto 0) + ); + end component uart_1MBd_rs232_3; + + signal rs232_readdata : std_logic_vector(31 downto 0); + signal rs232_readdata_int : std_logic_vector(31 downto 0); + signal local_readdata : std_logic_vector(31 downto 0); + signal uart_txd_int : std_logic; + signal tx_sel : std_logic_vector(1 downto 0); + signal tx_count : integer := 0; + +begin + + rs232_readdata <= rs232_readdata_int when avs_csr_read = '1' else (others => '0'); + avs_csr_readdata <= rs232_readdata or local_readdata; + + UART_TXD(0) <= uart_txd_int when tx_sel = "00" else '1'; + UART_TXD(1) <= uart_txd_int when tx_sel = "01" else '1'; + UART_TXD(2) <= uart_txd_int when tx_sel = "10" else '1'; + + tx_active <= '1' when tx_count /= 0 else '0'; + + rs232_i : component uart_1MBd_rs232_3 + generic map ( + BAUD_TICK_COUNT => CLK_FREQ_MHZ-1 + ) + port map ( + clk => clk, + reset => not rst_n, + address => avs_csr_address(0), + chipselect => avs_csr_read or avs_csr_write, + byteenable => "1111", + read => avs_csr_read, + write => avs_csr_write and not avs_csr_address(1), + writedata => avs_csr_writedata, + readdata => rs232_readdata_int, + irq => open, + UART_RXD => open, + UART_TXD => uart_txd_int, + sniff_strobe => open, + sniff_data => open + ); + + process (clk, rst_n) + begin + if rst_n = '0' then + + tx_sel <= (others=> '0'); + uart_tx_en <= '0'; + + elsif clk'event and clk = '1' then + + local_readdata <= (others => '0'); + + -- Sending a byte to the transmister + if avs_csr_write = '1' and avs_csr_address(3 downto 0) = "0000" then + tx_count <= CLK_FREQ_MHZ * 10 + 1; + elsif tx_count /= 0 then + tx_count <= tx_count - 1; + end if; + + if avs_csr_write = '1' and avs_csr_address(3 downto 0) = "0010" then + tx_sel <= avs_csr_writedata(1 downto 0); + uart_tx_en <= avs_csr_writedata(2); + end if; + + if avs_csr_read = '1' then + if avs_csr_address(3 downto 0) = "0010" then + local_readdata(1 downto 0) <= tx_sel; + local_readdata(2) <= uart_tx_en; + elsif avs_csr_address(3 downto 0) = "0011" then + local_readdata(0) <= irq_in; + end if; + end if; + + + end if; + end process; + +end architecture rtl; diff --git a/generic_ip/uart_1MBd/4/sim/wave.do b/generic_ip/uart_1MBd/4/sim/wave.do new file mode 100644 index 0000000..a976a9f --- /dev/null +++ b/generic_ip/uart_1MBd/4/sim/wave.do @@ -0,0 +1,100 @@ +onerror {resume} +quietly WaveActivateNextPane {} 0 +add wave -noupdate /tb/clk +add wave -noupdate /tb/rst_n +add wave -noupdate /tb/rs232_source_address +add wave -noupdate /tb/rs232_source_read +add wave -noupdate /tb/rs232_source_write +add wave -noupdate /tb/rs232_source_writedata +add wave -noupdate /tb/rs232_source_readdata +add wave -noupdate /tb/avs_csr_address +add wave -noupdate /tb/avs_csr_read +add wave -noupdate /tb/avs_csr_write +add wave -noupdate /tb/avs_csr_writedata +add wave -noupdate /tb/avs_csr_readdata_ref +add wave -noupdate /tb/avs_csr_readdata_alt +add wave -noupdate /tb/avs_csr_readdata_xil +add wave -noupdate /tb/triggers_ref +add wave -noupdate /tb/triggers_alt +add wave -noupdate /tb/triggers_xil +add wave -noupdate /tb/irq_ref +add wave -noupdate /tb/irq_alt +add wave -noupdate /tb/irq_xil +add wave -noupdate /tb/uart_txd +add wave -noupdate /tb/uart_txd_alt +add wave -noupdate /tb/uart_txd_xil +add wave -noupdate /tb/rs232_source_sniff_data +add wave -noupdate /tb/docheck +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/clk_en +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/en_cnt +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/tx_next_bit +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/tx_bit_cnt +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/tx_bit_tmr +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/tx_busy_out +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/tx_new_word +add wave -noupdate /tb/rs232_source_alt/uarttxrx_inst/tx_data +add wave -noupdate -divider ref +add wave -noupdate /tb/c_alt_uart_1MBd_ref/sniff_strobe +add wave -noupdate /tb/c_alt_uart_1MBd_ref/sniff_data +add wave -noupdate /tb/c_alt_uart_1MBd_ref/triggers +add wave -noupdate /tb/c_alt_uart_1MBd_ref/irq +add wave -noupdate /tb/c_alt_uart_1MBd_ref/codes_over +add wave -noupdate /tb/avs_csr_readdata_ref +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_readdata +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs323_readdata_valid +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/read_available +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/readdata +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/read_data_valid +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/RS232_In_Deserializer/RS232_In_FIFO/read_en +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/RS232_In_Deserializer/RS232_In_FIFO/clk +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/RS232_In_Deserializer/RS232_In_FIFO/fifo_is_empty +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/RS232_In_Deserializer/RS232_In_FIFO/read_data +add wave -noupdate /tb/c_alt_uart_1MBd_ref/rs232_0/RS232_In_Deserializer/RS232_In_FIFO/words_used +add wave -noupdate -divider alt +add wave -noupdate /tb/c_alt_uart_1MBd_4/sniff_strobe +add wave -noupdate /tb/c_alt_uart_1MBd_4/sniff_data +add wave -noupdate /tb/c_alt_uart_1MBd_4/triggers +add wave -noupdate /tb/c_alt_uart_1MBd_4/irq +add wave -noupdate /tb/avs_csr_readdata_alt +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_readdata +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs323_readdata_valid +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/read_available +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/read_data_valid +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/readdata +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/read_fifo_inst/empty +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/read_fifo_inst/rddata +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/read_fifo_inst/rdreq +add wave -noupdate /tb/c_alt_uart_1MBd_4/rs232_0/read_fifo_inst/usedw +add wave -noupdate -divider xil +add wave -noupdate /tb/c_xil_uart_1MBd_4/sniff_strobe +add wave -noupdate /tb/c_xil_uart_1MBd_4/sniff_data +add wave -noupdate /tb/c_xil_uart_1MBd_4/triggers +add wave -noupdate /tb/c_xil_uart_1MBd_4/irq +add wave -noupdate /tb/avs_csr_readdata_xil +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_readdata +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs323_readdata_valid +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/wrreq +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/wrdata +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/empty +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/rddata +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/rdreq +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/rst_n +add wave -noupdate /tb/c_xil_uart_1MBd_4/rs232_0/read_fifo_inst/sclr +TreeUpdate [SetDefaultTree] +WaveRestoreCursors {{Cursor 1} {20420261 ps} 0} {{Cursor 2} {10352222 ps} 0} +quietly wave cursor active 1 +configure wave -namecolwidth 461 +configure wave -valuecolwidth 157 +configure wave -justifyvalue left +configure wave -signalnamewidth 0 +configure wave -snapdistance 10 +configure wave -datasetprefix 0 +configure wave -rowmargin 4 +configure wave -childrowmargin 2 +configure wave -gridoffset 0 +configure wave -gridperiod 1 +configure wave -griddelta 40 +configure wave -timeline 0 +configure wave -timelineunits ns +update +WaveRestoreZoom {20179450 ps} {20478733 ps} diff --git a/generic_ip/uart_1MBd/4/src/uart_1MBd_4.vhd b/generic_ip/uart_1MBd/4/src/uart_1MBd_4.vhd new file mode 100644 index 0000000..8ff7377 --- /dev/null +++ b/generic_ip/uart_1MBd/4/src/uart_1MBd_4.vhd @@ -0,0 +1,248 @@ +------------------------------------------------------------------------------- +--! @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 +--! @li irq : Interrupt output +--! +--! @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) 2018 Global Inkjet Systems +--! @date 2018-04-05 +------------------------------------------------------------------------------- +-- altera vhdl_input_version vhdl_2008 +-- uart_1MBd_4.vhd + + +library IEEE; +use IEEE.std_logic_1164.all; +use IEEE.numeric_std.all; + +use work.uart_1MBd_4_pkg.all; + +entity uart_1MBd_4 is + generic ( + CLK_FREQ_MHZ : integer := 100; + DEVICE_FAMILY : string := "Cyclone V" + ); + port ( + clk : in std_logic; + rst_n : in std_logic; + + 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); + + UART_RXD : in std_logic; + + triggers : out std_logic_vector(3 downto 0); + + irq : out std_logic + + ); +end entity uart_1MBd_4; + +architecture rtl of uart_1MBd_4 is + + -- For 100M, oversample = 10, clock divide = 10 + constant OVERSAMPLE_RATE : integer := 10; + constant ENABLE_CLOCK_DIVIDER : integer := CLK_FREQ_MHZ/10; + + 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; + + subtype ALERT_CODE_BITS is natural range 7 downto 5; + subtype ALERT_ORDINAL_BITS is natural range 4 downto 0; + + signal avs_csr_readdata_int : std_logic_vector(31 downto 0); + signal rs232_readdata : std_logic_vector(31 downto 0); + signal control_read_pulse : std_logic; + signal control_pulse : std_logic; + signal rx_pulse : std_logic; + + signal codes_over : std_logic_vector(2 downto 0); + signal codes_over_rtn : std_logic_vector(2 downto 0); + signal codes_under : std_logic_vector(2 downto 0); + signal codes_under_rtn : std_logic_vector(2 downto 0); + + signal ovrg_en : std_logic_vector(31 downto 0); + signal ovrg_rtn_en : std_logic_vector(31 downto 0); + signal unrg_en : std_logic_vector(31 downto 0); + signal unrg_rtn_en : std_logic_vector(31 downto 0); + + signal clr_alert_int_pulse : std_logic; + signal irq_uart : std_logic; + signal irq_alert : std_logic; + signal irq_combined : std_logic; + + signal sniff_data : std_logic_vector( 7 downto 0); + signal sniff_strobe : std_logic; + + signal rs323_readdata_valid : std_logic; + signal triggers_last : std_logic_vector(3 downto 0); + +begin + + uart_1MBd_csr_regs_inst : entity work.uart_1MBd_4_csr_regs + generic map( + ADDR_DECODE_WIDTH => 4 + ) + port map ( + -- Clock and reset + clk => clk, + rst_n => rst_n, + + control_pulse => control_pulse, + control_read_pulse => control_read_pulse, + + -- RS232 write data comes directly from the avs_csr_writedata input wired to the block's + -- writedata port, so leave outputs open for the control register. Similarly for reads. + control_wr_int_en => open, + control_wr_int_en_in => '0', + control_rd_int => open, + control_rd_int_in => '0', + control_wr_int => open, + control_wr_int_in => '0', + control_wr_space => open, + control_wr_space_in => (others => '0'), + control_rd_int_en => open, + control_rd_int_en_in =>'0', + + rx_pulse => rx_pulse, + rx_parity_error => '0', + rx_read_data_valid => '0', + rx_read_data_avail => (others => '0'), + rx_read_data => (others => '0'), + + codes_over => codes_over, + codes_over_rtn => codes_over_rtn, + codes_under => codes_under, + codes_under_rtn => codes_under_rtn, + + ovrg_en => ovrg_en, + unrg_en => unrg_en, + ovrg_rtn_en => ovrg_rtn_en, + unrg_rtn_en => unrg_rtn_en, + + clr_alert_int_pulse => clr_alert_int_pulse, + clr_alert_int => open, + clr_alert_int_in => irq_combined, + + 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_int + + ); + + avs_csr_readdata <= rs232_readdata when rs323_readdata_valid else avs_csr_readdata_int; + + rs232_0 : entity work.uart_1MBd_rs232_4 + generic map ( + BAUD_TICK_COUNT => CLK_FREQ_MHZ-1, + DEVICE_FAMILY => DEVICE_FAMILY + ) + port map ( + clk => clk, + reset => not rst_n, + address => avs_csr_address(0), + chipselect => control_pulse or control_read_pulse or rx_pulse, + byteenable => "1111", + read => rx_pulse or control_read_pulse, + write => control_pulse, + writedata => avs_csr_writedata, + readdata => rs232_readdata, + irq => irq_uart, + UART_RXD => UART_RXD, + UART_TXD => open, + sniff_strobe => sniff_strobe, + sniff_data => sniff_data + ); + + SNIFF_LOGIC : process (clk, rst_n) + begin + if rst_n = '0' then + + triggers <= (others => '0'); + triggers_last <= (others => '0'); + irq_alert <= '0'; + rs323_readdata_valid <= '0'; + + elsif clk'event and clk = '1' then + + -- Default the return trigger outputs to 0 + triggers(OVR_RNG_RTN_TRIG_BIT) <= '0'; + triggers(UNDR_RNG_RTN_TRIG_BIT) <= '0'; + + triggers_last <= triggers; + + rs323_readdata_valid <= rx_pulse or control_read_pulse; + + -- Process the sniff code when the strobe active + if sniff_strobe = '1' then + + -- When over-range, set the over-range triggers output if enabled at ordinal + if sniff_data(ALERT_CODE_BITS) = codes_over then + triggers(OVR_RNG_TRIG_BIT) <= ovrg_en(to_integer(unsigned(sniff_data(ALERT_ORDINAL_BITS)))); + end if; + + -- When over-range return, set the over-range return triggers output if enabled at ordinal + if sniff_data(ALERT_CODE_BITS) = codes_over_rtn then + triggers(OVR_RNG_RTN_TRIG_BIT) <= ovrg_rtn_en(to_integer(unsigned(sniff_data(ALERT_ORDINAL_BITS)))); + triggers(OVR_RNG_TRIG_BIT) <= '0'; + end if; + + -- When under-range, set the under-range triggers output if enabled at ordinal + if sniff_data(ALERT_CODE_BITS) = codes_under then + triggers(UNDR_RNG_TRIG_BIT) <= unrg_en(to_integer(unsigned(sniff_data(ALERT_ORDINAL_BITS)))); + end if; + + -- When under-range return, set the under-range return triggers output if enabled at ordinal + if sniff_data(ALERT_CODE_BITS) = codes_under_rtn then + triggers(UNDR_RNG_RTN_TRIG_BIT) <= unrg_rtn_en(to_integer(unsigned(sniff_data(ALERT_ORDINAL_BITS)))); + triggers(UNDR_RNG_TRIG_BIT) <= '0'; + end if; + + end if; + + if clr_alert_int_pulse = '1' then + triggers_last <= (others => '0'); + triggers <= (others => '0'); + end if; + + -- Set the (sticky) alert IRQ if over or under range output pulses. Clear on CPU clear interrupt write, + -- but give priority to any new alert. + irq_alert <= (irq_alert and not clr_alert_int_pulse) or + (not triggers_last(OVR_RNG_TRIG_BIT) and triggers(OVR_RNG_TRIG_BIT)) or + (not triggers_last(UNDR_RNG_TRIG_BIT) and triggers(UNDR_RNG_TRIG_BIT)); + + end if; + end process; + + irq_combined <= irq_alert or irq_uart; + irq <= irq_combined; + +end architecture rtl; diff --git a/generic_ip/uart_1MBd/4/src/uart_1MBd_rs232_4.vhd b/generic_ip/uart_1MBd/4/src/uart_1MBd_rs232_4.vhd new file mode 100644 index 0000000..031eeb2 --- /dev/null +++ b/generic_ip/uart_1MBd/4/src/uart_1MBd_rs232_4.vhd @@ -0,0 +1,372 @@ +------------------------------------------------------------------------------- +--! @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 +--! @li irq : Interrupt output +--! +--! @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) 2018 Global Inkjet Systems +--! @date 2018-04-05 +------------------------------------------------------------------------------- +-- altera vhdl_input_version vhdl_2008 +-- uart_1MBd_rs232_4.vhd + + +library IEEE; +use IEEE.std_logic_1164.all; +use IEEE.numeric_std.all; + +entity uart_1MBd_rs232_4 is + generic ( + CW : integer := 7; -- Baud Counter Width + BAUD_TICK_COUNT : integer := 60; + TDW : integer := 10; -- Total Data Width + DW : integer := 8; -- Data Width + ODD_PARITY : integer := 0; + DEVICE_FAMILY : string := "Cyclone V" + ); + port ( + clk : in std_logic; + reset : in std_logic; + + address : in std_logic; + chipselect : in std_logic; + byteenable : in std_logic_vector(3 downto 0); + + read : in std_logic; + write : in std_logic; + writedata : in std_logic_vector(31 downto 0); + + UART_RXD : in std_logic; + + + irq : out std_logic; + readdata : out std_logic_vector(31 downto 0) := (others => '0'); + + UART_TXD : out std_logic; + + sniff_strobe : out std_logic; + sniff_data : out std_logic_vector(DW-1 downto 0) + + ); +end entity uart_1MBd_rs232_4; + +architecture rtl of uart_1MBd_rs232_4 is + + constant HALF_BAUD_TICK_COUNT : integer := BAUD_TICK_COUNT/2; + + --{{{ + component uart_1MBd_uart_txrd_4 + generic ( + OVERSAMPLE_RATE : integer := 16; + ENABLE_CLOCK_DIVIDER : integer := 52 + ); + port ( + clk : in std_logic; + + tx_en_in : in std_logic; + tx_dv_in : in std_logic; + tx_data_in : in std_logic_vector(7 downto 0); + + tx_out : out std_logic; + tx_rdy_out : out std_logic; + tx_busy_out : out std_logic; + + rx_in : in std_logic; + rx_en_in : in std_logic; + + de_out : out std_logic; + rx_error_out: out std_logic; + + rx_dv_out : out std_logic; + rx_data_out : out std_logic_vector(7 downto 0) + ); + end component uart_1MBd_uart_txrd_4; + --}}} + + -- For 100M, oversample = 10, clock divide = 10 + constant OVERSAMPLE_RATE : integer := 10; + constant ENABLE_CLOCK_DIVIDER : integer := 10; + + signal uart_tx_valid : std_logic; + signal uart_tx_data : std_logic_vector(DW-1 downto 0); + signal uart_tx_rdy : std_logic; + signal uart_tx_busy : std_logic; + --signal UART_RXD : std_logic; + signal uart_rx_valid : std_logic; + signal uart_rx_valid_d : std_logic; + signal uart_rx_data : std_logic_vector(DW-1 downto 0); + signal write_interrupt : std_logic; + signal write_interrupt_en : std_logic; + signal read_interrupt : std_logic; + signal read_interrupt_en : std_logic; + signal write_fifo_write_en : std_logic; + signal data_to_uart : std_logic_vector(DW-1 downto 0); + signal parity_error : std_logic; + signal write_data_parity : std_logic; + signal read_fifo_wr_req : std_logic; + signal read_fifo_wr_data : std_logic_vector(DW-1 downto 0); + signal read_fifo_wr_full : std_logic; + signal read_fifo_wr_used : std_logic_vector(6 downto 0); + signal read_fifo_read_en : std_logic; + signal read_data : std_logic_vector(DW-1 downto 0); + signal read_fifo_rd_empty : std_logic; + signal read_available : std_logic_vector(7 downto 0); + signal read_data_valid : std_logic; + signal write_space : std_logic_vector(7 downto 0); + signal write_fifo_wr_used : std_logic_vector(6 downto 0); + signal write_fifo_wr_full : std_logic; + signal write_fifo_read_en : std_logic; + signal write_fifo_rd_empty : std_logic; + signal write_data : std_logic_vector(DW-1 downto 0); + +begin + + --{{{ + uarttxrx_inst : uart_1MBd_uart_txrd_4 + generic map ( + OVERSAMPLE_RATE => OVERSAMPLE_RATE, + ENABLE_CLOCK_DIVIDER => ENABLE_CLOCK_DIVIDER + ) + port map ( + clk => clk, + + tx_en_in => '1', + tx_dv_in => uart_tx_valid, + tx_data_in => uart_tx_data, + + tx_out => UART_TXD, + tx_rdy_out => uart_tx_rdy, + tx_busy_out => uart_tx_busy, + + rx_in => UART_RXD, + rx_en_in => '1', + + de_out => open, + rx_error_out => open, + + rx_dv_out => uart_rx_valid, + rx_data_out => uart_rx_data + ); + --}}} + + IRQ_PROC : process + begin + wait until rising_edge(clk); + if reset = '1' then + irq <= '0'; + else + irq <= write_interrupt or read_interrupt; + end if; + end process; + + READDATA_PROC : process + begin + wait until rising_edge(clk); + if reset = '1' then + readdata <= (others => '0'); + elsif chipselect = '1' then + if address = '0' then + readdata <= x"00" & -- 31..24 + read_available & -- 23..16 + read_data_valid & -- 15 + "00000" & -- 14..10 + parity_error & -- 9 + '0' & -- 8 + read_data(DW-1 downto 0); -- 7..0 + else + readdata <= x"00" & + write_space & + "000000" & + write_interrupt & + read_interrupt & + "000000" & + write_interrupt_en & + read_interrupt_en; + + end if; + end if; + end process; + + READ_IRQEN_PROC : process + begin + wait until rising_edge(clk); + if reset = '1' then + read_interrupt_en <= '0'; + elsif chipselect = '1' and write = '1' and address = '1' then + read_interrupt_en <= writedata(0); + end if; + end process; + + WRITE_IRQEN_PROC : process + begin + wait until rising_edge(clk); + if reset = '1' then + write_interrupt_en <= '0'; + elsif chipselect = '1' and write = '1' and address = '1' then + write_interrupt_en <= writedata(1); + end if; + end process; + + READ_IRQ_PROC : process + begin + wait until rising_edge(clk); + if reset = '1' or read_interrupt_en = '0' then + read_interrupt <= '0'; + else + read_interrupt <= read_available(7) or (read_available(6) and read_available(5)); + end if; + end process; + + WRITE_IRQ_PROC : process + begin + wait until rising_edge(clk); + if reset = '1' or write_interrupt_en = '0' then + write_interrupt <= '0'; + else + write_interrupt <= write_space(7) or (write_space(6) and write_space(5)); + end if; + end process; + + WRITE_DATA_PROC : process + begin + wait until rising_edge(clk); + if chipselect = '1' and write = '1' and address = '0' and reset = '0' then + write_fifo_write_en <= '1'; + else + write_fifo_write_en <= '0'; + end if; + + if reset = '1' then + data_to_uart <= (others => '0'); + else + data_to_uart <= writedata(DW-1 downto 0); + end if; + + end process; + + parity_error <= '0'; + + read_fifo_read_en <= chipselect and read and not address; + + WRITE_PARITY_PROC : process (all) + variable parity : std_logic; + begin + parity := '0'; + if ODD_PARITY = 1 then + parity := '1'; + end if; + + for i in data_to_uart'range loop + parity := parity xor data_to_uart(i); + end loop; + + write_data_parity <= parity; + end process; + + read_fifo_inst : entity work.sync_fifo + generic map( + DATA_WIDTH => DW, + DEPTH => 128, + REGISTER_OUTPUT => "OFF", + DEVICE_FAMILY => DEVICE_FAMILY, + SHOWAHEAD => "ON", + CHECKING => "OFF" + ) + port map + ( + clk => clk, + rst_n => not reset, + -- + wrreq => read_fifo_wr_req and not read_fifo_wr_full, + wrdata => read_fifo_wr_data, + full => read_fifo_wr_full, + usedw => read_fifo_wr_used, + + rdreq => read_fifo_read_en and not read_fifo_rd_empty, + rddata => read_data, + + empty => read_fifo_rd_empty + ); + + read_available(7) <= read_fifo_wr_full; + read_available(6 downto 0) <= read_fifo_wr_used; + read_data_valid <= not read_fifo_rd_empty; + + WRITE_READFIFO_PROC : process + begin + wait until rising_edge(clk); + uart_rx_valid_d <= uart_rx_valid; + read_fifo_wr_req <= '0'; + read_fifo_wr_data <= uart_rx_data; + + if reset = '0' and uart_rx_valid = '1' and uart_rx_valid_d = '0' and read_fifo_wr_full = '0' then + read_fifo_wr_req <= '1'; + end if; + end process; + + sniff_strobe <= read_fifo_wr_req; + sniff_data <= read_fifo_wr_data; + + write_fifo_inst : entity work.sync_fifo + generic map( + DATA_WIDTH => DW, + DEPTH => 128, + REGISTER_OUTPUT => "OFF", + DEVICE_FAMILY => DEVICE_FAMILY, + SHOWAHEAD => "ON", + CHECKING => "OFF" + ) + port map + ( + clk => clk, + rst_n => not reset, + -- + wrreq => write_fifo_write_en and not write_fifo_wr_full, + wrdata => data_to_uart, + full => write_fifo_wr_full, + usedw => write_fifo_wr_used, + + rdreq => write_fifo_read_en and not write_fifo_rd_empty, + rddata => write_data, + + empty => write_fifo_rd_empty + ); + + write_space(7) <= write_fifo_wr_full; + write_space(6 downto 0) <= write_fifo_wr_used; + + READ_WRITEFIFO_PROC : process + begin + wait until rising_edge(clk); + uart_tx_valid <= '0'; + write_fifo_read_en <= '0'; + uart_tx_data <= write_data; + + if reset = '0' and uart_tx_rdy = '1' and uart_tx_busy = '0' and write_fifo_rd_empty = '0' and write_fifo_read_en = '0' then + uart_tx_valid <= '1'; + write_fifo_read_en <= '1'; + end if; + end process; + +end architecture rtl; diff --git a/generic_ip/uart_1MBd/4/src/uart_1MBd_uart_txrd_4.v b/generic_ip/uart_1MBd/4/src/uart_1MBd_uart_txrd_4.v new file mode 100644 index 0000000..6fdca51 --- /dev/null +++ b/generic_ip/uart_1MBd/4/src/uart_1MBd_uart_txrd_4.v @@ -0,0 +1,265 @@ +//---------------------------------------------------------------------------- +// Module Description +// +// This is a simple UART, capable of full duplex operation. +// Data format is fixed as 8N1, LSB first. +// It is intended to be used with a FIFO if necessary. +// Timings are set by parameters at instantiation. +// +// Interface: +// rx_dv is valid for 1 clock pulse +// rx_data_out is valid when dv is high +// tx_dv shall be valid for 1 clock pulse, after which tx_rdy is de-asserted. +// tx_data shall be valid when tx_dv is high. +// If tx_dv is asserted while tx_rdy is de-asserted, tx_dv is ignored. +// +//---------------------------------------------------------------------------- +// Revision Date Author Notes +// +// 01 22-02-2009 Gordon Leather First written +// +//---------------------------------------------------------------------------- + + +`resetall +`timescale 1ns/1ps +module uart_1MBd_uart_txrd_4 #( + parameter OVERSAMPLE_RATE = 5'd16, + parameter ENABLE_CLOCK_DIVIDER = 6'd52 +) +( + // Port Declarations + input wire clk, + output wire de_out, + output wire [7:0] rx_data_out, + output wire rx_dv_out, + input wire rx_en_in, + input wire rx_in, + output wire rx_error_out, + input wire [7:0] tx_data_in, + input wire tx_dv_in, + input wire tx_en_in, + output wire tx_out, + output wire tx_rdy_out, + output wire tx_busy_out +); + +// Parameters +// Internal Declarations +localparam DATA_BITS = 4'd8; +localparam EDGE_DETECT_POSITION = OVERSAMPLE_RATE/2; + +// RX State machine constants +localparam RX_IDLE = 3'd0; +localparam RX_GET_START = 3'd1; +localparam RX_GET_DATA = 3'd2; +localparam RX_GET_STOP = 3'd3; +localparam RX_GET_STOP_1 = 3'd4; + +// TX State machine constants +localparam TX_IDLE = 2'b0; + + + +// Internal Declarations +reg [OVERSAMPLE_RATE-1:0] rx_sr = 0; +reg [5:0] en_cnt = 6'd0; +reg [4:0] rx_bit_tmr = (OVERSAMPLE_RATE-5'd1); +reg [4:0] tx_bit_tmr = (OVERSAMPLE_RATE-5'd1); + +wire clk_en; +wire rx_neg_edge; +wire rx_bit_valid; +reg [2:0] rx_state = RX_IDLE; +reg [3:0] rx_bit_cnt = DATA_BITS - 4'd1; +reg rx_receiving = 1'b0; +reg rx_done = 1'b0; +reg rx_error = 1'b0; +reg [7:0] rx_data = 8'd0; + +wire tx_new_word; +wire tx_next_bit; +wire tx_last_bit; +wire tx_done; +reg [3:0] tx_bit_cnt = DATA_BITS + 4'd2; +reg tx_ignore_idle = 1'b0; + +reg [9:0] tx_data = 10'h3ff; // Extra bits allow easy addition of start bit to data. +reg tx_rdy = 1'b1; +reg tx_busy = 1'b0; + + + +// Output assignments +assign rx_data_out = rx_data; +assign rx_dv_out = rx_done; +assign rx_error_out = rx_error; + +assign tx_rdy_out = tx_rdy & ~tx_dv_in & tx_en_in; // Combinatorial output, often required for RDY interface to FIFOs +assign tx_out = tx_data[0]; +assign de_out = tx_en_in; + +assign tx_busy_out = tx_busy; + + +//---------------------------------------------------------------------------- +// Common Functions +//---------------------------------------------------------------------------- +// +// Clock enable generation +assign clk_en = (en_cnt == (ENABLE_CLOCK_DIVIDER-1)); + +always @(posedge clk) +begin + if (clk_en) en_cnt <= 6'd0; + else en_cnt <= en_cnt + 6'd1; +end +//---------------------------------------------------------------------------- +// END Common Functions +//---------------------------------------------------------------------------- + + + + + + + +//---------------------------------------------------------------------------- +// UART Transmitter +//---------------------------------------------------------------------------- +// +always @(posedge clk) +begin + if (tx_en_in) + begin + if (tx_new_word) tx_ignore_idle <= ~tx_busy; + else if (tx_next_bit) tx_ignore_idle <= 1'b0; + + if (tx_new_word) tx_data <= {tx_data_in, 1'b0, 1'b1}; // Data, Start bit, Idle bit. Ignore writes when busy, + else if (tx_next_bit) tx_data <= {1'b1, tx_data[9:1]}; // Shift data along. Shifting '1's in gives us the stop bit. + + if (~tx_busy | tx_next_bit) tx_bit_tmr <= (OVERSAMPLE_RATE-5'd1); + else if (clk_en) tx_bit_tmr <= tx_bit_tmr - 5'd1; + + if (tx_new_word) tx_bit_cnt <= DATA_BITS + 4'd2; + else if (tx_next_bit) tx_bit_cnt <= tx_bit_cnt - 4'd1; + + if (tx_new_word) tx_busy <= 1'b1; // This takes priority, to allow continuous transmission + else if (tx_done) tx_busy <= 1'b0; + + if (tx_new_word) tx_rdy <= 1'b0; + else if (~tx_busy | tx_last_bit) tx_rdy <= 1'b1; // Ready for new data as soon as the last bit has been transmitted, or when not busy. + end + else + begin + tx_bit_tmr <= (OVERSAMPLE_RATE-5'd1); + tx_bit_cnt <= DATA_BITS + 4'd2; + tx_data <= 10'h3ff; + tx_busy <= 1'b0; + tx_rdy <= 1'b1; + end +end + +assign tx_new_word = tx_dv_in & tx_rdy; // Ignore writes when not ready +assign tx_next_bit = clk_en & ((tx_busy & (tx_bit_tmr == 0)) | tx_ignore_idle); +assign tx_last_bit = tx_next_bit & (tx_bit_cnt == 1); +assign tx_done = tx_next_bit & (tx_bit_cnt == 0); +//---------------------------------------------------------------------------- +// END UART Transmitter +//---------------------------------------------------------------------------- + + + + + +//---------------------------------------------------------------------------- +// UART Receiver +//---------------------------------------------------------------------------- +// +// Rx edge detect and bit timer +always @(posedge clk) +begin + if (clk_en) + begin + if (rx_en_in) rx_sr <= {rx_sr[OVERSAMPLE_RATE-2:0], rx_in}; // Oversample Rx input + else rx_sr <= 0; // Reset required to avoid false triggers on restart + + if (rx_receiving && (rx_bit_tmr>0)) rx_bit_tmr <= rx_bit_tmr - 5'd1; // Timer for sampling bits + else rx_bit_tmr <= (OVERSAMPLE_RATE - 5'd1); + end +end + +assign rx_neg_edge = (rx_sr[EDGE_DETECT_POSITION] & ~rx_sr[EDGE_DETECT_POSITION-1]); + +assign rx_bit_valid = (rx_bit_tmr == 0); + + + + +// Rx State machine +// This state machine only supports 8N1 data format +always @(posedge clk) +begin + if (clk_en) + begin + if (rx_en_in) + begin + case (rx_state) + RX_GET_START : if (rx_neg_edge) + begin + rx_bit_cnt <= DATA_BITS- 4'd1; // Ensure correct number of bits are counted + rx_receiving <= 1'b1; + rx_state <= RX_GET_DATA; // State transition + end + + + RX_GET_DATA : if (rx_bit_valid) + begin + rx_data <= {rx_in, rx_data[7:1]}; // Register Data + rx_bit_cnt <= rx_bit_cnt - 4'd1; // and decrement bit counter + + if (rx_bit_cnt == 0) rx_state <= RX_GET_STOP; // Change state on last bit + end + + + RX_GET_STOP : if (rx_bit_valid) + begin + rx_error <= ~rx_in; // Stop bit should be a '1' + rx_state <= RX_GET_STOP_1; // State transition + end + + + RX_GET_STOP_1 : if (rx_bit_tmr == (OVERSAMPLE_RATE/2)) // Delay to ensure Stop bit is fully received + begin // This is necessary to prevent turnaround time problems + rx_done <= 1'b1; + rx_receiving <= 1'b0; + rx_state <= RX_IDLE; // State transition + end + + + default : begin + rx_receiving <= 1'b0; + rx_done <= 1'b0; + rx_error <= 1'b0; + + if (rx_en_in) rx_state <= RX_GET_START; // State transition + end + endcase + end + else + begin + rx_state <= RX_IDLE; + rx_done <= 1'b0; + end + end + else + begin + rx_done <= 1'b0; // Required so only one pulse given. + end +end +//---------------------------------------------------------------------------- +// END UART Receiver +//---------------------------------------------------------------------------- + + +endmodule diff --git a/generic_ip/uart_1MBd/versions.txt b/generic_ip/uart_1MBd/versions.txt new file mode 100644 index 0000000..5eb109f --- /dev/null +++ b/generic_ip/uart_1MBd/versions.txt @@ -0,0 +1 @@ +v4 fpga vendor independent. Removed reliance on Altera copyright serialiser code