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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

160 lines
5.2 KiB

2 years ago
  1. //////////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // //
  4. // Author: meisq //
  5. // msq@qq.com //
  6. // ALINX(shanghai) Technology Co.,Ltd //
  7. // heijin //
  8. // WEB: http://www.alinx.cn/ //
  9. // BBS: http://www.heijin.org/ //
  10. // //
  11. //////////////////////////////////////////////////////////////////////////////////
  12. // //
  13. // Copyright (c) 2017,ALINX(shanghai) Technology Co.,Ltd //
  14. // All rights reserved //
  15. // //
  16. // This source file may be used and distributed without restriction provided //
  17. // that this copyright statement is not removed from the file and that any //
  18. // derivative work contains the original copyright notice and the associated //
  19. // disclaimer. //
  20. // //
  21. //////////////////////////////////////////////////////////////////////////////////
  22. //================================================================================
  23. // Revision History:
  24. // Date By Revision Change Description
  25. //--------------------------------------------------------------------------------
  26. //2017/8/1 1.0 Original
  27. //*******************************************************************************/
  28. module uart_tx
  29. #(
  30. parameter CLK_FRE = 50, //clock frequency(Mhz)
  31. parameter BAUD_RATE = 115200 //serial baud rate
  32. )
  33. (
  34. input clk, //clock input
  35. input rst_n, //asynchronous reset input, low active
  36. input[7:0] tx_data, //data to send
  37. input tx_data_valid, //data to be sent is valid
  38. output reg tx_data_ready, //send ready
  39. output tx_pin //serial data output
  40. );
  41. //calculates the clock cycle for baud rate
  42. localparam CYCLE = CLK_FRE * 1000000 / BAUD_RATE;
  43. //state machine code
  44. localparam S_IDLE = 1;
  45. localparam S_START = 2;//start bit
  46. localparam S_SEND_BYTE = 3;//data bits
  47. localparam S_STOP = 4;//stop bit
  48. reg[2:0] state;
  49. reg[2:0] next_state;
  50. reg[15:0] cycle_cnt; //baud counter
  51. reg[2:0] bit_cnt;//bit counter
  52. reg[7:0] tx_data_latch; //latch data to send
  53. reg tx_reg; //serial data output
  54. assign tx_pin = tx_reg;
  55. always@(posedge clk or negedge rst_n)
  56. begin
  57. if(rst_n == 1'b0)
  58. state <= S_IDLE;
  59. else
  60. state <= next_state;
  61. end
  62. always@(*)
  63. begin
  64. case(state)
  65. S_IDLE:
  66. if(tx_data_valid == 1'b1)
  67. next_state <= S_START;
  68. else
  69. next_state <= S_IDLE;
  70. S_START:
  71. if(cycle_cnt == CYCLE - 1)
  72. next_state <= S_SEND_BYTE;
  73. else
  74. next_state <= S_START;
  75. S_SEND_BYTE:
  76. if(cycle_cnt == CYCLE - 1 && bit_cnt == 3'd7)
  77. next_state <= S_STOP;
  78. else
  79. next_state <= S_SEND_BYTE;
  80. S_STOP:
  81. if(cycle_cnt == CYCLE - 1)
  82. next_state <= S_IDLE;
  83. else
  84. next_state <= S_STOP;
  85. default:
  86. next_state <= S_IDLE;
  87. endcase
  88. end
  89. always@(posedge clk or negedge rst_n)
  90. begin
  91. if(rst_n == 1'b0)
  92. begin
  93. tx_data_ready <= 1'b0;
  94. end
  95. else if(state == S_IDLE)
  96. if(tx_data_valid == 1'b1)
  97. tx_data_ready <= 1'b0;
  98. else
  99. tx_data_ready <= 1'b1;
  100. else if(state == S_STOP && cycle_cnt == CYCLE - 1)
  101. tx_data_ready <= 1'b1;
  102. end
  103. always@(posedge clk or negedge rst_n)
  104. begin
  105. if(rst_n == 1'b0)
  106. begin
  107. tx_data_latch <= 8'd0;
  108. end
  109. else if(state == S_IDLE && tx_data_valid == 1'b1)
  110. tx_data_latch <= tx_data;
  111. end
  112. always@(posedge clk or negedge rst_n)
  113. begin
  114. if(rst_n == 1'b0)
  115. begin
  116. bit_cnt <= 3'd0;
  117. end
  118. else if(state == S_SEND_BYTE)
  119. if(cycle_cnt == CYCLE - 1)
  120. bit_cnt <= bit_cnt + 3'd1;
  121. else
  122. bit_cnt <= bit_cnt;
  123. else
  124. bit_cnt <= 3'd0;
  125. end
  126. always@(posedge clk or negedge rst_n)
  127. begin
  128. if(rst_n == 1'b0)
  129. cycle_cnt <= 16'd0;
  130. else if((state == S_SEND_BYTE && cycle_cnt == CYCLE - 1) || next_state != state)
  131. cycle_cnt <= 16'd0;
  132. else
  133. cycle_cnt <= cycle_cnt + 16'd1;
  134. end
  135. always@(posedge clk or negedge rst_n)
  136. begin
  137. if(rst_n == 1'b0)
  138. tx_reg <= 1'b1;
  139. else
  140. case(state)
  141. S_IDLE,S_STOP:
  142. tx_reg <= 1'b1;
  143. S_START:
  144. tx_reg <= 1'b0;
  145. S_SEND_BYTE:
  146. tx_reg <= tx_data_latch[bit_cnt];
  147. default:
  148. tx_reg <= 1'b1;
  149. endcase
  150. end
  151. endmodule