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.

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