Line 38... |
Line 38... |
constant UART_START : std_logic := '0';
|
constant UART_START : std_logic := '0';
|
constant PARITY_EN : std_logic := '1';
|
constant PARITY_EN : std_logic := '1';
|
constant RST_LVL : std_logic := '1';
|
constant RST_LVL : std_logic := '1';
|
|
|
-- Types
|
-- Types
|
type state is (idle,data,parity,stop1,stop2); -- Stop1 and Stop2 are inter frame gap
|
type state is (idle,data,parity,stop1,stop2); -- Stop1 and Stop2 are inter frame gap signals
|
|
|
-- Signals
|
-- RX Signals
|
signal rx_fsm : state; -- Control of reception
|
signal rx_fsm : state; -- Control of reception
|
signal tx_fsm : state; -- Control of transmission
|
signal rx_clk_en : std_logic; -- Received clock enable
|
signal clock_en : std_logic; -- Internal clock enable
|
signal rx_rcv_init : std_logic; -- Start of reception
|
|
signal rx_par_bit : std_logic; -- Calculated Parity bit
|
-- RX Data Temp
|
signal rx_data_tmp : std_logic_vector(7 downto 0); -- Serial to parallel converter
|
signal rx_par_bit : std_logic;
|
signal rx_data_cnt : std_logic_vector(2 downto 0); -- Count received bits
|
signal rx_data_tmp : std_logic_vector(7 downto 0);
|
|
signal rx_data_cnt : std_logic_vector(2 downto 0);
|
|
|
|
-- TX Data Temp
|
-- TX Signals
|
signal tx_par_bit : std_logic;
|
signal tx_fsm : state; -- Control of transmission
|
signal tx_data_tmp : std_logic_vector(7 downto 0);
|
signal tx_clk_en : std_logic; -- Transmited clock enable
|
signal tx_data_cnt : std_logic_vector(2 downto 0);
|
signal tx_par_bit : std_logic; -- Calculated Parity bit
|
|
signal tx_data_tmp : std_logic_vector(7 downto 0); -- Parallel to serial converter
|
|
signal tx_data_cnt : std_logic_vector(2 downto 0); -- Count transmited bits
|
|
|
begin
|
begin
|
|
|
clock_manager:process(clk)
|
tx_clk_gen:process(clk)
|
variable counter : integer range 0 to conv_integer((CLK_FREQ*1_000_000)/SER_FREQ-1);
|
variable counter : integer range 0 to conv_integer((CLK_FREQ*1_000_000)/SER_FREQ-1);
|
begin
|
begin
|
if clk'event and clk = '1' then
|
if clk'event and clk = '1' then
|
-- Normal Operation
|
-- Normal Operation
|
if counter = (CLK_FREQ*1_000_000)/SER_FREQ-1 then
|
if counter = (CLK_FREQ*1_000_000)/SER_FREQ-1 then
|
clock_en <= '1';
|
tx_clk_en <= '1';
|
counter := 0;
|
counter := 0;
|
else
|
else
|
clock_en <= '0';
|
tx_clk_en <= '0';
|
counter := counter + 1;
|
counter := counter + 1;
|
end if;
|
end if;
|
-- Reset condition
|
-- Reset condition
|
if rst = RST_LVL then
|
if rst = RST_LVL then
|
|
tx_clk_en := '0';
|
counter := 0;
|
counter := 0;
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
tx_proc:process(clk)
|
tx_proc:process(clk)
|
variable data_cnt : std_logic_vector(2 downto 0);
|
variable data_cnt : std_logic_vector(2 downto 0);
|
begin
|
begin
|
if clk'event and clk = '1' then
|
if clk'event and clk = '1' then
|
if clock_en = '1' then
|
if tx_clk_en = '1' then
|
-- Default values
|
-- Default values
|
tx_end <= '0';
|
tx_end <= '0';
|
tx <= UART_IDLE;
|
tx <= UART_IDLE;
|
-- FSM description
|
-- FSM description
|
case tx_fsm is
|
case tx_fsm is
|
Line 138... |
Line 139... |
end if;
|
end if;
|
end if;
|
end if;
|
end if;
|
end if;
|
end process;
|
end process;
|
|
|
|
rx_start_detect:process(clk)
|
|
variable deb_buf : std_logic_vector(3 downto 0);
|
|
variable deb_val : std_logic;
|
|
variable deb_old : std_logic;
|
|
begin
|
|
if clk'event and clk = '1' then
|
|
-- Store previous debounce value
|
|
deb_old := deb_val;
|
|
-- Debounce logic
|
|
if deb_buf = "0000" then
|
|
deb_val := '0';
|
|
elsif deb_buf = "1111" then
|
|
deb_val := '1';
|
|
end if;
|
|
-- Data storage to debounce
|
|
deb_buf := deb_buf(2 downto 0) & rx;
|
|
|
|
-- Check RX idle state
|
|
if rx_fsm = idle then
|
|
-- Falling edge detection
|
|
if deb_old = '1' and deb_val = '0' then
|
|
rx_rcv_init <= '1';
|
|
end if;
|
|
-- Default assignments
|
|
else
|
|
rx_rcv_init <= '0';
|
|
end if;
|
|
-- Reset condition
|
|
if rst = RST_LVL then
|
|
deb_old := '0';
|
|
deb_val := '0';
|
|
deb_buf <= (others=>'0');
|
|
rx_rcv_init <= '0';
|
|
end if;
|
|
end if;
|
|
end if;
|
|
|
|
|
|
rx_clk_gen:process(clk)
|
|
variable counter : integer range 0 to conv_integer((CLK_FREQ*1_000_000)/SER_FREQ-1);
|
|
begin
|
|
if clk'event and clk = '1' then
|
|
-- Normal Operation
|
|
if counter = (CLK_FREQ*1_000_000)/SER_FREQ-1 and rx_rcv_init = '1' then
|
|
rx_clk_en <= '1';
|
|
counter := 0;
|
|
else
|
|
rx_clk_en <= '0';
|
|
counter := counter + 1;
|
|
end if;
|
|
-- Reset condition
|
|
if rst = RST_LVL then
|
|
rx_clk_en <= '0';
|
|
counter := 0;
|
|
end if;
|
|
end if;
|
|
end process;
|
|
|
rx_proc:process(clk)
|
rx_proc:process(clk)
|
begin
|
begin
|
if clk'event and clk = '1' then
|
if clk'event and clk = '1' then
|
if clock_en = '1' then
|
if rx_clk_en = '1' then
|
-- Default values
|
-- Default values
|
rx_ready <= '0';
|
rx_ready <= '0';
|
-- FSM description
|
-- FSM description
|
case rx_fsm is
|
case rx_fsm is
|
-- Wait to transfer data
|
-- Wait to transfer data
|
when idle =>
|
when idle =>
|
if rx = UART_START then
|
if rx_rcv_init = '1' then
|
rx_fsm <= data;
|
rx_fsm <= data;
|
end if;
|
end if;
|
rx_par_bit <= '0';
|
rx_par_bit <= '0';
|
rx_data_cnt <= (others=>'0');
|
rx_data_cnt <= (others=>'0');
|
-- Data receive
|
-- Data receive
|