Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: VHDL | Size: 3.55 KB | Hits: 82 | Expires: Never
Copy text to clipboard
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.STD_LOGIC_ARITH.ALL;
  4. use IEEE.STD_LOGIC_UNSIGNED.ALL;
  5. --------------------------------------------
  6. entity RS232_RX is
  7.     Port ( RxDO : out  STD_LOGIC_VECTOR (7 downto 0);
  8.            RxRdy : out  STD_LOGIC;
  9.            Reset : in  STD_LOGIC;
  10.            Clk : in  STD_LOGIC;
  11.            RS232_RxD : in  STD_LOGIC;
  12.                           idx_test: out std_logic_vector( 3 downto 0 ));
  13. end RS232_RX;
  14. --------------------------------------------
  15. architecture Behavioral of RS232_RX is
  16.        
  17.         -- stany FSM
  18.         type state_type is (
  19.         sReset,
  20.         sWait,
  21.         sSync,
  22.         sReady,
  23.         sBusy);
  24.        
  25.         -- sygnaly pomocnicze FSM
  26.         signal State, next_State : state_type;
  27.        
  28.         -- licznik 434*50Mhz = 115207Hz
  29.         signal counter: std_logic_vector( 8 downto 0 ) := ( others => '0' );
  30.        
  31.         -- licznik 217*50Mhz =~ 57600Hz
  32.         signal fcount : std_logic_vector ( 7 downto 0 ) := ( others => '0' );
  33.        
  34.         -- indeks bitowy  1b start + 8b dane + 1b stop
  35.         signal idx: std_logic_vector( 3 downto 0 ) := ( others => '0' );
  36.        
  37.         -- kopia sygnalu TxDI(+bit start,stop) zatrzaskiwana gdy TxStart = '1', a bylo '0'
  38.         signal TxDO_cpy : STD_LOGIC_VECTOR (9 downto 0) := ( others => '0' );
  39.        
  40.        
  41. begin
  42. -----------------------------------------------------
  43. FSM_zmiana: process (Clk)
  44.         begin
  45.         if rising_edge(clk) then
  46.                         -------------------
  47.                 if State = sReset then
  48.                         counter <= (others => '0');
  49.                         idx <= (others => '0');
  50.                         fcount <= (others => '0');
  51.                         TxDO_cpy <= (others => '0');
  52.                         --------------------
  53.                 elsif State = sSync then
  54.                         if fcount = "11011001" then
  55.                                 TxDO_cpy (conv_integer(idx)) <= RS232_RxD;
  56.                                 idx <= idx + 1;
  57.                         else
  58.                                 fcount <= fcount + 1;
  59.                         end if;
  60.                         --------------------
  61.                 elsif State = sBusy then
  62.                         if counter = "110110010" then
  63.                                 counter <= (others => '0');
  64.                                 TxDO_cpy (conv_integer(idx)) <= RS232_RxD;
  65.                                 idx <= idx + 1;
  66.                         else
  67.                                 counter <= counter + 1;
  68.                         end if;
  69.                         --------------------
  70.                 end if;
  71.         end if;
  72.         end process FSM_zmiana;
  73. -----------------------------------------------------
  74. FSM_next: process (Clk,Reset)
  75.         begin
  76.                 if Reset = '1' then state <= sReset;
  77.                 elsif rising_edge(Clk) then state <= next_State;
  78.                 end if;
  79.         end process FSM_next;
  80. -----------------------------------------------------  
  81. FSM_przejsc: process (State,RS232_RxD,idx)
  82.         begin
  83.                 next_state <= state;
  84.                 case State is
  85.                         when sReset => next_state <= sWait;    
  86.                         --------------------------------------------
  87.                         when sBusy  => if conv_integer(idx) < 10 then
  88.                                                                                 next_state <= sBusy;
  89.                                                                         else
  90.                                                                                 next_state <= sReady;
  91.                                                                 end if;
  92.                         --------------------------------------------                                   
  93.                         when sWait  => if RS232_RxD = '0' then
  94.                                                         next_state <= sSync;
  95.                                                                 else next_state <= sWait;
  96.                                                                 end if;
  97.                         --------------------------------------------
  98.                         when sReady => next_state <= sReset;
  99.                         --------------------------------------------
  100.                         when sSync => if idx = "0000" then
  101.                                                           next_state <= sSync;
  102.                                                           else
  103.                                                           next_state <= sBusy;
  104.                                                           end if;
  105.                 end case;
  106.         end process FSM_przejsc;
  107. -----------------------------------------------------
  108. FSM_wyjsc: process (State)
  109.         begin
  110.         case State is
  111.         when sReset => RxDO <= "00000000";
  112.                                                 RxRdy <= '0';
  113.         when sWait      => RxDO <= "00000000";
  114.                                                 RxRdy <= '0';
  115.         when sSync  => RxDO <= "00000000";
  116.                                                 RxRdy <= '0';
  117.         when sBusy  => RxDO <= "00000000";
  118.                                                 RxRdy <= '0';
  119.         when sReady => RxDO <= TxDO_cpy (9 downto 2);
  120.                                                 RxRdy <= '1';
  121.         end case;
  122.         end process FSM_wyjsc;
  123. -----------------------------------------------------
  124. idx_test <= idx;
  125. end Behavioral;