Sure! Here's a detailed explanation of each section of the VHDL code:
Library and Entity Declarations
library ieee;
use ieee.std_logic_1164.ALL;
use ieee.numeric_std.all;
entity UART_RX is
generic (
g_CLKS_PER_BIT : integer := 115
);
port (
i_Clk : in std_logic;
i_RX_Serial : in std_logic;
o_RX_DV : out std_logic;
o_RX_Byte : out std_logic_vector(7 downto 0)
);
end UART_RX;
- The first section
library ieee specifies the IEEE standard libraries that are going to be used in the VHDL code.
- The second section
use ieee.std_logic_1164.ALL specifies that the VHDL design will use std_logic_1164 package, which provides the standard logic data type std_logic required to represent the state of digital signals.
- The third section
use ieee.numeric_std.all specifies the numeric_std package, which provides a wider range of operators for maths and conversions, such as the unsigned and signed types.
- The entity declaration describes the inputs and the outputs of the design. This particular entity has four ports:
i_Clk: The system clock input signal.
i_RX_Serial: The serial input signal.
o_RX_DV: Output signal indicating the arrival of a full byte.
o_RX_Byte: 8-bit bus to transmit the received byte data.
Architecture Declaration
architecture rtl of UART_RX is
type t_SM_Main is (s_Idle, s_RX_Start_Bit, s_RX_Data_Bits,s_RX_Stop_Bit, s_Cleanup);
signal r_SM_Main : t_SM_Main := s_Idle;
signal r_RX_Data_R : std_logic := '0';
signal r_RX_Data : std_logic := '0';
signal r_Clk_Count : integer range 0 to g_CLKS_PER_BIT-1 := 0;
signal r_Bit_Index : integer range 0 to 7 := 0;
signal r_RX_Byte : std_logic_vector(7 downto 0) := (others => '0');
signal r_RX_DV : std_logic := '0';
begin
- The VHDL architectural statements image the hardware modules and their interconnections. The
rtl architecture type is used in this code.
- A type declaration
t_SM_Main is defined to capture the states of the state machine.
- The
signal declarations indicate the register variables used in this logic block.
r_SM_Main is a state machine variable, which takes one of the states that have been defined by the type t_SM_Main. It is initialized to s_Idle.
r_RX_Data_R and r_RX_Data are register variables used for double registering the data for avoiding metastability issues.
r_Clk_Count is an integer variable used as a counter for the number of clock cycles to detect the state of the incoming serial data.
r_Bit_Index is an index variable that tracks the number of bits received.
r_RX_Byte is a standard logic vector used to store the received byte.
r_RX_DV is the output signal indicating a full byte has been received.
Double Register for Incoming Data
p_SAMPLE : process (i_Clk)
begin
if rising_edge(i_Clk) then
r_RX_Data_R <= i_RX_Serial;
r_RX_Data <= r_RX_Data_R; end if; end process p_SAMPLE;
- The
p_SAMPLE process double-registers the incoming data in order to avoid metastability issues. This prevents the data from being interpreted incorrectly due to clock domain crossings.
- The double register is created by
r_RX_Data_R (first register) and r_RX_Data (second register), with the parallel inputs of the registers being i_RX_Serial and r_RX_Data_R (respectively).
- On the rising edge of the clock (
if rising_edge(i_Clk) then), r_RX_Data_R is loaded with the value of i_RX_Serial and r_RX_Data is assigned the value of r_RX_Data_R.
State Machine
p_UART_RX : process (i_Clk) begin
if rising_edge(i_Clk) then
case r_SM_Main is
when s_Idle =>
r_RX_DV <= '0';
r_Clk_Count <= 0;
r_Bit_Index <= 0;
if r_RX_Data = '0' then
r_SM_Main <= s_RX_Start_Bit;
else
r_SM_Main <= s_Idle;
end if;
when s_RX_Start_Bit =>
if r_Clk_Count = (g_CLKS_PER_BIT-1)/2 then
if r_RX_Data = '0' then
r_Clk_Count <= 0;
r_SM_Main <= s_RX_Data_Bits;
else
r_SM_Main <= s_Idle;
end if;
else
r_Clk_Count <= r_Clk_Count + 1;
r_SM_Main <= s_RX_Start_Bit;
end if;
when s_RX_Data_Bits =>
if r_Clk_Count < g_CLKS_PER_BIT-1 then
r_Clk_Count <= r_Clk_Count + 1;
r_SM_Main <= s_RX_Data_Bits;
else
r_Clk_Count <= 0;
r_RX_Byte(r_Bit_Index) <= r_RX_Data;
if r_Bit_Index < 7 then
r_Bit_Index <= r_Bit_Index + 1;
r_SM_Main <= s_RX_Data_Bits;
else
r_Bit_Index <= 0;
r_SM_Main <= s_RX_Stop_Bit;
end if;
end if;
when s_RX_Stop_Bit =>
if r_Clk_Count < g_CLKS_PER_BIT-1 then
r_Clk_Count <= r_Clk_Count + 1;
r_SM_Main <= s_RX_Stop_Bit;
else
r_RX_DV <= '1';
r_Clk_Count <= 0;
r_SM_Main <= s_Cleanup;
end if;
when s_Cleanup =>
r_SM_Main <= s_Idle;
r_RX_DV <= '0';
when others =>
r_SM_Main <= s_Idle;
end case;
end if;
end process p_UART_RX;
- The
p_UART_RX process is the main state machine that controls the receiving of the incoming data and actions the state transitions during a byte reception.
- A
case statement is used to switch between different states of the state machine r_SM_Main.
s_Idle is the default state in which the receiver module is waiting for the start bit to be detected.
s_RX_Start_Bit is the state where the code checks if the middle of the start bit has been detected. If so, it transitions to the s_RX_Data_Bits state.
s_RX_Data_Bits is the state in which the receiver reads the data bits and saves them in the r_RX_Byte vector.
s_RX_Stop_Bit is the state where the receiver waits for the stop bit to be detected before transitioning to the s_Cleanup state.
s_Cleanup is the state where the receiver waits for an additional clock cycle to perform cleanup operations before transitioning to the s_Idle state.
r_RX_DV is set to 0 in the s_Idle state to represent that no byte has been received yet.
- The
r_Clk_Count variable is used to control the number of clock cycles to detect the incoming serial data.
r_SM_Main variable is used to update the state of the state machine based on various inputs and variables.
- A
when others statement is used as a default fallback that resets the state machine to s_Idle.
Output Signals
o_RX_DV <= r_RX_DV;
o_RX_Byte <= r_RX_Byte;
end rtl;
o_RX_DV is the output signal for indicating a full byte has been received. It is set to r_RX_DV.
o_RX_Byte is an output port of 8-bit bus that is used to transfer the received byte data. It receives its bytes from r_RX_Byte.
- The
rtl block declares the end of the architecture.