uart_rx.v 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * uart_rx.v
  3. *
  4. * vim: ts=4 sw=4
  5. *
  6. * Copyright (C) 2019 Sylvain Munaut <tnt@246tNt.com>
  7. * All rights reserved.
  8. *
  9. * BSD 3-clause, see LICENSE.bsd
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of the <organization> nor the
  19. * names of its contributors may be used to endorse or promote products
  20. * derived from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. `default_nettype none
  34. module uart_rx #(
  35. parameter integer DIV_WIDTH = 8,
  36. parameter integer GLITCH_FILTER = 2
  37. )(
  38. input wire rx,
  39. output wire [7:0] data,
  40. output reg stb,
  41. input wire [DIV_WIDTH-1:0] div, // div - 2
  42. input wire clk,
  43. input wire rst
  44. );
  45. // Signals
  46. wire rx_val;
  47. wire rx_fall;
  48. wire go, done, ce;
  49. reg active;
  50. reg [DIV_WIDTH:0] div_cnt;
  51. reg [4:0] bit_cnt;
  52. reg [8:0] shift;
  53. // Input stage (synchronizer / de-glitch / change detect)
  54. generate
  55. // Glitch filter
  56. if (GLITCH_FILTER > 0)
  57. glitch_filter #(
  58. .L(GLITCH_FILTER)
  59. ) gf_I (
  60. .pin_iob_reg(rx),
  61. .cond(1'b1),
  62. .val(rx_val),
  63. .rise(),
  64. .fall(rx_fall),
  65. .clk(clk),
  66. .rst(rst)
  67. );
  68. // Or simple synchronizer
  69. else begin
  70. reg [1:0] rx_sync;
  71. reg rx_fd;
  72. always @(posedge clk)
  73. begin
  74. rx_sync <= { rx_sync[0], rx };
  75. rx_fd <= rx_sync[1] & ~rx_sync[0];
  76. end
  77. assign rx_fall = rx_fd;
  78. assign rx_val = rx_sync[1];
  79. end
  80. endgenerate
  81. // Control
  82. assign go = rx_fall & ~active;
  83. assign done = ce & bit_cnt[4];
  84. always @(posedge clk or posedge rst)
  85. if (rst)
  86. active <= 1'b0;
  87. else
  88. active <= (active & ~done) | go;
  89. // Baud rate generator
  90. always @(posedge clk)
  91. if (~active)
  92. div_cnt <= { 2'b00, div[DIV_WIDTH-1:1] } - 1;
  93. else if (div_cnt[DIV_WIDTH])
  94. div_cnt <= { 1'b0, div };
  95. else
  96. div_cnt <= div_cnt - 1;
  97. assign ce = div_cnt[DIV_WIDTH];
  98. // Bit counter
  99. always @(posedge clk)
  100. if (~active)
  101. bit_cnt <= 5'h08;
  102. else if (ce)
  103. bit_cnt <= bit_cnt - 1;
  104. // Shift register
  105. always @(posedge clk)
  106. if (ce)
  107. shift <= { rx_val, shift[8:1] };
  108. // Outputs
  109. assign data = shift[7:0];
  110. always @(posedge clk)
  111. stb <= ce & bit_cnt[4] & rx_val;
  112. endmodule // uart_rx