Library of reusable VHDL components
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.3 KiB

  1. -- ======================================================================
  2. -- UART Receiver
  3. -- Copyright (C) 2020 Torsten Meissner
  4. -------------------------------------------------------------------------
  5. -- This program is free software; you can redistribute it and/or
  6. -- modify it under the terms of the GNU Lesser General Public
  7. -- License as published by the Free Software Foundation; either
  8. -- version 3 of the License, or (at your option) any later version.
  9. --
  10. -- This program is distributed in the hope that it will be useful,
  11. -- but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. -- Lesser General Public License for more details.
  14. --
  15. -- You should have received a copy of the GNU Lesser General Public License
  16. -- along with this program; if not, write to the Free Software Foundation,
  17. -- Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. -- ======================================================================
  19. library ieee;
  20. use ieee.std_logic_1164.all;
  21. use ieee.numeric_std.all;
  22. library libvhdl;
  23. use libvhdl.UtilsP.all;
  24. entity UartRx is
  25. generic (
  26. DATA_LENGTH : positive range 5 to 9 := 8;
  27. PARITY : boolean := true;
  28. CLK_DIV : natural := 10
  29. );
  30. port (
  31. reset_n_i : in std_logic; -- async reset
  32. clk_i : in std_logic; -- clock
  33. data_o : out std_logic_vector(DATA_LENGTH-1 downto 0); -- data output
  34. error_o : out std_logic; -- rx error
  35. valid_o : out std_logic; -- output data valid
  36. accept_i : in std_logic; -- output data accepted
  37. rx_i : in std_logic -- uart rx input
  38. );
  39. end entity UartRx;
  40. architecture rtl of UartRx is
  41. function to_integer (data : in boolean) return integer is
  42. begin
  43. if data then
  44. return 1;
  45. else
  46. return 0;
  47. end if;
  48. end function to_integer;
  49. type t_uart_state is (IDLE, RECEIVE, VALID);
  50. signal s_uart_state : t_uart_state;
  51. signal s_data : std_logic_vector(DATA_LENGTH+1+to_integer(PARITY) downto 0);
  52. signal s_clk_en : boolean;
  53. begin
  54. ClkDivP : process (clk_i, reset_n_i) is
  55. variable v_clk_cnt : natural range 0 to CLK_DIV-1;
  56. begin
  57. if (reset_n_i = '0') then
  58. s_clk_en <= false;
  59. v_clk_cnt := CLK_DIV-1;
  60. elsif (rising_edge(clk_i)) then
  61. s_clk_en <= false;
  62. if (s_uart_state = IDLE) then
  63. v_clk_cnt := CLK_DIV-2;
  64. elsif (s_uart_state = RECEIVE) then
  65. if (v_clk_cnt = 0) then
  66. v_clk_cnt := CLK_DIV-1;
  67. else
  68. v_clk_cnt := v_clk_cnt - 1;
  69. end if;
  70. if (v_clk_cnt = CLK_DIV/2-1) then
  71. s_clk_en <= true;
  72. end if;
  73. end if;
  74. end if;
  75. end process ClkDivP;
  76. RxP : process (clk_i, reset_n_i) is
  77. variable v_bit_cnt : natural range 0 to s_data'length-1;
  78. begin
  79. if (reset_n_i = '0') then
  80. s_uart_state <= IDLE;
  81. s_data <= (others => '0');
  82. valid_o <= '0';
  83. v_bit_cnt := 0;
  84. elsif (rising_edge(clk_i)) then
  85. FsmL : case s_uart_state is
  86. when IDLE =>
  87. valid_o <= '0';
  88. v_bit_cnt := s_data'length-1;
  89. if (rx_i = '0') then
  90. s_uart_state <= RECEIVE;
  91. end if;
  92. when RECEIVE =>
  93. if (s_clk_en) then
  94. s_data <= rx_i & s_data(s_data'length-1 downto 1);
  95. if (v_bit_cnt = 0) then
  96. valid_o <= '1';
  97. s_uart_state <= VALID;
  98. else
  99. v_bit_cnt := v_bit_cnt - 1;
  100. end if;
  101. end if;
  102. when VALID =>
  103. valid_o <= '1';
  104. if (valid_o = '1' and accept_i = '1') then
  105. valid_o <= '0';
  106. s_uart_state <= IDLE;
  107. end if;
  108. end case;
  109. end if;
  110. end process RxP;
  111. ParityG : if PARITY generate
  112. data_o <= s_data(s_data'length-3 downto 1);
  113. error_o <= '1' when odd_parity(s_data(s_data'length-3 downto 1)) /= s_data(s_data'length-2) or
  114. s_data(s_data'length-1) = '0' else
  115. '0';
  116. else generate
  117. data_o <= s_data(s_data'length-2 downto 1);
  118. error_o <= '1' when s_data(s_data'length-1) = '0' else '0';
  119. end generate ParityG;
  120. end architecture rtl;