Public paste
Undefined
By: Guest | Date: Oct 31 2011 00:23 | Format: VHDL | Expires: never | Size: 4.01 KB | Hits: 866

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. use work.math_pkg.all;
  5.  
  6. entity serial_port_receiver_fsm is
  7.         generic(
  8.                         CLK_DIVISOR : integer
  9.                         );
  10.         port(
  11.                         clk, res_n, rx : in std_logic;
  12.                         data : out std_logic_vector(7 downto 0);
  13.                         data_new : out std_logic
  14.                         );
  15. end serial_port_receiver_fsm;
  16.  
  17. architecture behav of serial_port_receiver_fsm is
  18.         type rcv_state is (
  19.                                                 IDLE,
  20.                                                 WAIT_START_BIT,
  21.                                                 GOTO_MIDDLE_OF_START_BIT,
  22.                                                 MIDDLE_OF_START_BIT,
  23.                                                 WAIT_DATA_BIT,
  24.                                                 MIDDLE_OF_DATA_BIT,
  25.                                                 WAIT_STOP_BIT,
  26.                                                 MIDDLE_OF_STOP_BIT
  27.                                                 );
  28.         signal state, next_state: rcv_state;
  29.         signal clk_count, next_clk_count: std_logic_vector(log2c(CLK_DIVISOR) downto 0);
  30.         signal bit_count, next_bit_count: std_logic_vector(2 downto 0);
  31.         signal data_int, next_data_int: std_logic_vector(7 downto 0);
  32.         signal data_out, next_data_out: std_logic_vector(7 downto 0);
  33.         signal data_new_out, next_data_new_out: std_logic;
  34.        
  35.         begin
  36.         synchronisation: process(clk, res_n)
  37.                 begin
  38.                         if res_n = '0' then
  39.                                 state <= IDLE;
  40.                                 clk_count <= (others => '0');
  41.                                 bit_count <= (others => '0');
  42.                                 data <= (others => '0');
  43.                                 data_new <= '0';
  44.                                 data_int <= (others => '0');
  45.                         elsif clk'event and clk = '1' then
  46.                                 state <= next_state;
  47.                                 clk_count <= next_clk_count;
  48.                                 bit_count <= next_bit_count;
  49.                                 data_out <= next_data_out;
  50.                                 data <= next_data_out;
  51.                                 data_new_out <= next_data_new_out;
  52.                                 data_new <= next_data_new_out;
  53.                                 data_int <= next_data_int;
  54.                         end if;
  55.         end process synchronisation;
  56.        
  57.         state_transition: process(rx, clk_count, bit_count)
  58.                 begin
  59.                         next_state <= IDLE;
  60.                         case state is
  61.                                 when IDLE =>   
  62.                                         if rx = '1' then
  63.                                                 next_state <= WAIT_START_BIT;
  64.                                         end if;
  65.                                 when WAIT_START_BIT =>
  66.                                         if rx = '0' then
  67.                                                 next_state <= GOTO_MIDDLE_OF_START_BIT;
  68.                                         end if;
  69.                                 when GOTO_MIDDLE_OF_START_BIT =>
  70.                                         if clk_count = std_logic_vector(to_unsigned((CLK_DIVISOR/2)-2,clk_count'length)) then
  71.                                                 next_state <= MIDDLE_OF_START_BIT;
  72.                                         end if;
  73.                                 when MIDDLE_OF_START_BIT =>
  74.                                         next_state <= WAIT_DATA_BIT;
  75.                                 when WAIT_DATA_BIT =>
  76.                                         if clk_count = std_logic_vector(to_unsigned(CLK_DIVISOR - 2,clk_count'length)) then
  77.                                                 next_state <= WAIT_DATA_BIT;
  78.                                         end if;
  79.                                 when MIDDLE_OF_DATA_BIT =>
  80.                                         if bit_count < std_logic_vector(to_unsigned(7,bit_count'length)) then
  81.                                                 next_state <= WAIT_DATA_BIT;
  82.                                         elsif bit_count = std_logic_vector(to_unsigned(7,bit_count'length)) then
  83.                                                 next_state <= WAIT_STOP_BIT;
  84.                                         end if;
  85.                                 when WAIT_STOP_BIT =>
  86.                                         if clk_count = std_logic_vector(to_unsigned(CLK_DIVISOR - 2,clk_count'length)) then
  87.                                                 next_state <= MIDDLE_OF_STOP_BIT;
  88.                                         end if;
  89.                                 when MIDDLE_OF_STOP_BIT =>
  90.                                         if rx = '0' then
  91.                                                 next_state <= IDLE;
  92.                                         elsif rx = '1' then
  93.                                                 next_state <= WAIT_START_BIT;
  94.                                         end if;
  95.                         end case;
  96.         end process state_transition;
  97.        
  98.         output_logic: process(state,clk_count,bit_count,data_int,data_out,data_new_out)
  99.                 begin
  100.                         next_clk_count <= clk_count;
  101.                         next_bit_count <= bit_count;
  102.                         next_data_new_out <= '0';
  103.                         next_data_out <= data_out;
  104.                         next_data_int <= data_int;
  105.                         case state is
  106.                                 when IDLE =>
  107.                                 when WAIT_START_BIT =>
  108.                                         next_bit_count <= (others => '0');
  109.                                         next_clk_count <= (others => '0');
  110.                                 when GOTO_MIDDLE_OF_START_BIT =>
  111.                                         next_clk_count <= std_logic_vector(unsigned(clk_count) + 1);
  112.                                 when MIDDLE_OF_START_BIT =>
  113.                                         next_clk_count <= (others => '0');
  114.                                 when WAIT_DATA_BIT =>
  115.                                         next_clk_count <= std_logic_vector(unsigned(clk_count) + 1);
  116.                                 when MIDDLE_OF_DATA_BIT =>
  117.                                         next_clk_count <= (others => '0');
  118.                                         next_bit_count <= std_logic_vector(unsigned(bit_count) + 1);
  119.                                         next_data_int <= rx & data_int;
  120.                                 when WAIT_STOP_BIT =>
  121.                                         next_clk_count <= std_logic_vector(unsigned(clk_count) + 1);
  122.                                 when MIDDLE_OF_STOP_BIT =>
  123.                                         next_data_new_out <= '1';
  124.                                         next_data_out <= data_int;
  125.                         end case;
  126.         end process output_logic;
  127. end behav;