async_8n1_tx_v2.vhd 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. entity async_8n1_tx_v2 is
  5. port
  6. (
  7. clk : in std_logic;
  8. rst : in std_logic;
  9. en : in std_logic;
  10. wr : in std_logic;
  11. data_in : in unsigned (7 downto 0);
  12. rdy : out std_logic;
  13. data_out : out std_logic
  14. );
  15. end entity;
  16. architecture beh of async_8n1_tx_v2 is
  17. component generic_counter is
  18. generic (
  19. counter_bits : integer := 4
  20. );
  21. port(
  22. clk : in std_logic;
  23. rst : in std_logic;
  24. en : in std_logic;
  25. cnt : out unsigned (counter_bits -1 downto 0)
  26. );
  27. end component;
  28. signal i_rdy : std_logic := '1';
  29. signal i_data_reg : unsigned(8 downto 0) := (others => '1');
  30. signal i_cnt : unsigned(3 downto 0);
  31. signal i_clear_counter : std_logic;
  32. begin
  33. data_out <= i_data_reg(0);
  34. rdy <= i_rdy;
  35. i_counter : generic_counter
  36. port map(
  37. clk => clk,
  38. rst => i_clear_counter,
  39. en => en,
  40. cnt => i_cnt);
  41. i_clear_counter <= i_rdy;
  42. -- Shift register
  43. -- Affects registers i_data_reg and i_rdy
  44. process(clk) is
  45. begin
  46. if rising_edge(clk) then
  47. if rst = '1' then
  48. -- Reset registers
  49. i_data_reg <= (others => '1');
  50. i_rdy <= '1';
  51. elsif en = '1' then
  52. -- Take a step
  53. if i_rdy = '1' then
  54. if wr = '1' then
  55. -- Load data with start bit
  56. i_data_reg <= data_in & '0';
  57. -- and start transfer
  58. i_rdy <= '0';
  59. else
  60. -- Remain idle
  61. i_data_reg <= i_data_reg;
  62. i_rdy <= i_rdy;
  63. end if;
  64. else
  65. -- In transmission
  66. -- Shift one step, add fill with stop bits
  67. i_data_reg <= '1' & i_data_reg(8 downto 1);
  68. -- Did transfer finish?
  69. if i_cnt = to_unsigned(8, 4) then
  70. -- Yes, next bit will be stop bit and we're ready for next transfer
  71. i_rdy <= '1';
  72. else
  73. -- No, keep transmitting
  74. i_rdy <= i_rdy;
  75. end if;
  76. end if;
  77. else
  78. -- don't take a step
  79. i_data_reg <= i_data_reg;
  80. i_rdy <= i_rdy;
  81. end if;
  82. end if;
  83. end process;
  84. end architecture;