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.

139 lines
4.0 KiB

  1. -- ======================================================================
  2. -- UART transmitter
  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 UartTx 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_i : in std_logic_vector(DATA_LENGTH-1 downto 0); -- data input
  34. valid_i : in std_logic; -- input data valid
  35. accept_o : out std_logic; -- inpit data accepted
  36. tx_o : out std_logic -- uart tx data output
  37. );
  38. end entity UartTx;
  39. architecture rtl of UartTx is
  40. function odd_parity (data : in std_logic_vector(DATA_LENGTH-1 downto 0)) return std_logic is
  41. begin
  42. return not xor_reduce(data);
  43. end function odd_parity;
  44. function to_integer (data : in boolean) return integer is
  45. begin
  46. if data then
  47. return 1;
  48. else
  49. return 0;
  50. end if;
  51. end function to_integer;
  52. type t_uart_state is (IDLE, SEND);
  53. signal s_uart_state : t_uart_state;
  54. signal s_data : std_logic_vector(DATA_LENGTH+1+to_integer(PARITY) downto 0);
  55. signal s_clk_en : boolean;
  56. begin
  57. ClkDivP : process (clk_i, reset_n_i) is
  58. variable v_clk_cnt : natural range 0 to CLK_DIV-1;
  59. begin
  60. if (reset_n_i = '0') then
  61. s_clk_en <= false;
  62. v_clk_cnt := CLK_DIV-1;
  63. elsif (rising_edge(clk_i)) then
  64. if (s_uart_state = IDLE) then
  65. v_clk_cnt := CLK_DIV-2;
  66. s_clk_en <= false;
  67. elsif (s_uart_state = SEND) then
  68. if (v_clk_cnt = 0) then
  69. v_clk_cnt := CLK_DIV-1;
  70. s_clk_en <= true;
  71. else
  72. v_clk_cnt := v_clk_cnt - 1;
  73. s_clk_en <= false;
  74. end if;
  75. end if;
  76. end if;
  77. end process ClkDivP;
  78. TxP : process (clk_i, reset_n_i) is
  79. variable v_bit_cnt : natural range 0 to s_data'length-1;
  80. begin
  81. if (reset_n_i = '0') then
  82. s_uart_state <= IDLE;
  83. s_data <= (0 => '1', others => '0');
  84. accept_o <= '0';
  85. v_bit_cnt := 0;
  86. elsif (rising_edge(clk_i)) then
  87. FsmL : case s_uart_state is
  88. when IDLE =>
  89. accept_o <= '1';
  90. v_bit_cnt := s_data'length-1;
  91. if (valid_i = '1' and accept_o = '1') then
  92. accept_o <= '0';
  93. if (PARITY) then
  94. s_data <= '1' & odd_parity(data_i) & data_i & '0';
  95. else
  96. s_data <= '1' & data_i & '0';
  97. end if;
  98. s_uart_state <= SEND;
  99. end if;
  100. when SEND =>
  101. if (s_clk_en) then
  102. s_data <= '1' & s_data(s_data'length-1 downto 1);
  103. if (v_bit_cnt = 0) then
  104. accept_o <= '1';
  105. s_uart_state <= IDLE;
  106. else
  107. v_bit_cnt := v_bit_cnt - 1;
  108. end if;
  109. end if;
  110. end case;
  111. end if;
  112. end process TxP;
  113. tx_o <= s_data(0);
  114. end architecture rtl;