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.

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