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.

150 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. entity UartRx is
  23. generic (
  24. DATA_LENGTH : positive range 5 to 9 := 8;
  25. PARITY : boolean := true;
  26. CLK_DIV : natural := 10
  27. );
  28. port (
  29. reset_n_i : in std_logic; -- async reset
  30. clk_i : in std_logic; -- clock
  31. data_o : out std_logic_vector(DATA_LENGTH-1 downto 0); -- data output
  32. error_o : out std_logic; -- rx error
  33. valid_o : out std_logic; -- output data valid
  34. accept_i : in std_logic; -- output data accepted
  35. rx_i : in std_logic -- uart rx input
  36. );
  37. end entity UartRx;
  38. architecture rtl of UartRx is
  39. function odd_parity (data : in std_logic_vector(DATA_LENGTH-1 downto 0)) return std_logic is
  40. variable v_data : std_logic := '0';
  41. begin
  42. for i in data'range loop
  43. v_data := v_data xor data(i);
  44. end loop;
  45. return not v_data;
  46. end function odd_parity;
  47. function to_integer (data : in boolean) return integer is
  48. begin
  49. if data then
  50. return 1;
  51. else
  52. return 0;
  53. end if;
  54. end function to_integer;
  55. type t_uart_state is (IDLE, RECEIVE, VALID);
  56. signal s_uart_state : t_uart_state;
  57. signal s_data : std_logic_vector(DATA_LENGTH+1+to_integer(PARITY) downto 0);
  58. signal s_clk_en : boolean;
  59. begin
  60. ClkDivP : process (clk_i, reset_n_i) is
  61. variable v_clk_cnt : natural range 0 to CLK_DIV-1;
  62. begin
  63. if (reset_n_i = '0') then
  64. s_clk_en <= false;
  65. v_clk_cnt := CLK_DIV-1;
  66. elsif (rising_edge(clk_i)) then
  67. s_clk_en <= false;
  68. if (s_uart_state = IDLE) then
  69. v_clk_cnt := CLK_DIV-2;
  70. elsif (s_uart_state = RECEIVE) then
  71. if (v_clk_cnt = 0) then
  72. v_clk_cnt := CLK_DIV-1;
  73. else
  74. v_clk_cnt := v_clk_cnt - 1;
  75. end if;
  76. if (v_clk_cnt = CLK_DIV/2-1) then
  77. s_clk_en <= true;
  78. end if;
  79. end if;
  80. end if;
  81. end process ClkDivP;
  82. RxP : process (clk_i, reset_n_i) is
  83. variable v_bit_cnt : natural range 0 to s_data'length-1;
  84. begin
  85. if (reset_n_i = '0') then
  86. s_uart_state <= IDLE;
  87. s_data <= (others => '0');
  88. valid_o <= '0';
  89. v_bit_cnt := 0;
  90. elsif (rising_edge(clk_i)) then
  91. FsmL : case s_uart_state is
  92. when IDLE =>
  93. valid_o <= '0';
  94. v_bit_cnt := s_data'length-1;
  95. if (rx_i = '0') then
  96. s_uart_state <= RECEIVE;
  97. end if;
  98. when RECEIVE =>
  99. if (s_clk_en) then
  100. s_data <= rx_i & s_data(s_data'length-1 downto 1);
  101. if (v_bit_cnt = 0) then
  102. valid_o <= '1';
  103. s_uart_state <= VALID;
  104. else
  105. v_bit_cnt := v_bit_cnt - 1;
  106. end if;
  107. end if;
  108. when VALID =>
  109. valid_o <= '1';
  110. if (valid_o = '1' and accept_i = '1') then
  111. valid_o <= '0';
  112. s_uart_state <= IDLE;
  113. end if;
  114. end case;
  115. end if;
  116. end process RxP;
  117. ParityG : if PARITY generate
  118. data_o <= s_data(s_data'length-3 downto 1);
  119. error_o <= '1' when odd_parity(s_data(s_data'length-3 downto 1)) /= s_data(s_data'length-2) or
  120. s_data(s_data'length-1) = '0' else
  121. '0';
  122. else generate
  123. data_o <= s_data(s_data'length-2 downto 1);
  124. error_o <= '1' when s_data(s_data'length-1) = '0' else '0';
  125. end generate ParityG;
  126. end architecture rtl;