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.

202 lines
7.3 KiB

2 years ago
  1. ////////////////////////////////////////////////////////
  2. // RS-232 RX and TX module
  3. // (c) fpga4fun.com & KNJN LLC - 2003 to 2016
  4. // The RS-232 settings are fixed
  5. // TX: 8-bit data, 2 stop, no-parity
  6. // RX: 8-bit data, 1 stop, no-parity (the receiver can accept more stop bits of course)
  7. //`define SIMULATION // in this mode, TX outputs one bit per clock cycle
  8. // and RX receives one bit per clock cycle (for fast simulations)
  9. ////////////////////////////////////////////////////////
  10. module async_transmitter(
  11. input clk,
  12. input TxD_start,
  13. input [7:0] TxD_data,
  14. output TxD,
  15. output TxD_busy
  16. );
  17. // Assert TxD_start for (at least) one clock cycle to start transmission of TxD_data
  18. // TxD_data is latched so that it doesn't have to stay valid while it is being sent
  19. parameter ClkFrequency = 50000000; // 50MHz
  20. parameter Baud = 115200;
  21. generate
  22. if(ClkFrequency<Baud*8 && (ClkFrequency % Baud!=0)) ASSERTION_ERROR PARAMETER_OUT_OF_RANGE("Frequency incompatible with requested Baud rate");
  23. endgenerate
  24. ////////////////////////////////
  25. `ifdef SIMULATION
  26. wire BitTick = 1'b1; // output one bit per clock cycle
  27. `else
  28. wire BitTick;
  29. BaudTickGen #(ClkFrequency, Baud) tickgen(.clk(clk), .enable(TxD_busy), .tick(BitTick));
  30. `endif
  31. reg [3:0] TxD_state = 0;
  32. wire TxD_ready = (TxD_state==0);
  33. assign TxD_busy = ~TxD_ready;
  34. reg [7:0] TxD_shift = 0;
  35. always @(posedge clk)
  36. begin
  37. if(TxD_ready & TxD_start)
  38. TxD_shift <= TxD_data;
  39. else
  40. if(TxD_state[3] & BitTick)
  41. TxD_shift <= (TxD_shift >> 1);
  42. case(TxD_state)
  43. 4'b0000: if(TxD_start) TxD_state <= 4'b0100;
  44. 4'b0100: if(BitTick) TxD_state <= 4'b1000; // start bit
  45. 4'b1000: if(BitTick) TxD_state <= 4'b1001; // bit 0
  46. 4'b1001: if(BitTick) TxD_state <= 4'b1010; // bit 1
  47. 4'b1010: if(BitTick) TxD_state <= 4'b1011; // bit 2
  48. 4'b1011: if(BitTick) TxD_state <= 4'b1100; // bit 3
  49. 4'b1100: if(BitTick) TxD_state <= 4'b1101; // bit 4
  50. 4'b1101: if(BitTick) TxD_state <= 4'b1110; // bit 5
  51. 4'b1110: if(BitTick) TxD_state <= 4'b1111; // bit 6
  52. 4'b1111: if(BitTick) TxD_state <= 4'b0010; // bit 7
  53. 4'b0010: if(BitTick) TxD_state <= 4'b0011; // stop1
  54. 4'b0011: if(BitTick) TxD_state <= 4'b0000; // stop2
  55. default: if(BitTick) TxD_state <= 4'b0000;
  56. endcase
  57. end
  58. assign TxD = (TxD_state<4) | (TxD_state[3] & TxD_shift[0]); // put together the start, data and stop bits
  59. endmodule
  60. ////////////////////////////////////////////////////////
  61. module async_receiver(
  62. input clk,
  63. input RxD,
  64. output reg RxD_data_ready = 0,
  65. output reg [7:0] RxD_data = 0, // data received, valid only (for one clock cycle) when RxD_data_ready is asserted
  66. // We also detect if a gap occurs in the received stream of characters
  67. // That can be useful if multiple characters are sent in burst
  68. // so that multiple characters can be treated as a "packet"
  69. output RxD_idle, // asserted when no data has been received for a while
  70. output reg RxD_endofpacket = 0 // asserted for one clock cycle when a packet has been detected (i.e. RxD_idle is going high)
  71. );
  72. parameter ClkFrequency = 25000000; // 25MHz
  73. parameter Baud = 115200;
  74. parameter Oversampling = 8; // needs to be a power of 2
  75. // we oversample the RxD line at a fixed rate to capture each RxD data bit at the "right" time
  76. // 8 times oversampling by default, use 16 for higher quality reception
  77. generate
  78. if(ClkFrequency<Baud*Oversampling) ASSERTION_ERROR PARAMETER_OUT_OF_RANGE("Frequency too low for current Baud rate and oversampling");
  79. if(Oversampling<8 || ((Oversampling & (Oversampling-1))!=0)) ASSERTION_ERROR PARAMETER_OUT_OF_RANGE("Invalid oversampling value");
  80. endgenerate
  81. ////////////////////////////////
  82. reg [3:0] RxD_state = 0;
  83. `ifdef SIMULATION
  84. wire RxD_bit = RxD;
  85. wire sampleNow = 1'b1; // receive one bit per clock cycle
  86. `else
  87. wire OversamplingTick;
  88. BaudTickGen #(ClkFrequency, Baud, Oversampling) tickgen(.clk(clk), .enable(1'b1), .tick(OversamplingTick));
  89. // synchronize RxD to our clk domain
  90. reg [1:0] RxD_sync = 2'b11;
  91. always @(posedge clk) if(OversamplingTick) RxD_sync <= {RxD_sync[0], RxD};
  92. // and filter it
  93. reg [1:0] Filter_cnt = 2'b11;
  94. reg RxD_bit = 1'b1;
  95. always @(posedge clk)
  96. if(OversamplingTick)
  97. begin
  98. if(RxD_sync[1]==1'b1 && Filter_cnt!=2'b11) Filter_cnt <= Filter_cnt + 1'd1;
  99. else
  100. if(RxD_sync[1]==1'b0 && Filter_cnt!=2'b00) Filter_cnt <= Filter_cnt - 1'd1;
  101. if(Filter_cnt==2'b11) RxD_bit <= 1'b1;
  102. else
  103. if(Filter_cnt==2'b00) RxD_bit <= 1'b0;
  104. end
  105. // and decide when is the good time to sample the RxD line
  106. function integer log2(input integer v); begin log2=0; while(v>>log2) log2=log2+1; end endfunction
  107. localparam l2o = log2(Oversampling);
  108. reg [l2o-2:0] OversamplingCnt = 0;
  109. always @(posedge clk) if(OversamplingTick) OversamplingCnt <= (RxD_state==0) ? 1'd0 : OversamplingCnt + 1'd1;
  110. wire sampleNow = OversamplingTick && (OversamplingCnt==Oversampling/2-1);
  111. `endif
  112. // now we can accumulate the RxD bits in a shift-register
  113. always @(posedge clk)
  114. case(RxD_state)
  115. 4'b0000: if(~RxD_bit) RxD_state <= `ifdef SIMULATION 4'b1000 `else 4'b0001 `endif; // start bit found?
  116. 4'b0001: if(sampleNow) RxD_state <= 4'b1000; // sync start bit to sampleNow
  117. 4'b1000: if(sampleNow) RxD_state <= 4'b1001; // bit 0
  118. 4'b1001: if(sampleNow) RxD_state <= 4'b1010; // bit 1
  119. 4'b1010: if(sampleNow) RxD_state <= 4'b1011; // bit 2
  120. 4'b1011: if(sampleNow) RxD_state <= 4'b1100; // bit 3
  121. 4'b1100: if(sampleNow) RxD_state <= 4'b1101; // bit 4
  122. 4'b1101: if(sampleNow) RxD_state <= 4'b1110; // bit 5
  123. 4'b1110: if(sampleNow) RxD_state <= 4'b1111; // bit 6
  124. 4'b1111: if(sampleNow) RxD_state <= 4'b0010; // bit 7
  125. 4'b0010: if(sampleNow) RxD_state <= 4'b0000; // stop bit
  126. default: RxD_state <= 4'b0000;
  127. endcase
  128. always @(posedge clk)
  129. if(sampleNow && RxD_state[3]) RxD_data <= {RxD_bit, RxD_data[7:1]};
  130. //reg RxD_data_error = 0;
  131. always @(posedge clk)
  132. begin
  133. RxD_data_ready <= (sampleNow && RxD_state==4'b0010 && RxD_bit); // make sure a stop bit is received
  134. //RxD_data_error <= (sampleNow && RxD_state==4'b0010 && ~RxD_bit); // error if a stop bit is not received
  135. end
  136. `ifdef SIMULATION
  137. assign RxD_idle = 0;
  138. `else
  139. reg [l2o+1:0] GapCnt = 0;
  140. always @(posedge clk) if (RxD_state!=0) GapCnt<=0; else if(OversamplingTick & ~GapCnt[log2(Oversampling)+1]) GapCnt <= GapCnt + 1'h1;
  141. assign RxD_idle = GapCnt[l2o+1];
  142. always @(posedge clk) RxD_endofpacket <= OversamplingTick & ~GapCnt[l2o+1] & &GapCnt[l2o:0];
  143. `endif
  144. endmodule
  145. ////////////////////////////////////////////////////////
  146. // dummy module used to be able to raise an assertion in Verilog
  147. module ASSERTION_ERROR();
  148. endmodule
  149. ////////////////////////////////////////////////////////
  150. module BaudTickGen(
  151. input clk, enable,
  152. output tick // generate a tick at the specified baud rate * oversampling
  153. );
  154. parameter ClkFrequency = 25000000;
  155. parameter Baud = 115200;
  156. parameter Oversampling = 1;
  157. function integer log2(input integer v); begin log2=0; while(v>>log2) log2=log2+1; end endfunction
  158. localparam AccWidth = log2(ClkFrequency/Baud)+8; // +/- 2% max timing error over a byte
  159. reg [AccWidth:0] Acc = 0;
  160. localparam ShiftLimiter = log2(Baud*Oversampling >> (31-AccWidth)); // this makes sure Inc calculation doesn't overflow
  161. localparam Inc = ((Baud*Oversampling << (AccWidth-ShiftLimiter))+(ClkFrequency>>(ShiftLimiter+1)))/(ClkFrequency>>ShiftLimiter);
  162. always @(posedge clk) if(enable) Acc <= Acc[AccWidth-1:0] + Inc[AccWidth:0]; else Acc <= Inc[AccWidth:0];
  163. assign tick = Acc[AccWidth];
  164. endmodule
  165. ////////////////////////////////////////////////////////