Advertisement
Jerry77224

Untitled

May 19th, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 3.00 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.all;
  3. use IEEE.NUMERIC_STD.all;
  4. use IEEE.std_logic_unsigned.all;
  5.  
  6. entity serialrx is
  7.   generic (
  8.     CLOCKRATE : integer := 100000000;
  9.     BAUDRATE  : integer := 115200);
  10.   port (
  11.     clk : in std_logic;
  12.     rst : in std_logic;
  13.     dout : out std_logic_vector(7 downto 0);
  14.     rxdone : out std_logic;
  15.     RX : in std_logic
  16.   );
  17.  
  18. end serialrx;
  19.  
  20. architecture Behavioral of serialrx is
  21.  
  22.   constant TBIT : integer := CLOCKRATE / BAUDRATE;
  23.  
  24.   type states is (IDLE, START, DATA, STOP);
  25.   signal state, state_next : states;
  26.  
  27.   signal rx_start, rx_negedge : std_logic;
  28.   signal rdata, rdata_reg : std_logic_vector(7 downto 0);
  29.   signal rbits, rbits_reg : integer range 0 to 8;
  30.   signal rxdone_reg : std_logic;
  31.   signal timecnt, tmax : integer range 0 to TBIT;
  32.   signal rxd : std_logic_vector(2 downto 0);
  33.  
  34.  
  35. begin
  36.  
  37.   process (clk) begin
  38.     if rising_edge(clk) then
  39.       rxd <= rxd(1 downto 0) & RX;
  40.     end if;
  41.   end process;
  42.  
  43.   rx_negedge <= '1' when rxd(2 downto 1) = "10" else '0';
  44.   rx_start <= '1' when (rx_negedge = '1' AND (state = IDLE)) else '0';
  45.  
  46.   process (clk) begin
  47.     if rising_edge(clk) then
  48.       if rst then
  49.         timecnt <= 0;
  50.       elsif timecnt < tmax then
  51.         timecnt <= timecnt + 1;
  52.       else
  53.         timecnt <= 0;
  54.       end if;
  55.     end if;
  56.   end process;
  57.  
  58.   process (clk) begin
  59.     if rising_edge(clk) then
  60.       if rst then
  61.         state <= IDLE;
  62.       else
  63.         state <= state_next;
  64.       end if;
  65.     end if;
  66.   end process;
  67.  
  68.   process (clk) begin
  69.     if rising_edge(clk) then
  70.       if rst then
  71.         rdata_reg <= (others => '0');
  72.         rbits_reg <= 0;
  73.         rxdone_reg <= '0';
  74.       else
  75.         rdata_reg <= rdata;
  76.         rbits_reg <= rbits;
  77.         rxdone_reg <= rxdone;
  78.       end if;
  79.     end if;
  80.   end process;
  81.  
  82.   process (all) begin
  83.     rdata <= rdata_reg;
  84.     rbits <= rbits_reg;
  85.     rxdone <= rxdone_reg;
  86.     case(state) is
  87.     when IDLE =>
  88.       rxdone <= '0';
  89.       tmax <= 0;
  90.       rbits <= 0;
  91.       if rx_start then
  92.         state_next <= START;
  93.       else
  94.         state_next <= IDLE;
  95.       end if;
  96.     when START =>
  97.       rdata <= (others => '0');
  98.       rbits <= 0;
  99.       tmax <= TBIT/2 - 1;
  100.       if timecnt = tmax then
  101.         state_next <= DATA;
  102.       else
  103.         state_next <= START;
  104.       end if;
  105.     when DATA =>
  106.       tmax <= TBIT - 1;
  107.       state_next <= DATA;
  108.       if timecnt = tmax then
  109.         rdata <= rxd(2) & rdata(7 downto 1);
  110.         if rbits = 7 then
  111.           state_next <= STOP;
  112.         else
  113.           rbits <= rbits_reg + 1;
  114.         end if;
  115.       end if;
  116.     when STOP =>
  117.       tmax <= TBIT - 1;
  118.       rbits <= 0;
  119.       if timecnt = tmax then
  120.         rxdone <= '1';
  121.         state_next <= IDLE;
  122.       else
  123.         state_next <= STOP;
  124.       end if;
  125.     when others =>
  126.       state_next <= IDLE;
  127.       rxdone <= '0';
  128.       rdata <= (others => '0');
  129.       rbits <= 0;
  130.     end case;
  131.   end process;
  132.  
  133.   dout <= rdata_reg;
  134.  
  135. end Behavioral;
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement