大神用VHDL写的uart_rx模块,没看明白,请问是在哪里接收的数据?cnt是什么?

2019-07-15 20:52发布

  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;

  4. entity uart_rx is
  5. port(
  6.                 rst_n        :in std_logic:='0';
  7.                 clk                :in std_logic:='0';               
  8.                 data_bits        :in std_logic_vector(3 downto 0):="0000";
  9.                 parity_en        :in std_logic_vector(2 downto 0):="000";
  10.                 rx_bit_in        :in std_logic:='1';
  11.                 rx_dv                :out std_logic:='0';
  12.                 rx_data                :out std_logic_vector(7 downto 0):=(others=>'0');
  13.                 error                :out std_logic:='0'
  14. );
  15. end entity;

  16. architecture rtl of uart_rx is

  17. type status_type is ( s_idle,
  18.                                                          s_rx_start_bit,
  19.                                                          s_rx_data,
  20.                                                          s_rx_parity,
  21.                                                          s_stop_bit);
  22. signal c_status:status_type:=s_idle;
  23. signal cnt:natural range 0 to 31:=0;
  24. signal rx_data_r:std_logic_vector(7 downto 0):=(others=>'0');
  25. --signal data_bits_r:std_logic_vector(3 downto 0):="0000";        --0101=5 stop bit --0110=6 stop bit --0111=7 stop bit --1000=8 stop bit
  26. --signal parity_en_r:std_logic_vector(2 downto 0):="000";        --0=no parity --1=0 parity --2=1 parity --3=odd parity --4=even parity
  27. signal bit_index:natural range 0 to 8:=0;
  28. signal parity_r:std_logic:='0';
  29. signal rx_bit_r:std_logic:='0';

  30. begin
  31.         
  32.         --数据同步 data synchronization
  33.         process(clk)
  34.         begin
  35.         if rising_edge(clk) then
  36.                 rx_bit_r<=rx_bit_in;
  37.         else
  38.         null;
  39.         end if;
  40.         end process;
  41.         
  42.         process(rst_n,clk)
  43.         begin
  44.         if rst_n='0' then
  45.                 c_status<=s_idle;
  46.                 rx_dv<='0';
  47.                 rx_data<=(others=>'0');
  48.                 rx_data_r<=(others=>'0');
  49.                 error<='0';
  50.                 bit_index<=0;
  51.                 cnt<=0;
  52.         elsif rising_edge(clk) then
  53.                 case c_status is
  54.                
  55.                 when s_idle                                =>        if rx_bit_r='0' then
  56.                                                                                         c_status<=s_rx_start_bit;
  57.                                                                                 else
  58.                                                                                         c_status<=s_idle;
  59.                                                                                 end if;
  60.                                                                                 cnt<=0;
  61.                                                                                 bit_index<=0;

  62.                 when s_rx_start_bit        =>        if cnt>=6 then
  63.                                                                                         if rx_bit_r='0' then
  64.                                                                                                 c_status<=s_rx_data;
  65.                                                                                         else
  66.                                                                                                 c_status<=s_idle;
  67.                                                                                         end if;
  68.                                                                                         cnt<=0;
  69.                                                                                 else
  70.                                                                                         c_status<=s_rx_start_bit;
  71.                                                                                         cnt<=cnt+1;
  72.                                                                                 end if;
  73.                                                                                 rx_data_r<=(others=>'0');
  74.                                                                                 rx_dv<='0';
  75.                                                                                 rx_data<=(others=>'0');
  76.                                                                                 error<='0';
  77.                
  78.                 when s_rx_data                        =>                if cnt>=15 then
  79.                                                                                                 cnt<=0;                                                                                       
  80.                                                                                                 if bit_index>=(unsigned(data_bits)-1) then
  81.                                                                                                         case parity_en is                                                                                       
  82.                                                                                                         when "001"        =>        parity_r<='0';        --0 parity
  83.                                                                                                                                         c_status<=s_rx_parity;
  84.                                                                                                         when "010"        =>        parity_r<='1';        --1 parity
  85.                                                                                                                                         c_status<=s_rx_parity;
  86.                                                                                                         when "011"        =>        parity_r<=(rx_data_r(0) xor rx_data_r(1)) xor
  87.                                                                                                                                                           (rx_data_r(2) xor rx_data_r(3)) xor
  88.                                                                                                                                                           (rx_data_r(4) xor rx_data_r(5)) xor
  89.                                                                                                                                                           (rx_data_r(6) xor rx_data_r(7)) xor '1';        --odd parity (奇校验)
  90.                                                                                                                                         c_status<=s_rx_parity;
  91.                                                                                                         when "100"        =>        parity_r<=(rx_data_r(0) xor rx_data_r(1)) xor
  92.                                                                                                                                                           (rx_data_r(2) xor rx_data_r(3)) xor
  93.                                                                                                                                                           (rx_data_r(4) xor rx_data_r(5)) xor
  94.                                                                                                                                                           (rx_data_r(6) xor rx_data_r(7)) ;        --even parity (偶校验)
  95.                                                                                                                                         c_status<=s_rx_parity;
  96.                                                                                                         when others        =>        c_status<=s_stop_bit;
  97.                                                                                                         end case;
  98.                                                                                                         bit_index<=0;
  99.                                                                                                 else
  100.                                                                                                         bit_index<=bit_index+1;                                                                                                
  101.                                                                                                 end if;
  102.                                                                                                 rx_data_r(bit_index)<=rx_bit_r;
  103.                                                                                 else
  104.                                                                                         cnt<=cnt+1;
  105.                                                                                 end if;                                                                                                                                                                        
  106.                                                                                 
  107.                 when s_rx_parity                =>                if cnt>=15 then
  108.                                                                                         if rx_bit_r/=parity_r then                                                                                                
  109.                                                                                                 error<='1';                                                                                                
  110.                                                                                         else
  111.                                                                                         null;
  112.                                                                                         end if;
  113.                                                                                         c_status<=s_stop_bit;
  114.                                                                                         cnt<=0;
  115.                                                                                         rx_dv<='1';
  116.                                                                                         rx_data<=rx_data_r;
  117.                                                                                 else
  118.                                                                                         cnt<=cnt+1;
  119.                                                                                         c_status<=s_rx_parity;
  120.                                                                                 end if;
  121.                
  122.                 when s_stop_bit                =>                if cnt>=15 then
  123.                                                                                                 cnt<=0;
  124.                                                                                                 c_status<=s_idle;
  125.                                                                                         else
  126.                                                                                                 cnt<=cnt+1;
  127.                                                                                                 c_status<=s_stop_bit;
  128.                                                                                         end if;
  129.                                                                                         rx_dv<='1';
  130.                                                                                         rx_data<=rx_data_r;                                                                                       
  131.                
  132.                 when others                                =>                c_status<=s_idle;
  133.                                                                                 rx_dv<='0';
  134.                                                                                 error<='0';
  135.                 end case;
  136.         else
  137.         null;
  138.         end if;
  139.         end process;

  140. end rtl;
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。