Sure, here is a detailed explanation of each section of the VHDL code:
Libraries:
library ieee;
use ieee.std_logic_1164.ALL;
use ieee.numeric_std.all;
These are the standard VHDL libraries that are used to define the data types and operators used by the module.
Entity declaration:
entity UART_RX is
generic (
g_CLKS_PER_BIT : integer := 115 -- Needs to be set correctly
);
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;
This is the declaration of UART_RX module's entity that defines its interface.
- The entity contains a generic "g_CLKS_PER_BIT" integer that specifies the number of clock cycles required to sample a single bit of data.
- The entity contains four ports:
- "i_Clk": input clock.
- "i_RX_Serial": input serial data.
- "o_RX_DV": output data valid signal.
- "o_RX_Byte": output 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; -- 8 Bits Total
signal r_RX_Byte : std_logic_vector(7 downto 0) := (others => '0');
signal r_RX_DV : std_logic := '0';
begin
This section defines the internals of the UART_RX module.
- A FSM is defined (type "t_SM_Main") with five states.
- The signal "r_SM_Main" is the current state of the state machine, initialized to "s_Idle".
- "r_RX_Data_R" and "r_RX_Data" are double-registered signals that store the incoming serial data.
- "r_Clk_Count" is a counter that is used to count the number of clock cycles required to sample a single bit of data.
- "r_Bit_Index" is a counter that increments when a bit is received, up to a value of 7 which represents the final bit.
- "r_RX_Byte" is an 8-bit signal that stores the received data byte.
- "r_RX_DV" is a data valid signal that is used to indicate that a byte of data has been received.
Double-Register process:
-- Purpose: Double-register the incoming data.
-- This allows it to be used in the UART RX Clock Domain.
-- (It removes problems caused by metastabiliy)
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;
This process is used to synchronize the incoming serial data with the internal clock domain by double-registering the data to remove problems caused by metastability.
UART Receive state machine:
-- Purpose: Control RX 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 -- Start bit detected
r_SM_Main <= s_RX_Start_Bit;
else
r_SM_Main <= s_Idle;
end if;
-- Check middle of start bit to make sure it's still low
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; -- reset counter since we found the middle
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;
This is the process that controls the UART Receive FSM.
- The process waits for a rising edge on the input clock signal "i_Clk".
- Depending on the current state of the FSM, it executes the relevant operations.
- When the incoming serial data receives a start bit, the state transitions to "s_RX_Start_Bit".
- The process waits for half of the bit period to check the middle of the start bit to make sure it's still low.
- When the middle of the start bit is detected, the FSM transitions to "s_RX_Data_Bits" to receive the actual data bits.
- In this state, the FSM waits for a complete bit period and samples the data bit.
- The received bit is then stored in the corresponding position of "r_RX_Byte".
- Once all eight bits are received, the FSM transitions to "s_RX_Stop_Bit" to wait for the stop bit.
- When the stop bit is detected, the FSM sets "r_RX_DV" to '1' to indicate valid data is available.
- The state machine then transitions to "s_Cleanup" to complete the transaction and reset the state machine.
- Finally, the FSM transitions to "s_Idle" and resets the RX_DV signal.
Assigning the output signals:
o_RX_DV <= r_RX_DV;
o_RX_Byte <= r_RX_Byte;
end rtl;
This is the final stage of the VHDL code where the output signals "r_RX_DV" and "r_RX_Byte" are assigned to the corresponding output ports "o_RX_DV" and "o_RX_Byte".