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.

149 lines
4.5 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 odd_parity (data : in std_logic_vector(DATA_LENGTH-1 downto 0)) return std_logic is
  42. begin
  43. return not xor_reduce(data);
  44. end function odd_parity;
  45. function to_integer (data : in boolean) return integer is
  46. begin
  47. if data then
  48. return 1;
  49. else
  50. return 0;
  51. end if;
  52. end function to_integer;
  53. type t_uart_state is (IDLE, RECEIVE, VALID);
  54. signal s_uart_state : t_uart_state;
  55. signal s_data : std_logic_vector(DATA_LENGTH+1+to_integer(PARITY) downto 0);
  56. signal s_clk_en : boolean;
  57. begin
  58. ClkDivP : process (clk_i, reset_n_i) is
  59. variable v_clk_cnt : natural range 0 to CLK_DIV-1;
  60. begin
  61. if (reset_n_i = '0') then
  62. s_clk_en <= false;
  63. v_clk_cnt := CLK_DIV-1;
  64. elsif (rising_edge(clk_i)) then
  65. s_clk_en <= false;
  66. if (s_uart_state = IDLE) then
  67. v_clk_cnt := CLK_DIV-2;
  68. elsif (s_uart_state = RECEIVE) then
  69. if (v_clk_cnt = 0) then
  70. v_clk_cnt := CLK_DIV-1;
  71. else
  72. v_clk_cnt := v_clk_cnt - 1;
  73. end if;
  74. if (v_clk_cnt = CLK_DIV/2-1) then
  75. s_clk_en <= true;
  76. end if;
  77. end if;
  78. end if;
  79. end process ClkDivP;
  80. RxP : process (clk_i, reset_n_i) is
  81. variable v_bit_cnt : natural range 0 to s_data'length-1;
  82. begin
  83. if (reset_n_i = '0') then
  84. s_uart_state <= IDLE;
  85. s_data <= (others => '0');
  86. valid_o <= '0';
  87. v_bit_cnt := 0;
  88. elsif (rising_edge(clk_i)) then
  89. FsmL : case s_uart_state is
  90. when IDLE =>
  91. valid_o <= '0';
  92. v_bit_cnt := s_data'length-1;
  93. if (rx_i = '0') then
  94. s_uart_state <= RECEIVE;
  95. end if;
  96. when RECEIVE =>
  97. if (s_clk_en) then
  98. s_data <= rx_i & s_data(s_data'length-1 downto 1);
  99. if (v_bit_cnt = 0) then
  100. valid_o <= '1';
  101. s_uart_state <= VALID;
  102. else
  103. v_bit_cnt := v_bit_cnt - 1;
  104. end if;
  105. end if;
  106. when VALID =>
  107. valid_o <= '1';
  108. if (valid_o = '1' and accept_i = '1') then
  109. valid_o <= '0';
  110. s_uart_state <= IDLE;
  111. end if;
  112. end case;
  113. end if;
  114. end process RxP;
  115. ParityG : if PARITY generate
  116. data_o <= s_data(s_data'length-3 downto 1);
  117. error_o <= '1' when odd_parity(s_data(s_data'length-3 downto 1)) /= s_data(s_data'length-2) or
  118. s_data(s_data'length-1) = '0' else
  119. '0';
  120. else generate
  121. data_o <= s_data(s_data'length-2 downto 1);
  122. error_o <= '1' when s_data(s_data'length-1) = '0' else '0';
  123. end generate ParityG;
  124. end architecture rtl;