UARTtx.v 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //////////////////////////////////////////////////////////////////////
  2. // File Downloaded from http://www.nandland.com
  3. //////////////////////////////////////////////////////////////////////
  4. // This file contains the UART Transmitter. This transmitter is able
  5. // to transmit 8 bits of serial data, one start bit, one stop bit,
  6. // and no parity bit. When transmit is complete o_Tx_done will be
  7. // driven high for one clock cycle.
  8. //
  9. // Set Parameter CLKS_PER_BIT as follows:
  10. // CLKS_PER_BIT = (Frequency of i_Clock)/(Frequency of UART)
  11. // Example: 25 MHz Clock, 115200 baud UART
  12. // (25000000)/(115200) = 217
  13. module UARTtx(
  14. input i_Clock,
  15. input reset,
  16. input i_Tx_DV,
  17. input [7:0] i_Tx_Byte,
  18. output o_Tx_Active,
  19. output reg o_Tx_Serial,
  20. output o_Tx_Done
  21. );
  22. localparam CLKS_PER_BIT = 50; //1MBaud
  23. localparam s_IDLE = 3'b000;
  24. localparam s_TX_START_BIT = 3'b001;
  25. localparam s_TX_DATA_BITS = 3'b010;
  26. localparam s_TX_STOP_BIT = 3'b011;
  27. localparam s_CLEANUP = 3'b100;
  28. reg [2:0] r_SM_Main;
  29. reg [8:0] r_Clock_Count;
  30. reg [2:0] r_Bit_Index;
  31. reg [7:0] r_Tx_Data;
  32. reg r_Tx_Done;
  33. reg r_Tx_Active;
  34. initial
  35. begin
  36. r_SM_Main = 3'd0;
  37. r_Clock_Count = 9'd0;
  38. r_Bit_Index = 3'd0;
  39. r_Tx_Data = 8'd0;
  40. r_Tx_Done = 1'b0;
  41. r_Tx_Active = 1'b0;
  42. o_Tx_Serial = 1'b1;
  43. end
  44. /*
  45. always @(posedge i_Clock)
  46. begin
  47. if (reset)
  48. begin
  49. o_Tx_Done_l <= 1'b0;
  50. end
  51. else
  52. begin
  53. o_Tx_Done_l <= o_Tx_Done;
  54. end
  55. end
  56. */
  57. always @(posedge i_Clock)
  58. begin
  59. if (reset)
  60. begin
  61. r_SM_Main <= 3'd0;
  62. r_Clock_Count <= 9'd0;
  63. r_Bit_Index <= 3'd0;
  64. r_Tx_Data <= 8'd0;
  65. r_Tx_Done <= 1'b0;
  66. r_Tx_Active <= 1'b0;
  67. o_Tx_Serial <= 1'b1;
  68. end
  69. else
  70. begin
  71. case (r_SM_Main)
  72. s_IDLE :
  73. begin
  74. o_Tx_Serial <= 1'b1; // Drive Line High for Idle
  75. r_Tx_Done <= 1'b0;
  76. r_Clock_Count <= 0;
  77. r_Bit_Index <= 0;
  78. if (i_Tx_DV == 1'b1)
  79. begin
  80. r_Tx_Active <= 1'b1;
  81. r_Tx_Data <= i_Tx_Byte;
  82. r_SM_Main <= s_TX_START_BIT;
  83. end
  84. else
  85. r_SM_Main <= s_IDLE;
  86. end // case: s_IDLE
  87. // Send out Start Bit. Start bit = 0
  88. s_TX_START_BIT :
  89. begin
  90. o_Tx_Serial <= 1'b0;
  91. // Wait CLKS_PER_BIT-1 clock cycles for start bit to finish
  92. if (r_Clock_Count < CLKS_PER_BIT-1)
  93. begin
  94. r_Clock_Count <= r_Clock_Count + 1;
  95. r_SM_Main <= s_TX_START_BIT;
  96. end
  97. else
  98. begin
  99. r_Clock_Count <= 0;
  100. r_SM_Main <= s_TX_DATA_BITS;
  101. end
  102. end // case: s_TX_START_BIT
  103. // Wait CLKS_PER_BIT-1 clock cycles for data bits to finish
  104. s_TX_DATA_BITS :
  105. begin
  106. o_Tx_Serial <= r_Tx_Data[r_Bit_Index];
  107. if (r_Clock_Count < CLKS_PER_BIT-1)
  108. begin
  109. r_Clock_Count <= r_Clock_Count + 1;
  110. r_SM_Main <= s_TX_DATA_BITS;
  111. end
  112. else
  113. begin
  114. r_Clock_Count <= 0;
  115. // Check if we have sent out all bits
  116. if (r_Bit_Index < 7)
  117. begin
  118. r_Bit_Index <= r_Bit_Index + 1;
  119. r_SM_Main <= s_TX_DATA_BITS;
  120. end
  121. else
  122. begin
  123. r_Bit_Index <= 0;
  124. r_SM_Main <= s_TX_STOP_BIT;
  125. end
  126. end
  127. end // case: s_TX_DATA_BITS
  128. // Send out Stop bit. Stop bit = 1
  129. s_TX_STOP_BIT :
  130. begin
  131. o_Tx_Serial <= 1'b1;
  132. // Wait CLKS_PER_BIT-1 clock cycles for Stop bit to finish
  133. if (r_Clock_Count < CLKS_PER_BIT-1)
  134. begin
  135. r_Clock_Count <= r_Clock_Count + 1;
  136. r_SM_Main <= s_TX_STOP_BIT;
  137. end
  138. else
  139. begin
  140. r_Tx_Done <= 1'b1;
  141. r_Clock_Count <= 0;
  142. r_SM_Main <= s_CLEANUP;
  143. r_Tx_Active <= 1'b0;
  144. end
  145. end // case: s_Tx_STOP_BIT
  146. // Stay here 1 clock
  147. s_CLEANUP :
  148. begin
  149. r_Tx_Done <= 1'b1;
  150. if (!i_Tx_DV) r_SM_Main <= s_IDLE;
  151. else r_SM_Main <= s_CLEANUP;
  152. end
  153. default :
  154. r_SM_Main <= s_IDLE;
  155. endcase
  156. end
  157. end
  158. assign o_Tx_Active = r_Tx_Active;
  159. assign o_Tx_Done = r_Tx_Done;
  160. endmodule