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.

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